/*Packed MyLightbox MyPost javascript*/
/*Lightbox v2.04n by Lokesh Dhakar - http://www.lokeshdhakar.com Last Modification: 2/9/08 For more information, visit:http://lokeshdhakar.com/projects/lightbox2/Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/- Free for use in both personal and commercial projects- Attribution requires leaving author name, author link, and the license info intact.Thanks: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), and Thomas Fuchs(mir.aculo.us) for ideas, libs, and snippets.Artemy Tregubenko (arty.name) for cleanup and help in updating to latest ver of proto-aculous.*/

LightboxOptions = Object.extend({
	backBImage:        'http://www.netweek.it/sport_ivan/public/immagini/prevlabel.gif',   
    nextBImage:        'http://www.netweek.it/sport_ivan/public/immagini/nextlabel.gif',     
    closeBImage:       'http://www.netweek.it/sport_ivan/public/immagini/close.gif',

    overlayOpacity: 0.5,   // controls transparency of shadow overlay

    animate: true,         // toggles resizing animations
    resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)

    borderSize: 10,         //if you adjust the padding in the CSS, you will need to update this variable
    lightboxWidth:500,
    lightboxHeight:500,
	// When grouping images this is used to write: Image # of #.
	// Change it for non-english localization
	labelImage: "Articolo",
	labelOf: "di"
}, window.LightboxOptions || {});

// -----------------------------------------------------------------------------------

var Lightbox = Class.create();

Lightbox.prototype = {
    linkArray: [],
    activeLink: undefined,
   
    // initialize()
    // Constructor runs on completion of the DOM loading. Calls updateLinkList and then
    // the function inserts html at the bottom of the page which is used to display the shadow 
    // overlay and the image container.
    //
    initialize: function() {    
        
    	if (LightboxOptions.resizeSpeed > 10) LightboxOptions.resizeSpeed = 10;
        if (LightboxOptions.resizeSpeed < 1)  LightboxOptions.resizeSpeed = 1;
	    this.resizeDuration = LightboxOptions.animate ? ((11 - LightboxOptions.resizeSpeed) * 0.15) : 0;
	    this.overlayDuration = LightboxOptions.animate ? 0.2 : 0;  // shadow fade in/out duration
    	var size = (LightboxOptions.animate ? 400 : 1) + 'px';

    	/*
    	    <div id="overlay"></div>
    	    <div id="lightbox">
    	        <div id="textbox"></div>
    	        <div id="navControl">
    	            <a id="backB"></a>
    	            <div id="info"></div>
    	            <a id="nextB"></a>
    	            <a id="closeB"></a> 
    	        </div>
    	    </div>
    	*/
        var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'overlay'}));
	
        objBody.appendChild(Builder.node('div',{id:'lightbox'}, [
            
            Builder.node('div', {id:'navControl'}, [             
                       
                        Builder.node('div',{id:'info'},  " "),  
                        Builder.node('a',{id:'closeB', href: 'javascript: void(0);' },Builder.node('img', { src: LightboxOptions.closeBImage })),
                        Builder.node('a',{id:'backB', href: 'javascript: void(0);' },Builder.node('img', { src: LightboxOptions.backBImage })),
                        Builder.node('a',{id:'nextB', href: 'javascript: void(0);' },Builder.node('img', { src: LightboxOptions.nextBImage }))
                       
           ]),
           Builder.node('div',{id:'textbox'}, 
             " "
            ),
        ]));
                   
                
          
        
      
        
        $('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
        $('closeB').observe('click', (function() { this.end(); }).bind(this));
        $('nextB').observe('click', (function() { this.cambiaArticolo('avanti'); }).bind(this));
        $('backB').observe('click', (function() { this.cambiaArticolo('indietro'); }).bind(this));
		$('lightbox').hide();
		
		//trasforma in oggetti i seguenti id
		 var th = this;
        (function(){
            var ids = 
                'overlay lightbox outerImageContainer imageContainer lightboxImage hoverNav prevLink nextLink loading loadingLink ' + 
                'imageDataContainer imageData imageDetails caption numberDisplay bottomNav bottomNavClose backB nextB info closeB';   
            $w(ids).each(function(id){ th[id] = $(id); });
        }).defer();
        
        this.updateLinkList();
        
     
    },

    //
    // updateLinkList()
    // Ciclo che estrae gli elementi con il tag rel='lightbox' e gli applica l'evento onclick-->funzione start
    // Loops through anchor tags looking for 'lightbox' references and applies onclick
    // events to appropriate links. You can rerun after dynamically adding images w/ajax.
    //
    updateLinkList: function() {   
        this.updateLinkList = Prototype.emptyFunction;

        document.observe('click', (function(event){
            var target = event.findElement('a[rel^=lightbox]') || event.findElement('area[rel^=lightbox]');
            if (target) {
                event.stop();
                this.start(target);// 
            }
        }).bind(this));  

    },
    


    //
    //  start()
    //  Visualizza l'ovrlay e il lightbox bianco. imposta l'array con le immagini.
    //
    start: function(imageLink) {  
 	
    
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

        // prende la dimensione della pagina e crea l'overlay nero
        var arrayPageSize = this.getPageSize();
        
        $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
        new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: LightboxOptions.overlayOpacity });

        //controlla il tag rel dell'immagine passata, 
        //se è una singola immagine la aggiunge all'array linkArray
        //se sono piu immagini le aggiunge tutte
        this.linkArray = [];
        var linkNum = 0;       

        if ((imageLink.rel == 'lightbox')){
            // if image is NOT part of a set, add single image to linkArray
            this.linkArray.push([imageLink.href, imageLink.title]);         
        } else {
            // if image is part of a set..
            this.linkArray = 
                $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
                collect(function(anchor){ return [anchor.href, anchor.title]; }).
                uniq();
            
            while (this.linkArray[linkNum][0] != imageLink.href) { linkNum++; }
           
        }

        //imposta la posizione dell' lightbox bianco in pagina
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var lightboxLeft = (arrayPageSize[0]-600)/2;
        this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
        this.showPag(linkNum);
      
    },
    
    // Mostra il l'immagine di loading e carica l'articolo
    showPag: function(linkNum) {  
        this.activeLink = linkNum;//salva nella variabile globale in numero del link
        $('textbox').innerHTML='<img id="img_loading" align="center" src="http://www.inetsport.it/public/immagini/loading.gif">';
		
        //ajax updater
        var url=this.linkArray[this.activeLink]+" ";
        lunghezza=url.length;
        lunghezza=lunghezza-2;
        url=url.substr(0,lunghezza);
        new Ajax.Updater('textbox', url,{method:'get'});   
        this.aggiornaControlliNavigazione();
     
    },
    
    //nasconde o visualizza i tasti per la navigazione tra gli articoli
    aggiornaControlliNavigazione: function(){
    	//crea e mostra le info sugli articoli
    	
    	if(this.linkArray.length>1)this.info.update( 'Articolo ' + (this.activeLink+1) + ' di ' + this.linkArray.length).show();
    	else{this.info.hide();}
    	
    	
        this.backB.show();
        this.nextB.show();
        //nasconde quelli che non servono
    	if(this.activeLink==0) this.backB.hide();
    	if(this.activeLink==this.linkArray.length-1) this.nextB.hide();
    },
    
    //passa all'articolo successivo
    cambiaArticolo:function(direzione){
    	
    	if(direzione=="avanti") this.showPag(this.activeLink+1);
    	else{ this.showPag(this.activeLink-1)};
    },
  
    //  getPageSize()
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	},
	
	
	  end: function() {
//      this.disableKeyboardNav();
        this.lightbox.hide();
        new Effect.Fade(this.overlay, { duration: this.overlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
        //nasconde navigator bar se è aperto
        if($('navigator_slide')){
        	if($('navigator_slide').style.display!='none'){
        		toggle_slide('navigator_slide');
        	}
        }
    }


}

document.observe('dom:loaded', function () { new Lightbox(); });
