/////////////////////////////////////////////////////////////////////////////////////////////
// NTool[SITE GUEST]		JS - Effect Slider
// Copyright (C) 				2007 - 2011  Gruppo36 (All rights reserved)
// Autore 							Chiodi Paolo
/////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////
GetElements = function( Elements ){
	////////////////////////////////////
	var Items = {};
	for( var i=0; i<Elements.length; i++ ){
		var Obj = Elements[i].T;
		if( Elements[i].O ){
			Obj = Elements[i].O;
		}
		Items[Obj] = {tag:document.getElementById(Elements[i].T), style:getDivStyle(Elements[i].T)};
	}
	////////////////////////////////////
	if( Elements.length==1 ) return Items[Obj];
	return Items;
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
setStyleN = function( n ){
	////////////////////////////////////
	return n+'px';
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
CustEvent = function( element, eventType, handler, capture ){
	//////////////////////////////////// 
	try	{
		if( element.addEventListener ){
			element.addEventListener(eventType, handler, capture);
		}	else if( element.attachEvent ){
			element.attachEvent('on' + eventType, handler);
		} else {
			alert('CustEvent '+element, e+"\n"+handler );
			return;
		}
		return handler;
	}	catch (e){  /*alert('CustEvent ('+element+', '+eventType+')' + "\n" + e + "\n" + handler );*/  }
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////
WindowDim = function(){
	//////////////////////////////////// 	
	if( typeof window.innerWidth!='undefined' ){
		return {W:window.innerWidth, H:window.innerHeight};	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	} 
	if(typeof document.documentElement!='undefined'&& typeof document.documentElement.clientWidth!='undefined' && document.documentElement.clientWidth!=0 ){
		return {W:document.documentElement.clientWidth, H:document.documentElement.clientHeight};		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	}
	return {W:document.getElementsByTagName('body')[0].clientWidth, H:document.getElementsByTagName('body')[0].clientHeight};
	//////////////////////////////////// 	
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider = function( xTg, xDim, xN, xVerso, xSpeedMode ){
	//////////////////////////////////// 
	this.Tg = xTg;
	this.TgTo = xTg;
	this.Obj = 'Obj'+xTg;
	//////////////////////////////////// 
	switch( xSpeedMode ){
		case 'linear':	xSpeedMode = 'linear';			break;
		default: 				xSpeedMode = 'progressive';	break;
	}
	////////////////////////////////////
	var xDimN;
	if( !isNaN(xDim) ){
		xDimN = Math.ceil(xDim/xN);
		this.ResizeMode = 'fixed';
	} else {
		xDimN = 0;
		this.ResizeMode = String(xDim);
		xDim = 0;
	}
	//////////////////////////////////// 
	this.Time = 		{Delay:Math.floor(1000/40), DelayNext:15000, Speed:24, SpeedMode:xSpeedMode, SpeedMin:4};
	this.Display = 	{N:xN, Dim:xDim, DimN:xDimN, Moving:false, Over:true};
	this.Timer = 		{Move:null, Next:null};
	this.Scroll = 	{From:0, To:0, Ind:0, Step:Math.max(this.Display.Dim/1000*this.Time.Speed, this.Time.SpeedMin)};
	//////////////////////////////////// 
	if( !xVerso || xVerso=='vertical' ){
		this.Verso = {Verso:'vertical', 	Dim:'height', D:'H', 	Pos:'scrollTop'};
	} else {
		this.Verso = {Verso:'horizontal', Dim:'width', 	D:'W',	Pos:'scrollLeft'};
	}
	//////////////////////////////////// 
	this.Ele = GetElements([{T:this.Tg, O:'Main'}, {T:this.Tg+'Container', O:'ContainerFrom'}, {T:this.TgTo+'Container', O:'Container'}, {T:this.TgTo+'Buttons', O:'Buttons'}, {T:'Debug'}]);
	//////////////////////////////////// 
	this.Templates = {Button:'', ButtonSel:''};
	if( this.Ele.Buttons.tag ){
		this.Templates = {Button:document.getElementById(this.Tg+'Button').innerHTML, ButtonSel:document.getElementById(this.Tg+'ButtonSel').innerHTML};
	}
	//////////////////////////////////// 
	this.Items = new Array();
	this.Images = new Array();
	//////////////////////////////////// 
	for( var i=0; i<this.Ele.ContainerFrom.tag.childNodes.length; i++ ){
		var xItem = this.Ele.ContainerFrom.tag.childNodes[i];
		if( xItem.innerHTML ){
			if( this.ResizeMode!='fixed' ){
				var Imgs = xItem.getElementsByTagName('img');
				if( Imgs.length>0 ){
					this.Images.push({R:Imgs[0].width/Imgs[0].height, W:Imgs[0].width, H:Imgs[0].height});
					this.Items.push(i);
				}
			} else {
				this.Items.push(i);
			}
		}
	}
	//////////////////////////////////// 	
	var FirstItem = '';	
	this.ItemsTot = this.Items.length;
	if( this.Items.length>0 ){
		this.Items.push(this.Items[0]);
		FirstItem = this.Ele.Container.tag.childNodes[this.Items[0]].innerHTML;
		if( this.ResizeMode!='fixed' ){
			this.Images.push(this.Images[0]);
		}
	}
	//////////////////////////////////// 		
	var _Self = this;		
	var HTML = this.Ele.ContainerFrom.tag.innerHTML;
	this.Ele.Main.style.overflow = 'hidden';
	this.Ele.ContainerFrom.tag.innerHTML
	this.Ele.Container.tag.innerHTML = HTML + FirstItem;
	this.Ele.Container.style.display = 'none';
	////////////////////////////////////
	this.Slide = {Dim:this.Display.DimN*this.ItemsTot};
	this.Ele.Container.style[this.Verso.Dim] = setStyleN(this.Slide.Dim*2+50);
	////////////////////////////////////
	if( this.ResizeMode!='fixed' ){
		////////////////////////////////////
		if( this.ResizeMode=='resize_full' ){
			////////////////////////////////////
			this.ResizeImage = function( iNode, iObj ){
				////////////////////////////////////
				var WinF, ImgF, MTop = '0px', MLeft = '0px';
				if( this.Win.Ratio>iObj.R ){
					WinF = this.Win.RatioG;
					ImgF = ( iObj.W>iObj.H )?'W':'H';
					MTop = Math.floor((this.Win.Dim.H-iObj.H)*0.5)+'px';
				} else {
					WinF = this.Win.RatioL;	
					ImgF = ( iObj.W>iObj.H )?'H':'W';
					MLeft = Math.floor((this.Win.Dim.W-iObj.W)*0.5)+'px';
				}
				iNode.style.marginTop = MTop;
				iNode.style.marginLeft = MLeft;
				this['ResizeImage'+ImgF]( iNode, this.Win.Dim[WinF], iObj );
				////////////////////////////////////
			}
			////////////////////////////////////
		} else {
			////////////////////////////////////
			switch( this.Verso.Verso ){
				case 'horizontal':		this.ResizeImage = function( iNode, iObj ){	this.ResizeImageW( iNode, this.Win.Dim.W, iObj );	};		break;
				default:							this.ResizeImage = function( iNode, iObj ){	this.ResizeImageH( iNode, this.Win.Dim.H, iObj );	};		break;
			}
			////////////////////////////////////
		}
		////////////////////////////////////					
		CustEvent( window, 'resize', 	function(){		_Self.Listener_Resize();	}, false );
		////////////////////////////////////
		this.Listener_Resize();
		////////////////////////////////////
	} else {
		////////////////////////////////////
		this.Ele.Main.style[this.Verso.Dim] = setStyleN(xDimN);
		////////////////////////////////////
	}
	////////////////////////////////////
	CustEvent( window, 'load', 		function(){		_Self.Listener_Load();		}, false );
	//////////////////////////////////// 
}	
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Init = function(){
	////////////////////////////////////
	if( this.Display.Over==true ){
		var _Self = this;		
		CustEvent( this.Ele.Main.tag, 'mouseover', 	function(){		_Self.Listener_RollOver();		}, false );
		CustEvent( this.Ele.Main.tag, 'mouseout', 	function(){		_Self.Listener_RollOut();			}, false );
	}
	////////////////////////////////////
	if( this.ItemsTot-this.Display.N>0 ){
		this.Scroll.Ind = (this.Time.SpeedMode=='progressive')?-1:0;
		this.MoveNext();
	} else {
		this.SetButtons();
	}
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Listener_Load = function(){
	////////////////////////////////////		
	if( this.ResizeMode!='fixed' ){
		this.Listener_Resize();
	} else {
		this.Resize();
	}
	this.Ele.Container.style.display = '';
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.ResizeImageW = function( iNode, W, iObj ){
	////////////////////////////////////
	iNode.width = W;
	iNode.height = Math.floor(W/iObj.R);
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.ResizeImageH = function( iNode, H, R ){
	////////////////////////////////////
	iNode.height = H;
	iNode.width = Math.floor(H*iObj.R);
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Listener_Resize = function(){
	////////////////////////////////////
	var WDim = WindowDim();
	this.Win = {Dim:WDim, Ratio:WDim.W/WDim.H, RatioG:'W', RatioL:'H' };
	if( WDim.W<WDim.H ){
		this.Win.RatioG = 'H';
		this.Win.RatioL = 'W';
	}
	//////////////////////////////////// 
	this.Display.Dim = Number(this.Win.Dim[this.Verso.D]);
	this.Display.DimN = Math.floor(this.Display.Dim/this.Display.N);
	this.Scroll.Step = Math.max(this.Display.Dim*0.001*this.Time.Speed, this.Time.SpeedMin);
	//////////////////////////////////// 
	this.Ele.Main.style.width = setStyleN(this.Win.Dim.W);
	this.Ele.Main.style.height = setStyleN(this.Win.Dim.H);
	////////////////////////////////////
	this.Slide = {Dim:this.Display.DimN*this.ItemsTot};
	this.Ele.Container.style[this.Verso.Dim] = setStyleN(this.Slide.Dim*2+50);
	////////////////////////////////////
	this.Scroll.From = this.Scroll.Ind*this.Display.DimN;
	if( !this.Timer.Move ){
		this.Ele.Main.tag[this.Verso.Pos] = Math.floor(this.Scroll.From);
		this.TimerClear('Move');
	}	
	////////////////////////////////////
	var DimPx = setStyleN(this.Display.Dim);
	for( var i=0; i<this.Images.length; i++ ){
		////////////////////////////////////
		var xItem = this.Ele.Container.tag.childNodes[this.Items[i]];
		xItem.style[this.Verso.Dim] = DimPx;
		////////////////////////////////////
		var Imgs = xItem.getElementsByTagName('img');
		this.ResizeImage( Imgs[0], this.Images[i] );
		////////////////////////////////////
	}
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Resize = function(){
	////////////////////////////////////
	this.Ele.Main.style[this.Verso.Dim] = setStyleN(this.Display.DimN*this.Display.N);
	////////////////////////////////////
	for( var i=0; i<this.Items.length; i++ ){
		////////////////////////////////////
		var xItem = this.Ele.Container.tag.childNodes[this.Items[i]];
		xItem.style[this.Verso.Dim] = setStyleN(this.Display.Dim);
		////////////////////////////////////
	}
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.MoveTo = function( xTo ){
	//////////////////////////////////// 			
	if( xTo==0 && this.Scroll.Ind>=(this.ItemsTot/2) ){
		this.Scroll.Ind = Number(xTo);	
		this.Scroll.To = (this.Display.DimN*this.ItemsTot);
	} else {	
		this.Scroll.Ind = Number(xTo);	
		this.Scroll.To = (this.Display.DimN*this.Scroll.Ind);
	}
	//////////////////////////////////// 	
	this.SetButtons();
	this.Move();					
	//////////////////////////////////// 	
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.MoveNext = function(){
	//////////////////////////////////// 	
	this.Display.Moving = true;
	//////////////////////////////////// 			
	if( this.Scroll.Ind<(this.ItemsTot-1) ){
		this.MoveTo(Number(this.Scroll.Ind)+1);
	} else {	
		this.MoveTo(0);
	}	
	//////////////////////////////////// 	
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Move = function(){
	//////////////////////////////////// 
	var Move = 'Move_'+this.Time.SpeedMode;
	//////////////////////////////////// 
	this.TimerClear( 'Move' );
	this.TimerClear( 'Next' );
	//////////////////////////////////// 
	if( this.Scroll.From != this.Scroll.To ){
		if( this.Scroll.From<this.Scroll.To ){
			this.Timer.Move = setInterval( this.Obj+'.'+Move+'Forward()', this.Time.Delay);
		} else {
			this.Timer.Move = setInterval( this.Obj+'.'+Move+'Back()', this.Time.Delay);
		}
	} else {
		this.MoveEnd( false );
	}
	//////////////////////////////////// 
}	
/////////////////////////////////////////////////////////////////////////////////////////////	
NJslider.prototype.MoveEnd = function( xMove ){
	////////////////////////////////////
	if( xMove==false ){
		if( this.Display.Moving==true ){
			this.TimerClear( 'Move' );
			this.Timer.Next = setTimeout( this.Obj+'.MoveNext()', (this.Time.DelayNext==0)?this.Time.Delay:this.Time.DelayNext);
		}
	}
	//////////////////////////////////// 
}	
/////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////// FORWARD PROGRESSIVE 
NJslider.prototype.Move_progressiveForward = function(){
	////////////////////////////////////
	this.Scroll.From += Math.max((this.Scroll.To-this.Scroll.From)/100*this.Time.Speed, this.Time.SpeedMin);			
	this.Move_limitForward( true )		
	////////////////////////////////////
}	
///////////////////////////////////////////////////////////////////////////////////////////// FORWARD LINEAR 
NJslider.prototype.Move_linearForward = function(){
	////////////////////////////////////
	this.Scroll.From += this.Scroll.Step;			
	this.Move_limitForward( true )
	////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////// FORWARD LIMIT
NJslider.prototype.Move_limitForward = function( xMove ){
	////////////////////////////////////
	if( this.Scroll.From >= this.Slide.Dim ){
		this.Scroll.From = 0;
		this.Scroll.To = 0;
	}
	////////////////////////////////////
	if( this.Scroll.From >= this.Scroll.To ){
		this.Scroll.From = this.Scroll.Ind*this.Display.DimN;
		xMove = false
	}
	this.Ele.Main.tag[this.Verso.Pos] = Math.floor(this.Scroll.From);
	this.MoveEnd( xMove );
	////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////// BACK PROGRESSIVE 
NJslider.prototype.Move_progressiveBack = function(){
	////////////////////////////////////
	this.Scroll.From += Math.min((this.Scroll.To-this.Scroll.From)/100*this.Time.Speed, -this.Time.SpeedMin);	
	this.Move_limitBack( true );
	////////////////////////////////////
}	
///////////////////////////////////////////////////////////////////////////////////////////// BACK LINEAR 
NJslider.prototype.Move_linearBack = function(){
	////////////////////////////////////
	this.Scroll.From -= this.Scroll.Step;	
	this.Move_limitBack( true );
	////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////// BACK LIMIT
NJslider.prototype.Move_limitBack = function( xMove ){
	////////////////////////////////////
	if( this.Scroll.From <= this.Scroll.To ){
		this.Scroll.From = this.Scroll.Ind*this.Display.DimN;
		xMove = false
	}
	this.Ele.Main.tag[this.Verso.Pos] = Math.floor(this.Scroll.From);
	this.MoveEnd( xMove );
	////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.SetButtons = function(){
	////////////////////////////////////
	if( this.Ele.Buttons.tag ){
		//////////////////////////////////// 					
		var ButtonsHTML = '';
		for( var i=0; i<this.ItemsTot; i++ ){
			if( this.Scroll.Ind==i ){
				ButtonHTML = String(this.Templates.ButtonSel);
			} else {
				ButtonHTML = String(this.Templates.Button);
				ButtonHTML = ButtonHTML.replace('href=""', 'href="javascript:'+this.Obj+'.Listener_Button('+i+');"');
			}
			ButtonsHTML += ButtonHTML; 
		}
		this.Ele.Buttons.tag.innerHTML = ButtonsHTML;
		//////////////////////////////////// 
	}
	//////////////////////////////////// 
}	
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Listener_Button = function( xTo ){
	//////////////////////////////////// 			
	this.TimerClear( 'Move' );
	this.TimerClear( 'Next' );
	this.MoveTo( xTo );
	//////////////////////////////////// 	
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Listener_RollOver = function(){
	//////////////////////////////////// 
	this.Display.Moving = false;
	this.TimerClear( 'Next' );
	if( this.Time.DelayNext==0 ){
		this.TimerClear( 'Move' );
	}
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.Listener_RollOut = function(){
	//////////////////////////////////// 
	this.Display.Moving = true;
	this.TimerClear( 'Next' );
	this.Timer.Next = setTimeout( this.Obj+'.MoveNext()', this.Time.DelayNext);
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////
NJslider.prototype.TimerClear = function( what ){
	//////////////////////////////////// 
	if( this.Timer[what] ){
		clearTimeout( this.Timer[what] );
		this.Timer[what] = null;
	}
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////





