//declaramos variables
id_producto_activo=1;
var id_mensaje_activo=1;
var id_mensaje_anterior=1;
var z_index=1;
var dmCantidad;
var tmCambiarCantidad;

var fnCambiarCantidad = function(nCantidad)
{
	if( nCantidad < 0 )
	{
		if( $("#cart_quantity" ).val() == dmCantidad )
		{
			clearInterval( tmCambiarCantidad );
			return;
		}
	}
	
	$("#cart_quantity" ).val( parseInt($("#cart_quantity" ).val()) + nCantidad );
};

var fnCheckCantidad = function()
{
	 if( parseInt($("#cart_quantity" ).val()) < dmCantidad )
		$("#cart_quantity" ).val( parseInt(dmCantidad) );
}

$(document).ready(function(){
	var tmCheckCantidad;

	if( $("#cart_quantity" ) )
	{
		dmCantidad = $("#cart_quantity" ).val();
	}

	$("#cart_quantity").keyup( function()
	{
		clearInterval( tmCheckCantidad );
		tmCheckCantidad = setTimeout(fnCheckCantidad,250);
	});

	$("#mas_cantidad").click( function()
	{
		fnCambiarCantidad(1);
	});
	
	$("#mas_cantidad").mousedown( function()
	{
		clearInterval( tmCambiarCantidad );
		tmCambiarCantidad = setInterval( "fnCambiarCantidad(1)", 150 );
	});
	
	$("#mas_cantidad").mouseup( function()
	{
		clearInterval( tmCambiarCantidad );
	});
	
	$("#menos_cantidad").click( function()
	{
		fnCambiarCantidad(-1);
	});
	
	$("#menos_cantidad").mousedown( function()
	{
		clearInterval( tmCambiarCantidad );
		tmCambiarCantidad = setInterval( "fnCambiarCantidad(-1)", 150 );
	});
	
	$("#menos_cantidad").mouseup( function()
	{
		clearInterval( tmCambiarCantidad );
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$(".menu li").hover(
      function () {
		$(this).find('ul').fadeIn(200);
      }, 
      function () {
		$(this).find('ul').fadeOut(300);
      }
    );
	$(".menu li ul").hover(
      function () {
		$('#informacion').addClass("hover");
      }, 
      function () {
		$('#informacion').removeClass("hover");
      }
    );
	
	$("#cabecerabuscar").focus(function()
	{
		$(this).val( ($(this).val() == "Buscar..." ? "" : $(this).val() ) );
	}).blur(function()
	{
		$(this).val( ($(this).val() == "" ? "Buscar..." : $(this).val() ) );
	});
	
	$("#checkoutbotoncarro").click(function(e)
	{
		e.stopPropagation();
		dmForm = $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent().parent();
		
		dmForm.attr("action", dmForm.attr("action") + "&comprar=true");
		dmForm.submit();
		
		return false;
	});
	
/*	$("#atributos_productos").change(function (){
		var v = $(this).val();
		var texto = $("#atributos_productos option[value="+v+"]").text();
		texto = texto.replace('EUR', '');
		
		array = texto.split('(');
		suplemento=parseFloat(array[1]);
		if (suplemento > 0) {} else { suplemento=0;}
			precio = $('#precio_original').text();
			precio = precio.replace('EUR', '');
			precio=parseFloat(precio);
			precio_total=(precio+suplemento)+'€';
			$('#precio_producto').fadeOut(150).fadeIn(125).text(precio_total);
			//$('#precio_producto').text(precio_total);
			//$('#precio_producto').show(50);
	});*/
	$(".categorias .categorias_fondo ul .pestana").click(function (){
		$(".categorias .categorias_fondo ul a").removeClass('hover');
		$(this).addClass('hover');

		$(".submenu").hide();
		$(this).parent('li').find('ul').fadeIn(150);
		hijos=$(this).parent('li').find('ul').children().size(); 
		if (hijos > 0)	return false;
	});	
	$('.activo').parent('li').find('ul').fadeIn(150);
	$('.activo').addClass('hover');
//animar mensajes
	
	$('#mensaje_'+id_mensaje_activo).animate({
		opacity: 1,
		top: '0px'
	}, 400, function() {
		//terminado
	});	
	
	setInterval("animar_mensajes()",4500);
	setInterval("reproducir_destacados()",6000);
	$('#paginacion_enlace_' + id_producto_activo).addClass('hover');
	$("#producto_destacado_"+id_producto_activo).fadeIn(400);
	/* productos destacados  */
	$(".paginacion_enlace").click(function (){
		$(".paginacion_enlace").removeClass('hover');
		$(this).addClass('hover');
		$(".mini_productos_destacados").fadeOut(450);
		id=$(this).attr('rel');
		$("#producto_destacado_"+id).fadeIn(400);
		id_producto_activo=parseInt(id);
	});
});
	
function animar_mensajes() {
	var num_maximo=$('.mensajes').children().size(); 
	id_mensaje_anterior=id_mensaje_activo;


	
	$('#mensaje_'+id_mensaje_activo).animate({
			opacity: 0,
			top: '-30px'
		}, 400, function() {
			//terminado
	});
	if (id_mensaje_activo>=num_maximo) {
		id_mensaje_activo=1;
	}
	else
	{
		id_mensaje_activo=id_mensaje_activo+1;	
	}
	$('#mensaje_'+id_mensaje_activo).css('z-index', z_index)
	$('#mensaje_'+id_mensaje_activo).animate({
			opacity: 1,
			top: '0px'
		}, 400, function() {
		//terminado
	});	
	$('#mensaje_'+id_mensaje_anterior).animate({
			top: '30px'
		}, 0, function() {
		//terminado
	});	
	z_index=z_index+1;
	
}
function reproducir_destacados() {
		var num_maximo_destacados=$('#slide').children().size(); 
		
		if (id_producto_activo>=num_maximo_destacados) {
			id_producto_activo=1;
		}
		else
		{
			id_producto_activo=id_producto_activo+1;
		}
		
		$(".paginacion_enlace").removeClass('hover');
		$('#paginacion_enlace_' + id_producto_activo).addClass('hover');
		$(".mini_productos_destacados").fadeOut(450);
		id=$('#paginacion_enlace_' + id_producto_activo).attr('rel');
		$("#producto_destacado_"+id_producto_activo).fadeIn(400);
}
