// JavaScript Document
// Developed by Stuart Runyan

//Slider Variables
var position = $('#wrapper').attr('style');
var ml = 0;
var timer;
var timer_is_on = 0;

//Slider
function runTimer(operator)
{
	var position = $('#wrapper').attr('style');
	var operator = operator;
	if(position == 'left: 50px;')
	{
		//stop sliding left
		stopTimer();
		$('#wrapper').css('left','45px');
		return;
	}
	if(position == 'left: -3290px;')
	{
		//stop sliding right
		stopTimer();
		$('#wrapper').css('left','-3284px');
		return;
	}
	
	if(operator == 'left')
	{
		ml = ml + 10;
	}
	if(operator == 'right')
	{
		ml = ml - 10;	
	}
	
	$('#wrapper').css('left', ml);
	
}
function startTimer(operator) 
{
	if (!timer_is_on)
	  {
		  timer_is_on = 1;
		  timer = setInterval("runTimer('"+operator+"')", 1); 
	  }
}
function stopTimer()
{
	clearInterval(timer);
	timer_is_on = 0;
}


function animateDown(slide){
	$('#'+slide+'').find('.project-info').stop(true, true).slideDown(800);
}


//Form validation
function validateName(input)
{	
	if(input.value == '')
	{
		var error = 'enter your full name';
		$('.error').html(error);
		return;	
	}
}
function validateEmail(input)
{	
	if(input.value == '')
	{
		var error = 'enter a valid email address';
		$('.error').html(error);
		return;	
	}	
}
function validateMessage(input)
{	
	if(input.value == '')
	{
		var error = 'enter your message';
		$('.error').html(error);
		return;	
	}
}


//Functions to run on document load
$(document).ready(function(){
						   
	var scrollSpeed = 1600;
	
	//Set Javascript styles
	$('body').css({
		'overflow-x': 'hidden'
	});
	$('#wrapper').css({
		'width': '7000px',
		'position': 'relative'				  
	});	
	$('.slide').css({
		'float': 'left'
	});	
			
	//Left and Right Scrolling
	$('#left').css({
		'background-color': 'transparent',
		'background-image': 'url(images/side_glow_l.png)',
		'background-position':'left 170px',
		'background-repeat':'no-repeat'
	});
	$('#left').hover(
		function(){
			$('#left').css({'background-image': 'url(images/side_glow_l_ovr.png)'});
		}, function(){
			$('#left').css({'background-image': 'url(images/side_glow_l.png)'});
	});
	$('#right').css({
		'background-color': 'transparent',
		'background-image': 'url(images/side_glow_r.png)',
		'background-position':'left 170px',
		'background-repeat':'no-repeat'
	});
	$('#right').hover(
		function(){
			$('#right').css({'background-image': 'url(images/side_glow_r_ovr.png)'});
		}, function(){
			$('#right').css({'background-image': 'url(images/side_glow_r.png)'});
	});	   
	//Slider events
	$('#left').mouseover(function(){ startTimer('left'); });	
	$('#left').mouseout(function(){ stopTimer(); });	
	$('#right').mouseover(function(){ startTimer('right'); });	
	$('#right').mouseout(function(){ stopTimer(); });
	
	
	//Nav slide on click
	$("#nhome").click(function(event){
		$('#wrapper').stop(true, true).animate({'left':'0'},scrollSpeed);
		$('#wrapper').css('left','0px');
			ml = 0;
		event.preventDefault();
	});
	$("#nportfolio").click(function(event){
		$('#wrapper').stop(true, true).animate({'left':'-823px'},scrollSpeed);
		$('#wrapper').css('left','-823px');
			ml = -823;
		event.preventDefault();
	});
	$("#ncontact").click(function(event){
		$('#wrapper').stop(true, true).animate({'left':'-3284px'},scrollSpeed);
		$('#wrapper').css('left','-3284px');	
		ml = -3284;
		event.preventDefault();
	});

		
	//Portfolio slide hovers
	var timerClear = 0;
	$('.project-info').css('display','none');
	$('.portfolio-item').hover(
		function()
		{	
			$(this).find('.project-info').stop(true, true).slideDown(800);
		},
		function()
		{
			$(this).find('.project-info').stop(true, true).slideUp(400);
		}
	);
	
	//Column  hovers
	$('.column').hover(
		function(){
			$(this).stop().animate({backgroundColor: '#333333'}, 800);
		},
		function(){
			$(this).stop().animate({backgroundColor: '#484848'}, 800);
		}
	);
	
	//More paragraph
	$('#learn-more p').css('display','none');
	$('#learn-more .button').toggle(
	  function(){
		  $('#learn-more p').stop(true, true).slideDown(600);
	  }, function(){
  		  $('#learn-more p').stop(true, true).slideUp(600);
    });
	

	//call form validation
	/*$('#email').focusout(function(){validateEmail(this)});
	$('#name').focusout(function(){validateName(this)});
	$('#message').focusout(function(){validateMessage(this)});
	*/
	//Form Submitting
	$('.error').hide();
	$('#submit').click(function(){
		//Validate and process form here
		$('.error').hide();
		var name = $("input#name").val();  
		if (name == "") {  
			$("label#name_error").show();  
			$("input#name").focus();  
			return false;  
		}  
		var email = $("input#email").val();  
		if (email == "") {  
			$("label#email_error").show();  
			$("input#email").focus();  
			return false;  
		}  
		var message = $("textarea#message").val();  
		if (message == "") {  
			$("label#message_error").show();  
			$("textarea#message").focus();  
			return false;  
		}
		
		 var dataString = 'name='+ name + '&email=' + email + '&message=' + message;  
		 //alert (dataString);return false;  
		 $.ajax({  
		   type: "POST",  
		   url: "scripts/contact-form-processing.php",  
		   data: dataString,  
		   success: function() {  
			 $('#contact_form').html("<div id='success'></div>");  
			 $('#success').html("<h2>Contact Form Submitted!</h2>")  
			 .append("<p>I'll will be in touch soon.</p>")  
			 .hide()  
			 .fadeIn(1500, function() {  
			   $('#success').append("");  
			 });  
		   }  
		 });  
		 return false;
	});		   
	
	//Add beginning and ending html elements
	$('#slide1').before('<div id="where-left"><p>Where ya going?</p></div>');
	$('#slide5').after('<div id="where-right"><p>Where ya going?</p></div>');



});