$(document).ready(function() {
						   
	//SLIDER CODE
	lastBlock = $("#common");
	maxWidth = 587;
	minWidth = 27;
	$(".slide").click(
		function(){
    	$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:800 });
		$(this).animate({width: maxWidth+"px"}, { queue:false, duration:800});
		lastBlock = this;
	  }
	);

	//SETTING UP OUR POPUP
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;

	//loading popup with jQuery magic!
	function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		}
	}

	//disabling popup with jQuery magic!
	function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		document.cform.subject.value = "";
		document.cform.message.value = "";
		popupStatus = 0;
		}
	}

	//centering popup
	function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$('#popupContact').fadeIn("slow");//this is the code to show the popup after send//
	$('.popupContact').fadeIn("slow");//this is the code to show the popup after send//
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	}
	
//CONTROLLING EVENTS IN jQuery
	//LOADING POPUP
	//Click the button event!
	$(".arrows").click(function(){
		//centering with css
		centerPopup();

		//load popup
		loadPopup();
	});
				
//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});


//submission scripts
  $('.popupContact').submit( function(){
		//statements to validate the form	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('e-mail');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.cform.name.value == "") {
			$('.name-missing').show();
		} else {$('.name-missing').hide();}	
		if (document.cform.ccompany.value == "") {
			$('.ccompany-missing').show();
		} else {$('.ccompany-missing').hide();}	
		if (document.cform.subject.value == "") {
			$('.subject-missing').show();
		} else {$('.subject-missing').hide();}	
		if (document.cform.message.value == "") {
			$('.message-missing').show();
		} else {$('.message-missing').hide();}		
		if ((document.cform.name.value == "") || (document.cform.ccompany.value == "") || (!filter.test(email.value)) || (document.cform.subject.value == "") || (document.cform.message.value == "")){
			return false;
		} 
		
		if ((document.cform.name.value != "") && (document.cform.ccompany.value != "") && (filter.test(email.value)) && (document.cform.subject.value != "") && (document.cform.message.value != "")) {
			//hide the form
			$('.popupContact').hide();
		
			//send the ajax request
			$.post('script.php',{name:$('#name').val(),
							  	ccompany:$('#ccompany').val(),
							 	email:$('#e-mail').val(),
							  	subject:$('#subject').val(),
							  	message:$('#message').val()},
		
			//return the data
			function(data){
				$('#popupContact').append(data);
			});
			
			//waits 2000, then closes the form and fades out
			setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#popupContact").slideUp("slow")', 4000);
			
			//stay on the page
			return false;
		} 
  });
	//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": document.documentElement.clientHeight 
	});  
});
