function calculateStars(){
	var result = 0;
	var selectsValues = $('.stars_selects select');
	var voteText = ['Terrible / Evitar','Mala / Para Pocos','Regular / Pasable','Bueno / Normal','Muy Bueno / Vale la Pena','Excelente / Recomendable'];
	selectsValues.each(function(){
		result = result + parseInt($(this).val());
	});
	result = Math.round(result / selectsValues.length);
	
	$('.stars_show').empty();
	for(i=0;i<5;i++) {
		if(i < result) {
			$('.stars_show').append('<img src="/img/img_green_star.gif" alt="" border="0"');
		} else {
			$('.stars_show').append('<img src="/img/img_gray_star.gif" alt="" border="0"');
		}
	}
	
	for(var i in voteText){
		if(i == result) {
			$('.stars_text').empty();
			$('.stars_text').append(voteText[i]);
			//console.log(i);
		}
	}
}

$(document).ready(function(){
	calculateStars();
	$('.stars_selects select').change(function(){
		calculateStars();
	});
	
//////////////	MENU	////////////////////////

	$("ul#parentmenu > li > a").click(function(){	
		//console.log($(this).text());
		if($(this).siblings("ul.childmenu").css("display") == "none"){
			
			$("ul.childmenu").hide("slow");
			$(this).siblings("ul.childmenu").show("fast");
		
		}else{
			
			$(this).siblings("ul.childmenu").hide("slow");
			
			}
			return false;
		});
	/*	FIX FOR IE6 MENU 	*/
	$("ul.childmenu > li > a").hover(function(){
	
		$(this).parent("li").addClass("hover");
	
	},function(){
	
		$(this).parent("li").removeClass("hover");
	
	});

////////////////////	END MENU	///////////////////////////////////	
	
	$(".picture table tr td a#select_picture").click(function(){
	
		
		if($(".picture table tr td .pick_selec").css("display") == "none"){
			
			$(this).siblings(".div_pick_selec").children(".pick_selec").slideToggle("fast");
			$(".errorFile").remove();
			//return false;
			
			}else{
				
			$(this).siblings(".div_pick_selec").children(".pick_selec").hide("slow");	
			
			
			}
	return false;
	});
	
	$(".box_pick a").click(function(){
		
		$('.content_picture'+' img').attr("src", $(this).children('img').attr("src"));
		
		$('#UserDefaultImage').val($(this).children('img').attr("src"));
		
		$(this).parent().parent().hide("fast");	
		
		$("input#AvatarTypeUpload").val("predefined");//set input hidden for predefined image in register
		
		return false;
		
	});
	
	//set input hidden for upload image in register
	$("input#UserArchive").click(function(){
	
		$("input#AvatarTypeUpload").val("upload");
		return true;
	});
	
	$("#UserLoginForm input.a").focus(function () {
									   
	if (!($(this).css("backgorund") == "#ffffff")){
		
		 $("#UserLoginForm input.a").css('background','#FFFFFF');
         $(this).css('background','#FEFF99');
	}else{
		
		$(this).css('background','#FFFFFF');
		}
    });
	
	$("#UserRegisterForm input.a").focus(function () {
									   
	if (!($(this).css("background") == "#ffffff")){
		
		 $("#UserRegisterForm input.a").css('background','#FFFFFF');
         $(this).css('background','#FEFF99');
	}else{
		
		$(this).css('background','#FFFFFF');
		}
    });
	
	
	
	$(".top .left a.bluelight").click(function(){
	
		$(".locationBox").slideToggle("slow");
		return false;
	});
	
	if(navigator.appVersion.indexOf("MSIE 6.")>=0){
	$("button.usereditSubmit").hover(function(){

		$(this).css("background","url(/img/btn-enviar-hover.gif) no-repeat");
	
	},function(){
	
		$(this).css("background","url(/img/img_button_enviar.jpg) no-repeat");
	
	});
	}
	
});

function hoverbuttons(){

	$("#overShadow button.usereditSubmit").hover(function(){

		$(this).css("background","url(/img/btn-enviar-hover.gif) no-repeat");
	
	},function(){
	
		$(this).css("background","url(/img/img_button_enviar.jpg) no-repeat");
	
	});
	
	$("#overShadow button.btnReportar").hover(function(){

		$(this).css("background","url(/img/btn-reportar-hover.gif) no-repeat");
	
	},function(){
	
		$(this).css("background","url(/img/btn-reportar.gif) no-repeat");
	
	});
	
}
/////////////	SHADOW FUNCTIONS 	//////////////////////////////

function showshadow(datos){
	
	//$("form#UserUsereditForm select").hide();
	if(navigator.appVersion.indexOf("MSIE 6.")>=0) $("#all select").css("visibility","hidden");
		
	$("#shadow").css("display","block");
	$("body").append('<div id="overShadow"><div class="contentShadow"></div></div>');
	$("#shadow").fadeTo(1000, 0.4,function(){
						
		var garbage = randomvar();
		datos = datos + '/' + garbage;
							
		$.get(datos,{},function(data){
			$(".contentShadow").html(data);
			
			
			$("#UserReportAbuseForm").ajaxForm({
				success:showResponse
			});
			
			$("#UserUserMessageForm").ajaxForm({
				success:showResponse
			});
			
			$("#UserNewCategoryForm").ajaxForm({
				success:showResponse
			});
			
			$("#UserLostPasswordForm").ajaxForm({
				success:showResponse
			});
			
			$("#UserRecommendToFriendForm").ajaxForm({
				success:showResponse
			});
			
			$("#UserNewCityForm").ajaxForm({
				success:showResponse
			});

			$("#UserRecommendLista3Form").ajaxForm({
				success:showResponse
			});				
			
			hoverbuttons();
		});
		
	});
}

function showResponse(responseText, statusText)  {
  
	$(".contentShadow").html(responseText);  
	
	$("#UserReportAbuseForm").ajaxForm({
		success:showResponse
	});
	
	$("#UserUserMessageForm").ajaxForm({
		success:showResponse
	});
	
	$("#UserNewCategoryForm").ajaxForm({
		success:showResponse
	});
     
	$("#UserLostPasswordForm").ajaxForm({
		success:showResponse
	});
	
	$("#UserRecommendToFriendForm").ajaxForm({
		success:showResponse
	});
	
	$("#UserNewCityForm").ajaxForm({
		success:showResponse
	});
	
	hoverbuttons();
		 
}


function closeShadow(){
	
  $(".contentShadow").remove();
	$("#overShadow").remove();

	$("#shadow").fadeTo(1000, 0,function(){
    $("#shadow").css("display","none");
  });
 //$("form#UserUsereditForm select").show();
	if(navigator.appVersion.indexOf("MSIE 6.")>=0)$("#all select").css("visibility","visible");
}

function randomvar(){
	var rnd = Math.round((Math.random())*1000);
	var string = '?irnd='+rnd;
	return string;
}

/*	Funciones auxiliares	*/
function valRegister(){
	var ok = true;
			
			if($("#UserPassword").val().length < 4)
			{
				ok = false;
				if($("#UserPassword").siblings().is(".error"))$("#UserPassword").siblings(".error").remove();	
				$("#UserPassword").after("<span class='error'>Su contraseña debe tener un mínimo de 4 caracteres.</span>");
				
			}else{
				
				if($("#UserPassword").siblings().is(".error"))$("#UserPassword").siblings(".error").remove();
			}
			
			if(!$("input[@name='data[User][sex]']").is(":checked"))
			{
				ok = false;				
				if($("#UserSexHombre").siblings().is(".error"))$("#UserSexHombre").siblings(".error").remove();				
				$("#UserSexHombre").after("<span class='error'>Tildar una opción.</span>");
				
			}else{
				
				if($("#UserSexHombre").siblings().is(".error"))$("#UserSexHombre").siblings(".error").remove();				
			}
			
			if($("#UserBirthdateDay").val() == "" || $("#UserBirthdateMonth").val() == "" || $("#UserBirthdateYear").val() == "")
			{
				ok = false;				
				if($("#UserBirthdateYear").siblings().is(".error"))$("#UserBirthdateYear").siblings(".error").remove();			
				$("#UserBirthdateYear").after("<span class='error'>Por favor, ingresa tu fecha de nacimiento.</span>");
				
			}else{
				if($("#UserBirthdateYear").siblings().is(".error"))$("#UserBirthdateYear").siblings(".error").remove();
				
			}
			
			if($("input#UserArchive").val() == "" && $("input#UserDefaultImage").val() == "")
			{
				ok = false;				
				if($("#select_picture").siblings().is(".error"))$("#select_picture").siblings(".error").remove();
				$("#select_picture").after("<span class='error errorFile'>Por favor, sube o elije una imagen para que te represente.</span>");
				
			}else{
				if($("#select_picture").siblings().is(".error"))$("#select_picture").siblings(".error").remove();
				
			}
			
			if(!$("input#UserAgree").is(":checked") )
			{
				ok = false;				
				if($("#UserAgree").siblings().is(".error"))$("#UserAgree").siblings(".error").remove();			
				$("#UserAgree").before("<span class='error'>Tildar aquí: </span>");
				
			}else{
				if($("#UserAgree").siblings().is(".error"))$("#UserAgree").siblings(".error").remove();			
			}
			
	return ok;
}

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					
}

