jQuery(document).ready(function($) {

	var	nav				= $('#nav'),
		home 			= $('#home'),
		homeLink		= $('#homeLink'),

		about 			= $('#about'),
		aboutLink		= $('#aboutLink'),

		videos			= $('#videos'),
		videoLink		= $('#videoLink'),

		contact			= $('#contact'),
		contactLink		= $('#contactLink'),
		close			= $('.close')
		
		pages			= $('.page'),
		currentPage		= null,
		pageOpen 		= false,
		navShow			= false,
		videoPlayer		= $('#video_player iframe'),
		navBeingShown	= false,
		overlay			= $('#overlay');
	
	//Add Listeners
	homeLink.click(function() {showPage(home)});
	aboutLink.click(function() {showPage(about)});
	videoLink.click(function() {showPage(videos)});
	contactLink.click(function() {showPage(contact)});
	close.click(function() {closePage(currentPage)});;
	overlay.click(function() {closePage(currentPage)});;
	

	function showPage (page) {
		nav.hide('clip', 'fast', function() {
			page.show( 'clip', 'slow', function(){
				close.show('slow');
				pageOpen = true;
				page.jScrollPane({autoReinitialise:true});
			});
			currentPage = page;
			$('body').append(' <div id="overlay"></div> ')
			$('#overlay').click(function() {
					closePage();
					$('#overlay').remove();
			});
		});
		pages.slideUp('fast');
		
		return false;	
	}
	
	 function showNav () {
			nav.show('clip','slow')
	 }
	
	
	
	function closePage () {
		if (pageOpen == true) {
			close.hide();
			currentPage.hide('clip','slow',function  () {
				showNav();
				videoPlayer.hide();
				currentPage = '';
			});
			pageOpen = false;		
		};
	}
	
	function showVideo () {
		$(this).click(function(event) {
			$('img.vid_thumb').click(function() {
						var videoSrc = $(this).attr('data-videolink');
						videoPlayer.attr('src', videoSrc);
						videoPlayer.fadeIn();
			});
		});
	}
	
	
	//Contact Form Stuff
	
		
	
  //  	$('#contact_form').submit(function(e){
  //  		
  //  		//stop the form from being submitted
  //  		e.preventDefault();
  //  		
  //  		/* declare the variables, var error is the variable that we use on the end
  //  		to determine if there was an error or not */
  //  		var error 		= false;
  //  		var name 		= $('#name').val();
  //  		var email 		= $('#email').val();
  //  		var phone 		= $('#phone').val();
  //  		var message 	= $('#message').val();
  //  		var $data	 = "{name: "+name+", email: "+email+", phone: "+phone+", message: "+message+", Submit: true}";
  //  		var $form_data	= $('#contact_form').serialize();
  //  		
  //  		console.log($data);
  //  		
  //  		
  //  		if(name.length == 0){
  //  			var error = true;
  //  			$('#name_error').fadeIn(500);
  //  		}else{
  //  			$('#name_error').fadeOut(500);
  //  		}
  //  		if(email.length == 0 || email.indexOf('@') == '-1'){
  //  			var error = true;
  //  			$('#email_error').fadeIn(500);
  //  		}else{
  //  			$('#email_error').fadeOut(500);
  //  		}
  //  		if(phone.length == 0){
  //  			var error = true;
  //  			$('#phone_error').fadeIn(500);
  //  		}else{
  //  			$('#phone_error').fadeOut(500);
  //  		}
  //  		if(message.length == 0){
  //  			var error = true;
  //  			$('#message_error').fadeIn(500);
  //  		}else{
  //  			$('#message_error').fadeOut(500);
  //  		}
  //  		
  //  		if(error == false){
  //  			$('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
  //  			
  //  			$.post("includes/contact.php", $data ,function(result){
  //  					console.log(result);
  //  				if(result == 'sent'){
  //  					console.log("Post Sent");
  //  					 $('#button').remove();
  //  					$('#mail_success').fadeIn(500);
  //  				}else{
  //  					console.log("Post Not Sent");
  //  					$('#mail_fail').fadeIn(500);
  //  					$('#send_message').removeAttr('disabled').attr('value', 'Submit');
  //  				}
  //  			});
  //  		}
  //  	});
	
	
//	$('#send_message').click(function(e){
//		
//		//stop the form from being submitted
//		e.preventDefault();
//		
//		/* declare the variables, var error is the variable that we use on the end
//		to determine if there was an error or not */
//		var error = false;
//		var name = $('#name').val();
//		var email = $('#email').val();
//		var phone = $('#phone').val();
//		var message = $('#message').val();
//		
//		/* in the next section we do the checking by using VARIABLE.length
//		where VARIABLE is the variable we are checking (like name, email),
//		length is a javascript function to get the number of characters.
//		And as you can see if the num of characters is 0 we set the error
//		variable to true and show the name_error div with the fadeIn effect. 
//		if it's not 0 then we fadeOut the div( that's if the div is shown and
//		the error is fixed it fadesOut. 
//		
//		The only difference from these checks is the email checking, we have
//		email.indexOf('@') which checks if there is @ in the email input field.
//		This javascript function will return -1 if no occurence have been found.*/
//		if(name.length == 0){
//			var error = true;
//			$('#name_error').fadeIn(500);
//		}else{
//			$('#name_error').fadeOut(500);
//		}
//		if(email.length == 0 || email.indexOf('@') == '-1'){
//			var error = true;
//			$('#email_error').fadeIn(500);
//		}else{
//			$('#email_error').fadeOut(500);
//		}
//		if(message.length == 0){
//			var error = true;
//			$('#message_error').fadeIn(500);
//		}else{
//			$('#message_error').fadeOut(500);
//		}
//		
//		//now when the validation is done we check if the error variable is false (no errors)
//		if(error == false){
//			//disable the submit button to avoid spamming
//			//and change the button text to Sending...
//			$('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
//			
//			/* using the jquery's post(ajax) function and a lifesaver
//			function serialize() which gets all the data from the form
//			we submit it to send_email.php */
//			$.post("../includes/contact.php", $("#contact_form").serialize(),function(result){
//				//and after the ajax request ends we check the text returned
//				if(result == 'sent'){
//					//if the mail is sent remove the submit paragraph
//					 $('#button').remove();
//					//and show the mail success div with fadeIn
//					$('#mail_success').fadeIn(500);
//				}else{
//					//show the mail failed div
//					$('#mail_fail').fadeIn(500);
//					//reenable the submit button by removing attribute disabled and change the text back to Send The Message
//					$('#send_message').removeAttr('disabled').attr('value', 'Submit');
//				}
//			});
//		}
//	});
	
	
 //   $("#contact_form form").submit(function(event) {
 //       /* stop form from submitting normally */
 //       event.preventDefault(); 
 //
 //       /* get some values from elements on the page: */
 //       var $form	= $( this ),
 //           $name 	= $form.find( 'input[name="name"]' ).val(),
 //   		$email 	= $form.find( 'input[name="email"]' ).val(),
 //   		$phone 	= $form.find( 'input[name="phone"]' ).val(),
 //   		$message = $form.find( 'textarea[name="message"]' ).val(),
 //   		$data	 = "{ name: "+$name+", email: "+$email+", phone: "+$phone+", message: "+$message+", submit: true }"
 //           url = $form.attr( 'action' );
 //   
 //   		console.log($data);
 //
 //       /* Send the data using post and put the results in a div */
 //       $.post( "./includes/contact.php", $data ,function() {
 //   	  //optional stuff to do after success
 //   	  	console.log('Post Successful');
 //   	
 //   	});
 //   	
 //   
 //   	/* 	Hide the Form and display message */
 //   	//$form.hide('slow', function() {
 //   	//	$('#contact_form').append("<h2>Thank you for sending us a message.</h2>");
 //   	//});
 //   	
 //   });
  
		
	//Initialize the page
	function init () {
		pages.jScrollPane();
		pages.hide();
		close.hide();
		videoPlayer.hide();
		showVideo();
		
		//Plugins
		$( function() {
		  $.vegas({src:'images/background.jpg',valign:'top',});
		  $.vegas('overlay',{src:'overlays/05.png'});
		});
		
	}
	init();


});

