/////////////////////////////////////	GLOBAL VARIABLES START	///////////////////////////////////////////////
var imageResourceArrayForImagePlayer= null;
var imageTextArrayForImagePlayer 	= null;
var imagePlayerObject 				= null;

/////////////////////////////////////	GLOBAL VARIABLES END	///////////////////////////////////////////////

/////////////////////////////////////	CUSTOM OBJECTS START	///////////////////////////////////////////////

function ImagePlayerObject(height,width,divIdImageContainer,imageTextId,resourcesArray,textArray,intervalTime){
	var oInstance			= this;
	this.imageElement 		= null;
	this.playerHeight		= 300;
	this.playerWidth		= 300;
	this.imageResourceArray = resourcesArray;
	this.imageTextArray 	= textArray;
	this.counter 			= 0;
	this.interval			= null;
	this.imageTextElement   = document.getElementById(imageTextId); 
	var divElement 			= document.getElementById(divIdImageContainer);
	if(divElement){
		this.imageElement = divElement.getElementsByTagName("img")[0];
	}
	this.playerHeight		= height;
	this.playerWidth		= width;
	
	/*
	alert('imageElement.src:'+imageElement.src);
	alert('imageResourceArray.length:'+resourcesArray.length);
	alert('imageTextArray.length:'+textArray.length);
	*/
	this.play = function(){
		if(this.interval == null){
			this.interval = setInterval(function(){oInstance.changeImage()}, intervalTime);
			 
		}		
	}
	
	this.stop = function(){
		if(this.interval != null)
			clearInterval(oInstance.interval);	
		this.interval = null;
	}
	
	this.first= function(){
		if(this.interval != null)
			clearInterval(oInstance.interval);	
		this.interval = null;	
		this.counter = -1;	
		this.changeImage();
		
	}
	
	this.last= function(){
		if(this.interval != null)
			clearInterval(oInstance.interval);	
		this.interval = null;	
		this.counter = this.imageResourceArray.length - 2 ;	
		this.changeImage();
		
	}
	
	this.changeImage = function (){
		this.counter++;
		//alert('timer');
		if(this.imageElement ){
			if(this.counter < this.imageResourceArray.length){
				this.imageElement.src="/servlets/resources?resourceId="+this.imageResourceArray[this.counter];	
			}else {
				this.counter = 0; 
				this.imageElement.src="/servlets/resources?resourceId="+this.imageResourceArray[this.counter];
			}
			if(this.imageTextArray != null && this.imageTextArray.length ==  this.imageResourceArray.length && this.imageTextElement != null)	
				this.imageTextElement.innerHTML = this.imageTextArray[this.counter];
			try{
				var heightDiff = (this.playerHeight - this.imageElement.height)/2 ;
				this.imageElement.parentNode.style.top = 	heightDiff+"px";
				var widthDiff = (this.playerWidth - this.imageElement.width)/2 ;
				this.imageElement.parentNode.style.left = 	widthDiff+"px";
				this.imageElement.parentNode.parentNode.className= divElement.className;
			}catch(e){}

			

		}	
	}
	
}


function EterbitGoogleMapObject(divIdMapContainer,containerWidth,containerHeight,divDetailID,address,vMarkers,sName,sFeatures,zoom,smallControlEnabled,typeControlEnabled,disableDragging,disableScrollWheelZoom,disableDoubleClickZoom){
	//document.write('<script type="text/javascript\" src="http://maps.google.com/maps?file=api&v=2&key=abcdefg&hl=it"></script>');
	//document.write('<script type="text/javascript\" src="http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/src/markermanager.js"></script>');
	this.sFeatures 		= sFeatures;
	this.sName	 		= sName;
	this.address		= address; 
	this.markers		= null;
	this.markersIterator= 0;
	this.divElement 	= null;
	this.divElementWidth= null;
	this.divElementHeight= null;
	this.map			= null;
	this.dcenter 		= null;
	this.dzoom 			= null;
	this.smallControlEnabled = true;
	this.typeControlEnabled  = true;
	this.disableDragging 	 = true;
	this.disableScrollWheelZoom = true;
	this.disableDoubleClickZoom= true;
	this.detailElement	= null;
	this.mmgr 			= null;
	var geocoder 		= null;
	var gLatLngTmp 		= null;
	////////	START INIT	/////
	
	this.divElement 		= document.getElementById(divIdMapContainer);
	this.divElementWidth	= containerWidth;
	this.divElementHeight	= containerHeight;
	
	geocoder 		= new GClientGeocoder();
	this.markers	= vMarkers;
	//this.dcenter 	= address;
	this.dzoom 		= zoom;
	this.detailElement	= document.getElementById(divDetailID);
	this.smallControlEnabled = smallControlEnabled;
	this.typeControlEnabled  = typeControlEnabled;
	this.disableDragging 	 = disableDragging;
	this.disableScrollWheelZoom = disableScrollWheelZoom;
	this.disableDoubleClickZoom= disableDoubleClickZoom;
	
	document.body.onunload="GUnload();";
	////////	END INIT	/////
	
	
	
	this.loadMap = function (instanceO){
		geocoder.getLatLng(
		    instanceO.address,
		    function(point) {
		      if (!point) {
			alert(instanceO.address + " not found");
		      } else {
			 	//this.dcenter = point;
				//alert(point.lat());
			 	try{
					instanceO.dcenter 	= point;
			 		try{
			 			instanceO.map 		= new GMap2(instanceO.divElement);
			 		}catch(er){}
			 		instanceO.map.setCenter(point);
			 		instanceO.map.setZoom(instanceO.dzoom);
			 		instanceO.detailElement	= document.getElementById(divDetailID);
					if(instanceO.smallControlEnabled)
			 			instanceO.map.addControl(new GSmallMapControl());
					if(instanceO.typeControlEnabled)
						instanceO.map.addControl(new GMapTypeControl());
					if(instanceO.disableDragging)
						instanceO.map.disableDragging();
					if(instanceO.disableScrollWheelZoom)
						instanceO.map.disableScrollWheelZoom();
					if(instanceO.disableDoubleClickZoom)
						instanceO.map.disableDoubleClickZoom();
					instanceO.mmgr      = new MarkerManager(instanceO.map);
					//GEvent.addListener(instanceO.map, 'tilesloaded', function() {instanceO.loadMarkers(instanceO);});
					window.setTimeout(function(){instanceO.loadMarkers(instanceO);},1000);  
			 	}catch(e){}
				
		      }
		    }
		  );
		instanceO.divElement.style.width	= instanceO.divElementWidth;
		instanceO.divElement.style.height	= instanceO.divElementHeight;
	}
	
	this.loadMarkers = function (instanceO){
			//alert("ciao");
			if(instanceO.markersIterator < instanceO.markers.length){
				var markerInfo = instanceO.markers[instanceO.markersIterator];
				if(markerInfo){
					var text 		= markerInfo.TEXT;
					var url  		= markerInfo.URL;
					var address  	= markerInfo.ADDRESS;
						geocoder.getLatLng(
						    address,
						    function(point) {
						      if (!point) {
							alert(address + " not found");
						      } else {
									var marker = new GMarker(point);
									instanceO.mmgr.addMarker(marker,0,17);
									if(url!=null && url != ""){
										GEvent.addListener(marker, 'click', function() {
										window.open(url,instanceO.sName,instanceO.sFeatures);
											}
											);
									}		
									if(text!=null && text != ""){
										GEvent.addListener(marker, 'mouseover', function() {
										instanceO.detailElement.innerHTML = text;
											}
											);
										
										GEvent.addListener(marker, 'mouseout', function() {
										instanceO.detailElement.innerHTML = '<em>'+""+'</em>';
											}
											);	
									}	 
									instanceO.markersIterator ++;	
									instanceO.loadMarkers(instanceO);
						      }
						    }
						);	    			
					
				}
			}
		
	}
	

	
	
}


/////////////////////////////////////	CUSTOM OBJECTS END	///////////////////////////////////////////////

function setWMode(value){
	var elements = document.getElementsByName('wmode');
	for (i=0;i<elements.length;i++){
		elements[i].value=value;
	}
}

function fnAppendDiv(id,classe){
	var oNewNode = document.createElement("DIV");
	oNewNode.id=id;
	oNewNode.className=classe;
	document.body.appendChild(oNewNode);
}


function loadYUILibraries(){
	if(!isYUILoaded){
		document.write('<link rel="stylesheet" type="text/css" href="wmanager/yui/build/fonts/fonts-min.css" />');
		document.write('<link rel="stylesheet" type="text/css" href="wmanager/yui/build/container/assets/skins/sam/container.css" />');
		document.write('<script type="text/javascript" src="wmanager/yui/build/container/container-min.js" ></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/animation/animation-min.js"></script>');
		document.write('<script type="text/javascript" src="wmanager/yui/build/yahoo/yahoo-min.js" ></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/yahoo-dom-event/yahoo-dom-event.js"></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/event/event-min.js"></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/dragdrop/dragdrop-min.js"></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/utilities/utilities.js"></script>');
		document.write('<script type="text/javascript\" src="manager/yui/build/container/container_core-min.js"></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/element/element-beta-min.js"></script>');
		document.write('<script type="text/javascript\" src="wmanager/yui/build/connection/connection-min.js"></script>');
	}
	isYUILoaded = true;
 }
 
 
 function getWindowSize() {
  var myWidth = 0, myHeight = 0, ar = new Array();

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  ar[0]=myWidth;
  ar[1]=myHeight;
  return ar;
}

function printIframeContent(divId)  {  
// var url = document.getElementById(divId).src;
 var a = window.open('','','width=500,height=400');  
 a.document.open("text/html");  
 a.document.write(document.getElementById(divId).contentWindow.document.body.innerHTML);  
 a.document.close();  
 a.print();  
}  
function copyIframeContent(divId){
	try{
	document.getElementById(divId).contentWindow.document.execCommand('SelectAll',false);
	document.getElementById(divId).contentWindow.document.execCommand("Copy", true);
	document.getElementById(divId).contentWindow.document.execCommand("UnSelect", true); 
	}catch(e){}
}


function galleryImagesGoUp(previuosButtonId,nextButtonId,divName,rowsMaxNumber){
	try{
		var divList = document.getElementsByName(divName);
		if((parseInt(divList.length) > parseInt(rowsMaxNumber)) && (divList[divList.length-1].style.display == 'none' )){
			var lastDisplayed = divList.length;
			for (i=divList.length-1;i>=0;i--){
				var element = divList[i];
				if(element.style.display == 'block' || element.style.display == ''){
					lastDisplayed = i;	
					break;
				}
			}
			var firstNotDisplayed = 0;
			for (i=0;i<divList.length;i++){
				var element = divList[i];
				if(element.style.display == 'none' && i > lastDisplayed){
					firstNotDisplayed = i;	
					break;
				}
				
			}
			for (i=0;i<divList.length;i++){
				var element = divList[i];
				element.style.display = 'none';
			}
			var temp = firstNotDisplayed+rowsMaxNumber;
			if(temp > parseInt(divList.length))
				temp = parseInt(divList.length);
			for (i=firstNotDisplayed;i<temp;i++){
				var element = divList[i];
				element.style.display = 'block';
			}
		}
		checkDisplayPreviousNextButton(previuosButtonId,nextButtonId,divName);
	}catch(e){}
}

function galleryImagesGoDown(previuosButtonId,nextButtonId,divName,rowsMaxNumber){
	try{
		var divList = document.getElementsByName(divName);
		if((parseInt(divList.length) > parseInt(rowsMaxNumber)) && (divList[0].style.display == 'none' )){
			var firstNotDisplayed = 0;
			for (i=0;i<divList.length;i++){
				var element = divList[i];
				if(element.style.display != 'none' ){
					firstNotDisplayed = i;	
					break;
				}
				
			}
			for (i=0;i<divList.length;i++){
				var element = divList[i];
				element.style.display = 'none';
			}
			var temp = firstNotDisplayed-rowsMaxNumber;
			for (i=temp;i<firstNotDisplayed;i++){
				var element = divList[i];
				element.style.display = 'block';
			}
		}
		checkDisplayPreviousNextButton(previuosButtonId,nextButtonId,divName);
	}catch(e){}
}

function checkDisplayPreviousNextButton ( previuosButtonId,nextButtonId,divName) {
	try{
		var imgPreviousId 	= document.getElementById(previuosButtonId);
		var imgnextId 		= document.getElementById(nextButtonId);
		var divList 		= document.getElementsByName(divName);
		imgPreviousId.parentNode.style.display='none';
		imgnextId.parentNode.style.display='none';
		if(divList!=null && divList[0]!=null && divList[0].style.display=='none' ){
			imgPreviousId.parentNode.style.display='block';	
		}
		if(divList!=null && divList[divList.length-1] !=null && divList[divList.length-1].style.display=='none' ){
			imgnextId.parentNode.style.display='block';	
		}
	}catch(e){}	
}


function getElementLeft(elm) {
    var x = 0;
    //set x to elms offsetLeft
    x = elm.offsetLeft;

    //set elm to its offsetParent
    elm = elm.offsetParent;

    //use while loop to check if elm is null
    // if not then add current elms offsetLeft to x
    //offsetTop to y and set elm to its offsetParent

    while(elm != null){
        x = parseInt(x) + parseInt(elm.offsetLeft);
        elm = elm.offsetParent;
    }
    return x;
}

function getElementTop(elm) {
    var y = 0;
    //set x to elms offsetLeft
    y = elm.offsetTop;

    //set elm to its offsetParent
    elm = elm.offsetParent;

    //use while loop to check if elm is null
    // if not then add current elms offsetLeft to x
    //offsetTop to y and set elm to its offsetParent

    while(elm != null)
    {
        y = parseInt(y) + parseInt(elm.offsetTop);
        elm = elm.offsetParent;
    }

    return y;
}

function OpenLargeImageOnMouseOver(obj,bigImagePath,resizeEnabled,height,width){
try{	
        
    var smallImageWidth  = 40;
	var smallImageHeight = 40;
	var smallImageLeft   = 0;
	var smallImageTop    = 0;	
	try{
		var objImgElement = obj.getElementsByTagName("IMG")[0];	
		if(objImgElement!= null){
			//obj = objImgElement	;
			smallImageWidth 	= objImgElement.width;
			smallImageHeight 	= objImgElement.height;
		}else{
			objImgElement = obj;
			smallImageWidth = obj.getElementsByTagName('IMG')[0].width;
			smallImageHeight = obj.getElementsByTagName('IMG')[0].height;
		}
		smallImageLeft = getElementLeft(objImgElement);
		smallImageTop  = getElementTop(objImgElement);
	    }catch(er){}		
	var left	= smallImageLeft+smallImageWidth;
	var top		= smallImageTop+smallImageHeight;    
	if(height == "" || height == "null")
    		height = 250;
    if(width == "" || width == "null")
    	width = 250;
    var imgbox=document.getElementById("imgbox");
	if(!imgbox){
		fnAppend(document.body,"IMG","imgbox","");
		imgbox=document.getElementById("imgbox");
	 }
	var image = new Image();
	image.src = bigImagePath;
	image.onload = function(){
		imgbox.style.visibility='visible';
		imgbox.src=bigImagePath;	
		if(!resizeEnabled){
			width = image.width;
			height = image.height; 	
		}	
		imgbox.style.width=width+'px';
		imgbox.style.height=height+'px';
		if((top + height) > document.documentElement.clientHeight)
			top	= smallImageTop - height;
		if((left + width) > document.documentElement.clientWidth)
			left	= smallImageLeft - width;	
		imgbox.style.left=left +'px';
		imgbox.style.top=top + 'px';	
	};
	imgbox.style.visibility='visible';
	imgbox.src=bigImagePath;	
	if(!resizeEnabled){
		width = image.width;
		height = image.height; 	
	}	
	imgbox.style.width=width+'px';
	imgbox.style.height=height+'px';
	if((top + height) > document.documentElement.clientHeight)
		top	= smallImageTop - height;
	if((left + width) > document.documentElement.clientWidth)
		left	= smallImageLeft - width;	
	imgbox.style.left=left +'px';
	imgbox.style.top=top + 'px';
			 
	if(imgbox.addEventListener){
		obj.addEventListener('mouseout',Out,false);
	 } else {
		obj.attachEvent('onmouseout',Out);
	 }
	
	}catch(e){}
}

function Out(){
    document.getElementById("imgbox").style.visibility='hidden';
}

function fnAppendDiv(id,classe){
	var oNewNode = document.createElement("DIV");
	oNewNode.id=id;
	oNewNode.className=classe;
	document.body.appendChild(oNewNode);
}

function fnAppend(NodeToAppendTo,nodeType,id,name){
	var oNewNode = document.createElement(nodeType);
	oNewNode.name=name;
	oNewNode.id=id; 
	NodeToAppendTo.appendChild(oNewNode);
}   

function changeTextOnMouseOver(obj,elementName,text){

	try{	
	    var element =document.getElementById(elementName);
	    element.value = text;
	}catch(e){}

} 

function outImage(elementName){
	
	try{	
	    var element =document.getElementById(elementName);
	    element.value = "";	    
	}catch(e){}
}

function isNumeric(evt){
	evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
		return false;
	}
    return true;
}
 
  