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') ) {
                    $$('.toggleCompartir').addEvent('click', function(e){
                        e = new Event(e);
                        
                        var buttonPos = e.page.y - 158;
                        var url = this.href;
                    
                        var deliciousURL = 'http://del.icio.us/post?url=';
                        var blinklistURL = 'http://es.blinklist.com/index.php?Action=Blink/addblink.php&Url=';
                        var favoritingURL = 'http://www.favoriting.com/nuevoFavorito.asp?qs_origen=3&qs_url=';
                        var technoratiURL = 'http://www.technorati.com/faves?add=';
                        var blogmarksURL = 'http://blogmarks.net/my/new.php?mini=1&url=';
                        var meneameURL = 'http://meneame.net/submit.php?url=';
                        var latafaneraURL = 'http://latafanera.cat/submit.php?url=';
                        var politicatURL = 'http://politi.cat/submit.php?url=';
                        
                        var enlaces = $$('a.socialink');
                        
                        enlaces.each(function(element) {
                            element.href = eval(element.title + 'URL') + url;
                        });
                    
                        $('panelCompartir').toggleClass('mostrar');
                        $('panelCompartir').setStyle('top', buttonPos);
                        $('panelCompartir').setStyle('left', 491);
                        
                        $('panelCompartir').toggleClass('showflecha');
                        
                        e.stop();
                    });
                }

				// Municipios
				if( $('btnMunicipi') ) {
					var count = 0;
					$('btnMunicipi').addEvent('click', function(e){
						e = new Event(e);
						if( $('txtMunicipi').value.trim() != '' ) {
							var muni = new Element('li');
							var randomID = count;
							muni.setProperty('id', "li_" + randomID);
							var txtMuni = $('txtMunicipi').value;
							muni.setHTML('<input type="button" name="munidel_'+ randomID +'" id="munidel_'+ randomID +'" class="submit image"  onclick="Site.removeElement(\'li_' + randomID + '\')" /> ' + txtMuni );
							muni.injectInside( $('list_municipis') );
						}
						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) {
            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);
	},
    
    
    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(); });
