$(window).load(function() {
	//$("#menu_list li div.sub_1_col").parent("li").mouseover(function(){ $(this).children("div.sub_1_col").css("display","block"); });
	//$("#menu_list li div.sub_1_col").parent("li").mouseout(function(){ $(this).children("div.sub_1_col").css("display","none"); });
	$("#menu_list li div.sub_2_col").parent("li").mouseover(function(){ $(this).children("div.sub_2_col").css("display","block"); });
	$("#menu_list li div.sub_2_col").parent("li").mouseout(function(){ $(this).children("div.sub_2_col").css("display","none"); });
	
	$("#menu_list li div.sub_2_col").parent("li").mouseover(function(){ 
			$('.compartir').css("z-index","-1"); 
			$("#desple").css("z-index","-1"); 
			$("#list_4_col li .img").css("z-index","-1");
	});
	$("#menu_list li div.sub_2_col").parent("li").mouseout(function(){ 
			$('.compartir').css("z-index","10");
			$("#desple").css("z-index","999");
			$("#list_4_col li .img").css("z-index","1");
	});
	
	$("#menu_list li div.sub_1_col").parent("li").mouseover(function(){ 
		$(this).children("div.sub_1_col").css("display","block"); 
		$("#desple").css("z-index","-1"); 
		$("#list_4_col li .img").css("z-index","-1");
	});
	$("#menu_list li div.sub_1_col").parent("li").mouseout(function(){ 
			$(this).children("div.sub_1_col").css("display","none"); 
			$("#desple").css("z-index","999");
			$("#list_4_col li .img").css("z-index","1");
		});
	
	$('select#elegir_caract').selectmenu({style:'dropdown'});
	$("#desple").css("position","relative");
	
	ancho_video = 320;
	alto_video = 165;
	if ($.browser.msie || ($.browser.version == 7.0)) {
		ancho_total = ancho_video+5;
		alto_total = alto_video+45+25;
 	}else{
		ancho_total = ancho_video+5;
		alto_total = alto_video+45;
	}

	if ($("#elegir_caract").length>0){
		$('#elegir_caract').selectmenu({style:'dropdown'}).change(function(){if($(this).val()!=""){window.location=$(this).val();}return false;});
	}

	if ($('.ad-gallery').length>0){
		var galleries = $('.ad-gallery').adGallery({description_wrapper: false, effect: 'fade'});
	}

	if ($('#list_3_col').length>0) $('#list_3_col').equalHeights(3);
	if ($('#list_4_col').length>0) $('#list_4_col').equalHeights(4);
	if ($('#list_5_col').length>0) $('#list_5_col').equalHeights(5);
	
	if ($(".pop_log").length>0)		$(".pop_log").colorbox({iframe:true, width:ancho_total+20+"px", height:alto_total+30+"px"});
	if ($(".pop_no_log").length>0)	$(".pop_no_log").colorbox({iframe:true, width:"390px", height:"420px"});
	//if ($(".pop_act").length>0)		$(".pop_act").colorbox({iframe:true, width:"460px", height:"755px"});
	if ($(".pop_fans").length>0)	$(".pop_fans").colorbox({iframe:true, width:"460px", height:"495px"});
	if ($(".pop_preg").length>0)	$(".pop_preg").colorbox({iframe:true, width:"430px", height:"430px"});
	if ($(".pop_video").length>0){
		ancho_video = 640;
		alto_video = 385;
		
		if ($.browser.msie || ($.browser.version == 7.0)) {
			ancho_total = ancho_video+5;
			alto_total = alto_video+45+25;
	 	}else{
			ancho_total = ancho_video+5;
			alto_total = alto_video+45;
		}
		$(".pop_video").colorbox({iframe:true, width:ancho_total+"px", height:alto_total+"px"});
	}
	
	var barra_width = $("#puntos_historia").width()+30;
	//var barra_width = $("#puntos_historia").width()-30;
	var selector = $("#puntos_historia li");
	var num_puntos = selector.length;
	var primer_punto = selector.eq(0).find("a").text();
	var ultimo_punto = selector.eq(num_puntos-1).find("a").text();
	var distancia = barra_width / num_puntos;
	var suma = 0;
	
	selector.each(function(i){
		
		/*
		var actual_punto = selector.eq(i).find("a").text();
		var resta = 0;
		var anterior = 0;
		
		if(i > 0) anterior_punto = selector.eq(i-1).find("a").text();
		else anterior_punto = 0;
		if (( (actual_punto-anterior_punto)<=2) && i>0) {
			var lis = selector.eq(i-1);
			var lis_sin_px = lis.css("left").replace('px', '');
			lis.css("left", (lis_sin_px-30)+"px");
			suma = 30;
		}else{
			suma = 0;
		}
		calculo = (barra_width / (ultimo_punto - primer_punto)) * (actual_punto - primer_punto);
		*/
		calculo = distancia * i;
		//selector.eq(i).css("left", calculo+suma);
		selector.eq(i).css("left", calculo);
		selector.eq(i).find("a").css("visibility","visible");
	});

	if($('#cont_prof').length>0){
		$('#cont_prof #nombre').attr('tabindex',1);
		$('#cont_prof #apellidos').attr('tabindex',2);
		$('#cont_prof #cargo').attr('tabindex',3);
		$('#cont_prof #empresa').attr('tabindex',4);
		$('#cont_prof #act_emp').attr('tabindex',5);
		$('#cont_prof #web').attr('tabindex',6);
		$('#cont_prof #email').attr('tabindex',7);
		$('#cont_prof #email_conf').attr('tabindex',8);
		$('#cont_prof #pais').attr('tabindex',9);
		$('#cont_prof #tipo_dir').attr('tabindex',10);
		$('#cont_prof #dir').attr('tabindex',11);
		$('#cont_prof #num').attr('tabindex',12);
		$('#cont_prof #esc').attr('tabindex',13);
		$('#cont_prof #pis').attr('tabindex',14);
		$('#cont_prof #pue').attr('tabindex',15);
		$('#cont_prof #cp').attr('tabindex',16);
		$('#cont_prof #pob').attr('tabindex',17);
		$('#cont_prof #prov').attr('tabindex',18);
		$('#cont_prof #tel_mov').attr('tabindex',19);
		$('#cont_prof #tel_fijo').attr('tabindex',20);
		$('#cont_prof #fax').attr('tabindex',21);
		$('#cont_prof #comentarios').attr('tabindex',22);
		$('#cont_prof #caracteres').attr('tabindex',23);
		$('#cont_prof #bt_enviar').attr('tabindex',24);
	}
	$.datepicker.setDefaults($.datepicker.regional['es']);
	/*
	if ($("#fecha_nac").length>0){
		$("#fecha_nac").datepicker({
			changeMonth: true,
			changeYear: true,
			yearRange: '1930:2011',
			dateFormat: 'dd-mm-yy',
			firstDay: 1,
			gotoCurrent: true
		});
	}
	*/
	

	if ($('.equipo').length>0){

		ancho_video = 400;
		alto_video = 500;
		
		if ($.browser.msie || ($.browser.version == 7.0)) {
			ancho_total = ancho_video+5;
			alto_total = alto_video+45+25;
	 	}else{
			ancho_total = ancho_video+5;
			alto_total = alto_video+45;
		}

		$(".pop_equipo").colorbox({iframe:true, width:ancho_total+"px", height:alto_total+"px"});
		
		$('.equipo').click(function(){
			$('.aten_col li a.activo').removeClass('activo');
			$('.equipo_detalle').css("display","none");
			$(this).addClass('activo');
			$('.'+$(this).attr('id')).attr('style','display:visible');
		});
		
		$(".equipo_detalle").css("display","none");

	}
	
	
	if($('span.wpcf7-not-valid-tip-no-ajax').length>0){
		$('.wpcf7-form-control-wrap').find(':input').focus(function() {
			$(this).parent('span').find('span.wpcf7-not-valid-tip-no-ajax').not(':hidden').fadeOut('fast');
		});
	}
	if ($(".dif_sel").length>0){
		$(".dif_sel").dropdownchecklist( { forceMultiple: true, onComplete: function(selector) {
			var values = "";
			for( i=0; i < selector.options.length; i++ ) {
				if (selector.options[i].selected && (selector.options[i].value != "")) {
					if ( values != "" ) values += ";";
					values += selector.options[i].value;
				}
			}
		} });
	}
	
	/*if ($("#cp").length>0){
		$("#cp").blur( function() {
			$.getJSON('/wp-content/themes/beter/cp.php?codpostal='+$(this).val(), function(data) {
				$('#prov').val(data.provincia);
				$('#pob').val(data.poblacion);
				if($('#prv').length>0){
					$('#prv').val(data.provincia);
					$('#pobb').val(data.poblacion);
				}
			});
		});
	}*/
	
	if ($("#cp").length>0)
	{
		$("#cp").blur( function()
		{
			if(document.getElementById('pais').value.substring(0,3)=='Esp')
			{
				if(document.getElementById('loadingCP1')!=null){document.getElementById('loadingCP1').style.display='';}
				if(document.getElementById('loadingCP2')!=null){document.getElementById('loadingCP2').style.display='';}
				document.getElementById('provinciaSpan').style.display='none';
				document.getElementById('municipioSpan').style.display='none';
				document.getElementById('pob').style.display='none';
				document.getElementById('pob2').style.display='none';
				document.getElementById('prov').style.display='none';
			}

			$.getJSON('/wp-content/themes/beter/cp.php?codpostal='+$(this).val(), function(data)
			{
				if(document.getElementById('pais').value.substring(0,3)!='Esp')
				{
					document.getElementById('provinciaSpan').style.display='none';
					document.getElementById('municipioSpan').style.display='none';
					document.getElementById('pob').style.display='';
					document.getElementById('prov').style.display='';
					return;
				}
				
				if(document.getElementById('loadingCP1')!=null){document.getElementById('loadingCP1').style.display='none';}
				if(document.getElementById('loadingCP2')!=null){document.getElementById('loadingCP2').style.display='none';}


				document.getElementById('provinciaSpan').style.display='';
				document.getElementById('municipioSpan').style.display='';
				document.getElementById('pob').style.display='none';
				document.getElementById('prov').style.display='none';

				var sel = document.getElementById('pob2');

				while(sel.childNodes.length>0)
				{
					var victim = sel.lastChild;
					sel.removeChild(victim);
				}

				// No se ha encontrado el código postal
				if(data.poblacion.length==0)
				{
					$('#prov').val('');
					$('#pob').val('');
					document.getElementById('municipioSpan').style.display='';
					document.getElementById('pob2').style.display='none';
				}

				// Sólo hay un municipio
				if(data.poblacion.length==1)
				{
					$('#prov').val(data.provincia);
					$('#pob').val(data.poblacion[0].name);
					document.getElementById('municipioSpan').style.display='';
					document.getElementById('pob2').style.display='none';
				}

				if(data.poblacion.length>1)
				{
					$('#prov').val(data.provincia);
					$('#pob').val(data.selected);
					document.getElementById('municipioSpan').style.display='none';

					sel.style.display='';
					

					for(x=0;x<data.poblacion.length;x++)
					{
						var opt = document.createElement('option');
						opt.value = data.poblacion[x].name;
						opt.appendChild(document.createTextNode(data.poblacion[x].name));
						sel.appendChild(opt);
						sel.value = data.selected;
					}
				}

				document.getElementById('municipioSpan').innerHTML = document.getElementById('pob').value;
				document.getElementById('provinciaSpan').innerHTML = document.getElementById('prov').value;
			});
		});
	}
	
	if ($('#enlaces').length>0) $('#enlaces').equalHeights(3);
	
	if ($('#mapa_web_list').length>0) $('#mapa_web_list').equalHeights(3);

	if ($('#recaptcha').length>0){
		$('#recaptcha').click(function() {
			$.getJSON('/wp-content/themes/beter/recaptcha.php', function(data) {
				$('#imgcaptcha').attr("src",data.img);
				$('#prefix').val(data.prefix);
			});
			return false;
		});
	}
	
	if ($('#slide_home').length>0){
		$.fn.loopedSlider.defaults.addPagination = true;
		$.fn.loopedSlider.defaults.autoStart = 10000;
		$('#slide_home').loopedSlider();
	}
	
	if ($('#modal').length>0){
		$('#modal').dialog({ modal: true, resizable: false, autoOpen: false });
	}

	
	$.textarea_comment = function($p) {	
		if ($p.length>0){
			$.comment = $p.val();
			$p.click( function(){
				if ($p.val()==$.comment){
					$p.val("");
				}
			}).blur( function(){
				if ($p.val()==""){
					$p.val($.comment);
				}
			});
		}
	}
	$.textarea_comment($('#comentario'));
	$.textarea_comment($('#comment'));
	
});
function beter_update_product(_action, _key, _newvalue)
{
	$('#modal').dialog('open');
	$.get('/wp-content/plugins/wp-e-commerce/themes/beter/checkout_functions.php', { action: _action, key: _key, newvalue: _newvalue }, 
			function(data){
				//alert("data: "+data+" - action: "+_action);
				//alert("subtotal: "+data.subtotal);
				//alert("total: "+data.total);
				$('#modal').dialog('close');
				if(_action == "remove"){
					if(data == "error"){
						alert("Error!");
					}else{
						$("#tbl_prod_"+_key).fadeOut("slow");
						$("#subtotal_carrito").html(data.subtotal);
						$("#total_carrito").html(data.total);
					}
				}else if(_action == "quantity"){
					if(data == "error"){
						alert("Error!");
					}else{
						$("#col_total_"+_key).html(data.prod);
						$("#subtotal_carrito").html(data.subtotal);
						$("#total_carrito").html(data.total);
					}
				}else if(_action == "country"){
					if(data == "error"){
						alert("Error!");
					}else{
						$("#total_shipping").html(data.tarifa);
						$("#total_carrito").html(data.total);
						if(data.cupon=="true" || data.cupon == true){
							$("#promocion_detalle_num").html("- "+data.tarifa);
						}
					}
				}
			}, "json"
	);
}

function open_loginpop(){
	ancho_video = 320;
	alto_video = 165;
	if ($.browser.msie || ($.browser.version == 7.0)) {
		ancho_total = ancho_video+5;
		alto_total = alto_video+45+25;
 	}else{
		ancho_total = ancho_video+5;
		alto_total = alto_video+45;
	}
	$.colorbox({iframe:true, width:ancho_total+20+"px", height:alto_total+30+"px"});
}

function PaisChanged(sender)
{
	document.getElementById('provinciaSpan').style.display = 'none';
	document.getElementById('municipioSpan').style.display = 'none';
	document.getElementById('prov').style.display = 'none';
	document.getElementById('pob').style.display='none';
	document.getElementById('pob2').style.display='none';

	if(sender.value.substring(0,3)=="Esp")
	{
		if(isNaN(document.getElementById('cp').value))
		{
			document.getElementById('cp').value='';
		}

		document.getElementById('cp').focus();

		if(document.getElementById('usuario')!=null && document.getElementById('usuario').value=='Usuario')
		{
			document.getElementById('usuario').focus();
		}
		else
		{
                     // JUAN CASTILLA - Para poder hacer pasar al test.eshop como anónimo en PRODUCTIVO
			if(document.getElementById('pais').style.display!='none')
                     {
                         document.getElementById('pais').focus();
                     }
                     else
                     {
                         document.getElementById('nombre').focus();
                     }
		}
		
		if(document.getElementById('tel_mov')!=null)
		{
			if(isNaN(document.getElementById('tel_mov').value))
			{
				document.getElementById('tel_mov').value = '';
			}
		}
		
		if(document.getElementById('tel_fijo')!=null)
		{
			if(isNaN(document.getElementById('tel_fijo').value))
			{
				document.getElementById('tel_fijo').value = '';
			}
		}
		
		//ahoms. Esconder tipo via para no España
		document.getElementById('tipo_dir').style.display = 'block';
		document.getElementById('label_address').style.display = 'inline-block';
		
	}
	else
	{
		document.getElementById('pob').style.display='';
		document.getElementById('prov').style.display='';
		
		//ahoms. Esconder tipo via para no España
		document.getElementById('tipo_dir').style.display = 'none';
		document.getElementById('label_address').style.display = 'block';
	}
}

function CPIsNumeric(e, sender)
{
	if(document.getElementById('pais').value.substring(0,3)=="Esp")
	{
		var charCode = (e.which) ? e.which : event.keyCode;
	    //if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;

		if (
			(
				(e.keyCode > 47 && e.keyCode < 58) 
				|| (e.keyCode > 95 && e.keyCode < 106)
				|| (e.keyCode > 36 && e.keyCode < 41) 
				|| e.keyCode == 8 
				|| e.keyCode == 46
				|| e.keyCode == 27
				|| e.keyCode == 45
				|| e.keyCode == 35
				|| e.keyCode == 36
				|| e.keyCode == 9
				|| e.keyCode == 13
				)
				&& 
				(
					e.altKey !== true && e.shiftKey !== true && e.metaKey !== true
				) 
			) 
		{
			return true
		}
		else 
		{
			return false;
		}	
	}
}

function IsNumeric(e, sender)
{
	var charCode = (e.which) ? e.which : event.keyCode;
	//if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;

	if (
		(
			(e.keyCode > 47 && e.keyCode < 58) 
			|| (e.keyCode > 95 && e.keyCode < 106)
			|| (e.keyCode > 36 && e.keyCode < 41) 
			|| e.keyCode == 8 
			|| e.keyCode == 46
			|| e.keyCode == 27
			|| e.keyCode == 45
			|| e.keyCode == 35
			|| e.keyCode == 36
			|| e.keyCode == 9
			|| e.keyCode == 13
			)
			&& 
			(
				e.altKey !== true && e.shiftKey !== true && e.metaKey !== true
			) 
		) 
	{
		return true
	}
	else 
	{
		return false;
	}		
}

function IsPostalCode(text)
{
	var re = /^\d{5}([\-]\d{4})?$/;
	return (re.test(text));
}
