var carouselCount = 0,
	bloque_activo = 1,
	timeout,
	hastext = 0;

function bloqueActivo() {
	bloque_activo = $(this).index()  +  1;
}

function scrollTo(e) {
	e.preventDefault();

	var target = $("#menuwrap .scrollwrap:eq(" + (bloque_activo - 1) + ") .menu-scroll").data('jcarousel'),
		nuevo = $(this).text(),
		int_nuevo = parseFloat(nuevo);

	target.scroll(int_nuevo);
}

function marcarControl(carousel) {
	var index = carousel.first;
	$("#menuwrap .scrollwrap:eq(" + (bloque_activo - 1) + ") .menu-controls a").removeClass('activo');
	$("#menuwrap .scrollwrap:eq(" + (bloque_activo - 1) + ") .menu-controls a[rel = " + index + "]").addClass('activo');
}

function menuSlideInit(carousel, state) {
	var count = carousel.size(),
		i = 1;

	for (i = 1; i <= count;	i++) {
		$('#menuwrap .scrollwrap:eq(' + carouselCount + ')').find('.menu-controls').append("<a rel = '" + i + "'>" + i + "</a>");
	}

	$('#menuwrap .scrollwrap:eq(' + carouselCount + ') .menu-controls a:eq(0)').addClass('activo');

	carousel.clip.hover(function () {
		carousel.startAuto(1);
	}, function () {
		carousel.stopAuto();
	});

	carouselCount++;
}

function ocultaMenuTimeout() {
	timeout = window.setTimeout('ocultaMenu()', 500);
}

function startCarousel() {
	$(".menu-scroll").jcarousel({
		animation: 150,
		wrap: 'both',
		scroll: 1,
		initCallback: menuSlideInit,
		itemFirstInCallback: marcarControl,
		itemFallbackDimension: 119
	});
}

function muestraMenu() {
	var cat = $(this).attr('rel'),
		url = "/plantillas/menuitems.php?cat=" + cat;

	clearTimeout(timeout);

	$("#menuwrap").fadeOut(300, function () {

		$(this).empty();

		carouselCount = 0;
		$("#menuwrap").load(url, function () {
			$("#menuwrap").fadeIn(300, function () {
				$(".menu-scroll").jcarousel({
					animation: 150,
					wrap: 'both',
					scroll: 1,
					initCallback: menuSlideInit,
					itemFirstInCallback: marcarControl,
					itemFallbackDimension: 119
				});
			});
		});
	});

	$("#menu_desplegable").slideDown(300);
}

function ocultaMenu() {
	$("#menu_desplegable").slideUp(300);
}
function borraTimeout() {
	clearTimeout(timeout);
}

function esconder_globos(nombre_globo) {
	$(nombre_globo).hide();
}
function clearLabel() {
	if (hastext == 0) {
		$(this).val('').addClass('on');
		hastext = 1;
	}
}
function addLabel() {
	if (!$(this).val()) {
		$(this).val('Buscar...').removeClass('on');
		hastext = 0;
	} else {
		hastext = 1;
	}
}

$(document).ready(function () {
	$("#search").focus(clearLabel);
	$("#search").blur(addLabel);
	$('#slider').nivoSlider({
		effect: 'fade',
		slices: 1,
		boxCols: 1,
		boxRows: 1,
		controlNavThumbs: true,
		controlNavThumbsFromRel: true
	});
	$("a.single_image").fancybox({
		'cyclic': true
	});
	$('#result').hide();
	$('#form-contacto').show();

	$('#form-contacto').submit(function () {
		$('#form-contacto').fadeOut();
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function (data) {
				$('#result').html(data);
				$('#result').fadeIn(400);
			}
		});
		return false;
	});

	$(".globo").mouseover(function () {
		clearTimeout(timer);
	});
	$('#globo_te').hide();
	$('#globo_cafe').hide();
	$('#globo_aire_acondicionado').hide();
	$('#globo_maquina_agua').hide();
	$('#globo_maquina_agua2').hide();
	$('#globo_pedestal').hide();
	$('#globo_filtros_urinarios').hide();
	$('#globo_contenedor_higiene').hide();
	$('#globo_pisos_negros').hide();
	$('#globo_cafetera').hide();
	$('#showroom_te,#globo_te').mouseover(function () {
		$('#globo_te').fadeIn(300);
	});
	$('#showroom_te,#globo_te').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_te')", 300);
	});
	$('#showroom_cafe,#globo_cafe').mouseover(function () {
		$('#globo_cafe').fadeIn(300);
	});
	$('#showroom_cafe,#globo_cafe').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_cafe')", 300);
	});
	$('#showroom_aire_acondicionado,#globo_aire_acondicionado').mouseover(function () {
		$('#globo_aire_acondicionado').fadeIn(300);
	});
	$('#showroom_aire_acondicionado,#globo_aire_acondicionado').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_aire_acondicionado')", 300);
	});

	$('#showroom_maquina_agua,#globo_maquina_agua').mouseover(function () {
		$('#globo_maquina_agua').fadeIn(300);
	});
	$('#showroom_maquina_agua,#globo_maquina_agua').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_maquina_agua')", 300);
	});
	$('#showroom_maquina_agua2,#globo_maquina_agua2').mouseover(function () {
		$('#globo_maquina_agua2').fadeIn(300);
	});
	$('#showroom_maquina_agua2,#globo_maquina_agua2').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_maquina_agua2')", 300);
	});
	$('#showroom_pedestal,#globo_pedestal').mouseover(function () {
		$('#globo_pedestal').fadeIn(300);
	});
	$('#showroom_pedestal,#globo_pedestal').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_pedestal')", 300);
	});
	$('#showroom_contenedor_higiene,#globo_contenedor_higiene').mouseover(function () {
		$('#globo_contenedor_higiene').fadeIn(300);
	});
	$('#showroom_contenedor_higiene,#globo_contenedor_higiene').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_contenedor_higiene')", 300);
	});
	$('#showroom_filtros_urinarios,#globo_filtros_urinarios').mouseover(function () {
		$('#globo_filtros_urinarios').fadeIn(300);
	});
	$('#showroom_filtros_urinarios,#globo_filtros_urinarios').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_filtros_urinarios')", 300);
	});
	$('#showroom_cafetera,#globo_cafetera').mouseover(function () {
		$('#globo_cafetera').fadeIn(300);
	});
	$('#showroom_cafetera,#globo_cafetera').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_cafetera')",300);
	});
	$('#showroom_pisos_negros,#globo_pisos_negros').mouseover(function () {
		$('#globo_pisos_negros').fadeIn(300);
	});
	$('#showroom_pisos_negros,#globo_pisos_negros').mouseout(function () {
		timer = setTimeout("esconder_globos('#globo_pisos_negros')", 300);
	});
	$('#menu_primario a').hover(muestraMenu,ocultaMenuTimeout);
	$(".scrollwrap").live('mouseover',bloqueActivo);
	$(".menu-controls a").live('click',scrollTo);
	$(".menu-controls a[rel = '1']").addClass('activo');
	$('#menu_desplegable').live('mouseenter',borraTimeout);
	$('#menu_desplegable').mouseleave(ocultaMenuTimeout);
	$('#mensaje').limit('300','#count_left')
}
);

