$(document).ready(function() {
	Shadowbox.init();
	
		
	$(".up ul li").hover(function(){
		
		
		$(this).children("a.proj").css("background-position","0px -41px");
		$(this).children("a.serv").css("background-position","0px -40px");
		$(this).children(".submenu").children("ul").show("fast");
		
	},function(){
	  
	   $(this).children("a.proj").css("background-position","0px 0px");
	   $(this).children("a.serv").css("background-position","0px 0px");
	   $(this).children(".submenu").children("ul").hide("fast");
		
	});

	
	/*Estas lineas son de las imagenes que cambian con un click en current projects...*/
		
	$(".ven_pict #location").click (function(){			
			
			$(".images").css("display","none");
			$(".location").css("z-index","1");
			setEmailus();
			setHeightW();
			return false;
			}
	
	);
	
	$(".ven_pict #images").click (function(){
	
			$(".location").css("z-index","-1");
			$(".images").css("display","block");
			setEmailus();
			setHeightW();
			return false;
			}	
	);
						   
					   
	$(".main .left .down .down_in ul li:last-child").css("background","none");
	
	/*	 contact us	*/
	$("form#SettingContactusForm").find(".required").keyup(function(){
		if(this.value == "")
		{
			//$(this).siblings(".error").remove();
			//$(this).after("<p class='error' style='font-size:10px;color:#f00;margin:0;padding:0;text-align:left;'>This field is required.</p>");
			$(this).addClass("redBorder");

		}else {
			//$(this).siblings(".error").remove();
			$(this).removeClass("redBorder");
		}
   });
	$("form#SettingContactusForm").find(".required").filter(".email").keyup(function(){
		if(!valid_email(this.value))
		{
			//$(this).siblings(".error").remove();
			//$(this).after("<p class='error' style='font-size:10px;color:#f00;margin:0;padding:0;text-align:left;'>This field is required.</p>");
			$(this).addClass("redBorder");

		}else {
			//$(this).siblings(".error").remove();
			$(this).removeClass("redBorder");
		}
   });
   
   $("form#emailus").find(".required").not(".email").each(function(){
		$(this).keyup(function(){
			if(IsBlank($(this).val()) || $(this).val() == 'Name' || $(this).val() == 'Comments' || $(this).val() == 'Bussiness Name') 
			{					
				if($(this).siblings("span.error").length == 0 ){ 
					$(this).siblings("span.error").remove();					
					$(this).after('<span class="error">Required</span>');
					$(this).siblings("span.error").css("display","block");
				}
			}
			else
			{
				$(this).siblings("span.error").remove();
			}
		});
		
	});


	$("form#emailus").find(".email").filter(".required").each(function(){
		$(this).keyup(function(){
			if(!valid_email($(this).val())){
				
				if($(this).siblings("span.error").length == 0 ){ 
					$(this).siblings("span.error").remove();					
					$(this).after('<span class="error">Email is invalid.</span>');
					$(this).siblings("span.error").css("display","block");
				}
				
			}else{		
				
				$(this).siblings("span.error").remove();
				
			}	
		});
	});

});
//SHOW AND HIDE IMAGES IN current projects 
/*
@target -> element to show
@alltarget -> class or id that is shared for all elements
@thisCtrl -> element with onclick event (or parents)
@allCtrl-> class or id that is shared for all elements with onclick (or parents)
*/
function showAndHide(target, allTarget, thisCtrl, allCtrl){

	$(allTarget).css("display","none");
	$(target).css("display","block");
	$(allCtrl).removeClass("active");
	$(thisCtrl).addClass("active");
	setHeightW();
}

/*Validaciòn de los campos por id (mas detallado...)*/

function validate()  /*Nombre de la función que se retornará en el fomulario*/
	{
	var form = document.form; //name
	var ok = true;
	//Validating First Name
	var name = document.getElementById('MessageFirstName').value;
	if (IsBlank(name))
	{
		//alert ("Please enter a valid First Name!");
		$("input#MessageFirstName").addClass("redBorder");
		$("input#MessageFirstName").focus();
		ok = false;
		//return false;
	}

	//Validate Last Name
	var lname = document.getElementById('MessageLastName').value;
	if (IsBlank(lname))
	{
		//alert ("Please enter a valid Last Name!");
		$("input#MessageLastName").addClass("redBorder");
		if(ok)$("input#MessageLastName").focus();
		//document.getElementById('MessageLastName').focus();
		ok = false;
		//return false;
	}

	//Validate Email
	var email= document.getElementById('MessageEmail').value;
	if ( ( IsBlank(email) ) || ( !valid_email(email) ) )
	{
		/*alert ("Please enter a valid email address!");
		document.getElementById('MessageEmail').focus();*/
		$("input#MessageEmail").addClass("redBorder");
		if(ok)$("input#MessageEmail").focus();
		ok = false;
		//return false;
    }
	
    //Validate Comment
	var comment = document.getElementById('MessageComments').value;
	if (IsBlank(comment))
	{
		/*alert ("Please fill the field Comment!");
		document.getElementById('MessageComments').focus();	*/
		$("textarea#MessageComments").addClass("redBorder");
		if(ok)$("textarea#MessageComments").focus();
		ok = false;
		//return false;
	}
	
	
//    form.submit();
	return ok;
}




/*funciòn que no deja pasar si está en blanco algún campo...*/
function IsBlank (strString)
{
	if (strString.length == 0)
		return true;

	for (i = 0; i < strString.length; i++)
	{
		strChar = strString.charAt(i);
		if (strChar != " ")
			return false;
	}
	return true;
}



/*funcciòn que valida que el email sea escrito correctamente*/
function valid_email(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	 if (str.indexOf(" ")!=-1){
		return false;
	 }
	 return true
}
/**/
function convert(form){

						// find out which option is selected then
						// assign the value of the selected option 

							  var i = form.fromunits.selectedIndex;
							  var fromvalue = form.fromunits.options[i].value;
							  var j = form.tounits.selectedIndex;
							  var tovalue = form.tounits.options[j].value;

						// perform the conversion
						   form.tovalue.value = (form.fromvalue.value * fromvalue) / tovalue;
						 return true;
						}

function clearfield(form)
{
	form.fromvalue.value = "";
	return true;
}


function validnum(s)
{
     // Check for number
	 num = new RegExp(/^(?:\+|-)?\d+$/);
     if (!num.test(s)) {
          return false;
     }
	return true;
}

/*	new form ajaxSubmit*/
function closeForm(){

	$(".closeForm").slideUp(500,showQ);

}
function showQ(){

	setTimeout(function(){
		$(".questionsEmailus").slideDown();	
		},600)

}
function saveForm(){

	$("form#emailus").ajaxSubmit({
        success: showMsg	
	});

}

function showMsg(){
	
	$(".questionsContForm").hide();
	$(".questionsContMsg").show();

}

function openForm(){
	
	$(".questionsEmailus").slideUp(200,showForm);
	//$(".closeForm").slideDown();	
}

function showForm(){

	setTimeout(function(){
		$(".closeForm").slideDown(500)
		$(".questionsContForm").show();
		$(".questionsContMsg").hide();		
		},300)

}

function validateEmailus(formId)
	{
		//var form = document.form;
		var ok = true;
		
		$("form#"+formId).find(".required").not(".email").each(function(){
			if(IsBlank($(this).val()) || $(this).val() == 'Name' || $(this).val() == 'Comments' || $(this).val() == 'Bussiness Name') // si el valor del input está vacío…
			{		
				ok = false;
				if($(this).siblings("span.error").length == 0 ){ 
					$(this).siblings("span.error").remove();
					//$(this).css("border","1px solid #f00");
					$(this).after('<span class="error">Required</span>');
					$(this).siblings("span.error").css("display","block");
				}
			}
			else    // si no está vacío el campo . . . 
			{
				//$(this).css("border","1px solid #969696");
				$(this).siblings("span.error").remove();
			}
		});
	
	
		$("form#"+formId).find(".email").filter(".required").each(function(){ 
			if(!valid_email($(this).val())){ // este input es pasado por la función ‘valid_email()’ … si no es correcto…
				ok = false; // el ok sera ‘false’
				if($(this).siblings("span.error").length == 0 ){ 
					$(this).siblings("span.error").remove();
					//$(this).css("border","1px solid #f00");
					$(this).after('<span class="error">Email is invalid.</span>');
					$(this).siblings("span.error").css("display","block");
				}
				
			}else{
			
				//$(this).css("border","1px solid #969696");
				$(this).siblings("span.error").remove();
				
			}			
		});
		
		if(ok) saveForm();
		else return false;
	}
