$(document).ready(function(){
	controlarFancybox();
	//controlarRollover();
	ciclo();
	if($("#formatos").length == 1){
		controlarTabla();
	}
	controlarVideo();
	if($("#mapaAmpliado").length == 1){
		ampliarMapa();
	}
	if($('form').length == 1){
		$('input, textarea').focus(function(){
			$(this).addClass('activo');
		});
		$('input, textarea').blur(function(){
			$(this).removeClass('activo');
		});
		if($('#contacto #nombre').length == 1){
			$('#nombre').focus();
		}
	}
	//clicable(".casillaProd");
	//clicable(".colTriple");
	//clicable(".bloqueNovedad");
	//clicable("#mejora");
	$('.casillaProd, #mejora').clicable();
});

function controlarFancybox(){
	$(".ampliar, a.ampliarSin").fancybox({
		'padding'           : 0,
		'zoomSpeedIn'		: 500, 
		'zoomSpeedOut'		: 500, 
		'overlayShow'		: true,
		'easingIn'			: 'easeOutBack',
		'easingOut'			: 'easeInBack',
		'overlayOpacity'	: 0.8,
		'centerOnScroll'	: true
	});
}

function controlarRollover(){
	$("#menu").children("li").unbind();
	$("#menu").children("li").hover(
		function(){
			if(!$(this).hasClass("on")){
				$(this).addClass("on");
			}
		},
		function(){
			if($(this).hasClass("on") && $(this).attr("id") != "act"){
				$(this).removeClass("on");
			}
		}
	);
}

function ciclo(){
		$('#banner').cycle({
		fx:     'fade',
		timeout: 5000
		});
	}
	
function controlarMenu($obj_animated, $obj_clicked){
			$obj_clicked.unbind('hover');
			$obj_animated.animate({height:'toggle', opacity: 'toggle'}, 'slow','',null);
			$obj_animated.children(".boton").click(function(){$obj_animated.animate({height:'hide', opacity: 'hide'}, 'slow');});
			$obj_animated.unbind('hover').hover(
				function () {
					
				},
				function () {
					$obj_animated.animate({height:'hide', opacity: 'hide'}, 'slow');
				}
			);
}

function controlarTabla(){
	$fila = $('#formatos tr');
	$fila.unbind('hover').hover( 
		function(){
			if($(this).children('td').children('h3').length == 0){
				$(this).children('td').css('background', '#e4eaf1');
			}
		},
		function(){
			if($(this).children('td').children('h3').length == 0){
			 $(this).children('td').css('background', 'transparent');
			}
		}
	);
	$fila.each( function(){
		if($(this).children('td').children('h3').length == 0){
			$(this).children('td:nth-child(1)').html('<img src="img/boton_ver_prod.png" width="37" height="34" alt="Ver producto" />');
			$(this).children('td:nth-child(1)').css('cursor','pointer');
		}
	});

	$fila.children('td:nth-child(1)').click(function(){
		if($(this).children('h3').length == 0){											 
			$(this).attr("id","x");
			ean = $.trim($('td#x + td').html());
			$(this).removeAttr("id");
			muestraProd(ean);
			
		}
	});
}


function muestraProd (ean) {
	error = "this.onerror=null;this.src='img/fichas_producto/ean/default_"+idiom+".gif'";
	switch (idiom){
		case "es":
		titulo = 'C&oacute;digo EAN';
		break;
		case "en":
		titulo = 'EAN Code';
		break;
		case "fr":
		titulo = 'Code EAN';
		break;
	}
	var dialog = $("<div id='ventana' title='"+titulo+": "+ean+"'><img src='img/fichas_producto/ean/"+ean+".jpg' onerror="+error+" width='408' height='306' /></div>").insertAfter('body');
	$(dialog).dialog({
		resizable: false,
		modal: false,
		overlay: { 
				opacity: 0.5, 
				background: "black" 
		},
		width: 406,
		height: 340
	});
	
	$('.ui-dialog-titlebar-close').click(function() {
		$("#ventana").dialog('destroy').remove();            
	})
	
}

function muestraAviso (idioma) {
	switch(idioma) {
		case "en":
			mensaje = "Web in updating process";
			titulo = "Notice";
			break;
		case "fr":
			mensaje = "Web en processus d&rsquo;actualisation";
			titulo = "Avis";
			break;
	}
	var dialog = $("<div id='ventana' title='"+titulo+"' ><p class='aviso azulOscuro'>"+mensaje+"</p></div>").insertAfter('body');
	$(dialog).dialog({
		resizable: false,
		draggable: true,
		modal: true,
		overlay: { 
				opacity: 0.8, 
				background: "black" 
		},
		width: 300,
		height: 160
	});
	
	$('.ui-dialog-titlebar-close').click(function() {
		$("#ventana").dialog('destroy').remove();            
	})
}

function muestraLegal (src, titulo) {
	var dialog = $("<div id='ventana' title='"+titulo+"' ><iframe src='"+src+"' frameborder='0' id='legal'></div>").insertAfter('body');
	$(dialog).dialog({
		resizable: false,
		draggable: true,
		modal: true,
		overlay: { 
				opacity: 0.8, 
				background: "black" 
		},
		width: 470,
		height: 470
	});
	
	$('.ui-dialog-titlebar-close').click(function() {
		$("#ventana").dialog('destroy').remove();            
	})
}

function controlarVideo (){
	var videos = new Array();
	var anchos = new Array();
	var altos = new Array();
	
	$(".video").each(function(){
		marcoVideo = $(this).attr("href");
		ancho = $(marcoVideo).children(".objeto").css("width");
		alto = $(marcoVideo).children(".objeto").css("height");
		ancho = Number(ancho.substr(0,Number(ancho.length-2)));
		alto = Number(alto.substr(0,Number(alto.length-2)));
		videos.push($(this));
		anchos.push(ancho);
		altos.push(alto);
		for (i=0;i<videos.length; i++){
			videos[i].fancybox ({
				'zoomSpeedIn'		: 500, 
				'zoomSpeedOut'		: 0, 
				'overlayShow'		: true,
				'easingIn'			: 'easeOutBack',
				'overlayOpacity'	: 0.8,
				'padding'           : 0,
				'centerOnScroll'	: true,
				'frameWidth'        : anchos[i],
				'frameHeight'       : altos[i],
				'callbackOnClose'   : function(){
										$("#fancy_div").children(".objeto").remove();
									  },
				'callbackOnShow'    : function(){
										$("#fancy_div").children(".objeto").css("visibility", "visible");
									  }
			});
		}
	});
}

function ampliarMapa (){
	$("#mapaAmpliado").fancybox ({
				'zoomSpeedIn'		: 500, 
				'zoomSpeedOut'		: 0, 
				'overlayShow'		: true,
				'easingIn'			: 'easeOutBack',
				'overlayOpacity'	: 0.8,
				'padding'           : 0,
				'centerOnScroll'	: true,
				'frameWidth'        : 600,
				'frameHeight'       : 400,
				'hideOnContentClick': false
			});
}

function alertar (mensaje, funcion) {
	var dialog = $("<div id='confirmar' title='Informaci&oacute;n'>"+mensaje+"</div>").insertAfter('body');
		$(dialog).dialog({
			resizable: false,
			modal: true,
			overlay: {
			opacity: 0.5,
			background: "black"
		},
	
		width: 400,
	
		buttons: {
			"Continuar": function() {		
				if ((typeof(funcion)=="undefined") || (funcion=="")) {
					$("#confirmar").dialog('destroy').remove();			
				} else {
					eval(funcion);
				}
			}
		}

});



$('.ui-dialog-titlebar-close').click(function() {

$("#confirmar").dialog('destroy').remove();

})

 

}
