/************** Zoom effect JS ****************/
 
 risoluzione = $(window).width();
  
// init arrays
var scrollers = Array;
scrollers['headerimage'] = { timer: null };
scrollers['featureslide'] = { timer: null };
var curSlide = 1;

// easing
function cubicInOut(t, b, c, d)
{
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}

// easing
function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
		
// perform the move	
function move(scroll,container)
{
	if (scroll.time > scroll.duration)
	{
		clearInterval(scrollers[container].timer);
		scrollers[container].timer = null;
	}
	else
	{	
		element = document.getElementById(scroll.container);
		element.scrollLeft = cubicInOut(scroll.time, scroll.begin, scroll.change, scroll.duration);
		scroll.time++;
	}
}

// initialise the move
function startMove(container,pos,duration)
{	
	if (scrollers[container].timer != null)
	{
		clearInterval(scrollers[container].timer);
		scrollers[container].timer = null;
	}
	
	start = document.getElementById(container).scrollLeft;
	end = pos;
	begin = document.getElementById(container).scrollLeft;	
										
	scrollers[container].time = 0;
	scrollers[container].begin = begin;
	scrollers[container].change = end - start;
	scrollers[container].duration = duration;
	scrollers[container].container = container;
	scrollers[container].timer = setInterval(function(){move(scrollers[container],container);}, 15);
}


function getImgPath(e,state)
{
	if(!(imgSRC = getPattern(document.getElementById(e).childNodes[0].currentStyle.backgroundImage,"\"(.*)_(.*)\.png\"")))
	 imgSRC = getPattern(document.getElementById(e).childNodes[0].currentStyle.filter,"src='(.*)_(.*)\.png',");
	if (state)
		return imgSRC+"_on.png";
	else
		return imgSRC+"_off.png";
	//return imgSRC+".png";
}





// set active tabs
function setNavClass(navID)
{
	if(/MSIE (5\.5|6\.)/.test(navigator.userAgent))
	{
		fixElement('hn_eolico',getImgPath('hn_eolico',false));
		fixElement('hn_fotovoltaico',getImgPath('hn_fotovoltaico',false));
		fixElement('hn_bioenergia',getImgPath('hn_bioenergia',false));
		fixElement(navID,getImgPath(navID,true));	
	}
	else
	{
		document.getElementById('hn_eolico').className = 'off';
		document.getElementById('hn_fotovoltaico').className = 'off';
		document.getElementById('hn_bioenergia').className = 'off';	
		document.getElementById(navID).className = 'on';	
	}
}


// set deactive tabs //************MAX**********//
function deNavClass(navID)
{
	if(/MSIE (5\.5|6\.)/.test(navigator.userAgent))
	{
		fixElement('hn_eolico',getImgPath('hn_eolico',false));
		fixElement('hn_fotovoltaico',getImgPath('hn_fotovoltaico',false));
		fixElement('hn_bioenergia',getImgPath('hn_bioenergia',false));
		fixElement(navID,getImgPath(navID,false));	
	}
	else
	{
		document.getElementById('hn_eolico').className = 'off';
		document.getElementById('hn_fotovoltaico').className = 'off';
		document.getElementById('hn_bioenergia').className = 'off';	
		document.getElementById(navID).className = 'off';	
	}
}
//************MAX**********//


// change the navigation (perform all functions)
function changeNav(nav)
{
	if (nav == 'left')
	{
		nav = curSlide;
		if (nav > 1)
		{
			nav--;
			curSlide = nav;
		}
	}
	if (nav == 'right')
	{
		nav = curSlide;
		if (nav < 3)
		{
			nav++;	
			curSlide = nav;
		}
	}	
	
	// ALE: apre/chiude sotto menu
	if ( $('#featureslide').css('height') == "0px" ) {
	//	console.log ("chiuso > apri");
		openSottomenu(); 
	} else {
		switch ( nav ) {
			case 1:
				if ( $('#hn_eolico').hasClass('on') ) closeSottomenu();
				break;
			case 2:
				if ( $('#hn_fotovoltaico').hasClass('on') ) closeSottomenu();
				break;
			case 3:
				if ( $('#hn_bioenergia').hasClass('on') ) closeSottomenu();
				break;				
		}
	}
	// end ALE

	if (nav == 1)
	{
		document.getElementById('home_arrow_left').style.display = "none";
		document.getElementById('home_arrow_right').style.display = "block";
		if (document.getElementById('headerimage') != null)
		{
			startMove('headerimage',0,80);
		/**MAX to close button**/
		if ( $('#hn_eolico').hasClass('off') ){ 
		setNavClass('hn_eolico');		
		} else if ( $('#hn_eolico').hasClass('on') ) {
        deNavClass('hn_eolico');		
		}/**MAX to close button**/
		//setNavClass('hn_eolico');
		
		} 
		startMove('featureslide',0,100); 			
		return false;
	}
	if (nav == 2)
	{
		document.getElementById('home_arrow_left').style.display = "block";
		document.getElementById('home_arrow_right').style.display = "block";
		
		if (document.getElementById('headerimage') != null)
		{	
			
			 if(risoluzione<=1060) {
				 startMove('headerimage',958,80);
			 } else {
				 startMove('headerimage',998,80); 
			}
		/**MAX to close button**/
		if ( $('#hn_fotovoltaico').hasClass('off') ){ 
		setNavClass('hn_fotovoltaico');		
		} else if ( $('#hn_fotovoltaico').hasClass('on') ) {
        deNavClass('hn_fotovoltaico');		
		}/**MAX to close button**/
		//	setNavClass('hn_fotovoltaico');			
		
		}
		
			 if(risoluzione<=1060) {
		startMove('featureslide',1010,100);
		return false;
				 
			 } else {
				 
		startMove('featureslide',1050,100); 					
		return false;
		
			 }
	}
	if (nav == 3)
	{
		document.getElementById('home_arrow_left').style.display = "block";
		document.getElementById('home_arrow_right').style.display = "none";
		
		if (document.getElementById('headerimage') != null)
		{	
			 if(risoluzione<=1060) {
				 startMove('headerimage',1916,80);
			 } else {
						
			startMove('headerimage',1996,80);
			}
			
			
		/**MAX to close button**/
		if ( $('#hn_bioenergia').hasClass('off') ){ 
		setNavClass('hn_bioenergia');		
		} else if ( $('#hn_bioenergia').hasClass('on') ) {
        deNavClass('hn_bioenergia');		
		}/**MAX to close button**/
			
		//setNavClass('hn_bioenergia');	
		}
		
		if(risoluzione<=1060) {
		startMove('featureslide',2016,100);			
		return false;
			 } else {
		startMove('featureslide',2096,100);			
		return false;
			 }
	}	
}


function fixElement(e,imgSrc)
{
	element = document.getElementById(e).childNodes[0];
	if (imgSrc)
	{
    	element.style.backgroundImage = "none";
		element.style.backgroundColor = "transparent";
		element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
		imgSrc + "',sizingMethod='crop')";
	}
}

function getPattern(inputString,regPat)
{
//alert("PAT: "+regPat);
//alert("INP: "+inputString);
	var reg=new RegExp(regPat,"i");
	var ar = reg.exec(inputString);
 	if(ar!=null){
 	// alert("MATCH: "+ar[1]);
 	 return ar[1];
 	}
	return null;
}


// ALE: apre/chiude sotto menu
function openSottomenu ()
{	
	$('#featureslide').animate( { height: "207px" }, 1000 );

	/*var p = $("#wrapper");
	var position = p.position(); 
	//console.log( "dopo: " + position.top );
	$('#middle_shadow_bottom').css ( "z-index", 9999 );
	$('#middle_shadow_bottom').animate( { display: "block", width: p.css('width'), height: p.css('height') }, 1000 );
	*/
	/*
	var p = $("#main");
	var position = p.position(); console.log( "dopo: " + position.top );
	$('#mask-main').css ( "z-index", 9999 );
	$('#mask-main').animate( { display: "block", position: "absolute", left: position.left, top: position.top, width: p.css('width'), height: p.css('height'), backgroundColor: '#000000' }, 1000 );
	*/
	
	$('#wrapper').animate( { opacity: 0.5 /*, backgroundColor: '#666666' */ }, 1000 );
	//TO DO//
	$('#slider, #slider img, .nivo-controlNav').animate( { opacity: 0.5 }, 1000 );
	$('#slider .nivo-caption').animate( { opacity: 0.3 }, 1100 );
	
	
	//$('#lay-int #wrapper').animate( { opacity: 1 /*, backgroundColor: '#666666' */ }, 1000 );
	//$('#main').append( "<div id='mask-main'>mask-main</div>" );
	//$('#mask-main').css( { width: 100, height: 100, color: "#f00" } );
}

function closeSottomenu ()
{

	$('#wrapper').animate( { opacity: 1 /*, backgroundColor: '#E6E7E8' */ }, 1000 );
//TO DO//
	$('#slider, #slider img, .nivo-controlNav').animate( { opacity: 1 }, 1000 );
	$('#slider .nivo-caption').animate( { opacity: 0.7 }, 1100 );
	
	$('#featureslide').animate( { height: "0px" }, 1000 );
	$('#middle_shadow_bottom').animate( { opacity: 1 }, 1000 );	
}
// end ALE
