// SETUP INITIAL VARIBLES
var lights = 0;
var currSection = "home";


// jQUERY DOM READY FUNTION
$(document).ready(function() {

	// Function to load next page of gigs table in an ajax kinda way
	loadNextEvents();
	
	function loadNextEvents() {
		$('a.page-numbers').click(function(e){
			e.preventDefault();
			var link = $(this).attr('href');
			var $link = "" + link + " #contenttograb";
			
			$('#contenttograb').addClass('loading').load($link, function(){
				$('#contenttograb').removeClass('loading');
				$('#contenttograb #contenttograb').unwrap();
				loadNextEvents();
			});
			
		});
	}
	
	//kill dead links on the gigs table
	$('.bookingLink a').each(function(){
		var link = $(this);
		if(link.attr('href') === ""){
			link.click(function(e){
				e.preventDefault();
			});
			link.hover(
				function(){
					link.css({'color':'white', 'cursor':'default'});
				},
				function(){
					link.css('color','white');
				}
			);
		}
	});
	

	// HIDE ALL ELEMENTS WITH THE HIDE CLASS
	$('.hide').hide();
	$('#videos_player').css("left", "-99999px");
	
	
	// SIGNUP FORM STYLING FUNCTIONS
	$('#subscribe_email').focus(function(){
		$(this).val("");
		$(this).css("background", "transparent url(/wp-content/themes/lz7/images/black_50.png)");
		return false;
	});
	
	$('#subscribe_email').focusout(function(){
		if(!$(this).val()){
		$(this).css("background", "transparent url(/wp-content/themes/lz7/images/signup_back.png) no-repeat left");
		}
		return false;
	});	
	
	$('#subscribe_submit').hover(
	  function () {
		$('div#home_signup_form form input').css("border-top", "3px solid #aa0000");
		$('div#home_signup_form form input').css("border-bottom", "3px solid #aa0000");
		$('div#home_signup_form form input').css("border-left", "3px solid #aa0000");
	  }, 
	  function () {
		$('div#home_signup_form form input').css("border-top", "3px solid #fff");
		$('div#home_signup_form form input').css("border-bottom", "3px solid #fff");
		$('div#home_signup_form form input').css("border-left", "3px solid #fff");
	  }
	);
	
	$('#mce-EMAIL').focus(function(){
		$(this).val("");
		$(this).css("background", "transparent url(/wp-content/themes/lz7/images/black_50.png)");
		return false;
	});
	
	$('#mce-EMAIL').focusout(function(){
		if(!$(this).val()){
		$(this).css("background", "transparent url(/wp-content/themes/lz7/images/signup_back.png) no-repeat left");
		}
		return false;
	});	
	
	$('span.btn_signup').hover(
	  function () {
		$('div#home_signup_form form input').css("border-top", "3px solid #aa0000");
		$('div#home_signup_form form input').css("border-bottom", "3px solid #aa0000");
		$('div#home_signup_form form input').css("border-left", "3px solid #aa0000");
	  }, 
	  function () {
		$('div#home_signup_form form input').css("border-top", "3px solid #fff");
		$('div#home_signup_form form input').css("border-bottom", "3px solid #fff");
		$('div#home_signup_form form input').css("border-left", "3px solid #fff");
	  }
	);
	
	
	// HOME YOUTUBE VID OVERLAY HIDE FUNCTION
	$('div#home_player_overlay').click(function(){
		$('div#home_player').show();
		$(this).hide();
		return false;
	});
	
	
	// NAVIGATION FOLLOW SCROLL
	$( 'div#main_nav_container' ).scrollFollow( {
			speed: 400,
			offset: 40//,
			//killSwitch: 'exampleLink',
			//onText: 'Disable Follow',
			//offText: 'Enable Follow'
	} );

	
	// NAVIGATION BACKGROUND HOVER STATE
	$('div#main_nav_content').hover(
	  function () {
		$('div#main_nav').css("background", "transparent url(http://lz7.co.uk/wp-content/themes/lz7/images/nav_back.png) left bottom repeat-x");
	  }, 
	  function () {
		$('div#main_nav').css("background", "transparent url(http://lz7.co.uk/wp-content/themes/lz7/images/nav_back.png) left top repeat-x");
	  }
	);
	
	// SETUP CURRENTLY SELECTED SITE SECTION	
	function setSection(section){
		currSection = section;
	};
	
	$('div.navigation li.menu-item a').click(function(){
		$.scrollTo( $(this).attr('href'), 1400 );
		
		var sectionName = $(this).attr('href').substring(1);
		setSection(sectionName);
		
		return false;
	});
	
	$('div.subnavigation li.menu-item a').click(function(){
		$.scrollTo( $(this).attr('href'), 1400 );
		
		var sectionName = $(this).attr('href').substring(1);
		setSection(sectionName);
		
		/*
		$('div#main_nav').hide("slide", { direction: "left" }, 400, function () {
			$('div#main_nav_icon').show("slide", { direction: "left" }, 100);
		});
		*/
		
		return false;
	});
	
	
	// HIDE/SHOW CONTROLS/CONTENT
	$('span#hide_nav a').click(function(){
		$('div#main_nav').hide("slide", { direction: "left" }, 400, function () {
			$('div#main_nav_icon').show("slide", { direction: "left" }, 100);
		  });

		//$('div#main_nav_icon').show();
		return false;
	});
	$('div#main_nav_icon').click(function(){
		$('div#main_nav_icon').hide("slide", { direction: "left" }, 200, function () {
			$('div#main_nav').show("slide", { direction: "left" }, 400);
		  });
		return false;
	});
	$('span#hide_content a').click(function(){
		$('div.content').hide();
		$('span#hide_content').hide();
		$('span#show_content').show();
		return false;
	});
	$('span#show_content a').click(function(){
		$('span#show_content').hide();
		$('div.content').show();
		$('span#hide_content').show();
		return false;
	});
	

	///////////////////////////////////////
	// VIDEOS
	///////////////////////////////////////
	$('div#videos_player').hover(
	  function () {
		$('div.videos_overlay').hide();
		$('div#videos_overlay_title').show();
	  }, 
	  function () {
		$('div.videos_overlay').show();
		$('div#videos_overlay_title').hide();
	  }
	);
	
	$('div#videos_player_close').click(function(){
		$('div#videos_player').css("left", "-99999px");
		//$('div#videos_viewer').show();
		return false;
	});
	
	$('a.videos_player').click(function(){
		var title = $(this).attr('title');
		$('div#videos_overlay_title h1').html(title);
		Cufon.replace('div#videos_overlay_title h1', { fontFamily: 'Gotham' });
		return false;
	});
	
	
	///////////////////////////////////////
	// BLOG
	///////////////////////////////////////	
	$("div#blog_viewer").easySlider({
		//auto: true,
		//continuous: true,
		numeric: true,
		nextId: 'next_post',
		prevId: 'prev_post',
		nextText: '',
		prevText: '',
		numericId: 'post_controls'
	});
	
	$('span#next_post a, span#prev_post a, ol#post_controls li').click(function(){
		var days = $('div#blog_viewer ul li.current div#post_days').html();
		var month = $('div#blog_viewer ul li.current div#post_month').html();
		$('div#blog_date span#days').html(days);
		$('div#blog_date span#month').html(month);
		Cufon.replace('div#blog_date span#days, div#blog_date span#month', { fontFamily: 'Gotham' });
		return false;
	});
	
	var days = $('div#blog_viewer ul li#slide0 div#post_days').html();
	var month = $('div#blog_viewer ul li#slide0 div#post_month').html();
	$('div#blog_date span#days').html(days);
	$('div#blog_date span#month').html(month);
	Cufon.replace('div#blog_date span#days, div#blog_date span#month', { fontFamily: 'Gotham' });
	
	
	///////////////////////////////////////
	// SHARER
	///////////////////////////////////////	
	function urlencode(str) {
		// URL-encodes string  
		str = (str+'').toString();
		// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
		// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
		return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
	}
	//FANCYBOX
	$(".sharer").fancybox({
		'padding'			: 0,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000'
	});
	
	function getParams(name, info){
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec(info);
	  //var results = regex.exec( window.location.href ); //CURRENT URL
	  if( results == null )
		return "";
	  else
		return results[1];
	};
	
	function processShareLinks(info){
		var url = getParams('url', info);
		var title = getParams('title', info);
		var desc = getParams('desc', info);
		
		/*if(url) var share_url = urlencode(url);
		if(title) var share_title = urlencode(title);
		if(desc) var share_desc = urlencode(desc);*/
		
		if(url) var share_url = escape(url);
		if(title) var share_title = escape(title);
		if(desc) var share_desc = escape(desc);
		
		var share_digg = "http://digg.com/submit?phase=2&url=" + share_url +"%2F&title=" + share_title + "&bodytext=" + share_desc;
        var share_delicious = "http://delicious.com/post?url=" + share_url +"%2F&title=" + share_title + "&notes=" + share_desc;
        var share_facebook = "http://www.facebook.com/share.php?u=" + share_url +"%2F&t=" + share_desc;
        var share_twitter = "http://twitter.com/home?status=" + share_desc + "%20-%20" + share_url +"%2F";
        var share_myspace = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + share_url +"%2F&t=" + share_desc;
		
		$("div#sharer ul li a.share_digg").attr('href', share_digg);
		$("div#sharer ul li a.share_delicious").attr('href', share_delicious);
		$("div#sharer ul li a.share_facebook").attr('href', share_facebook);
		$("div#sharer ul li a.share_twitter").attr('href', share_twitter);
		$("div#sharer ul li a.share_myspace").attr('href', share_myspace);
	}
	$(".sharer").click(function(){
		var params = $(this).attr('name');
		processShareLinks(params);
		return false;
	});
	
	
	///////////////////////////////////////
	// CONTACT FORM
	///////////////////////////////////////	
	options = {
		//target:        '#contact_response',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
	};
	
	// bind to the form's submit event 
	$('#contact_form, #subscribe_form').submit(function() { 
		// inside event callbacks 'this' is the DOM element so we first 
		// wrap it in a jQuery object and then invoke ajaxSubmit		
		$(this).ajaxSubmit(options);
	
		// !!! Important !!! 
		// always return false to prevent standard browser submit and page navigation 
		return false; 
	});
	
	$('span#subscribe_submit').click(function(){
		$('#subscribe_form').ajaxSubmit(options);
	});
	
	// FORM INTERACTIVITY
	$('#contact_name').focus(function(){
		if(!$(this).val() || $(this).val() == "YOUR NAME..."){
			$(this).val("");
		};
		return false;
	});
	
	$('#contact_name').focusout(function(){
		if(!$(this).val()){
		$(this).val("YOUR NAME...");
		};
		return false;
	});
	
	$('#contact_email').focus(function(){
		if(!$(this).val() || $(this).val() == "YOUR EMAIL..."){
			$(this).val("");
		};
		return false;
	});
	$('#contact_email').focusout(function(){
		if(!$(this).val()){
		$(this).val("YOUR EMAIL...");
		};
		return false;
	});
	
	$('#contact_message').focus(function(){
		if(!$(this).val() || $(this).val() == "YOUR CHAT..."){
			$(this).val("");
		};
		return false;
	});
	$('#contact_message').focusout(function(){
		if(!$(this).val()){
		$(this).val("YOUR CHAT...");
		};
		return false;
	});
	
	var checked = true;
	$('#contact_subscriber, #contact_checkbox').click(function(){
		if(checked){
			$('#contact_checkbox').addClass("contact_unchecked").removeClass("contact_checked");
			checked = false;
			$('#contact_subscriber').val("false");
		}else{
			$('#contact_checkbox').addClass("contact_checked").removeClass("contact_unchecked");
			checked = true;
			$('#contact_subscriber').val("true");
		};
		return false;
	});
	
	/*$('span.btn_contact').click(function(){
		$('#contact_form').ajaxSubmit(options);
		return false;
	});*/
});



///////////////////////////////////////
// CONTACT FORM PRE + POST FUNCTIONS
///////////////////////////////////////
function showSignupResponse(){
	$.fancybox({
		'href'				: '#signup_response',
		'padding'			: 0,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000'
	});
}
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 	
	var form = jqForm[0];
	//alert(form.form_name.value);
	
	if(form.form_name == "contact"){
		if(form.name.value == "YOUR NAME..." || !form.name.value || form.name.value == " "){
			$('#signup_response').html('<h3>Please insert your name.</h3>');
			showSignupResponse();
			return false;	
		}
	}
	if(!validateEmail(form.email.value)){
		$('#signup_response').html("<h2>Schloop!</h2> <h3>Sorry. The email <span class=\"yellow\">" + form.email.value + "</span> isn't valid. Maybe check it through or try a different one.</h3>");
		showSignupResponse();
		return false;	
	}
    //alert('About to submit: \n\n' + queryString);
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	responseMessage = responseText;
	$('#signup_response').html(responseMessage);
	
 	$.fancybox({
		'href'				: '#signup_response',
		'padding'			: 0,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000'
	});
	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	//$('#contact_response').html(responseText);
}

function validateEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
