$(document).ready(function(){
	
	
	function isEmail(email){
		return /^[\w-+\.]+@([\w-]+\.)+[\w-]{2,}$/i.test(email);
	}
	
	function moveDownloadBox(open) {
		if (open) {
			window.location.replace('http://www.juviley.com/#');
			$('div#downloadFormContainer').slideToggle(500);
			$('div#downloadAlbum').animate({top:115}, 500);
			$('div#downloadAlbum h3').animate({top:26}, 500);
			$('div#coverStripBottom p.instructions').fadeOut(500);
			$('div#coverStripBottom').html('<p class="clickhere" style="display:none;">Click here to download</p>');
			$('div#coverStripBottom p.clickhere').fadeIn(500);
		}
		if (!open) {
			window.location.replace('http://www.juviley.com/#form');
			$('div#downloadAlbum h3').animate({top:15}, 500);
			$('div#downloadAlbum p.clickhere').fadeOut(500);
			$('div#coverStripBottom').fadeIn(500).html('<p class="instructions">Submit your info to get the download link</p>');
			$('div#downloadAlbum').animate({top:24}, 500);
			$('div#downloadFormContainer').slideToggle(500);
		}
	}
	
	//Twitter
	getTwitters('twitter', { 
	    id: 'juviley', 
	    count: 1, 
	    enableLinks: true, 
	    ignoreReplies: true, 
	    clearContents: true,
	    template: '<p class="twit">"%text%"</p><p class="time">%time% <a href="http://twitter.com/%user_screen_name%/" target="_blank">via Twitter</a></p>'
	});
	
	
	//Navigation
	$('ul#nav a.currentnav:contains("News")').live("click", function() {
		window.location.replace("http://www.juviley.com/page/1");
	});
	
	$('ul#nav a').live("click", function(){
		// display content
		$('div#content').children('div').hide();
		$('div#' + this.hash.substr(1) + '_div').show();
		// move play / pause button
		$('#play').children('div').andSelf().removeClass();
		$('#play').children('div').andSelf().addClass(this.hash.substr(1) + 'Player');
		// change nav class
		$('ul#nav a').removeClass('currentnav');
		$(this).addClass('currentnav');
		// display bottom controls
		$('span#previous').children('span').hide();
		$('span#previous').children('.' + this.hash.substr(1) + 'Bottom').show();
		$('span#next').children('span').hide();
		$('span#next').children('.' + this.hash.substr(1) + 'Bottom').show();
	});
	

	
	// Shows
	var allShows = $('ol#showList li').length;
	var showIndex = -1;
	// list control
	$('ol#showList a').live("click", function(){
		$("div#selectedShowContainer").show();
		// set show index
		showIndex = $(this).parent().prevAll().length-1;
		// display content
		$('div#selectedShow').children('div').hide();
		$('div#show_' + this.hash.substr(1)).show();
		// change li class
		$('ol#showList a').removeClass('currentShow');
		$(this).addClass('currentShow');
	});
	// close show box
	$('a#showClose').click( function() {
	  	$("div#selectedShowContainer").fadeOut('fast');
	}); 
	// previous show
	$('span#previousShow a').live("click", function(){
		$("div#selectedShowContainer").show();
		// set show index
		showIndex--;
		if (showIndex < 0) { showIndex = allShows-2 }
		// display content
		$('div#selectedShow').children('div').hide();
		$('div#selectedShow div').eq(showIndex).show();
		// change li class
		$('ol#showList a').removeClass('currentShow');
		$('ol#showList a').eq(showIndex).addClass('currentShow');
	});
	// next show
	$('span#nextShow a').live("click", function(){
		$("div#selectedShowContainer").show();
		// set show index
		showIndex++;
		if (showIndex == allShows-1) { showIndex = 0 }
		// display content
		$('div#selectedShow').children('div').hide();
		$('div#selectedShow div').eq(showIndex).show();
		// change li class
		$('ol#showList a').removeClass('currentShow');
		$('ol#showList a').eq(showIndex).addClass('currentShow');
	});
	// on blur
	$("div#showsPlaceholder").click(function () {
		showIndex = -1;
		$('div#selectedShow').children('div').hide();
		$('div#selectedShow div#default').show();
		$('ol#showList a').removeClass('currentShow');
	 });

	
	
	// Position inside Scrollbox
	$('div#middle').scroll( function() {
		$('div#selectedShowContainer').animate({
			bottom:- this.scrollTop, right:'15px'
		},40);
	});
	
	$('div#middle').scroll( function() {
		$('div.bottomScroll').animate({
			bottom:- this.scrollTop, right:'15px'
		},40);
	});
	
	
	
	
	// Show Flyer
	$('span.flyerUrl a').bind("click", function(){
		$('img.showFlyer').toggle();
		$(this).text($(this).text() == 'Flyer' ? 'Close Flyer' : 'Flyer');
		// disable link
		return false;
	});
	
	
	
	
	//Mailing list
	$("input[name='email']",'form#mailingListForm').focus(function(){
		this.value = '';
	}).blur(function(){
		if(this.value == ''){
			this.value = this.defaultValue;
		}
	});
	$("form#mailingListForm").submit(function(event){
		var form = this;
		event.preventDefault();
		if(form.email.defaultValue != form.email.value && isEmail(form.email.value)){
		
			$.ajax({
				type: "post",
	 			url: "contact.php",
	   			data: "email=" + form.email.value,
	   			success: function(msg){
	     			if(msg != ''){
	     				$(form.email).attr('disabled','disabled').css({backgroundColor:'white'}).val('Added successfully. Thank you!!');
	     				$(form.submit).attr('disabled','disabled').css({opacity:0.3});
	     			}
	   			}
			});
		}
	});
		
	
	//Download Music form	
	$("form#downloadForm").submit(function(event){
		
		var form = this;
		event.preventDefault();
		
		$('label#email strong').remove();
		
		if(form.email.value == '') {
			var msg = 'an email is required';
			$('label#email').append('<strong class="error">'+msg+'</strong>').slide(1000);
			return false;
		}
		
		if(!isEmail(form.email.value)) {
			var msg = 'not a valid email';
			$('label#email').append('<strong class="error">'+msg+'</strong>');
			return false;
		}
		
		var params = $(this).serialize();
		$.ajax({
			type: "post",
			url: "downloadMusic.php",
			data: params,
			success: function(msg){
				if(msg != ''){
					window.location.replace("http://www.juviley.com/#submitted");
					$('div#form').hide();
					$('div#done').fadeIn(600);
				}
			}
		});
	});
	
	// download form control
	var open=false;
	
	$('div#downloadAlbum div.boxControl').click(function () {
		moveDownloadBox(open);
		open = !open;
      }
	);

	$('a#downloadFormClose').click(function(){
		moveDownloadBox(open);
		open = false;
	});
	
	
	
	// press clippings
	$('div#pressClippings a').live("click", function(){
		$('div#press_'+this.hash.substring(1)).slideToggle(1000);
	});
	$('a.pressClose').live("click", function(){
		$(this).parent().fadeOut(500);
	});
	$('a.close').live("click", function(){
		$(this).parent().parent().slideToggle(500);
	});
	
	
	
	// press images
	$('div#pressImages a').live("click", function(){
		$('div#img_'+this.hash.substring(1)).slideToggle(500);
	});
});