// jQuery 1.5.1
//
// ReedThomasLawrence.com
//
// ©2011 Reed Thomas Lawrence
//

function getPage(currentPage) {
	
	if(currentPage == 'music' || currentPage == 'shows' || currentPage == 'about' || currentPage == 'video' || currentPage == 'contact') $('.content').hide();
	else { currentPage = 'home'; $('.news-page').hide(); $('#news-page-1').show();}
	$('#'+currentPage+'-container').show();
	window.location.hash = currentPage;
		$('#navigation-'+currentPage).removeClass().addClass('navigation-page');

};


function changePage(pageSlug) {
	
	requestedPage = window.location.hash.replace(/[#]/, '');
	
	if(requestedPage != pageSlug) {
	
		$('#navigation a').removeClass().addClass('navigation-link');
		$('#navigation-'+pageSlug).removeClass().addClass('navigation-page');
		$('.content').fadeOut(400);
	
		$.doTimeout('pageTimer', 410, function() {
			if(pageSlug == 'home') {
			 $('.news-page').hide(); 
				$('#news-page-1').show();
				$('#news-buttons a').removeClass().addClass('news-button');
				$('#news-button-1').removeClass().addClass('news-button-current');
			}
			$('#'+pageSlug+'-container').fadeIn(400);
			window.location.hash = pageSlug;
		}, true);
	
	};

};


function musicPlayerOver()  { /*$('#musicplayer-spacer').slideDown(400);*/ };
function musicPlayerLeave() { /*$('#musicplayer-spacer').slideUp(400);*/ };


function musicPlayer(currentSong) {
	
	//musicPlayerOver();
	//$('#musicplayer-spacer').show();
	
	timerC = setTimeout(function() {
		 //musicPlayerLeave();
	}, 8000);
	
	$('#music-tracklist li').click(function() {
			currentSong = parseInt($(this).attr('title'));
			changeSong(currentSong);
			$.scrollTo(0, 300);
			setTimeout(function() { musicPlayerOver(); }, 310); });
	
	$('#musicplayer-next').click(function() {
			if(currentSong >= parseInt($('#sc-length').val()))  currentSong = 1;
			else currentSong = currentSong+1;
			changeSong(currentSong); });
	
	$('#musicplayer-prev').click(function() {
			if(currentSong <= 1) currentSong = parseInt($('#sc-length').val());
			else currentSong = currentSong-1;
			changeSong(currentSong); });
	
	//for(sc = 1; sc <= parseInt($('#sc-length').val()); sc++) {
			//swfobject.registerObject('sc-object-'+sc, "8.0.0", "expressInstall.swf");
			//swfobject.registerObject('sc-embed-'+sc, "8.0.0", "expressInstall.swf");
	//}
	
	$('.soundcloudplayer').hide();
	$('#soundcloudplayer-'+currentSong).show();

};
	
	
function changeSong(currentSong) {
	
	if(typeof(timerC) != 'undefined') clearTimeout(timerC); 
	
	$('.soundcloudplayer').hide();
	
	for(jj = 1; jj <= $('#sc-length').val(); jj++) {
	  $('#sc-object-'+jj+' param').val($('#sc-object-'+jj+' param').val().replace('auto_play=false', 'auto_play=true'));	
	};	
	
	$('#soundcloudplayer-'+currentSong).show();
	$('#musicplayer-songtitle').html($('#soundcloudplayer-'+currentSong).attr('title'));    
	$('#musicplayer-buythistrack a:first').attr('href',$('#soundcloud-track-'+currentSong).attr('title'));
	
};


function photoSlider(currentSlide, sliderDirection, sliderTimer) {

	preloadSlide = currentSlide+1;
	$('#banner-placeholder').attr('src', $('#banner-hidden-'+preloadSlide).val());
	if(typeof(timerB) != 'undefined') clearTimeout(timerB); 
	
	$.doTimeout('timer', sliderTimer, function() {
		bannerCount = $('#banner-count').val();
	
		switch(sliderDirection) {
			case 0 : if(currentSlide <= 1) currentSlide = bannerCount; else currentSlide--;  break;
			case 1 : if(currentSlide >= bannerCount) currentSlide = 1; else currentSlide++;  break; };		
	
		$('#banner-bottom-container').css({'background-image':'url('+$('#banner-hidden-'+currentSlide).val()+')'});
		$('#banner-top-container').fadeOut(700);
		
		if($('#banner-hidden-'+currentSlide).attr('title') != '0') {
				$('#banner-button').css({'cursor':'pointer','cursor':'hand'});
				$('#banner-button').click(function() {	
				  window.open($('#banner-hidden-'+currentSlide).attr('title'));	});
		} else {
         $('#banner-button').unbind('click');
         $('#banner-button').css({'cursor':'default'});
		};
		
		timerB = setTimeout(function() {
			$('#banner-top-container').css({'background-image':'url('+$('#banner-hidden-'+currentSlide).val()+')'}).fadeIn(100);
		 photoSlider(currentSlide, 1, 12000);
		},720);
	
	}, true);
	
	$('#photoslider-prev').click(function() {
		$.doTimeout('timer', 0, function() {
			photoSlider(currentSlide, 0, 0);
		}, true); });
	
	$('#photoslider-next').click(function() {
		$.doTimeout('timer', 0, function() {
			photoSlider(currentSlide, 1, 0);
		}, true); });
	
};


function videos() {
	 for($v = 1; $v <= $('#video-list').val(); $v++) {
				swfobject.registerObject('video-'+$v, "8.0.0", "expressInstall.swf");
			//	swfobject.registerObject('video-'+$v+'b', "8.0.0", "expressInstall.swf");
		};	 
};


function validate(form) {
			
	$('#'+form+'-send').click(function() {
			var isValid = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/g.test($('#'+form+'-email').val());
			if (isValid && $('#'+form+'-email').val() != '') {
					$('#'+form+'-form').submit();
			} else {
					$('.'+form+'-valid').fadeOut(200);
					setTimeout(function() {
							$('.'+form+'-invalid').fadeIn(300);
					}, 210);
			};
	});

	$('#'+form+'-email').keyup(function () { 
			var isValid = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/g.test($('#'+form+'-email').val());
			if (isValid && $('#'+form+'-email').val() != '') {
					$('.'+form+'-invalid').fadeOut(200);
					setTimeout(function() {
							$('.'+form+'-valid').fadeIn(300);
					}, 210);
			};
	});
	
};


$(document).ready(function() {

	var currentPage = window.location.hash.replace(/[#]/, '');
	var currentSlide = 1;
	var currentSong = parseInt($('#sc-start').val());
	
	getPage(currentPage);
	photoSlider(currentSlide, 1, 20000);
	musicPlayer(currentSong);
	videos();
	validate('contact');
	validate('mailinglist');
	
	//$('#musicplayer-container').hoverIntent({ sensitivity:7, interval:100, over:musicPlayerOver, timeout:800, out:musicPlayerLeave });
	
	$('#navigation-home'   ).click(function() { changePage('home');    });
	$('#navigation-music'  ).click(function() { changePage('music');   });
	$('#navigation-shows'  ).click(function() { changePage('shows');   });
	$('#navigation-about'  ).click(function() { changePage('about');   });
	$('#navigation-video'  ).click(function() { changePage('video');   });
	$('#navigation-contact').click(function() { changePage('contact'); });

	$('#music-artwork').hover(function() {
			$('#music-artwork strong').css({'color':'#b4885d'});
			$('#music-artwork em').css({'color':'#fff'});
	}, function() {
			$('#music-artwork strong').css({'color':'#c1976e'});
			$('#music-artwork em').css({'color':'#d2c4b6'});
	});
	
	$('#news-buttons a').click(function() {
		
			requestedFeed = $(this).html();	
			$('#news-page').fadeOut(400);
			setTimeout(function() {
					$('#news-buttons a').removeClass().addClass('news-button');
					$('#news-button-'+requestedFeed).removeClass().addClass('news-button-current');
					$('.news-page').hide();
					$('#news-page-'+requestedFeed).show();
					$('#news-page').fadeIn(400);
			}, 410);
	
	});
	
	$('#musicplayer-container').hover(function() {
		// if(typeof(timerC) != 'undefined') clearTimeout(timerC); 
	});
	
});
