var ventana=0;

function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(200);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

function imprime(cual){
	var imagen = cual.childNodes[0];
	var alto = imagen.clientHeight+40;
	var ancho = imagen.clientWidth+20;
	
	var src = imagen.src;
	
	window.open('../promos.php?prom='+src, 'promo',"status=0,toolbar=0,location=0,menubar=0,directories=0,height="+alto+",width="+ancho);
}

function promo(cual){
	ventana ++;
	window.open(cual + '.php', 'club' + ventana,"status=0,toolbar=0,location=0,menubar=0,Scrollbars=1,directories=0,height=320,width=620,resizable=1");
}

var listaAfiliados = '[{}]';
var ie6 = false;


function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 

function arreglaIE6(){
	ie6 = $('html').hasClass('ie6');
}


$(document).ready(function(){
	mainmenu();
	$("#internalMenu").css({display: "none"});
	cargaForm();
	cargaAfiliados();
	if($('#center').height()<400){
		$('#center').css("min-height","400px");
	}
	if($('#empresa')){
		$('#clienteCont').hide();
		$('input.cliente').bind("click",function(){
			if($(this).attr("value")=="Si"){
				$('#clienteCont').show(300);
				$('#empresa').blur(function(){
					var valido = validateField(this,$(this).attr("validate"));
					if(valido){
						$(this).removeClass("error");
						$(this).attr("valid",true);
					}else{
						$(this).addClass("error");
						$(this).attr("valid",false);
					}
					var formValido=formValid();
					if(!formValido){
						$('#botnPrograme').attr("disabled",true);	
					}else{
						$('#botnPrograme').attr("disabled", false);	
					}
				});
				$('#dniC').blur(function(){
					var valido = validateField(this,$(this).attr("validate"));
					if(valido){
						$(this).removeClass("error");
						$(this).attr("valid",true);
					}else{
						$(this).addClass("error");
						$(this).attr("valid",false);
					}
					
				});
			}else{
				$('#clienteCont').hide();
				$('#empresa').removeClass("error").attr("valid",true);
				$('#dniC').removeClass("error").attr("valid",true);
			}
		});
	}
});

function cargaForm(){
		$('#loading').show();
		$.get(jsonLogin, {}, function(j){
			var orig = $('#search').html();
			$('#searchForm').html(j);
			if($('#promos').length != 0){
				if ($('#linkClub').length != 0) {
					$('#linkClub').show();
					$('#promos').hide();
					$('#centerIndex').css("height","500px");
				}
			}
			$('#loading').hide();
			cargaLogin();
			cargaSearch();
			cargaAfiliados();
			arreglaClub();
	});
}

function cargaLogin(){
	
$('#loginForm').bind("submit",function(){
	
	var dni=$('#dni').val();
	var check=$('#cookie:checked').val();
		
	$('#loginForm').hide();
	$('#loading').show();

	$.get(jsonLogin, {dni: dni, cookie: check}, function(j){
		$('#loading').hide();
		if (j == "error") {
			$('#dni').val('');
			$('#loginForm').show();
			
		}
		else {
			$('#searchForm').html(j);
			if ($('#linkClub').length == 0) {
			/* Pagina Interna */
			}
			else {
				$('#linkClub').show();
				$('#promos').hide();
				$('#centerIndex').css("height","500px");
			}
		}
		$('#loading').hide();
		cargaLogin();
		cargaSearch();
		cargaAfiliados();
		var ubica=window.location.pathname;
		if(ubica.substring(ubica.length-5,ubica.length-1)=="club"){window.location.reload();}
		arreglaClub();
	});
	return false;
});

}

function arreglaClub(){
	var divP=$('#promos>a');
	if(divP){
		divP.attr('href',urlClub);
		$('#promos>a>img').attr('src',urlImg + 'promociones.jpg');
	}
}

