$(document).ready(function() {	
	$('div#barraVerde form input').focus(function() {
		$(this).stop().animate({
			width: 200
		}, 500);
	}).blur(function () {
		$(this).stop().animate({
			width: 100
		}, 500);
	});
	
	$('#verCarro').each(function() {
		loading = $(this).find('.loading');
		loading.show();
		$(this).find('.center .cantidad').load('/cotizacion/total', function() {
			loading.hide();
		});
	});
	
	$("a.agregarCarro").click(function() {
		loading = $("#verCarro .loading");
		loading.show();
		$.get($(this).attr('href'), function() {
			$("#verCarro .center .cantidad").load('/cotizacion/total', function() {
				loading.hide();
			});
		});
		return false;
	});
	
	function actualizarCarro(loading) {
		carro = $('div#carroCotizacion table tbody');
		if (!loading)
			carro.empty().html('<tr><td colspan="3" class="center"><img src="/img/icons/loading.gif" class="loading" /></td></tr>');
		carro.load('/cotizacion/ver', function() {
			borrarCarro();
			cantidadCarro();
		});
	}
	
	$('a#verCarro').click(function() {
		actualizarCarro()
	});
		
	function borrarCarro() {
		$('a.borrarCarro').click(function() {
			carro = $('div#carroCotizacion table tbody');
			$(this).parents('tr').empty().html('<td colspan="3" class="center"><img src="/img/icons/loading.gif" class="loading" /></td>');
			loading = $("#verCarro .loading");
			loading.show();
			$.get($(this).attr('href'), function() {
				carro.load('/cotizacion/ver', function() {
					borrarCarro();
					cantidadCarro();
				});
				$("#verCarro .center .cantidad").load('/cotizacion/total', function() {
					loading.hide();
				});
			});
			return false;
		});
	}
	
	function cantidadCarro() {
		$('a.cantidadCarro').click(function() {
			carro = $('div#carroCotizacion table tbody');
			input = $(this).prev('input');
			$(this).parents('td').empty().html('<img src="/img/icons/loading.gif" class="loading" />');
			loading = $("#verCarro .loading");
			loading.show();
			$.get('/cotizacion/cantidad/' + input.attr('id') + '/' + input.val(), function() {
				carro.load('/cotizacion/ver', function() {
					borrarCarro();
					cantidadCarro();
				});
				$("#verCarro .center .cantidad").load('/cotizacion/total', function() {
					loading.hide();
				});
			});
			return false;
		});
	}
	
	$('div#carroCotizacion').appendTo($('div#cotizacionContactenos')).show();
	$('body#bodyContactenos #verCarro').remove();
	
	actualizarCarro();
	setInterval(function() {
		if (!$('div#carroCotizacion table input').is(':focus'))
			actualizarCarro(true);
	}, 5000);
	
	$("input[type=button], input[type=submit], button").button();
	
	$("form#buscar").submit(function() {
		if ($.trim($(this).find("input[name=busqueda]").val()) != "")
			window.location = '/productos/buscar/' + $(this).find("input[name=busqueda]").val().replace(' ', '+');
		return false;
	});
	
	$("form#buscar a").click(function() {
		$(this).parents("form").submit();
	});
	
	$("div#menu ul li a[href=#], body#bodyInicio a.activadorCarrusel").click(function() {
		id = $(this).attr('data-id');
		if ($("div#menu div#" + id).is(':visible')) {
			$("div#menu div#" + id).hide('slow');
			$("div#menu ul li a[data-id=" + id + "]").removeClass('selected');
		}
		else {
			$("div#menu div.submenu").hide('slow');
			$("div#menu ul li a[href=#]").removeClass('selected');
			$("div#menu div#" + id).show('slow');
			$("div#menu ul li a[data-id=" + id + "]").addClass('selected');
		}
		return false;
	});
	
	/*$("a.checkbox").click(function() {
		var checkbox = $(this).find("input[type=checkbox]");
		checkbox.attr("checked", !checkbox.attr("checked"));
	});*/
	
	$('.select .checkbox label').click(function() {
		checkbox = $(this).siblings('input[type=checkbox]');
		if (checkbox.is(':checked'))
			checkbox.attr('checked', false);
		else
			checkbox.attr('checked', true);
	});
	
	$('div.filtro').hover(function() {
		alto = Math.ceil($(this).find('li:visible').length / 3) * 24 + 35;
		$(this).stop().animate({
			width: 600,
			height: alto
		}, 800);
	}, function(){
		$(this).stop().animate({
			width: 300,
			height: 20
		}, 800);
	});
	
	$('a.verMas').click(function() {
		alto = Math.ceil($('div.productosMarca[data-id=' + $(this).attr('data-id') + '] div.producto:visible').length / 3) * 114 + 38;
		if ($('div.productosMarca[data-id=' + $(this).attr('data-id') + ']').css('height') != alto + 'px') {
			$('div.productosMarca[data-id=' + $(this).attr('data-id') + ']').animate({
				height: alto
			}, 800);
		}
		else {
			$('div.productosMarca[data-id=' + $(this).attr('data-id') + ']').animate({
				height: 150
			}, 800);
		}
		return false;
	});
	
	$('input.filtroGama').each(function() {
		if ($('div.producto[data-' + $(this).attr('id') + '=true]').length == 0)
			$(this).parents('li').hide();
	});
	
	function numeroProductos() {
		cantidad = $('div.producto').not('.hidden').length;
		$('h1 span.cantidad').html(cantidad);
		if (cantidad == 0)
			$('.sinResultados').show('slow');
		else
			$('.sinResultados').hide('slow');
		return cantidad;
	}
	
	numeroProductos();
	
	function generarCondiciones() {
		condiciones = '';
		$('input.filtroGama').each(function() {
			if ($(this).attr('checked'))
				condiciones += '[data-' + $(this).attr('id') + '=true]';			
		});
		return condiciones;
	}
	
	$('input.filtroGama').each(function() {
		$(this).attr('checked', false);
	});
	
	$('input.filtroGama').change(function() {
		if ($(this).attr('checked'))
			$('div.producto[data-' + $(this).attr('id') + '!=true]').hide('slow').addClass('hidden');
		else
			$('div.producto' + generarCondiciones()).show('slow').removeClass('hidden');	
		$('div.productosMarca').each(function() {
			cantidad = $(this).find('div.producto').not('.hidden').length;			
			alto = Math.ceil(cantidad / 3) * 114 + 38;
			if ($(this).css('height') > alto + 'px') {
				$(this).animate({
					height: alto
				}, 800);
			}
			else if ($(this).css('height') < alto + 'px' && $(this).css('height') > 150 + 'px') {
				$(this).animate({
					height: alto
				}, 800);
			}			
			$('a.verMas[data-id=' + $(this).attr('data-id') + '] em').html(cantidad - 3);
			if (cantidad <= 3)
				$('a.verMas[data-id=' + $(this).attr('data-id') + ']').hide('slow');
			else
				$('a.verMas[data-id=' + $(this).attr('data-id') + ']').show('slow');
			if (cantidad == 0)
				$(this).hide('slow');
			else
				$(this).show('slow');
			numeroProductos();
		});
	});
	
	$('ul.temas li').hide();
	$('ul.temas li input.default').attr('checked', true);	
	
	$('ul.categorias li a').click(function() {
		$('div#temasContactenos span').hide();
		$('ul.categorias li a').removeClass('selected');
		$('ul.temas li[data-seccion!=' + $(this).attr('data-seccion') + ']').hide('fade', 400);
		$('ul.temas li input.default').attr('checked', true);
		$(this).addClass('selected');
		$('ul.temas li[data-seccion=' + $(this).attr('data-seccion') + ']').delay(400).show('fade', 600);
		return false;
	});
	
	$('ul.cuadros').roundabout({
		minOpacity: 0.8,
		minScale: 0.50,
		//shape: 'figure8',
		duration:600,
		autoplay:6000,
		tilt:0,
		btnNext: '#siguiente',
        btnPrev: '#anterior'
	});
	
	$(".rounded").corner("5px keep");	
	$("ul#destacados li img.thumb, ul#destacados li .detalles, div.producto .imagen img, div.producto img.imagen, ul.cuadros li, div.productoImagenes .thumbs .imagen img, div#carroCotizacion table").corner("5px keep");
	$("ul#destacados li .detalles img.imagen").corner("4px keep top");
	$("div.submenu, div.filtro").corner("8px keep");
	$("div.tags a").corner("12px keep");
	$("form#contactenos input[type=text], form#contactenos textarea, div#carroCotizacion table th, div#carroCotizacion table td").corner("4px keep");
	
	
	$('body#bodyInicio a.activadorCarrusel').click(function() {
		$('div.carruselActivo').removeClass('carruselActivo').hide('fade', 400);
		$('div#main div#' + $(this).attr('data-carrusel')).delay(500).addClass('carruselActivo').show('fade', 600);
		return false;
	});
	
	$('div.cuadrosNavegacion ul li a[data-cuadro=0]').addClass('selected');

	$('div.cuadrosNavegacion ul li a').click(function() {
		carrusel = $(this).parents('div.cuadrosNavegacion').attr('data-carrusel');
		$('div#' + carrusel + ' ul.cuadros').roundabout_animateToChild($(this).attr('data-cuadro'));
		return false;
	});
	
	$(".confirmar").click(function() {
		return confirm("Estas seguro que deseas borrar este registro?");
	});
	
	$("a.fancy").fancybox({
		loop: true
	});
	
	$('a#verCarro').fancybox({
		minWidth: 550,
		minHeight: 300,
		maxWidth: 550,
		maxHeight: 400,
		fitToView: false,
		autoSize: false,
		closeClick: false,
		openEffect: 'none',
		closeEffect: 'none',
		closeBtn: false
	});
	
	$('a.fancyClose').click(function() {
		$.fancybox.close();
		return false;
	});
	
	$("#flashMessage, #authMessage").attr('title', 'Adaptor');
	
	$("#flashMessage, #authMessage").dialog();
	
	$(".html textarea").elrte({height:"250px"});	
});

