$(window).load(function(){
	$(window).scroll(function(){
		$('#loading').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 0});
	});
	var hashParts = window.location['hash'].split('=');
	var pageName = 'home';
	if(hashParts[1]) pageName = hashParts[1];
	page(pageName);
	new TWTR.Widget({
	  version: 2,
	  type: 'profile',
	  id: 'twitterFeed',
	  rpp: 4,
	  interval: 30000,
	  width: 300,
	  height: 100,
	  theme: {
	    shell: {
	      background: '#333333',
	      color: '#ffffff'
	    },
	    tweets: {
	      background: '#000000',
	      color: '#ffffff',
	      links: '#eb0707'
	    }
	  },
	  features: {
	    scrollbar: true,
	    loop: false,
	    live: true,
	    behavior: 'all'
	  }
	}).render().setUser('boyswithouttoys').start();
});
function getBT(){
	return {
		fill: '#c40000',
		strokeStyle: '#000',
		cssStyles: {color: '#000', 'font-size':'12px', width: 'auto', 'text-align': 'center'},
		width: 370,
		strokeWidth: 3,
		animate: false,
		spikeLength: 20,
		cornerRadius: 10,
		spikeGirth: 20,
		overlap: 10,
		positions: ['top', 'bottom', 'right', 'left'],
		closeWhenOthersOpen: true,
		trigger: ['click']
	};
}

var captions = new Array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");

function picHover(id){
	$('.pics img').removeClass("picSel");
	var $pic = $('#'+id).addClass('picSel');
	var $pop = $('.picPop');
	var src = $pic.attr('src');
	$pop.html('<img src="'+src+'" class="picPopImg" />')/*.offset({
		top: ($pic.offset().top)-($pop.innerHeight())-20,
		left: ($pic.offset().left)-($pop.innerWidth()/2)+($pic.innerWidth()/2)
	})*/.show();
	//$('#picPopArrow').show();
	$('#captions').html(captions[id]);
}
function albums(id){
	var $pics = $('#'+id+'Pics');
	$('.albums img').removeClass("albumSel");
	$('#'+id).addClass('albumSel');
	$('.picsWrap table').hide();
	$pics.show();
	$('#'+id+'Pics img').first().trigger('click');
}
function originals(id){
	var $songs = $('#'+id+'Songs');
	$('.albums img').removeClass("albumSel");
	$('#'+id).addClass('albumSel');
	$('.songs div').hide();
	$songs.show();
}
// function videosAlbums(id){
// 	var $vids = $('#'+id+'Vids');
// 	$('.albums img').removeClass("albumSel");
// 	$('#'+id).addClass('albumSel');
// 	$('.vidsWrap table').hide();
// 	$vids.show();
// }
function bioHover(id){
	$('.bioLinks td').removeClass("bioSel");
	var $bio = $('#'+id).addClass('bioSel');
	var $pop = $('.bioImg');
	var src = $bio.attr('src');
	$pop.html('<img src="'+src+'" class="bioPopImg" />')/*.offset({
		top: ($pic.offset().top)-($pop.innerHeight())-20,
		left: ($pic.offset().left)-($pop.innerWidth()/2)+($pic.innerWidth()/2)
	})*/;
	//$('#picPopArrow').show();
}
function page(url){
	$("#loading").ajaxStart(function(){
	   $(this).show();
	});
	$("#loading").ajaxStop(function(){
	   $(this).hide();
	});
	$('a').removeClass('sel');
	$('a.'+url).addClass('sel');
	$.post('/pages/'+url+'.php', null, function(r){
		$('#page').hide().html(r).slideDown(600, function(){
			$('iframe').show();
			var imgs = $('img'), i = 0, length = imgs.length;
			imgs.slideDown(800, function(){
				if (++i!==length) return;
				
				$('.subscribeForm').bind('submit', function(e){
					e.preventDefault();
					$form = $(this);
					$.post("/ajax/subscribe.php", $form.serialize(), function(data) {
						if (!data.subscribed) {
							$('.message').removeClass('green').addClass('red').html('Invalid email address!');
							return;
						}
						$('.message').removeClass('red').addClass('green').html("Your email address has been added to our fan list, thank you!");
					}, "json");
				});
				
				if(url=='originals'){
					/*$('.lyricsLink').each(function(){
						if($(this).attr('title')) $(this).bt(getBT());
					});*/
					$('.albums img').unbind('click').bind('click', function(){ originals($(this).attr("id")); });
					originals("itsRaining");
				}
				if(url=='videos'){
				// 	$('.albums img').unbind('click').bind('click', function(){ videosAlbums($(this).attr("id")); });
				// 	videosAlbums("triple3");
					var $firstOrig = $('#oVidPop .videoLink').first();
					$('.video').attr('src', 'http://www.youtube.com/embed/' + $firstOrig.attr('videolink') + '?rel=0');
					$('.videoTitle').html($firstOrig.attr('videotitle'));
					$('#oVidDrop').click(function(e){
						$('#cVidPop').hide();
						$('#oVidPop').toggle();
						e.stopPropagation();
					});
					$('#cVidDrop').click(function(e){
						$('#oVidPop').hide();
						$('#cVidPop').toggle();
						e.stopPropagation();
					});
					$('#oVidPop, #cVidPop').click(function(e) {
						e.stopPropagation();
					});
					$(window).click(function () {
						$('#oVidPop, #cVidPop').hide();
					});
					$('.videoLink').click(function(){
						$('#oVidPop, #cVidPop').hide();
						$('.video').attr('src', 'http://www.youtube.com/embed/' + $(this).attr('videolink') + '?rel=0&autoplay=1');
						$('.videoTitle').html($(this).attr('videotitle'));
						$.post('../ajax/player.php', '', function(r){
							$('#player').html(r);
						});
					});
				}
				if(url=='pics'){
					$('.albums img').unbind('click').bind('click', function(){ albums($(this).attr("id")); });
					$('.pics img').unbind('click').bind('click', function(){ picHover($(this).attr("id")); });
					/*$('.pics img').unbind('mouseout').bind('mouseout', function(){
						$('#picPop').hide().html("");
					});*/
					albums("peter");
				}
				if(url=='bios'){
					$('.bioLinks td').unbind('mouseover').bind('mouseover', function(){ bioHover($(this).attr("id")); });
				}
				if(url=='newSingle'){
					$('.NYElyricsLink').unbind('click').bind('click', function(){
						$('.NYElyrics').slideToggle(600, function(){
							if($(".NYElyricsLink").text()=="Show Lyrics") $(".NYElyricsLink").text("Hide Lyrics");
							else $(".NYElyricsLink").text("Show Lyrics");
						});
					});
				}
				if (url==='comments') {
					$('.commentForm').bind('submit', function(e) {
						e.preventDefault();
						var $form = $(this);
						$.post('/ajax/postComment.php', $form.serialize(), function (data) {
							console.log(data);
							if (!data.posted) {
								$('.message').html('Please enter a comment');
								return;
							}
							page('comments');	
						}, "json");
					});
				}
				if (url==='covers') {
					$('.coverWatchLink').click(function(){
						$.post('../ajax/player.php', '', function(r){
							$('#player').html(r);
						});
					});
				}
			});
		});
		window.location.hash = '#page='+url;
		$('.performances span').each(function(){
			if($(this).attr('title')) $(this).bt(getBT());
		});
		// var hoverBT = getBT();
		// hoverBT.trigger = ['hover'];
		// hoverBT.overlap = -6;
		// $('.albumThumb').each(function(){
		// 	if($(this).attr('title')) $(this).bt(hoverBT);
		// });
		if(document.contactForm){
			$('input').keypress(function(e){
				if(keyPress(e)==13){
					document.contactForm.send.onclick();
					return false;
				}
			});
		}
		/*if(url=='newSingle'){
			if($("#playerObj").attr('song')=="On New Years Eve.mp3"){
				if($("#playerObj").attr('auto')!="true") $.post('../ajax/player.php', 'file=On New Years Eve.mp3&auto=true', function(r){$('#player').html(r);});
			} else $.post('../ajax/player.php', 'file=On New Years Eve.mp3&auto=true', function(r){$('#player').html(r);});
		}*/
	});
};
function keyPress(e){
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    return code;
};
