var mouseEnterHandler =
{
	evtHash: [],

	ieGetUniqueID: function(_elem)
	{
		if (_elem === window) { return 'theWindow'; }
		else if (_elem === document) { return 'theDocument'; }
		else { return _elem.uniqueID; }
	},

	addEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.addEventListener != 'undefined')
		{
			if (_evtName == 'mouseenter')
				{ _elem.addEventListener('mouseover', mouseEnterHandler.mouseEnter(_fn), _useCapture); }
			else if (_evtName == 'mouseleave')
				{ _elem.addEventListener('mouseout', mouseEnterHandler.mouseEnter(_fn), _useCapture); } 
			else
				{ _elem.addEventListener(_evtName, _fn, _useCapture); }
		}
		else if (typeof _elem.attachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + mouseEnterHandler.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';
			var f = mouseEnterHandler.evtHash[key];
			if (typeof f != 'undefined')
				{ return; }
			
			f = function()
			{
				_fn.call(_elem);
			};
		
			mouseEnterHandler.evtHash[key] = f;
			_elem.attachEvent('on' + _evtName, f);

			window.attachEvent('onunload', function()
			{
				_elem.detachEvent('on' + _evtName, f);
			});
			key = null;
		}
		else
			{ _elem['on' + _evtName] = _fn; }
	},	

	removeEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.removeEventListener != 'undefined')
			{ _elem.removeEventListener(_evtName, _fn, _useCapture); }
		else if (typeof _elem.detachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + mouseEnterHandler.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';
			var f = mouseEnterHandler.evtHash[key];
			if (typeof f != 'undefined')
			{
				_elem.detachEvent('on' + _evtName, f);
				delete mouseEnterHandler.evtHash[key];
			}
		
			key = null;
		}
	},
	
	mouseEnter: function(_pFn)
	{
		return function(_evt)
		{
			var relTarget = _evt.relatedTarget;				
			if (this == relTarget || mouseEnterHandler.isAChildOf(this, relTarget))
				{ return; }

			_pFn.call(this, _evt);
		}
	},
	
	isAChildOf: function(_parent, _child)
	{
		if (_parent == _child) { return false };
		
		while (_child && _child != _parent)
			{ _child = _child.parentNode; }
		
		return _child == _parent;
	}	
};


function showCase()
{	
	var sUrl = '';
	var aXMLElements;
	var aElements  = new Array();
	var aLiNodes;
	var nLastViewed = "";
	var bIsLocked = false;
	var autoMoveInterval;
	var bToStop = false;
	var bFreeze = false;
	
	this.initShowCase = function()
	{
		var browser = navigator.userAgent.toLowerCase();
		var ie6 = (browser.indexOf("ie 6.0") > 0);
		if(ie6)
		{
			document.getElementById("carouselPrevious_button").style.marginLeft = "10px";
			document.getElementById("carouselPrevious_button").style.marginRight = "12px";
			document.getElementById("carouselNext_button").style.marginLeft = "12px";
			document.getElementById("carouselNext_button").style.marginRight = "0px";
		}
		var oXMLReq = vslGetXMLReq();
		if (!oXMLReq) 
			return(false);

		var sUrl = this.sUrl;
		oXMLReq.open('GET', sUrl, true);
		var _this = this;
		oXMLReq.onreadystatechange = function ()
		{
			if (oXMLReq.readyState != 4)
				return(false);
			try
			{
				if (oXMLReq.status == 404)
				{
					_this.setNPage(_this.getNPage()-1);
					return true;
				}
					
				if (oXMLReq.status != 200)
					return(false);

				checkXMLValue(oXMLReq.responseXML, _this)
			}
			catch (e)
			{	return(false);	}
			
			return(true);
		}
		oXMLReq.send(null);
	}
	
	this.lock = function()
	{
		var objC = this;
		this.bIsLocked = true;
		disableButtons(objC);
		clearInterval(this.autoMoveInterval);
	}
	
	this.unlock = function()
	{
		var objC = this;
		this.bIsLocked = false;
		enableButtons(objC);
		this.autoMoveInterval = setInterval(function()
		{
			if(!this.bIsLocked)
			{
				move(true, objC); 
			}
		}, 4000);
	}
}


function checkXMLValue(oXmlItems, inShowCase)
{
	var sResult = "";
	var aoItems = "";
	var oEl = document.getElementById("carouselUl");

	if (!oXmlItems.getElementsByTagName("item"))
		return(false);
	else
		var aoItems = oXmlItems.getElementsByTagName("item");
	
	if(!aoItems.length || aoItems.length <= 0)
		return(false);
	
	if(aoItems.length > 9)
	{
		if (oEl.getElementsByTagName("li")[0] && oEl.getElementsByTagName("li")[0].id.match("carouselFalse_element") != null) 
			oEl.removeChild(oEl.getElementsByTagName("li")[0]); 
		inShowCase.aElements = aoItems;
		return makeULFromXML(aoItems, inShowCase);
	}
	else
	{
		document.getElementById('homePageWindow').style.display = 'none';
		document.getElementById('homePageWindow').style.visibility = 'hidden';
	}
}

function makeULFromXML(aoItems, inShowCase)
{
	var oEl = document.getElementById("carouselUl");
	oEl.removeChild(oEl.firstChild);

	inShowCase.aLiNodes = new Array();
	
	for(var i = 0; i < aoItems.length; i++)
	{
		var liTmp = createLiElement(aoItems[i],i);
		inShowCase.aLiNodes.push(liTmp)
	}
	
	for(var j = 0; j < 7; j++)
	{
		oEl.appendChild(inShowCase.aLiNodes[j]);
	}
	
	inShowCase.nLastViewed = j-1;
	enableButtons(inShowCase);
	
	
	var container = document.getElementById("carouselContainer");

	mouseEnterHandler.addEvent(container, 'mouseenter', function() {
		inShowCase.bFreeze = true;
	}, false);

	mouseEnterHandler.addEvent(container, 'mouseleave', function() {
		setTimeout(function() {	inShowCase.bFreeze = false; }, 1000);
	}, true);
	
	
	inShowCase.autoMoveInterval = setInterval(function()
	{
		if(!inShowCase.bIsLocked)
		{
			inShowCase.bToStop = true;
			move(true, inShowCase);
		}
	}, 4000);
}


function enableButtons(inShowCase)
{
	var nextB = document.getElementById("carouselNext_button");
	var prevB = document.getElementById("carouselPrevious_button");

	nextB.onmousedown = function(){
		inShowCase.bToStop = false;
		nextB.className = nextB.className +' carouselNext_button_over';
		move(true,inShowCase);
	}
	
	nextB.onmouseup = function() {
		inShowCase.bToStop = true;
		nextB.className = nextB.className.replace('carouselNext_button_over', '');
	}
	
	nextB.onmouseout = function() {
		inShowCase.bToStop = true;
	}
	
	prevB.onmousedown = function() {
		inShowCase.bToStop = false;
		prevB.className = prevB.className +' carouselPrevious_button_over';
		move(false,inShowCase);
	}
	prevB.onmouseup = function() {
		inShowCase.bToStop = true
		prevB.className = prevB.className.replace('carouselPrevious_button_over', '');
	}
	
	prevB.onmouseout = function() {
		inShowCase.bToStop = true;
	}
}


function disableButtons(inShowCase)
{
	var nextB = document.getElementById("carouselNext_button");
	var prevB = document.getElementById("carouselPrevious_button");
	
	nextB.onmousedown = function() { return false; };
	nextB.onmouseout = function() { return false; };
	
	prevB.onmousedown = function() { return false; };
	prevB.onmouseout = function() { return false; };
}



function move(bLeft, inShowCase)
{
	setTimeout(function() {
	if(!inShowCase.bIsLocked)
		{
			animatedMoving(bLeft, inShowCase)
		}
	}, 15);
}

function createLiElement(item,nId)
{
	var sCarousel = "CarouselUl";
	var sUrl = readXmlItemByName(item, "url");
	var sAltUrl = readXmlItemByName(item, "urlAlt");
	var sImgSrc = readXmlItemByName(item, "img");
	var sTxt1 = readXmlItemByName(item, "txt_1");
	var sTxt2 = readXmlItemByName(item, "txt_2");
	var sComune = readXmlItemByName(item, "comune");
	var sNomeProgetto = readXmlItemByName(item, "nomeProgetto");
	var oLi = document.createElement("LI");
	var oDivImg = document.createElement("DIV");
	var oBDiv = document.createElement("DIV");
	var oA = document.createElement("A");
	var oA2 = document.createElement("A");
	var oAcomune =document.createElement("SPAN");
	var oAnomeProgetto =document.createElement("SPAN");
	var oImg = document.createElement("IMG");
	var oDiv = document.createElement("DIV");
	var oBold = document.createElement("b");
	var oTxt1 = document.createTextNode(sTxt1);
	var oBr = document.createElement("br");
	var oTxt2 = document.createTextNode(sTxt2);
	oLi.setAttribute("id", sCarousel + "_li_" + nId);
	oLi.setAttribute(document.all ? 'className' : 'class',"carouselLi");
	oDivImg.setAttribute("id", sCarousel + "_divImg_" + nId);
	oDivImg.setAttribute(document.all ? 'className' : 'class', "carouselDivImg");
	oDivImg.style.background="#EEE url(/vimages/default/image-loader.gif) no-repeat scroll center center";
	oA.setAttribute("id",sCarousel + "_a_" + nId);
	oA.setAttribute(document.all ? 'className' : 'class',"carouselA hoverNonUnderlined");
	oA.setAttribute("href",sUrl);
	oA2.setAttribute("href",sUrl);
	oAcomune.setAttribute("title",sComune);
	oAcomune.appendChild(oTxt2);
	oAnomeProgetto.setAttribute("title",sNomeProgetto);
	oAnomeProgetto.appendChild(oTxt1);
	if (sAltUrl != "")
		oA.setAttribute("target", "_blank");
	var sIdImg = sCarousel + "_img_" + nId;
	oImg.setAttribute("id", sIdImg);
	oImg.setAttribute(document.all ? 'className' : 'class', "carouselImg");
	var sLoadString = "centerImage(this, '100','75'); changeBackgroundImage('id', '"+sCarousel+"_divImg_"+nId+"')";
	oImg.setAttribute("onload",sLoadString);
	oImg.setAttribute("src", sImgSrc);
	oA2.appendChild(oImg);
	oDiv.setAttribute("id", sCarousel + "_divTxt_" + nId);
	oDiv.setAttribute(document.all ? 'className' : 'class', "carouselDivTxt");
	oBold.appendChild(oAnomeProgetto);
	oDiv.appendChild(oBold);
	oDiv.appendChild(oBr);
	oDiv.appendChild(oAcomune);
	oDivImg.appendChild(oA2);
	oBDiv.appendChild(oDivImg);
	oA.appendChild(oDivImg);
	oA.appendChild(oDiv);
	oLi.appendChild(oA); 
	return oLi;
}

function readXmlItemByName(oItem, sNodeName)
{
	if (oItem.getElementsByTagName(sNodeName).item(0) != null)
		return oItem.getElementsByTagName(sNodeName).item(0).firstChild.nodeValue;
	else
		return "";
}

function centerImage(img, width, height)
{
	img.style.position = "relative";
	img.style.left = "0px";
	img.style.top = "0px";
	var browser = navigator.userAgent.toLowerCase();
	if (browser.indexOf("msie")!=-1 && browser.indexOf("msie 7.0")==-1){
		var leftWin = 2;
		var topWin = 1;
	}
	else
	{
		var leftWin = 0;
		var topWin = 0;
	}

	var moveleft = Math.round((width - img.width) / 2) + leftWin;
	if (moveleft>0)
		img.style.left= moveleft + "px";

	var movetop = Math.round((height - img.height) / 2) + topWin;
	if (movetop>0)
		img.style.top= movetop + "px";
}


function changeBackgroundImage(div)
{
	if(document.getElementById(div))
	{
		odiv = document.getElementById(div);
		odiv.style.background= "#FFF url(/vimages/default/blank.gif) no-repeat scroll center center";
	}
}

function appendLiToUl(bInTail, inShowCase)
{
	var oEl = document.getElementById("carouselUl");
	if(bInTail)
	{	
		var nToTake = inShowCase.nLastViewed + 1;
		var nToAdd = (nToTake < inShowCase.aLiNodes.length) ? nToTake : (nToTake - inShowCase.aLiNodes.length);
		oEl.appendChild(inShowCase.aLiNodes[nToAdd], 2);
		oEl.style.left = "0px";
		inShowCase.nLastViewed = nToAdd;
	}

	else
	{	var nToTake = parseInt(inShowCase.nLastViewed - 7);
		var nToAdd = (nToTake > -1) ? nToTake : (inShowCase.aLiNodes.length + parseInt(nToTake));
		var torna = oEl.insertBefore(inShowCase.aLiNodes[nToAdd], oEl.childNodes[0]);
		oEl.style.left = "-125px";
	}
}

function animatedMoving(bInTail, inShowCase)
{
	if(!inShowCase.bIsLocked)
	{
		var oEl = document.getElementById("carouselUl");
		var nStart; 
		var nStop;
		var nIncrement = 5;
		var nToAdd;
		var nActual;
		inShowCase.lock();
		appendLiToUl(bInTail, inShowCase);
		
		if(bInTail)
		{
			nStart = 0;
			nStop = -125;
			nToAdd = -1;
		}
		else
		{
			nStart = -125;
			nStop = 0; 
			nToAdd = 1;
		}

		nActual = nStart;
		var bCond;

		var x  = setInterval(function()
		{
			if(!inShowCase.bFreeze)
			{
				nActual += (nIncrement*nToAdd);
				if(bInTail) 
				{
					bCond = nActual > -126;
				}
				else
				{
					bCond = nActual < 0;
				}
				
				if(bCond)
				{
					oEl.style.left = nActual+ "px";
				}
				else
				{
					nActual = nStart;
					if(bInTail)
					{
						oEl.removeChild(oEl.childNodes[0]);
					}
					else
					{
						inShowCase.nLastViewed = ((inShowCase.nLastViewed-1) > 0) ? (inShowCase.nLastViewed-1) : (inShowCase.aLiNodes.length+(inShowCase.nLastViewed -1)); 
						oEl.removeChild(oEl.lastChild);
					}
					oEl.style.left = "0px";

					if(!inShowCase.bToStop)
					{
						nActual = nStart;
						appendLiToUl(bInTail, inShowCase);
					}
					else
					{
						clearInterval(x);
						inShowCase.unlock();
					}
				}
			}
		}, 25);
	}
}

