var gbGalleryList = new Array();
var Site = {
   
	beforeStart : function() {
      	Site.prepareExternalLinks();
	},
	
	start: function(){
	},
    

	prepareExternalLinks : function() {
	    var linkList = $$("a");
	    linkList.each(function(el) {
	      if (el.getProperty('rel')=="external") el.target = "_blank";
	    }, this);
	},
	
	startGallery : function (idGallery, parentDiv, direction, numElements) {
		if ($(parentDiv)) {
            gbGalleryList[idGallery] = new Object();
            gbGalleryList[idGallery].gbParentDiv = parentDiv;
            gbGalleryList[idGallery].gbNumElements = numElements;
            gbGalleryList[idGallery].gbDirection = direction;
            gbGalleryList[idGallery].currentPosition = 1;
            gbGalleryList[idGallery].elementPrefix = "";
            gbGalleryList[idGallery].gbContainerDiv = "";
			
			// Get containerDiv
			var containerDiv = $(parentDiv).getFirst().id;
			gbGalleryList[idGallery].gbContainerDiv = containerDiv;
			
            // Get list prefix
            var lis = $$("#" + gbGalleryList[idGallery].gbContainerDiv + " ul li")
            var firstElement = lis[0].id;
            gbGalleryList[idGallery].elementPrefix = (lis[0].id).substring(0, (lis[0].id).lastIndexOf("_") + 1);
            
            var scroll = new Fx.Scroll(gbGalleryList[idGallery].gbParentDiv, { wait: false, duration: 0, offset: {'x': -3, 'y': -7}, transition: Fx.Transitions.Quad.easeInOut });
            scroll.toElement(firstElement);
            
            var sizeElements = this.getGalleryLong(gbGalleryList[idGallery].gbContainerDiv, gbGalleryList[idGallery].gbDirection);
			if (direction == "x") $(containerDiv).setStyle("width", sizeElements);
				else $(containerDiv).setStyle("height", sizeElements);
		}
	},
	
    // Devuelve el ancho/alto que debe ocupar la galería para quede en línea
	getGalleryLong : function(containerDiv, direction) {
        var rtnValue = 0;
        if ($(containerDiv)) {
            var lis = $$("#" + containerDiv + " ul li");
			lis.each(function(element) {
				var size = element.getSize();
				if (direction=="x") {
					size = size['size'].x;
					size += $(element).getStyle("marginRight").toInt() + 2;
					size += $(element).getStyle("marginLeft").toInt() + 2;
				} else {
					size = size['size'].y;
					size += $(element).getStyle("marginTop").toInt() + 2;
					size += $(element).getStyle("marginBottom").toInt() + 2;
				}
				rtnValue += size;
				
			});
        }
        
        return rtnValue;
    },
	
	galleryQuantity : function(which) {
		var rtnValue = 0;
		
		var lis = $$("#" + gbGalleryList[which].gbContainerDiv + " ul li")
		rtnValue = lis.length;
		
		return rtnValue;
	},
	
	Darrera : function(which) {
        var scroll = new Fx.Scroll(gbGalleryList[which].gbParentDiv, { wait: false, duration: 500, offset: {'x': -3, 'y': -7}, transition: Fx.Transitions.Quad.easeInOut });
         
        if (gbGalleryList[which].currentPosition > 1) {
            gbGalleryList[which].currentPosition--;
            scroll.toElement(gbGalleryList[which].elementPrefix + (gbGalleryList[which].currentPosition));
        }

     },

     Davant : function(which) {
        var scroll = new Fx.Scroll(gbGalleryList[which].gbParentDiv, { wait: false, duration: 500, offset: {'x': -3, 'y': -7}, transition: Fx.Transitions.Quad.easeInOut });
        
        if (this.galleryQuantity(which) > (gbGalleryList[which].currentPosition + (gbGalleryList[which].gbNumElements-1))) {
            
			var elementPos = ($(gbGalleryList[which].elementPrefix + gbGalleryList[which].currentPosition ).getPosition()).x;
			gbGalleryList[which].currentPosition++;
            scroll.toElement(gbGalleryList[which].elementPrefix + (gbGalleryList[which].currentPosition));
        }
         
     },
    

	
	getBaseUrl : function() {
		return (document.location.href).substring(0, (document.location.href).lastIndexOf("/") +1);
	}
    


};

window.addEvent('domready', function() { Site.beforeStart(); });
window.addEvent('load', function() { Site.start(); });
