var poco =
{
	// FIXME: remove useAjax setting for production use
	useAjax : true,
	useHistory : true,
	jukeboxFile : 'resources/jukebox/jukebox.swf',

	// Awkward but reliable.
	hashBase : '/',
	forumLocation : '/forum',

	loadingContent : '<div id="loading"><img alt="loading..." src="resources/images/preloader.gif"/></div>',
	contentId : '#content_main_text',
	headerIds : '#header_main, #content_main_header',
	menuIds : '#header_main ul',

	// Use 'on-top' class to dynamically set z-index of this calendar cell higher than surrounding cells
	//  (avoids overlap problems on IE < 8).
	showConcertData : function() {
		$(this).addClass('on-top');
		$('div.concert_data', $(this)).show();
	},

	hideConcertData : function() {
		$(this).removeClass('on-top');
		$('div.concert_data', $(this)).hide();
	},

	loadLoginPanel : function() {
		var requestUrl = 'login_panel';
		// Prevent IE6 from caching form result, since setting cache-control headers does not seem to be sufficient.
		if ($.browser.msie) {
			requestUrl += ('?' + new Date().getTime());
		}
		$.get(requestUrl, function(data) {
			$('#login_block').remove();
			$('#content_sidebar').prepend($('#login_block', data));

			poco.ajaxifyLocalLinks($('#login_block'));
		});
	},

	ajaxifyLocalLinks : function(content) {
		// FIXME: need to filter localLinks, both to avoid wasting time and
		//  to keep from "ajaxing" external links if not using history plugin.
		var localLinks = $('a', content);

		if (poco.useHistory) {
			localLinks.each(function() {
				var hashedHref = poco.convertLocalUrlToHash($(this).attr('href'));
				$(this).attr('href', hashedHref);
			});

		} else {
			// History plugin will take care of this for us if it's loaded.
			localLinks.click(function() {
				poco.ajaxLoad($(this).attr('href'));
				return false;
			});
		}

		// Set correct urls in sidebar paypal buttons
		$('#login_block input[name=shopping_url]').attr('value', window.location.href);
		$('#login_block input[name=cancel_return]').attr('value', window.location.href);

		$('#login_form').ajaxForm({ success: poco.loadLoginPanel });
	},

	ajaxLoad : function(url) {
		poco.displayWaitMessage();

		$.get(url, function(data) {
			document.title = ($('#title', data).text());
			poco.updateHeadersClass($('#class', data).text());
			poco.replaceContent($('#ajax_content', data));
			$('td.concert').hover(poco.showConcertData, poco.hideConcertData);
			poco.convertLightboxLinks($('#ajax_content'));
		});
	},

	updateHeadersClass : function(newClass) {
		$(poco.headerIds).removeClass().addClass(newClass);
		$(poco.menuIds + ' li').removeClass('active');
		$(poco.menuIds + ' li.' + newClass).addClass('active');
	},

	replaceContent : function(newContent) {
		poco.ajaxifyLocalLinks(newContent);
		$(poco.contentId).empty().append(newContent);
	},

	convertLocalUrlToHash : function(href) {
		// Workaround to avoid converting links to forum.
		if (href.match(poco.forumLocation)) return href;

		var re = new RegExp('^(http[s]?:\\/\\/' + location.hostname + ')?' + poco.hashBase + '(index.php)?' )
		return href.replace(re, poco.hashBase + '#');
	},

	displayWaitMessage : function() {
		$(poco.contentId).empty().append(poco.loadingContent);
	},

	convertLightboxLinks : function(context) {
		context = context || document;
		var lightbox = $('a[rel^=lightbox]', context);

		if (lightbox.length && typeof($.Lightbox) == 'object') {
		    lightbox.each(function() {
		        var large = $('img', this).attr('src').replace(/\/thumb\/|\/medium\//, '/large/');
		        $(this).attr('href', large);
		    });

		    lightbox.lightbox();
		}
	},

	hideLightbox : function() {
		$('#lightbox-overlay').hide();
		$('#lightbox').hide();
	},

	setupJukebox : function() {
		var so = new SWFObject(poco.jukeboxFile, "jukebox_object", "320", "140", "8", "#EAD7BB");
		so.addParam("wmode", "transparent");
		so.addParam("quality", "high");
		so.write("jukebox");
	}
};

$(function () {
	if (poco.useAjax) {

		if(poco.useHistory && typeof($.History) == 'object') {
			var hash = $.History.getHash();
			if (hash !== '') {
				poco.displayWaitMessage();
				// Fixme: need to extract controller name from hash
				poco.updateHeadersClass(hash);
			}

			$.History.bind(function(state) {
				// Avoid requesting an empty URL.
				state = state || 'news';
				poco.hideLightbox();
				poco.ajaxLoad(state);
			});
		}

		poco.ajaxifyLocalLinks($('#page'));
	}

	poco.convertLightboxLinks();
	poco.setupJukebox();
	$('td.concert').hover(poco.showConcertData, poco.hideConcertData);
	$('div.concert_data').live('click', function() { $(this).hide(); });
});

