if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: false,
	
	// init, something like a constructor
	init: function() {
		jsFrontend.favicon.init();
		jsFrontend.forms.init();
		jsFrontend.layout.init();
		jsFrontend.media.init();
		jsFrontend.search.init();
	},
	
	// end
	_eoo: true
}	

jsFrontend.favicon = {
	// init, something like a constructor
	init: function() {
		if($('.loadFavicons').length > 0) { jsFrontend.favicon.load(); }
	},
	
	// load favicons
	load: function() {
		$('.loadFavicons a[href^="http://"]').each(function() {
			var link = $(this);
			var favicon = new Image();
			favicon.src = link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
			favicon.onload = function() { 
				link.attr('style', 'background-image: url('+ favicon.src + ');');
				link.addClass('loaded');
			}
		});
	},

	// end
	_eoo: true		
}

jsFrontend.forms = {
	// init, something like a constructor
	init: function() {
		// submit form on change
		if($('#sortReceivedInfo').length > 0) { $('#sortReceivedInfoMethods').bind('change', function(evt) { $('#sortReceivedInfo').submit(); }); }
	},
	
	// end
	_eoo: true			
}

jsFrontend.layout = {
	// init, something like a constructor
	init: function() {
		// add odd and even classes to album tracklists
		$('.tracklist tr:nth-child(odd)').addClass('odd');
		$('.tracklist tr:nth-child(even)').addClass('even');
	
		// fix an issue with the subnavigation
		$('#subnavigation .selected').prev().find('a').css('borderBottom', 'none');
		$('#subnavigation li:first-child.selected').parent().css('borderTop', 'none');
	
		// add extraneous markup to the tabs so that they work
		$('.tabs .selected a').prepend('<span class="l">&nbsp;</span>');
		$('.tabs .selected a').append('<span class="r">&nbsp;</span></a>');
	},
	
	// end
	_eoo: true	
}

jsFrontend.media = {
	// init, something like a constructor
	init: function() {
		if($('a[rel^="prettyPhoto"]').length > 0) { $('a[rel^="prettyPhoto"]').prettyPhoto(); }
	},
	
	// end
	_eoo: true	
}

jsFrontend.search = {
	url: '/ajax.php?module=bands&action=autocomplete',
		
	// init, something like a constructor
	init: function() {
		if($('#search form').length > 0)
		{
			$('#search form').submit(function(evt) {
				evt.preventDefault();
				if($('#searchBox').val() == '') alert('No item found');
				return false;
			});
		
			$('#searchType').bind('change', function(evt) { 
				jsFrontend.search.url = '/ajax.php?module='+ $('#searchType').val() +'&action=autocomplete';
				jsFrontend.search.rebind();
			});
			
			jsFrontend.search.rebind();
		}
	},

	handle: function(event, data, formatted) {
		window.location = formatted.replace('&eacute;', 'e');
	},
	
	parse: function(json) {
		// init var
		var parsed = [];
		
		// results?
		if(json.content.count > 0) {
			for (i in json.content.data) {
				var entryName = json.content.data[i].name;
				var entryUrl = json.content.data[i].url;
				parsed[parsed.length] = { data: [entryName], value: entryUrl, result: entryName };
			}
		}
		return parsed;
	},
	
	rebind: function() {
		// unbind previous events
		$('#searchBox').unbind();
		$('#searchBox').val('');

		// bind
		$('#searchBox').autocomplete(jsFrontend.search.url, { matchContains: true, cacheLength: 0, selectFirst: true, delay: 1, minChars: 0, dataType: 'json', maxItemsToShow: 50, parse: jsFrontend.search.parse, width: 200 });
		// bind results
		$('#searchBox').result(jsFrontend.search.handle);
	},
	
	// end
	_eoo: true		
}


$(document).ready(function() { jsFrontend.init(); });