/* gallery v2.5 Copyright ch 070211 */
//x=[];for(p in e.srcElement){x.push(p)};x.sort();alert(x.join('\t'));

$(document).ready(function(){
	cdot.gallery.init();
});

// general cdot lib
var cdot;
if (!cdot) cdot = {};

// gallery lib
cdot.gallery = {
	WORK_SERVLET: '/gallery/work.do',
	// timeout after ms
	TIMEOUT: 1500,
	// swap content here
	GALLERY_BODY: '#GALLERY-BODY',
	// set in HTML gallery/@id
	galleryid: '',
	// set in HTML for each gallery
	ajaxenabled: false,
	// cache for historyhtml
	_history: {},
	// current history timer
	_historytimer: null,
	// history-frame used for history (needed for IE)
	// FF hash history would enable bookmarking, maybe add later
	_historyframe: null,
	// current image on show
	_current: null,
	// init gallery
	init: function() {
		if (this.ajaxenabled){
            $.ajaxSetup({timeout: this.TIMEOUT})
			this._initevents();
		}
	},
	// bind all links in nav and gallery image itself
	_initevents: function() {
		if ($('div.seriesnav').size() > 0) {
			$('#gallery-work').click(cdot.gallery._showwork);
			$('div.seriesnav a').each(
				function() {
					$(this).click(cdot.gallery._showwork);
				});
			// not for index pages with no series nav
			cdot.gallery._starthistorywatch();
		}
	},
	// history works for Firefox only currently :(
	_starthistorywatch: function() {
		cdot.gallery._historytimer = setTimeout(cdot.gallery._historywatch, 500);
	},
	// watches hash if still ok or has changed because of e.g. browser back
	_historywatch: function() {
		clearTimeout(cdot.gallery._historytimer);
		if (!cdot.gallery._historyframe) {
			var history = document.createElement("IFRAME");
  			history.id = "HISTORYFRAME";
  			history.name = "HISTORYFRAME";
			cdot.gallery._current = location.search.slice(1);
			history.src = '/h.html?' + cdot.gallery._current;
  			document.body.appendChild(history);
			cdot.gallery._historyframe = frames["HISTORYFRAME"];
		}
		else if (cdot.gallery._current !=
				 cdot.gallery._historyframe.location.search.slice(1)) {
			var query = cdot.gallery._historyframe.location.search.slice(1);
			cdot.gallery._load(query);
			return;
		}
		cdot.gallery._starthistorywatch();
	},
	// requests new GALLERY-BODY via AJAX
	_showwork: function(e) {
		clearTimeout(cdot.gallery._historytimer);
		try {
			var srclink = ('A' == e.target.tagName) ?
							$(e.target) : $(e.target).parents('a');
			var href = srclink.attr('href');
			var query = href.slice(href.indexOf('?')+1);
			return cdot.gallery._load(query, href);
		}
		catch (e) {
			return true;
		}
	},
	_load: function(query, href) {
		if (cdot.gallery._history[query] === undefined) {
			var url = cdot.gallery.WORK_SERVLET + '?' + query;
			$.get(url, {gallery: cdot.gallery.galleryid},
				// closure: passes query and href to callback
				function(data, msg) {
					cdot.gallery._display(data, msg, query, href)
				}
			)
		}
		else {
			// display from cache
			cdot.gallery._display(cdot.gallery._history[query], 'ok', query);
		}
		return false;
	},
	// displays AJAX response in #GALLERY-BODY
	_display: function(data, msg, query, href) {
		if ('error' != msg) {
			cdot.gallery._history[query] = data;
			// IE does not like fading out to nothing, so 0.1!
			$(cdot.gallery.GALLERY_BODY).fadeTo(300, 0.2, function() {
				$(this).html(data).fadeTo(600, 1.2)
				cdot.gallery._initevents();
				/* used on photo and io10: initPage(reload) */
				if (typeof initPage == 'function') initPage(true);
				// update histoy
				cdot.gallery._current = query;
				//location.hash = '#'+cdot.gallery._current;
				cdot.gallery._historyframe.location.href = '/h.html?' + cdot.gallery._current;
				cdot.gallery._starthistorywatch();
			})
		}
		else {
			// fallback
			if (href) location.href = href;
			cdot.gallery._starthistorywatch();
		}
	}
}
	/*$.ajax({
		type: 'GET',
		url: url,
		dataType: 'html',
		success: cdot.gallery._success,
		error: cdot.gallery._errorCallback,
		timeout: 3000 //ms
		}
	);*/
	/*
	// simply returns false for timeout or error (dummy code)
	_errorCallback: function (e, errorstr) {
		var timeout = false;
		try { if (!e.status || (0 == e.status)) timeout = true; // IE }
		catch (e) { timeout = true; } // Firefox throws...
		var status = e.status;
		if (500 == status) {}
		//return true;
	}
	*/
