
$(function() {
	nav.init();
	tabs.init();
	$('#contactlink').colorbox({
		inline: true, 
		href: "#contact", 
		onOpen: function(){_gaq.push(['_trackPageview', 'contact'])},
		onCleanup: function(){form.restore()}
	});
	$('#loginlink').colorbox({
		inline: true, 
		href: "#login", 
		onOpen: function(){_gaq.push(['_trackPageview', 'login'])},
		onCleanup:function(){form.restore()}
	});
	form.init('mailing-list');
	form.init('formmail');
	form.init('loginfrm');
	//Lazy load features
	$('.tabs a[rel]').each(function() {
		$($(this).attr('href')).load($(this).attr('rel'));
	});

	$('table tr:odd').addClass('odd');
	$('textarea').keypress(function(event){if(event.keyCode==32) return true});
});

form = {
	init: function(name) {
		$('#' + name).submit(function(){form.send(this);return false;});
	},
	send: function(f) {
		var $f = $(f);
		var $email = $f.find('input[id$="-email"]');
		var email = $email.val();
		if (email.indexOf('@') < 1 || email.indexOf('.') < 2) {
			$email.css('border-color','#ff7777');
			$f.find('.error').show();
			$.fn.colorbox.resize();
			return false;
		}
		$(f).hide();
		$('#' + f.id + '-process').show();
		$.ajax({
			type: "POST",
		   	url: f.action,
		   	data: $(f).serialize(),
		   	success: function(msg){
				if (f.name=='loginfrm') {
					 if (msg == 'false') {
						 $('#loginfrm-error').show();
						 $.fn.colorbox.resize();
						 return
					 }
					else {
						location.href = msg;
					}
				}
				form.confirm(f);
				_gaq.push(['_trackPageview', f.id + '/submit']);
			}
	 	});
	},
	confirm: function(f) {
		$('#' + f.id + '-process').hide();
		$('#' + f.id + '-confirm').show();
	},
	restore: function() {
		$('#formmail-confirm, #mailing-list-confirm').hide();
		$('#formmail, #mailing-list').show();
	}
}

tabs = {
	init: function() {
		$('.tabs ul a').click(function(){
			var tab = this.href.split('#')[1];
			var newHash = location.hash.split('&tab');
			newHash = newHash[0];
			if(newHash=='') newHash = 'index';
			location.hash = newHash + '&tab=' + tab;
			return false;
		});
	},
	change: function(tab) {
		$('.tabcontent').hide();
		$('.tabs ul a').removeClass('active');
		$('.tabs ul a[href$="' + tab + '"]').addClass('active');
		$('#' + tab).fadeIn();
		//music page only
		$('#main > div[id$="_main"]').hide();
		$('#' + tab + '_main').fadeIn();
	}
}

nav = {
	init: function() {
		$('a.hijax, #whatsnew a, #main a, #footer a').click(function(){
			//nav.hijax(this);
			var href = this.href;
			var ld = href.match(/:\/\/(.[^/]+)/)[1];
			var ext = href.split('.');
			ext = ext[ext.length-1];
			if(ld == document.domain && ext == 'php') {
				var targ = $(this).attr('href').replace('.php','');
				nav.changeTab(targ);
				window.location.hash = targ;
				return false;
			}
			
		});
		//nav.targ = '#index';
		var filename = window.location.pathname;
		var newHash = (filename!='/') ? filename.substring(1,filename.length-4) : 'index';
		window.location.hash = newHash;
		nav.hash = '#' + newHash;
		setInterval('nav.check()',100);
	},	
	hijax: function(a) {
		var href = a.href;
		var ld = href.match(/:\/\/(.[^/]+)/)[1];
		var ext = href.split('.');
		ext = ext[ext.length-1];
		if(ld == document.domain && ext == 'php') {
			var targ = $(a).attr('href').replace('.php','');
			nav.changeTab(targ);
			window.location.hash = targ;
			return false;
		}
	},
	set: false,
	hash: '',
	targ: '',
	tab: '',
	check: function() {
		var hash = location.hash;
		//console.log(hash+'/'+nav.hash);
		if(hash != nav.hash) {
			var targ = hash.split('&tab=');
			var tab = targ[1];
			targ = targ[0];
			if(targ!=nav.targ) {
				nav.load(targ,tab);
				nav.targ = targ;
			}
			else if(tab!=nav.tab) {
				if(!tab) tab = $('.tabs a').attr('href').substring(1);
				tabs.change(tab);
				nav.tab = tab;
			}
			nav.hash = hash;
		}
		else if (!nav.set) {
			$('#top,#main').fadeIn('slow');	
			nav.set = true;
		}
	},
	load: function(targ,tab) {
		targ = targ.substring(1);
		nav.changeTab(targ);
		$.ajax({
			url: targ + '.php',
			dataType: 'html',
			success: function(html) {
				var $html = $(html).find('#top');
				nav.replace('top', $html);
				$html = $(html).find('#main');
				nav.replace('main', $html);
			},
			complete: function() {
				if(tab&&tab!=nav.tab) {
					tabs.change(tab);
					nav.tab = tab;
				}
				var newHash = (tab) ? targ + '&tab=' + tab : targ;
				location.hash = newHash;
				tabs.init();
				$('#whatsnew a, #main a').click(function(){
					nav.hijax(this);
					//return false;
				});
				$('html, body').animate({scrollTop:0}, 'slow');
				
				$('.tabs a[rel]').each(function() {
					$($(this).attr('href')).load($(this).attr('rel'));
				});
			},
			error: function(xml, textStatus) {
				//alert(textStatus + '/test');
			}
		});	
		_gaq.push(['_trackPageview', targ]); 
	},
	changeTab: function(targ) {
		$('#mainnav a').removeClass('active');
		$('#mainnav a[href$="' + targ + '.php"]').addClass('active');
	},
	replace: function(section, $html) {
		if($('#' + section).length == 0) $('#bottom').before('<div id="top"></div>');
		$html.hide();
		$('#' + section).replaceWith($html);
		$('#top,#main').fadeIn('slow');
		$('table tr:odd').addClass('odd');
	}
}

function buildList(xml) {
	var list = '<dl>';
	$(xml).find('item').each(function(index){
		var thumb = ($(this).find('media\\:thumbnail').attr('url')) ? $(this).find('media\\:thumbnail').attr('url') : $(this).find('thumbnail').attr('url');
		list += '<dl><dt><a href="#' + index + '"><img src="' + thumb + '" />' + $(this).find('title').text() + '</a></dt>';
		list += '<dd>' + $(this).find('description').text() + '</dd></dl>';
	});
	list += '</dl>';
	$('#list').empty().prepend(list);
	
	$('#list dt a').click(function(){
		var vid = this.href.split('#');
		vid = vid[1];
		var vidplayer = $('#mpl')[0];
		vidplayer.sendEvent("ITEM",vid);
		vidplayer.sendEvent("PLAY",true);
		_gaq.push(['_trackEvent', 'Videos', 'Play', vidplayer.getPlaylist()[1].title]);
		//$("#jquery_jplayer").jPlayer("pause");
		return false;
		
	});
}
function playerReady() {
	$('#mpl')[0].addViewListener("PLAY","pauseMusic");
}

function pauseMusic() {
	$("#jquery_jplayer").jPlayer("pause");
	$('a.playalbum').removeClass('pausealbum');
}