
host = 'http://www.bodega-capricho.com';
 
var puntos;
// CACHE IMAGE
(function ($) { 
	$.extend($, {
		cacheImage: function (src, options) {
			if (typeof src === 'object') {
				$.each(src, function () { $.cacheImage(String(this), options);});
			}
			var image = new Image();
			options = options || {};
			$.each(['load', 'error', 'abort'], function () { // Callbacks
				var e = String(this);
				if (typeof options[e] === 'function') { $(image)[e](options[e]); }
				});
			image.src = src;
			return image;
			}
		});
	
	$.extend($.fn, {
		cacheImage: function (options) {
			return this.each(function () { $.cacheImage(this.src, options);});
			}
		});
	})(jQuery);


var __imVinculo = function (el) {
		// VINCULO
		/*var vinculo = $('a[rel="'+$(el).attr('alt')+'"]')[0];
		var h = $(vinculo).attr('href');
		var t = $(vinculo).text();
		var elem = '<a href="'+h+'">'+t+'</a>';
		if (!t=='') {$('body').find('#tip').append(elem)};*/
		};
		

// CENTRAR CONTENIDOS
$.fn.Centrar = function () {
	var t = Math.round((document.documentElement.clientHeight- $(this).height())/2) + $(window).scrollTop();
	var l = Math.round((document.documentElement.clientWidth- $(this).width())/2);
	
	// NO PASA DEL BORDE SUPERIOR DE LA PAGINA
	if (t <= 0) t = 0;
	if ($(this).height() > $(window).height()) t = $(window).scrollTop()

	// APLICAR
	$(this).css({
		'top': t + 'px',
		'left': l + 'px'
		});
		
	return this;
	}

// FULL SIZE 2
$.fn.FullSize = function() {

	return this.each(function() {

		//Gather browser and current image size
		var imagewidth = $(this).width();
		var imageheight = $(this).height();
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		
		//Define image ratio
		var ratio = imageheight/imagewidth;
		
		//When browser is taller	
		if (browserheight > browserwidth){
			imageheight = browserheight;
			$(this).height(browserheight);
			imagewidth = browserheight/ratio;
			$(this).width(imagewidth);

			if (browserwidth > imagewidth){
				imagewidth = browserwidth;
				$(this).width(browserwidth);
				imageheight = browserwidth * ratio;
				$(this).height(imageheight);
				}
			
			// CENTRAR LA IMAGEN
			$(this).css({
				'position': 'relative',
				'left':'-'+ ((imagewidth-browserwidth)/2) +'px'
				});
			}

		//When browser is wider
		if (browserwidth >= browserheight){
			imagewidth = browserwidth;
			$(this).width(browserwidth);
			imageheight = browserwidth * ratio;
			$(this).height(imageheight);

			if (browserheight > imageheight){
				imageheight = browserheight;
				$(this).height(browserheight);
				imagewidth = browserheight/ratio;
				$(this).width(imagewidth);
				}
			
			// CENTRAR LA IMAGEN
			$(this).css({
				'position': 'relative',
				'top':'-'+ ((imageheight-browserheight)/2) +'px'
				});
			
			}
		
		return false;
		});
	};

// LOADER
$.fn.cargador = function(content, aj, lg, fn) {
	
	// HTML DEL CARGADOR
	var htm = '<div class="cargador-fondo"><div id="loader-out"></div> </div> \
		<div id="ct-control"> \
				<div id="cerrar"> </div> \
				<div id="controles"> </div> \
				<div id="tip"> </div> \
				 \
			</div> \
		<div class="cargador-content"> \
			<div id="ct-modal"> </div> \
		</div>'
	
	jQuery.fn.cargador.created.id = "cargador";  
	$("body").append(jQuery.fn.cargador.created);
	
	// arrancar el cargador 
	var Cargador = $(jQuery.fn.cargador.created);
			
	Cargador
		.empty()
		.append(htm)
		.animate({opacity: 1},300)
	
	
	// definir valores del cargador fondo
	$('.cargador-fondo').css({opacity: 0}).click(function(){ CargadorRemove()});
	Loader(1,msg['carga-'+lg]);
	
	// definir ct-control
	$('#ct-control').css({opacity: 0});
	
	// definir valores para el cargador contenido
	$('.cargador-content')
		.css({opacity: 0})
		.Centrar();
	
	// definir acciones para el documento
	$('body').one('keydown', function(){ CargadorRemove();return false;})
	if ($.browser.safari) Cargador.css({'background-attachment':'scroll'});
	
	// FUNCIONES 
	function CargadorShow(){
		$('body, html').css({'overflow':'hidden'})
		
		// CONTROL PARA CERRAR EL CARGADOR
		$('#cerrar').append('<div class="cerrar"> </div>')
		$('.cerrar').bind('click', function() {CargadorRemove()})
		
		$('#ct-control').css({top: 30 + $(window).scrollTop()})
		$('#ct-control').append($('#loader-out')); // passar loader para cargador-content
		
		$('#controles').append($('#thumb').html()); // pasar los numeros
		$('#controles').css({'margin-top':$(window).height()/3 + 'px'}); // posiciona los controles (numeros) en relacion al logo
		// 29 octubre 2009 - inicio
		$('#controles').prepend('<a class="cierre-2" href="#">X</a>'); // colocar segundo boton de cierre
		$('.cierre-2').bind('click', function() {CargadorRemove()}) // hacer que funcione
		// 29 octubre 2009 - fin
		$('.cargador-fondo')
			.height($(document).height())
			.animate({opacity: 1},500, function() {
				$('#ct-control').css({opacity:1});
				$('#titulo').addClass('titulo_diapos');
				$('.titulo_diapos').css({'top':document.documentElement.clientHeight + $(window).scrollTop() - 110});
				});
		} 
		
	// hace el contenido visible el primer click
	function ContenidoShow(data) {
	
		Loader(0); // quitar loader
		// PONER CONTENIDO TODAVIA NO ESTA VISIBLE
		$('#ct-modal').append(data)
		
		// HACER VISIBLE
		$('.aj-img-content img').FullSize();
		$('.cargador-content')
			.height($(document).height()) // coje altura del documento
			.animate({opacity: 1},500);

		$('.cargador-fondo').height($(document).height());
		
		if (fn) fn();
		}
	
	function CambioImagen(im){
		$('#ct-modal').fadeIn();
		$('#ct-modal').find('img').replaceWith(im);//.attr({'src': im});
		$('.aj-img-content img').FullSize();
		Loader(); // remover loader
		}
	
	function CargadorRemove(){
		$('.cargador-fondo').append($('#loader-out')); // passar loader para cargador-fondo
		$('.cargador-content').animate({opacity: 0},500,function() {
			Cargador.empty().remove();
			$('#basura').empty();
			$('body').css({'overflow':'visible'})
			$('html').css({'overflow':''}) // ie bug
			})
		}
	
	function Loader(activo,msg,t) {
		if (!msg) msg = 'CARGANDO';
		if (activo) {
			$('#loader-out').append('<div id="loader">'+msg+' <span class="puntos"></span> </div>');
			puntos = setInterval("$('.puntos').append('.');if ($('.puntos').text()=='....'){$('.puntos').text('')};",500);
			}
		else {
			if (!t) t = 700;
			$('#loader').html(msg)
			$('.slider').bind('click',function() {$("#loader").remove();})
			setTimeout('$("#loader").remove();clearInterval(puntos)',t);
			//$("#loader").remove();
			}
		}
	
	// EVENTOS DE LOS ELEMENTOS 
	this.each(function(){  
		$(this).ready(function(){  
			
			// INICIALIZO EL FONDO
			CargadorShow();  
			
			//CONTENIDO CON UNA IMAGEN ***
			var src = $(content).children('img').attr('src');
			$.cacheImage(src ,{
				load : function (e) { 
					ContenidoShow(content);},
				error : function() {
					content = $(content).find('img', function() { $(this).remove()});
					ContenidoShow(content);
					Loader(0,msg['error-carga-'+lg],5000)}
				})
				 
			
			
			// PASE DE DIAPOSITIVAS
			$('#controles .slider').live('click', function() {
				
				
				// ENCONTRAR DIAPOS MARCADAS
				$('#controles').find('.diapo-marcar').removeClass('diapo-marcar').addClass('diapo-visitada');
				$('#thumb').find(':contains("'+$(this).html()+'")').addClass('diapo-visitada');
				$(this).removeClass('diapo-visitada').addClass('diapo-marcar');
				
				// LOS COMENTARIOS
				$('#ct-control #tip').html(''+$(this).attr('rel')+'');
				
				__imVinculo($(this))
				
				var src = $(this).attr('href');
				
				// GOOGLE ANALYTICS
				pageTracker._trackEvent('click img dentro', ''+src+'#n='+$(this).text(), ''+$(this).text() );
				
				// DESAPARECE la capa que contiena la imagen
				$('#ct-modal').fadeOut(500,function () {
					Loader(1,msg['carga-'+lg]);					
					// CAMBIO
					$.cacheImage(src ,{
						load : function (e) {
							CambioImagen(this);
							},
						error : function() {
							Loader(0,msg['error-carga-'+lg],5000)
							}
						})
					});
				return false;
				});
			});
			
		});
	
	}
	
//create the element (avoiding create multiple divisions for the tipBox)  
jQuery.fn.cargador.created = document.createElement("div");

// ##########################################################################
// FUNCIONES
// ##########################################################################

$(document).ready(function() {
	
	//PETICIONES
	var str = document.location + ''
	var query_1 = String(str.match('#[a-z]*[\_|\-]?[a-z]*[\_|\-]?[a-z]*[\_|\-]?[a-z]*[\_|\-]?[a-z]*[\=]?[a-z]*[0-9]*[a-z]*')).replace(/#/,'')
	var arrQuery = query_1.split('=')
	
	// INICIALES
	var lg = $('#lang').text()
	if (lg == false) lg = 'es';

	$('a#'+lg).css({'background-color':'#000'}).addClass('opaco').click(function() {return false});
	//$('a#'+lg).addClass('opaco').click(function() {return false});

	
	// IMAGENS
	var __img = function (im) {
		var html = '<div class="aj-img-content">'
		html += '<img src="'+ im +'" />';
		html += '</div>'
		$('body').cargador(html,0,lg);
		$(window).bind('resize', function(){$('.aj-img-content img').FullSize();});
		};

	
	$('#thumb .slider').click(function() {
		// GOOGLE ANALYTICS
		pageTracker._trackEvent('click img', str+'/slider' , ''+$(this).text() );
		
		// INTERNET EXPLORER 6
		if ($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 6) {
		
			var img = new Image();
			var titulo = $(this).attr('rel');
			img.src = $(this).attr('href');
			
			
			$.cacheImage(img.src ,{
				load : function (e) {
					var n = 0.70;
					//Gather browser and current image size
					var imagewidth = img.width;
					var imageheight = img.height;
					var browserwidth = ($(window).width()*n);
					var browserheight = ($(window).height()*n);
					
					//Define image ratio
					var ratio = imageheight/imagewidth;
					
					//When browser is taller	
					if (browserheight > browserwidth){
						imageheight = browserheight;
						$(img).height(browserheight);
						imagewidth = browserheight/ratio;
						$(img).width(imagewidth);
			
						if (browserwidth > imagewidth){
							imagewidth = browserwidth;
							$(img).width(browserwidth);
							imageheight = browserwidth * ratio;
							$(img).height(imageheight);
							}
						}
					//When browser is wider
					if (browserwidth >= browserheight){
						imagewidth = browserwidth;
						$(img).width(browserwidth);
						imageheight = browserwidth * ratio;
						$(img).height(imageheight);
			
						if (browserheight > imageheight){
							imageheight = browserheight;
							$(img).height(browserheight);
							imagewidth = browserheight/ratio;
							$(img).width(imagewidth);
							}			
						}
					
					
					$(img).css({
						'border':'8px solid white',
						'padding': '0px',
						'margin-left': '10px'
						});
					$(img).dialog({
						title: titulo,
						modal:true,
						height: imageheight,
						width: imagewidth,
						dialogClass: '',
						close:function() { $(this).dialog('destroy')}
						});
					
					
					},
				error : function() {
					
					}
				})
		
			return false;
			}
		else {	// TODOS LOS DEMAS BROWSERS
			// MARCAR OPCION
			$(this)
				.removeClass('diapo-visitada')
				.addClass('diapo-marcar');
			// CARGAR IMAGEN
			__img($(this).attr('href'));
			// PASAR INFORMACION
			$('#tip').append(''+$(this).attr('rel')+'');
			
			__imVinculo($(this));
			
			// EXCEPCIONES CSS
			$('#titulo').removeClass('no-opaco');
			$('#titulo img').attr({'src':$('#titulo img').attr('src').replace(/bodega_3/,'bodega_2')});
			
			// LANZAR EVENTO DE CIERRE
			var btn_1 = $(this);
			$('.cerrar, .cierre-2').live('click', function(){
				// EXCEPCIONES CSS
				$('#titulo').removeClass('titulo_diapos');
				if ($('.activo[alt="bodega"]').is('a')) $('#titulo').removeClass('opaco');
				$('#titulo img').attr({'src':$('#titulo img').attr('src').replace(/bodega_2/,'bodega_3')});
				btn_1
					.removeClass('diapo-marcar')
					.addClass('diapo-visitada');
				});
			return false;
			}
		});
	
	
	
	
	// 29 octubre 2009 - INICIO
	//  PASAR FOTOS
	
	$('.aj-img-content img').live('click', function () {
		var primera = $('.diapo-marcar'); // primera
		var vecina = $('#controles').find('.diapo-marcar').next().trigger('click'); // lanzar evento a slider
		if (!vecina.is('a')) { vecina = $('#controles a:first').next().trigger('click');}; // dar la vuelta
		if (vecina.attr('href') == primera.attr('href')) $('#controles a:first').trigger('click'); // cierre
		})
	
	// 29 octubre 2009 - FIN
	// TAMANIO FUENTE
	var fontSize = parseInt(arrQuery[1]);
	
	$('.acess').addClass('acess-control');
	
	function MM(mm) {
		if (!mm) mm = 0; var v = 3;
		$('.acess-text').each( function () {
			var fs = parseInt($(this).css('font-size').replace('px',''));
			var lh = parseInt($(this).css('line-height').replace('px',''));
			if (fs > 12 && mm == '-') $(this).css({'font-size': (fs - v) +'px','line-height':(lh-v)+'px'});
			if (fs < 19 && mm == '+') $(this).css({'font-size': (fs + v) +'px','line-height':(lh+v)+'px'});
			});
		}
	
	$('.mas').append('A +').click(function() {
		MM('+');
		if (fontSize && fontSize <= 2) {fontSize = fontSize + 1;}
		else if (fontSize == 3) {}
		else {fontSize = 1};
		document.location.href = '#font='+fontSize;
		pasarFont();		
		});
		
	$('.menos').append('a -').click(function() {
		MM('-');
		if (fontSize) {
			fontSize = fontSize - 1;
			document.location.href = '#font='+fontSize;
			pasarFont();
			}
		});
	
	function pasarFont() {
		$('a').each(function() {
			var href =  $(this).attr('href');
			href = href.split('#');
			$(this).attr({'href': href[0] + '#font='+fontSize});
			})
		}
	
	if (arrQuery[0]=='font'){if (fontSize <= 3) {for (i = 0; i < fontSize; i++) {MM('+')};pasarFont();}};
	
		
	
	// TITULOS IMAGENS
	$('.in-tip')
		.bind('mouseenter',function(){
			if  ($(this).attr('alt')) {
				var html = '<div class="tip" style="">';
				html += $(this).attr('alt');
				html += '</div>';
				$(this).parent().append(html);
				$().mousemove(function(e){
      					$('.tip').css({
      						left: e.pageX +15,
      						top: e.pageY + 15
      						});
   					});
				}
			})
		.bind('mouseleave',function() {
			$('.tip').empty().remove();
			});
			
	
	// OPCION PRINCIPAL
	//var navP = $('#nav-principal').attr('title');
	var navP = $('#nav-principal').text();
		
	// SCROLL VOLVER ARRIBA
	$('a.control').live('click',function () {
		if ($(this).attr('href') == '#arriba') {
			$('body, html').animate({scrollTop:0})
			}
			
		return false;
		})
	
				
	// IMPIDE PETICIONES REPETIDAS
	$('a[href="#"], .ignore').click(function(){return false})
	//$('.activo').parent().addClass('opaco');
	
	// FIN JQUERY
});

