var pozicija = 0;
var id = 1;
var visina = 220;
var mailSent = false;

function validacijaMail() {
	var ime = $('form#ContactForm > p:nth-child(1) input').val();
	var email = $('form#ContactForm > p:nth-child(2) input').val();
	var firma = $('form#ContactForm > p:nth-child(3) input').val();
	var poruka = $('form#ContactForm textarea').val();

	function emailValidan(email){
		var sablon = 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 sablon.test(email);
	}

	if(ime.length > 2 && firma.length > 2 && poruka.length > 5 && emailValidan(email)){
		var ideAjax = 'ime='+ ime + '&email=' + email + '&firma=' + firma + '&poruka=' + poruka;
		$.ajax({
			type: "POST",
			url: "php/mail.php",
			data: ideAjax,
			success: function(){
				mailSent = true;
				$('.contactInfo').css("margin-left","105px");
				$('#ContactForm').fadeOut('fast', function() {
				$('.ContactBoxRight').html('<p class="ty">Thank you for contacting <br /> Aligned Acquisitions Inc.</p>We will respond shortly.');
				$('.contentMask').animate({height: 340}, 300);
			});
		}});
	}
}
			
function pregledaj(sta){
	if((sta.val() == "" || sta.val() == " ") && (sta.css('opacity') > 0.45)){
		sta.fadeTo("slow", 0.4);
	}
}

function getVisina(){
	switch(id){
		case 1:
		visina = 220;
		$('.scrollL').fadeTo(0, 0.4);
		break;
		
		case 2:
		visina = 370;
		$('.scrollL').fadeTo(0, 1);
		$('.scrollR').fadeTo(0, 1);
		break;
		
		case 3:
		if (mailSent == false){
			visina = 670;
			$('.scrollR').fadeTo(0, 0.4);
		} 
		else {
			visina = 340;
			$('.scrollR').fadeTo(0, 0.4);
		}
		break;
		
		default:
		break;
	}
	
	return visina;
}

$(document).ready(function(){
	$('.scrollL').fadeTo(0, 0.4);
	$('input[type="text"], textarea').fadeTo(0, 0.4);
		
	$('input[type="text"], textarea').focus(function() {
		$(this).fadeTo("slow", 1);
	});
	
	$('input[type="text"], textarea').blur(function() {
		pregledaj($('form#ContactForm > p:nth-child(1) input'));
		pregledaj($('form#ContactForm > p:nth-child(2) input'));
		pregledaj($('form#ContactForm > p:nth-child(3) input'));
		pregledaj($('form#ContactForm textarea'));
	});
	
	$("#menu a:nth-child(2)").hover(
		function(){
			$("#menu a:nth-child(2) span").css("background-position","bottom");
		},
		function(){
			$("#menu a:nth-child(2) span").css("background-position","top");
		}
	);
	
	$("#menu a:nth-child(3)").hover(
		function(){
			$("#menu a:nth-child(3) span").css("background-position","bottom");
		},
		function(){
			$("#menu a:nth-child(3) span").css("background-position","top");
		}
	);
		
	$("#menu a:nth-child(4)").hover(
		function(){
			$("#menu a:nth-child(4) span").css("background-position","bottom");
		},
		function(){
			$("#menu a:nth-child(4) span").css("background-position","top");
		}
	);
	
	$("#menu a:nth-child(2), #footer p:nth-child(3) a:nth-child(1)").click(
		function(){
			scrollDir(1);
		}
	);
	
	$("#menu a:nth-child(3), #footer p:nth-child(3) a:nth-child(2)").click(
		function(){
			scrollDir(2);
		}
	);
		
	$("#menu a:nth-child(4), #footer p:nth-child(3) a:nth-child(3)").click(
		function(){
			scrollDir(3);
		}
	);
	
	$(".scrollR").bind('click', 
		function() {
			scrollCon("desno");
		}
	);
	
	$(".scrollL").bind('click', 
		function() {
			scrollCon("ljevo");
		}
	);
	
	function scrollCon(gdje){
		if(gdje == "ljevo" && pozicija < 0){
			pozicija += 660;
			id = id - 1;
			$('.contentWrapper').animate({left: pozicija}, 300);
			$('.contentMask').animate({height: getVisina()}, 300);
		} 
		
		else if(gdje == "desno" && pozicija > -1320) {
			pozicija -= 660;
			id = id + 1;
			$('.contentWrapper').animate({left: pozicija}, 300);
			$('.contentMask').animate({height: getVisina()}, 300);
		}
	}
	
	function scrollDir(index){
		switch(index){
			case 1:
			pozicija = 0;
			id = 1;
			$('.contentWrapper').animate({left: pozicija}, 300);
			$('.contentMask').animate({height: getVisina()}, 300);
			break;
			
			case 2:
			pozicija = -660;
			id = 2;
			$('.contentWrapper').animate({left: pozicija}, 300);
			$('.contentMask').animate({height: getVisina()}, 300);
			break;
			
			case 3:
			pozicija = -1320;
			id = 3;
			$('.contentWrapper').animate({left: pozicija}, 300);
			$('.contentMask').animate({height: getVisina()}, 300);
			break;
			
			default:
			break;
		}
	}
	
});	