$(document).ready(function() {
	var inicio = Math.floor(Math.random() * (10));
	$('#destacados_main').cycle({ 
		fx: 'fade', 
		speed: 500, 
		timeout: 0,
		startingSlide: inicio,
		pager:  '#destacados_menu_nada',
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#"><img id="logo" src="/img/menu_' + slide.id + '.jpg" width="156" /></a></li>'; 
		} 
	});
	
	$('.titulo').mouseover(function() {
		$('.nosotros_main_top').css('backgroundImage','url(/img/bgs/' + $(this).attr('id') + '.jpg)').fadeIn();
		//$('#' + 'texto_' + $(this).attr('id')).siblings().hide();
		$('#' + 'texto_' + $(this).attr('id')).fadeIn();
	});
	
	$('.titulo').mouseout(function() {
		//$('.nosotros_main_top').css('backgroundImage','url(/img/bgs/nosotros.jpg)').fadeIn();
		//$('#' + 'texto_' + $(this).attr('id')).siblings().hide();
		$('#' + 'texto_' + $(this).attr('id')).fadeOut();
	});
	
	$('span.titulo').each(function() {
		$(this).css('background', 'url(/img/texts/' + $(this).attr('id') + '.gif' + ') no-repeat left top');
	});
	
	$('#producto_imagenes li a').click(function () {
		$('#producto_imagenes li a').attr('class', '');
		$(this).attr('class', 'active');
		$('#producto_main').css('background', 'url(/img/products/details/' + $(this).attr('id') + '.jpg) 0 28px no-repeat');
		$('#zoom').attr('href', '/img/products/' + $(this).attr('id') + '_full.jpg');
	});
	
	$("#zoom").fancybox({
		'overlayOpacity':0.5,
		'overlayColor':'#FFF',
		'zoomSpeedIn':600,
		'zoomSpeedOut':400
	});
	
	$('#menu li').hoverIntent(function () {
		$(this).find('.desplegable').css('margin-left', '-' + (88 - $(this).width() / 2) + 'px').slideDown(200);
		
	},
	function() {
        $(this).find('.desplegable').slideUp(100);
    });
	
	$('#marcas li').click(function () {
		if ($(this).is(".selected")) {
			$(this).removeClass("selected");
		}
		else {
			$(this).addClass("selected");
		}
	});
	
	$('#buscar input').focus(function () {
		if ($(this).val() == "Buscar...")
			$(this).val('');
	});
	
	$('#buscar input').blur(function () {
		if ($(this).val() == "")
			$(this).val('Buscar...');
	});
	
	$('#buscar .campo').keyup(function () {
		$(this).parent().attr('action', '/buscar/productos/' + $(this).val());
	});
	
	$('#paginacion_top, #paginacion').each(function () {
		if ($(this).html().replace(/\s+/g,'') == "")
			$(this).css('display', 'none');
	});
	
	$("#cuadros").easySlider({
		continuous: false,
		prevId: 'prev_destacados',
		prevText: '« Anterior',
		nextId: 'next_destacados',	
		nextText: 'Siguiente »',
		firstShow: false,
		lastShow: false,
		botonera: 'botones_destacados', 
		speed: 700
	});
	
	$("#cuadros_chico").easySlider({
		continuous: false,
		prevId: 'prev_relacionados',
		prevText: '« Anterior',
		nextId: 'next_relacionados',	
		nextText: 'Siguiente »',
		firstShow: false,
		lastShow: false,
		botonera: 'botones_relacionados', 
		speed: 700
	});

	
	$(".listaLiveSearch input.campoLiveSearch").focus(function() {
		var pos = $(this).position();
		$(this).parent().find(".liveSearch").css({top: pos.top + $(this).outerHeight(), left: pos.left}).animate({opacity: "show", width: $(this).outerWidth() - 6}, 350);
	});
	
	$(".listaLiveSearch input.campoLiveSearch").blur(function() {
		$(this).parent().find(".liveSearch").animate({opacity: "hide", width: "0"}, 350);
	});
	
	$(".campoLiveSearch").each(function() {
		$(this).liveUpdate($(this).next(".liveSearch").find(".conLiveSearch"));
	});
	
	$(".campoLiveSearch").attr("autocomplete", "off");
	
	$(".conLiveSearch li").click(function() {
		$(this).parents().filter(".listaLiveSearch").find(".campoLiveSearch").val($(this).html());
	});
	
	$('#contacto_tema_pestana span').click(function() {
		$('#' + 'contacto_tema_' + $(this).attr('class')).siblings().hide();
		$('#' + 'contacto_tema_' + $(this).attr('class')).show();
		$(this).addClass('selected');
		$(this).siblings().removeClass('selected');
	});
	
	$('input[type="radio"]').click(function() {
		if ($(this).attr('id') != "") {
			$('input[type="radio"]').removeAttr('disabled');
			$('input.[type="radio"]').closest('tr').removeClass('deshabilitado');
			$('input.' + $(this).attr('id')).attr('disabled', true);
			$('input.' + $(this).attr('id')).closest('tr').addClass('deshabilitado');
		}
	});
	
	$('span.boton').click(function() {
		$(this).parents().filter("form").submit();
	});
	
	$('#contactoForm').validate({
		rules: {
			"data[Contacto][nombres]": "required",
			"data[Contacto][apellidos]": "required",
			"data[Contacto][correo]": {
				required: true,
				email: true
			},
			"data[Contacto][telefono]": "required",
			"data[Contacto][empresa]": "required",
			"data[Contacto][rutEmpresa]": "required",
			"data[Contacto][cargo]": "required"
		},
		messages: {
			"data[Contacto][nombres]": "Por favor ingrese su nombre",
			"data[Contacto][apellidos]": "Por favor ingrese sus apellidos",
			"data[Contacto][rut]": "Por favor ingrese su rut",
			"data[Contacto][correo]": "Por favor ingrese un correo v&aacute;lido",
			"data[Contacto][telefono]": "Por favor ingrese su tel&eacute;fono",
			"data[Contacto][empresa]": "Por favor ingrese la empresa en la que trabaja",
			"data[Contacto][rutEmpresa]": "Por favor ingrese el rut de la empresa",
			"data[Contacto][cargo]": "Por favor ingrese su cargo"
		}
	});
	
});
