function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}


document.getElementsByTagName('html')[0].className = "js";
$(document).ready(function() {

	if(typeof iexplorer6 == "undefined") iexplorer6 = false;
	idGalTienda = 0;
    idGalTiendaList = 0;	
	seccionActiva = "home";
	extendMarkup();
	setEvents();
	$("[rel^='prettyPhoto']").prettyPhoto();
    
    var width_list = $('#gal-list').width();
    $('#gal-list div').each(function(){
        margin_left = 0;
        if(width_list >= $(this).width()){
            margin_left = (width_list - $(this).width()) / 2;
            $(this).css('margin-left',margin_left+'px');
        }
    });
    
    
    /*$('#tiendas #imgs-wrap div img').each(function() {
    	this.style.position = 'absolute';
        alert($(this).parent().height() + ' ' + $(this).height() + ' ' + ($(this).parent().height() - $(this).height()));
    	this.style.top = ($(this).parent().height() - $(this).height())/2 + 'px';
    	this.style.marginTop = -$(this).height()/2 + 'px';
    	this.style.left = ($(this).parent().width() - $(this).width())/2 + 'px';
    });*/

});



function extendMarkup(){

	//reel texto en presentacion
	if($("#text-presentacion div").length > 1){
		$("#text-presentacion div:eq(0) p:last").append('<a href="#" title="leer m&aacute;s" id="leer-mas">leer m&aacute;s</a>');
		$("#text-presentacion div:eq(1) p:first").append('<p><a href="#" title="leer m&aacute;s" id="leer-atras">volver atr&aacute;s</a></p>');		
		$("#text-presentacion div:eq(1)").hide();
	}

	$('#leer-mas, #leer-atras').live('click', function(event) {
		$("#text-presentacion div").toggle();
		cancelEvent(event);
	});
	//ocultamos imagenes de galeria
	/*
	$("#imgs-wrap div").hide();
	$("#imgs-wrap div:eq(0)").show();	
	*/
	/*$("#imgs-wrap div img").hide();		
	$("#imgs-wrap div img:first-child").show();		
    galActiva = $("#imgs-wrap>div:eq(0)");	
     
	if($("img",galActiva).length < 2){
		 $("#gal-nav").hide();
	}*/
    
    $("#imgs-wrap div a").hide();		
	$("#imgs-wrap div a:first-child").show();		
    galActiva = $("#imgs-wrap>div:eq(0)");
    galActivaList = $("#gal-list>div:eq(0)");	
     
	if($("a",galActiva).length < 2){
		 $("#gal-nav").hide();
	}
	
	$("#mapa-wrap iframe").hide();
	$("#mapa-wrap iframe:first-child").show();	

	$("#gmaps-link li").hide();
	$("#gmaps-link li:first-child").show();
    
    $("#gal-list div").hide();
	$("#gal-list div:first-child").show();	
}
function setEvents(){
	$("#prev-img a").bind('click', function(event) {
		swapImg(-1);
		cancelEvent(event);
	});
	
	$("#next-img a").bind('click', function(event) {
		swapImg(1);
		cancelEvent(event);
	});
	$("#prev-tienda a").bind('click', function(event) {
		slideShop(-1);
		cancelEvent(event);
	});
	
	$("#next-tienda a").bind('click', function(event) {
		slideShop(1);
		cancelEvent(event);
	});

}
function swapImg(n){
	
	if($("a",galActiva).is(":animated")) return;
	var cantImgs = $("a",galActiva).length;
	var idNext = $("a:visible",galActiva).index() + n;

    if(idNext < 0){
		idNext = cantImgs -1;
	}
	if(idNext >= cantImgs){	
		idNext = 0;		
	}

	$("a:visible",galActiva).fadeOut();
	$("a:eq(" + idNext + ")",galActiva).fadeIn();
    
    $("img",galActivaList).attr("src",getBaseURL()+"css/images/image_off.png");
    $("img:eq(" + idNext + ")",galActivaList).attr("src",getBaseURL()+"css/images/image_on.png");
}

function slideShop(n){
	var pos = n * $("#slide-tiendas>div:first-child").innerHeight();
	var pos2 = n * $("#imgs-wrap>div:first-child").innerHeight();	

    //$("#slide-tiendas").height($("#slide-tiendas>div").size()*$("#slide-tiendas>div:first-child").outerHeight());
    //alert($("#slide-tiendas>div:first-child").outerHeight()+' '+$("#slide-tiendas").height());

	if(	$("#slide-tiendas").is(":animated")) return;
	idGalTienda += n;
    idGalTiendaList += n;
	galActiva = $("#imgs-wrap>div:eq(" + idGalTienda + ")");
    galActivaList = $("#gal-list>div:eq(" + idGalTiendaList + ")");
	$("a",galActiva).hide();
	$("a:first",galActiva).show();
	if($("a",galActiva).length < 2){
		 $("#gal-nav").hide();
	}else{
	 	$("#gal-nav").show();		
	}
    
    $("img",galActivaList).attr("src",getBaseURL()+"css/images/image_off.png");
    $("img:eq(0)",galActivaList).attr("src",getBaseURL()+"css/images/image_on.png");

	$("#gmaps-link li").hide();
	$("#gmaps-link li:eq(" + idGalTienda + ")" ).show();
    
    $("#gal-list div").hide('fast');
	$("#gal-list div:eq(" + idGalTiendaList + ")" ).fadeIn('slow');	
		
	$("#imgs-wrap").animate({"marginTop": '-='+pos2}, 500, function(){
		$("#mapa-wrap iframe:visible").hide();		
		$("#mapa-wrap iframe:eq(" + idGalTienda + ")").show();			
	});

	$("#slide-tiendas").animate({"marginTop": '-='+pos}, 500,function(){
		var mt = $(this).css("marginTop");
		var mt = Number(mt.substr(0,mt.length -2));
        
		if(mt < 0){
			 $("#prev-tienda").show();
		}else{
			 $("#prev-tienda").hide();	
		}
        
        //alert(mt+' '+ (-($("#slide-tiendas").height() - $("#slide-tiendas>div:first-child").outerHeight())));
		if(mt <= -($("#slide-tiendas").height() - $("#slide-tiendas>div:first-child").outerHeight()) ){
			 $("#next-tienda").hide();
             $("#layer_transparent").fadeOut();	
		}else{
			 $("#next-tienda").show();
             $("#layer_transparent").fadeIn().animate({opacity: 0.65});	
		}
	});
}

function setSizes(){
	//listado tiendas
	$("#prev-tienda").hide();
	var cantTiendas = 	$("#slide-tiendas>div").length;
	var hTiendas = 0;
	var hGal = 0;
	for(var i = 0; i< cantTiendas; i++){
		hTiendas += $("#slide-tiendas>div:eq(" + i + ")").outerHeight();
		hGal += $("#imgs-wrap>div:eq(" + i + ")").height();		
	}
	$("#slide-tiendas").height(hTiendas);
	$("#imgs-wrap").height(hGal);
	
	var w = 0;
	$("#wrap-slide ul li").each(function(index) {
	w += $("img",this).width() + 10;
//	  this.innerHTML = this + " is the element, " + index + " is the position";
	});
	$("#wrap-slide ul").width(w);
	$("#wrap-slide").jScrollPane();
}


$(window).load(function() {
	var theWindow = $(window),
    $bg = $("#bg"),
    aspectRatio = $bg.width() / $bg.height();
    $bg.show();

	function resizeBg() {
		// AARON
        // $bg.delay(300).fadeIn();
        $bg.show();
        // AARON
		var wWidth = $(window).width();
		var wHeight = $(window).height();

		if(wWidth < 1200){
			$("body").addClass("smallRes");
		}else{
			$("body").removeClass("smallRes");
		}
    	if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg.removeClass().addClass('bgheight');
                } else {
                    $bg.removeClass().addClass('bgwidth');
                }
				$("#home-bg").css({"display":"block","marginLeft":  - $("#home-bg").width()/2,"marginTop":  - $("#home-bg").height()/2}); 
				$("#red-bg").css({"marginLeft":  - $("#red-bg").width()/2}); 
        }
        theWindow.resize(function() {
                resizeBg();
				setSizes();
				$("#wrap-slide").jScrollPane();
				if($(window).width() > $("#wrap-slide ul").width()){
					$(".jspPane").css("left",0);
				}
        }).trigger("resize");
});

function cancelEvent(e){
		if (e && e.preventDefault)e.preventDefault();
		return false; 		
}
