
var b2c__cX = 0;
var b2c__cY = 0;
var b2c__rX = 0;
var b2c__rY = 0;

function UpdateCursorPosition(e)
{
	b2c__cX = e.pageX;
	b2c__cY = e.pageY;
}

function UpdateCursorPositionDocAll(e)
{ 
	b2c__cX = event.clientX;
	b2c__cY = event.clientY;
}

function ActivateMouseTrace()
{
	if(document.all) 
		{ document.onmousemove = UpdateCursorPositionDocAll; }
	else 
		{ document.onmousemove = UpdateCursorPosition; }
}

function AssignPosition(d) 
{
	if(self.pageYOffset) {
		b2c__rX = self.pageXOffset;
		b2c__rY = self.pageYOffset;
		}
	else if(document.documentElement && document.documentElement.scrollTop) {
		b2c__rX = document.documentElement.scrollLeft;
		b2c__rY = document.documentElement.scrollTop;
		}
	else if(document.body) {
		b2c__rX = document.body.scrollLeft;
		b2c__rY = document.body.scrollTop;
		}
	if(document.all) {
		b2c__cX += b2c__rX; 
		b2c__cY += b2c__rY;
		}
	d.style.left = (b2c__cX+10) + "px";
	d.style.top = (b2c__cY+10) + "px";
}

function HideContent(d) 
{
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) 
{
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}

function ReverseContentDisplay(d) 
{
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") { dd.style.display = "block"; }
	else { dd.style.display = "none"; }
}


/* Espande/nasconde una div */
function b2c_expand_DIV(argImgObj, argDIVObj, argImgPath, argImgClps, argImgExpd)
{

  var my_div = document.getElementById(argDIVObj);

  if(my_div.style.display != 'none')
  {
  	/*
  	Effect.BlindUp(my_div);
  	*/
  	my_div.style.display = 'none';
  }  else {
  	/*
  	Effect.BlindDown(my_div);
  	*/
	  my_div.style.display = 'block';
	}

	if (argImgObj != '')
	{
  	var my_img = document.getElementById(argImgObj);
  	
		if	(my_div.style.display == 'none')
		{
			my_img.src = argImgPath + "/" + argImgExpd;
		} else {
			my_img.src = argImgPath + "/" + argImgClps;	 
		}
  }
	
}


/* Scambia le immagini dello zoom */
function SwitchImage(ImageNum)
{
	
	var zoomtype = document.getElementById('B2BSZTV2').value;
	switch (zoomtype)
	{
	case "":
	   var preview = document.getElementById('IMGPREVIEW');
	   var zoom = document.getElementById('zoomimage');
	   InsertHidden(document.LANSA,'B2C_PTMG',ImageNum);
	   switch (ImageNum)
		  {
		  case 1:
			 preview.src=document.getElementById('B2C_ZIM1').value;
			 zoom.href=document.getElementById('B2C_BIG1').value;
			 break;
		  case 2:
			 preview.src=document.getElementById('B2C_ZIM2').value;
			 zoom.href=document.getElementById('B2C_BIG2').value;
			 break;
		  case 3:
			 preview.src=document.getElementById('B2C_ZIM3').value;
			 zoom.href=document.getElementById('B2C_BIG3').value;
			 break;
		  
		  case 4:
			 preview.src=document.getElementById('B2C_ZIM4').value;
			 zoom.href=document.getElementById('B2C_BIG4').value;
			 break;
		  }
	   break;
   case "F":
	var mydiv1 = document.getElementById('flash-zoom-1');
		var mydiv2 = document.getElementById('flash-zoom-2');
		var mydiv3 = document.getElementById('flash-zoom-3');
		var mydiv4 = document.getElementById('flash-zoom-4');
		switch (ImageNum)
			{
			case 1:
				mydiv1.style.display = 'block';
				mydiv2.style.display = 'none';
				mydiv3.style.display = 'none';
				mydiv4.style.display = 'none';				
				break;
			case 2:
				mydiv2.style.display = 'block';
				mydiv1.style.display = 'none';
				mydiv3.style.display = 'none';
				mydiv4.style.display = 'none';
				break;
			case 3:
				mydiv3.style.display = 'block';
				mydiv1.style.display = 'none';
				mydiv2.style.display = 'none';
				mydiv4.style.display = 'none';
				break;
			case 4:
				mydiv4.style.display = 'block';
				mydiv1.style.display = 'none';
				mydiv2.style.display = 'none';
				mydiv3.style.display = 'none';
				break;
			}
		break;
	}
}

 /* Mostra/nasconde lo splash screen */
function ToggleSplash(mode) 
	{
	
	var splashdivb = document.getElementById('splash-div-back');
	var splashdivf = document.getElementById('splash-div-fore');
	var slpashvar = document.getElementById('B2C_SPLS');

	if (mode == 'show'){
		{
		Effect.Appear(splashdivb, { from: 0.0, to: 0.8, duration: 1.0 });
		Effect.Appear(splashdivf, { from: 0.0, to: 1.0, duration: 1.0 });
		//Imposto un timeout per nascondere la splash-image in automatico //
		var alertTimerId  = setTimeout( "ToggleSplash('hide')", 3000 );

		}
	}
	else
	{
		{
		Effect.Fade(splashdivb, { from: 0.8, to: 0.0 });
		Effect.Fade(splashdivf, { from: 1.0, to: 0.0 });
		//Imposto il cookie per la sessione cosi la splash-image non compare di nuovo facendo indietro//
		SetCookie('SplashSkip','TRUE');
		//Azzero il timer//
		clearTimeout(alertTimerId);
		}
	slpashvar.value = "S";
	}
	}

/* Lettura dati del Cookie */
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

/* Lettura cookie */
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

/* Scrittura Cookie */
function SetCookie(name, value, expDays, path, domain, secure) {
	// Set cookie with name, value etc provided
	// in function call and date from above
	// Number of days the cookie should persist NB expDays='' or undef. => non-persistent
	if (expDays != null ) {
		var expires = new Date(); 
		expires.setTime(expires.getTime() + (expDays*24*60*60*1000));
	}	
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

/* Avvio pop-up per condivisione su FaceBook */
function sharefb(u,t)
{
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}
	
/* Inizializza la barra di scorrimento verticale */
function initVerticalScroll(scrollable, track, handle)
{
	var scrollable1 = document.getElementById(scrollable);
	var track1 = document.getElementById(track);
	var handle1 = document.getElementById(handle);
	
	scrollable1.scrollobj = new Control.Slider(handle1, track1, {
		range: $R(0,20),
		axis: 'vertical',
		onSlide: function(v) { scrollVertical(v, scrollable1, scrollable1.scrollobj);  },
		onChange: function(v) { scrollVertical(v, scrollable1, scrollable1.scrollobj); }
		});
		
	// disable vertical scrolling if text doesn't overflow the div
	if (scrollable1.scrollHeight <= scrollable1.offsetHeight) {
		scrollable1.scrollobj.setDisabled();
		track1.hide();
		}		
		
	// mozilla
	Event.observe(scrollable1, 'DOMMouseScroll', wheel);
	
	// IE/Opera
	Event.observe(scrollable1, 'mousewheel', wheel);
	}

/* scroll the element vertically based on its width and the slider maximum value */
 function scrollVertical(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
 }
 
 
/* Event handler for mouse wheel event. */
function wheel(event){
	var delta = 0;
	if (!event) /* For IE. */
		event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
		/** In Opera 9, delta differs in sign as compared to IE. */
		if (window.opera)
			delta = -delta;
	} else if (event.detail) { /** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		* Also, delta is multiple of 3.
		*/
		delta = -event.detail/3;
	}

	/** If delta is nonzero, handle it.
	* Basically, delta is now positive if wheel was scrolled up,
	* and negative, if wheel was scrolled down.
	*/
	
	var elem = null;
	var slidobj = null;
	
	// Verifica se il browser supporta srcElement
	if (event.srcElement)
	{
		elem = event.srcElement;
	} else {
		elem = event.target;
	}
	
	// Se l'elemento corrente non contiene il riferimento,
	// scorre all'indietro fino a trovarlo.
	while (!elem.scrollobj)
	{
	elem = elem.parentNode;
	}
		slidobj = elem.scrollobj;

	// Richiama lo scroll passando il delta
	if (delta)
	{
		handle(slidobj, delta);
	}

	/** Prevent default actions caused by mouse wheel.
	* That might be ugly, but we handle scrolls somehow
	* anyway, so don't bother here..
	*/
	if (event.preventDefault)
		event.preventDefault();
	
	event.returnValue = false;
}

/* Funzione di scrolling */
function handle(slidobj, delta) {
	slidobj.setValueBy(-delta);

}

/* Seleziona tutto il testo */
function SelectText(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

/* Cambio nazione */
function ChangeCountry(idItalia) {
	var comboValue; 
	var selIndex = document.LANSA.B2C_INAZ.selectedIndex; 
	document.LANSA.B2C_DNA2.value = document.LANSA.B2C_INAZ.options[selIndex].text;
	
	var divflag = document.getElementById('flagprov');
	
	if (document.LANSA.B2C_INAZ.options[selIndex].value == idItalia.value)
		{
		divflag.innerHTML = "*";
		}
		else
		{
		divflag.innerHTML = " ";
		}
}

/* Validatore indirizzo e-mail */
function validate(form_id,email,alertmsg) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {
      alert(alertmsg);
      return false;
   }
}

/* Lettura dimensione finestra browser */
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

/* Scambia la visualizzazione di 2 DIV */
function swapDivs(div1, div2, divstyle) {
	var vdiv1 = document.getElementById(div1);
	var vdiv2 = document.getElementById(div2);

	if (vdiv1.style.display == 'none') {
		vdiv1.style.display = divstyle;
		vdiv2.style.display = 'none';
		}
	else
		{
		vdiv1.style.display = 'none';
		vdiv2.style.display = divstyle;
		}
	}

function setCursor(cursor)
{
	if (cursor != '')
	{
	  document.body.style.cursor = cursor;
	}	else	{
	  document.body.style.cursor = 'auto';
	}
}


function edDivMouseOver(theDiv)
{
	var newSrc = theDiv.getAttribute("__moImage");
	var oldSrc = theDiv.getAttribute("__stdImage");
	if ((newSrc) && (!oldSrc))
	{
	   theDiv.setAttribute("__stdImage", theDiv.style.backgroundImage);
	   theDiv.style.backgroundImage = "url(" + newSrc + ")";
	}
}
function edDivMouseOut(theDiv)
{
   var oldSrc = theDiv.getAttribute("__stdImage");
   if (oldSrc)
   {
      theDiv.style.backgroundImage = oldSrc;
      theDiv.setAttribute("__stdImage", "");
   }
}
function checkdblclick(argObjTest)
  {
  argObjTest.value = parseInt(argObjTest.value) + 1;
  if (parseInt(argObjTest.value) > 1)
	 {
	 return true;
	 } else {
	 return false;
	 } 
  }

/* Imposto l'IFRAME del contenitore video*/  
function setupvideo(argVideoContainer)
{
	if (typeof(argVideoContainer)=="string") { argVideoContainer=document.getElementById(argVideoContainer); }
	if (argVideoContainer.innerHTML)
		{
		
		org = argVideoContainer.innerHTML;
		// Devo aggiungere i parametri per gestire correttamente il video
		argVideoContainer.innerHTML='';
		org = org.replace(/<iframe/i,'<IFRAME class="vimeo" id="player1"');
		org = org.replace('?','?api=1&amp;player_id=player1&amp;');
		if (org.search('autoplay=1'))
			{
			org = org.replace('autoplay=1','autoplay=0');
			}
		if (org.search('height=199'))
			{
			org = org.replace('height=199','height=197');
			}
		argVideoContainer.innerHTML=org;
	}
}


// Visualizza il video e nasconde la foto
function toggleVideo(argVideoContainer, argFotoContainer)
	{
	
	if (typeof(argVideoContainer)=="string") { argVideoContainer=document.getElementById(argVideoContainer); }
	if (typeof(argFotoContainer)=="string") { argFotoContainer=document.getElementById(argFotoContainer); }

	argImgPlay = document.getElementById("img-play");
	argImgStop = document.getElementById("img-stop");

	if (argVideoContainer.style.zIndex != '999'){

		/*
		org = argVideoContainer.innerHTML;
		argVideoContainer.innerHTML='';
		//org = org.replace('?','?autoplay=1&amp;');
		org = org.replace('autoplay=0','autoplay=1');
		argVideoContainer.innerHTML=org;
		*/

		argFotoContainer.style.display = 'none';
		//argVideoContainer.style.display = 'block'; 
		//argVideoContainer.style.width = "350px";
		//argVideoContainer.style.height = "350px";
		argVideoContainer.style.zIndex = '999'; 

		argImgPlay.style.display = 'none';
		argImgStop.style.display = 'block';
		// Enable the API on each Vimeo video
		jQuery('iframe.vimeo').each(function(){
			Froogaloop(this).addEvent('ready', ready);
			
		});

		function ready(playerID	){
			//alert('ready');
			// Add event listerns
			// http://vimeo.com/api/docs/player-js#events
			// Fa eseguire il video
			Froogaloop(playerID).api('seekTo',0);
			Froogaloop(playerID).api('play');
            Froogaloop(playerID).addEvent('finish', finish);
			return false;
		}

		// Al termine ripristina la foto
		function finish(playerID){
			// alert('finish');
			//toggleFoto(argVideoContainer, argFotoContainer)
			//argVideoContainer.style.display = "none"; 
			//argVideoContainer.style.width = "0px";
			//argVideoContainer.style.height = "0px";
			argVideoContainer.style.zIndex = '-1'; 
			argImgStop.style.display = 'none';
			argImgPlay.style.display = 'block';
			
			Froogaloop(playerID).api('pause');
			Froogaloop(playerID).api('seekTo',0);
			argFotoContainer.style.display = "block";
			return true;
		}
	}
	else
	{
		toggleFoto(argVideoContainer, argFotoContainer);
		/*
		argVideoContainer.style.zIndex = '-1'; 
		argFotoContainer.style.display = "block";
		
		argImgStop.style.display = 'none';
		argImgPlay.style.display = 'block';
		
		// Enable the API on each Vimeo video
		jQuery('iframe.vimeo').each(function(){
			Froogaloop(this).api('pause');
			Froogaloop(this).api('seekTo',0);
		});
		*/
	}
}

// Visualizza le foto e nasconde il video
function toggleFoto(argVideoContainer, argFotoContainer){
	
	if (typeof(argVideoContainer)=="string") { argVideoContainer=document.getElementById(argVideoContainer); }
	if (typeof(argFotoContainer)=="string") { argFotoContainer=document.getElementById(argFotoContainer); }

	if (argFotoContainer.style.display == "none"){

		jQuery('iframe.vimeo').each(function(){
			Froogaloop(this).addEvent('ready',readytostop);
		});

		function readytostop(playerID){
			Froogaloop(playerID).api('pause');
			Froogaloop(playerID).api('seekTo',0);
			argVideoContainer.style.zIndex = '-1'; 
			argFotoContainer.style.display = "block";
		
			argImgStop.style.display = 'none';
			argImgPlay.style.display = 'block';
		};

		//argFotoContainer.style.width = "0px";
		//argFotoContainer.style.height = "0px";
		//argVideoContainer.style.display = "none"; 
		
		/*
		argVideoContainer.style.zIndex = '-1'; 
		argFotoContainer.style.display = "block";
		
		argImgStop.style.display = 'none';
		argImgPlay.style.display = 'block';
		*/
		
	}
}
