$(document).ready(function() {
	/* Búsqueda */
	var url = jQuery("input[name=b], input[name=m]").parents("form:first").attr("action");
	jQuery("input[name=m]").autocomplete(url, {
		delay: 10,
		minChars: 0,
		matchSubset: 0,
		matchContains: true,
		autoFill: false,
		max: 100,
		extraParams: {
			ajax : true,
			t: function() { return "municipios" },
			location: function() { return jQuery("input[name=location]").val() }
		}
	});
	jQuery("input[name=b]").autocomplete(url, {
		delay: 10,
		minChars: 0,
		matchSubset: 0,
		matchContains: true,
		autoFill: false,
		max: 100,
		extraParams: {
			ajax : true,
			t: function() { return "bodegas" },
			location: function() { return jQuery("input[name=location]").val() }
		}
	});
	jQuery("input[name=b], input[name=m]").result(findValueCallback);
	function findValueCallback(event, data, formatted) {
		document.location.href = data[1];
	}
	jQuery("input[name=b], input[name=m]").clearingInput();
	
	/* Newsletter */
	jQuery("input#email, input#usuario, input#password").clearingInput();
	
	/* Dialog */
	jQuery.ui.dialog.defaults.bgiframe = true;
	jQuery('#dialog').dialog({
		modal: true,
		autoOpen: false,
		width: 500,
		height: 300,
		closeOnEscape: false
	});
	jQuery.each(jQuery('.recurso'), function() {
		if(jQuery(this).children('div.dialog').length) {
			jQuery(this).css('cursor', 'pointer');
			jQuery(this).click(function() {
				jQuery('#dialog').empty();
				jQuery(this).children('div.dialog').clone().appendTo("#dialog");
				jQuery('#dialog').dialog('option', 'title', jQuery(this).children('h5').text());
				jQuery('#dialog').dialog('open');
			});
		}
	});
	
	/* Galería */
	if(jQuery('#contenido.galeria').length) {
		jQuery('.nav').hide();
		jQuery('ul.gallery').galleria({
			history   : false,
			clickNext : true,
			insert    : '#imagen',
			onImage   : function(image,caption,thumb) {
				jQuery('#imagen').show();	
				jQuery('.nav').show();
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title', jQuery('.nav .next').text());
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});	
		jQuery('.nav .prev').click(function() {
			jQuery.galleria.prev();
			return false;
		});
		jQuery('.nav .next').click(function() {
			jQuery.galleria.next();
			return false;
		});
	}
	
    /* Consell Regulador */
	if(jQuery('#contenido.consellregulador').length) {
		jQuery('#graficos').tabs({
			fxSlide: true,
			fxFade: true,
			fxSpeed: 'normal'
		});
	}
	
	/* Home */
    jQuery('#slider').cycle({
        fx: 'scrollLeft',
        speed:    1500,
        timeout:  6000
    });
	
	/* Imprimir */
	jQuery(".script").show();
	if(jQuery('#imprimir').length) {
		jQuery('#imprimir').show();
		jQuery("#imprimir").css('cursor','pointer');
		jQuery('#imprimir').click(function() {
			window.print();
			return false;
		});
	}

	/* Enlaces externos */
	jQuery('.external').click(function () {
		window.open(this.href);
		return false;
   	});
});
