/* 
Developed by Codify Michael R. Wilson 
*/

var currentPage,totallines,totalpages,rotationInterval,interactionTimeout;

$(document).ready(function() {
	setvars();
	init();
	
});


function init(){
	$('.box.about').data('open', false);
	$('.button_about_us').click(function () { 
     	openAbout(!$('.box.about').data('open'))	
    });
    
    $('.box.contact').data('open', false);
	$('.button_contact_us').click(function () { 
     	openContact(!$('.box.contact').data('open'))	
    });
    
   

	$('.scrollbox .scrollbox_area .scroller .lines a.line').each(function(x){
		$(this).css('left',x*250+'px');
	
	})
	
	pageLines(0);
	startRotation();
	setForm();
	
}

function setvars(){
	currentPage = 0;
	totallines = $('.scrollbox .scrollbox_area .scroller .lines a.line').length;
	
	if(totallines%3 == 0){
		var add = 0;
	}else{
		var add = 1;
	}
	
	//totalpages = Math.floor(totallines/3) + add;
	totalpages = totallines - 3

}




function openAbout(arg){
	if(arg){
		openContact(false)
		$('.button_about_us').addClass('down');
		$('.box.about').data('open', true).slideDown('slow'); 
	}else{
		$('.button_about_us').removeClass('down');
		$('.box.about').data('open', false).slideUp("slow"); 
	}
}

function openContact(arg){
	if(arg){
		$('#after_submit').remove();
		openAbout(false)
		$('.button_contact_us').addClass('down');
		$('.box.contact').data('open', true).slideDown('slow'); 
	}else{
		$('.button_contact_us').removeClass('down');
		$('.box.contact').data('open', false).slideUp("slow"); 
	}
}


function pageLines(pagenum,slow){
	
	var spd;
	
	if(pagenum > totalpages) pagenum = 0;
	
	currentPage = pagenum;
	
	if(slow){
		spd = 1000;
	}else{
		spd = 200;
	}
	
	
	
	
	$('.lines').stop().animate({left: -currentPage * 250}, spd,function(){
		//enablebuttons(true)
	});
	
	if(currentPage == 0){
		$('.left_arrow').unbind('click').animate({opacity: 0}, 300);
	}else{
		$('.left_arrow').unbind('click').click(function () { 
			startInteraction();
     		pageLines(currentPage - 1);
    	}).animate({opacity: 1}, 100);
	}
	if(currentPage == totalpages){
		$('.right_arrow').unbind('click').animate({opacity: 0}, 300);
	}else{
		$('.right_arrow').unbind('click').click(function () { 
			startInteraction();
     		pageLines(currentPage + 1);
    	}).animate({opacity: 1}, 100);
	}
}

function startInteraction(){
	stopRotation();
	clearTimeout(interactionTimeout);
	interactionTimeout = setTimeout ( function(){startRotation()}, 10000 );
}

function startRotation(){
	rotationInterval = setInterval(function(){pageLines(currentPage + 1,true)},2000)
}

function stopRotation(){
	
	clearInterval(rotationInterval);
}

function setForm(){
	$('#Send').click(function() {  
		 	
		// name validation
		

		
		var nameVal = $("#name").val();
		if(nameVal == '') {
			
			$("#name_error").html('');
			$("#name").after('<span class="error" id="name_error">Please enter your name.</span>');
			return false
		}
		else
		{
			$("#name_error").html('');
		}
		
		/// email validation
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailaddressVal = $("#email").val();
		
		if(emailaddressVal == '') {
			$("#email_error").html('');
			$("#email").after('<span class="error" id="email_error">Please enter your email address.</span>');
			return false
		}
		else if(!emailReg.test(emailaddressVal)) {
			$("#email_error").html('');
			$("#email").after('<span class="error" id="email_error">Enter a valid email address.</span>');
			return false
		 
		}
		else
		{
			$("#email_error").html('');
		}
	
		$.post("form/post.php?"+$("#MYFORM").serialize(), {
	
		}, function(response){
		
	
		
		if(response==1)
		{
			$("#after_submit").html('');
			$("#Send").after('<span class="success" id="after_submit">Your message has been submitted.</span>');
			change_captcha();
			clear_form();
			setTimeout(function(){openContact(false)},2000);
		}else if(response==2){
			$("#after_submit").html('');
			$("#Send").after('<span class="error" id="after_submit">Error! Message Not sent.</span>');
		}else{
			$("#after_submit").html('');
			$("#Send").after('<span class="error" id="after_submit">Error! Invalid CAPTCHA code .</span>');
		}
		
		
	});
			
	return false;
	});
	
	// refresh captcha
	$('img#refresh').click(function() {  
		
		change_captcha();
	});
}

function change_captcha(){
	document.getElementById('captcha').src="form/get_captcha.php?rnd=" + Math.random();
}

function clear_form(){
	$("#name").val('');
	$("#email").val('');
	$("#message").val('');
	$("#code").val('');
}










