var gbGalleryList = new Array();
var slides = new Array();
var slidesActions = new Array();
var Site = {
   
	beforeStart : function() {
      	Site.prepareExternalLinks();

        var mesinfos = $$(".add_mes_info");
        mesinfos.each(function(element) {
            slides[element.id] = new Fx.Slide(element);
            slides[element.id].hide();
            slidesActions[element.id] = false;
        });
		
				// compartir toggle
				if( $('toggleCompartir') != null )
				{
				$('toggleCompartir').addEvent('click', function(e){
					e = new Event(e);
					$('panelCompartir').toggleClass('mostrar');
					$('toggleCompartir').toggleClass('showflecha');
					e.stop();
				});
				}
				// getlink toggle
				if( $('toggleGetCode') != null )
				{
				$('toggleGetCode').addEvent('click', function(e){
					e = new Event(e);
					$('panelGetCode').toggleClass('mostrar');
					$('toggleGetCode').toggleClass('showflecha');
					e.stop();
				});
				}
				// Municipios
				if( $('btnCercarActualitat') ) 
				{
					var count = 0;
					$('btnCercarActualitat').addEvent('click', function(e){
						e = new Event(e);
						
						if( $('txtTeuMunicipi').value.trim() != '' ) 
						{
							var muni = new Element('li');
							var randomID = count;
							muni.setProperty('id', "li_" + randomID);
							var txtMuni = $('txtTeuMunicipi').value;
							var idMuni = $('txtTeuMunicipi_real_filtering_field').value;
							muni.setHTML('<input type="button" name="munidel_'+ randomID +'" id="munidel_'+ randomID +'" class="submit image" onclick="Site.removeElement(\'li_' + randomID + '\')" /><input type="hidden" name="TerritoryNodeIDs[]" value="' + idMuni + '"/> ' + txtMuni );
							muni.injectInside( $('list_municipis') );
							// Reset field values
							$('txtTeuMunicipi').value = '';
							$('txtTeuMunicipi_real_filtering_field').value = '';
						}
						count++;
						//e.stop();
					});
				}
	},
	
	removeElement : function(param) {
		if( $(param) ) { $(param).setHTML(); $(param).setStyle("display", "none"); }
	},
	
	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) {
            if (gbGalleryList[which].gbDirection == "x") gbGalleryList[which].currentPosition--;
             else gbGalleryList[which].currentPosition -= gbGalleryList[which].gbNumElements;
            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;
          if (gbGalleryList[which].gbDirection == "x") gbGalleryList[which].currentPosition++;
             else gbGalleryList[which].currentPosition += gbGalleryList[which].gbNumElements;
          
          scroll.toElement(gbGalleryList[which].elementPrefix + (gbGalleryList[which].currentPosition));
        }
         
     },
    

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

        if (!slidesActions[_targetLayer]) {
            if ($(_sourceLayer).hasClass('veuremes')) { // Cerramos
                slidesActions[_targetLayer] = true;
                slides[_targetLayer].toggle().chain(function() {
                    $(_sourceLayer).removeClass('veuremes');
                    slidesActions[_targetLayer] = false;
                });
            } else {  // Abrimos
                $(_sourceLayer).addClass('veuremes');
                slidesActions[_targetLayer] = true;
                slides[_targetLayer].toggle().chain(function() {
                    slidesActions[_targetLayer] = false;
                });
                
            }
        }
    }
    


};

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