
/* *******************************************
*	Copyright © 2002-2009 ExpoCharger International, Inc. All rights reserved
* ****************************************** */


var ZINDEX_HIGHLIGHTER=200;
var ZINDEX_MYEXPO=250;
var ZINDEX_FVTIP=300;
var ZINDEX_CXMENU=400;

// ********** image map simulator/booth position awareness ****************
var _reservations = new Array();
function addHotSpots(dragHandle,addCxMenus){
	var fullViewMap = ECI.getElement("fullViewMap");
	if(!fullViewMap)
		return false;
	var areas = fullViewMap.getElementsByTagName("area");
	for(var i=0;i<areas.length;i++){	
		
		var salesMode=isSalesMode();
		//rem status of 1 means occupied.		
		//if((salesMode || areas[i].getAttribute("status")=="1")){
		
			var boothArea = __makeBoothArea(areas[i]);
			if(boothArea==null)
				continue;
				
			//save to _companyBoothList reservastion data
			var res = __getAreaReservations(boothArea.area);
			if(res && res.length){
				for(var r=0;r<res.length;r++){
					_reservations.push(res[r]);
				}
			}			
			
			var dv = document.createElement("div");
			dv.id="tip"+boothArea.boothNumber;
			dv.style.position = "absolute";
			dv.style.display="block";
			dv.style.zIndex=ZINDEX_FVTIP;
			dv.style.margin=dv.style.padding=dv.style.borderWidth="0px 0px 0px 0px";			
			dv.style.width = boothArea.width + "px";
			dv.style.height = boothArea.height + "px";
			
			if(navigator.userAgent.indexOf("MSIE")>0){
				dv.style.backgroundColor="white";
				dv.style.filter="alpha(opacity=0)"; 
			} else {
				dv.style.backgroundColor="transparent";
			}
			
			dv.style.left = boothArea.left + "px";
			dv.style.top = boothArea.top + "px";
			dragHandle.appendChild(dv);
			
			dv.style.cursor="pointer";
			
			//traditional tooltip:
			dv.onmouseover=boothArea.area.onmouseover;
			dv.onmouseout=boothArea.area.onmouseout;
						
			dv.boothArea = boothArea;
			if(addCxMenus){
				dv.oncontextmenu=function(ev){
					ev = ev||window.event;
					toolTip();
					cxMenu(ev,this,this.boothArea);
					return false;
				}	
			}
			
		//}//if salesMode || status==1
		
	}//for	
	if(addCxMenus){
		//hide menu when click anywhere on the page (except on the menu itself)
		//addEvent(document,"mousedown",function(evt){
		addEvent(document,"click",function(evt){
			evt=evt||window.event;
			var target=evt.target?evt.target:((evt.srcElement)?evt.srcElement:null);
			if( !(target && target.id && target.id.substr(0,4)=="fvCx") ){
				hideCxMenu();
			}
		});
	}
}
// ********** booth highlighting/tagging ****************
var __taggedBooths = {};
var __nextHLIndex = null;
function __newHL(boothNumber,left,top,width,height,isMyExpo){
	try{	
		var dv = document.createElement("div");
		dv.style.position = "absolute";
		dv.style.display="block";
		dv.style.zIndex=isMyExpo==true?ZINDEX_MYEXPO:ZINDEX_HIGHLIGHTER;
		
		//avoid browser border differences by nesting the bordered item inside the positioned item
		var dvInside = document.createElement("div");
		dv.appendChild(dvInside);
		var vdir = ECI.ECgetVdir(false);	
		var imgsrc=vdir + "Data/EC/images/highlighter.png";	
		if(!canPng())
			imgsrc=vdir + "Data/EC/images/highlighter.gif";	
		if(isMyExpo==true){
			imgsrc=vdir + "EC/images/push-pin.gif";	
			dvInside.innerHTML="<img src="+imgsrc+" border='0' align='center' valign='middle'>";
		} else {		
			dvInside.innerHTML="<img src="+imgsrc+" border='0' width='100%' height='100%'>";
		}

		dv.style.margin=dv.style.padding=dv.style.borderWidth="0px 0px 0px 0px";
		dv.style.width = width + "px";
		dv.style.height = height + "px";
		dv.style.backgroundColor="transparent";
		
		var fudge = 2;
		dv.style.left = left+fudge + "px";
		dv.style.top = top+fudge + "px";
		
		//expand it by 25%
		if(isMyExpo != true){
			dv.style.left = parseInt(dv.style.left) - width/4;
			dv.style.width = parseInt(dv.style.width) + width/2;
			dv.style.top = parseInt(dv.style.top) - height/4;
			dv.style.height = parseInt(dv.style.height) + height/2;
		}
		
		if(isMyExpo==true)
			dv.id = "myexpo-"+boothNumber;
		else
			dv.id = "tag-" + boothNumber;
		
		dv.tickle=function(){
				var intId=setInterval(function(){dv.style.backgroundColor=(dv.style.backgroundColor=="transparent")?"orange":"transparent";},80);
				setTimeout(function(){clearInterval(intId);dv.style.backgroundColor="transparent";},400);
			};		
					
		return dv;
	
	}catch(e){
		return null;
	}
}


function canPng(){
	//firefox and ie7 can render png's with transparency
	//ie6 cannot, but ie6 can show transparent gif (.net cannot generate a transparent gif)
	if(navigator.userAgent.indexOf("MSIE 7")>=0)
		return true
	else if(navigator.userAgent.indexOf("Gecko")>=0)
		return true;
	return false;
}
//cycle through highlighter divs -- return next and increment
function cycleHLid(){
	var a = new Array();
	for(var b in __taggedBooths){
		if(ECI.getElement("tag-" + b)){
			a.push(b);
		}
	}
	if(a.length==0)
		return null;
	if(__nextHLIndex==null || __nextHLIndex>a.length-1){
		__nextHLIndex=0;	
	}
	return __taggedBooths[a[__nextHLIndex++]];
}
function tagBooth(dragHandle,boothArea){
	//check if booth is already highlighted
	if(__taggedBooths[boothArea.boothNumber]){
		var hldv=ECI.getElement(__taggedBooths[boothArea.boothNumber]);
		if(hldv) return hldv;
	}				
	var dv = __newHL(boothArea.boothNumber,boothArea.left,boothArea.top,boothArea.width,boothArea.height,false);
	dragHandle.appendChild(dv);
	__taggedBooths[boothArea.boothNumber]=dv.id;//track highlighted booths
			
	return dv;
}
function tagBooths(dragHandle,booths){
	if(!booths.length)
		return;
	for(var i=0;i<booths.length;i++){
		var boothArea = findBoothArea(booths[i]);
		if(boothArea){
			tagBooth(dragHandle,boothArea);
		}
	}
}

function unTagBooth(dragHandle,boothno){
	if(!dragHandle)
		return;
	var dv = ECI.getElement(__taggedBooths[boothno]);
	if(dv){
		dragHandle.removeChild(dv);
		dv=null;
	}	
}
function deleteTag(dragHandle,boothno){
	if(!dragHandle)
		return;
	var dv = ECI.getElement(__taggedBooths[boothno]);
	if(dv){
		dragHandle.removeChild(dv);
		dv=null;
		delete __taggedBooths[boothno];
	}	
}
var _tagsVisible=true;
function toggleTags(dragHandle,toggleButton){
	if(!dragHandle)
		return;
		
	if(_tagsVisible){
		toggleButton.value=locdata?locdata.showTags:"Show Tags";
		hideTaggedBooths(dragHandle);
		_tagsVisible=false;
	} else {
		toggleButton.value=locdata?locdata.hideTags:"Hide Tags";
		showTaggedBooths(dragHandle);
		_tagsVisible=true;
	}
	
}
function hideTaggedBooths(dragHandle){
	if(!dragHandle)
		return;
	//var dragHandle = ECI.getElement("fvDragHandle"); 
	for(b in __taggedBooths){
		var dv = ECI.getElement(__taggedBooths[b]);
		if(dv){
			unTagBooth(dragHandle,b);
		}
	}
}
function showTaggedBooths(dragHandle){
	if(!dragHandle)
		return;
	for(b in __taggedBooths){
		var boothArea = findBoothArea(b);
		tagBooth(dragHandle,boothArea);
	}
}
function getTaggedBoothString(){
	//return __taggedBooths.join(",");
	var s="";
	var s;
	for(var b in __taggedBooths){
		s=s+b+",";
	}
	if(s.substr(s.length-1)==",")
		s=s.substr(0,s.length-1);
	return s;
}
function showMyExpoBooths(dragHandle,booths){	
	if(!booths.length)
		return;
	for(var i=0;i<booths.length;i++){
		var boothArea = findBoothArea(booths[i]);
		if(boothArea){
			var dv = __newHL(boothArea.boothNumber,boothArea.left,boothArea.top,boothArea.width,boothArea.height,true);
			dragHandle.appendChild(dv);
		}
	}		
}
function hideMyExpoBooths(dragHandle,booths){	
	if(!booths.length)
		return;
	for(var i=0;i<booths.length;i++){
		var dv = ECI.getElement("myexpo-" + booths[i]);
		if(dv){
			dragHandle.removeChild(dv);
			dv=null;
		}
	}		
}
// **************imageMap/boothArea parsing ****************
function __getAreaTooltip(area){
	var ttp = area.getAttribute("onmouseover");
	//ie returns attribute as unparsable function, extract it from the html
	var pattern = /toolTip\('.+('[,|\)])/i;
	var matches = ttp.toString().match(pattern);
	if(matches && matches.length>0){
		//we now have: toolTip('...')
		ttp=matches[0].substr(9);
		var lastescape=Math.max(ttp.lastIndexOf("\\'"),ttp.lastIndexOf("\\\""));
		var endoftip=ttp.indexOf("'",lastescape+2);
		ttp = ttp.substring(0,endoftip);
		ttp=ttp.replace(/\\'/,"'");
		ttp=ttp.replace(/\\"/,"\"");
		return ttp;

	}
	return "&nbsp;";
}
function __getAreaReservations(area){
	if(area.getAttribute("status") != "1"){
		return null;
	}
	var reservations = area.getAttribute("reservations");
	if(!reservations)
		return null;
	var titles = [];
	reservations = reservations.split("\t");
	for(var i=0;i<reservations.length;i++){
		titles.push({id:reservations[i].split("|")[0],name:reservations[i].split("|")[1],boothNumber:area.getAttribute("booth")});
	}
	return titles;
}
function getRes4Booth(booth){
	var rsrv=new Array();
	for(var i=0;i<_reservations.length;i++){
		if(_reservations[i].boothNumber==booth)
			rsrv.push(_reservations[i]);
	}
	return rsrv;
}
function findReservations(co){
	if(!co){
		return null;			
	}
	co=co.toLowerCase();
	var rsrvns = new Array();
	for(var i=0;i<_reservations.length;i++){
		if(_reservations[i].name.toLowerCase().indexOf(co)>-1){
			rsrvns.push(_reservations[i]);
		}
	}
	return rsrvns;
}
function __makeBoothArea(area){
		var allCoords = area.getAttribute("coords").split(",");
		if(area.getAttribute("shape").toLowerCase()=="rect"){
			var boothTop = Math.min(parseInt(allCoords[1]),parseInt(allCoords[3]));
			var boothBottom = Math.max(parseInt(allCoords[1]),parseInt(allCoords[3]));
			var boothLeft = Math.min(parseInt(allCoords[0]),parseInt(allCoords[2]));
			var boothRight = Math.max(parseInt(allCoords[0]),parseInt(allCoords[2]));
			var boothWidth = boothRight - boothLeft;
			var boothHeight = boothBottom - boothTop;
							
			//return makeBoothArea(area.getAttribute("booth"),boothLeft,boothTop,boothWidth,boothHeight,area);
			return {boothNumber:area.getAttribute("booth"),left:boothLeft,top:boothTop,width:boothWidth,height:boothHeight,area:area};
	
		} else if(area.getAttribute("shape").toLowerCase()=="poly"){
			var minX=Number.MAX_VALUE,minY=Number.MAX_VALUE,maxX=Number.MIN_VALUE,maxY=Number.MIN_VALUE;
			for(var p=0;p<allCoords.length/2;p++){
				var x=parseInt(allCoords[p*2]);
				var y=parseInt(allCoords[p*2+1]);
				if(x <= minX)
					minX = x;
				if(x >= maxX)
					maxX = x;
				if(y <= minY)
					minY = y;
				if(y >= maxY)
					maxY = y;
			}
			var boothTop = minY;
			var boothLeft = minX
			var boothWidth = maxX - minX;
			var boothHeight = maxY - minY;
			
			//return makeBoothArea(area.getAttribute("booth"),boothLeft,boothTop,boothWidth,boothHeight,area);
			return {boothNumber:area.getAttribute("booth"),left:boothLeft,top:boothTop,width:boothWidth,height:boothHeight,area:area};
		}
		return null;//unknown shape (no need to handle 'circle')				
}
function findBoothArea(boothNumber){
	if(!boothNumber){
		return null;			
	}
	var fullViewMap = ECI.getElement("fullViewMap");
	if(!fullViewMap)
		return false;
	boothNumber = boothNumber.toLowerCase();
	var areas = fullViewMap.getElementsByTagName("area");
	for(var i=0;i<areas.length;i++){
		if(areas[i].getAttribute("booth").toLowerCase()==boothNumber){
			return __makeBoothArea(areas[i]);
		} // end if found booth
	}// end for (areas)
	return null;
}
// ************** context menu/popup menu ****************
function cxMenu(evt,dv,boothArea){
	var fvCxMenu = ECI.getElement("fvCxMenu");
	try{
		var salesMode=isSalesMode();
		var rented=boothArea.area.getAttribute("status")=="1"?true:false;
		fvCxMenu.style.display="block";
		fvCxMenu.style.zIndex=ZINDEX_CXMENU;
		// **heading
		var fvCxCap = ECI.getElement("fvCxCap");
		if(rented){
			//menu caption
			fvCxCap.innerHTML=_boothCaption + "&nbsp;"+boothArea.boothNumber;
			var companies = getRes4Booth(boothArea.boothNumber);
			if(companies){
				for(var i=0;i<companies.length;i++){
					fvCxCap.innerHTML+="<br />&middot;"+companies[i].name;
				}
			}
		} else {
			//not rented
			fvCxCap.innerHTML=_boothCaption + "&nbsp;"+boothArea.boothNumber + boothArea.area.getAttribute("dimensions");
		}
		// **vbooth link
		var hrf=boothArea.area.getAttribute("href");
		if(rented && _isDbLinkEnabled){
			if(hrf){
				var fvCxVbooth = ECI.getElement("fvCxVbooth");
				fvCxVbooth.style.display="inline";
				if(salesMode){
					//sales mode--open in new window
					fvCxVbooth.onclick=function(){
						hideCxMenu();
											
						if(hrf.indexOf("javascript:")==0){
							eval(hrf);
						} else {
							showWindow(boothArea.area.getAttribute("href"),"_blank","630","800");
						}
					}
				} else {
					//not sales mode--open in same window
					fvCxVbooth.onclick=function(){
						document.location.href=boothArea.area.getAttribute("href");
					}
				}
			}//if hrf
		} else {
			ECI.getElement("fvCxVbooth").style.display="none";
		}
		// ** tag/untag
		if(__taggedBooths[boothArea.boothNumber]){
			//if tagged
			var fvCxTag = ECI.getElement("fvCxTag");
			ECI.getElement("fvCxTag").style.display="none";
			var fvCxUntag = ECI.getElement("fvCxUntag");
			ECI.getElement("fvCxUntag").style.display="inline";
			fvCxUntag.onclick=function(){				
				hideCxMenu();
				var hl=ECI.getElement(__taggedBooths[boothArea.boothNumber]);
				if(hl){
					deleteTag(hl.parentNode,boothArea.boothNumber);
				}// end if hl
			}// end li.onclick fn
		} else {
			//not tagged
			var fvCxTag = ECI.getElement("fvCxTag");
			fvCxTag.style.display="inline";
			ECI.getElement("fvCxUntag").style.display="none";
			fvCxTag.onclick=function(){
				hideCxMenu();
				tagBooth(dv.parentNode,boothArea);
			}
		}// end if tagged
		// ** space request (or salesperson hold)
		var fvCxSpace = ECI.getElement("fvCxSpace");
		if(hrf && salesMode && !rented){			
			fvCxSpace.style.display="inline";
			var hrf=boothArea.area.getAttribute("href");
			var salesPersonMode = (hrf.indexOf("salesPlaceOnHold")>-1)?true:false;
			if(salesPersonMode){
				fvCxSpace.innerHTML="Place Hold/Remove Hold";
			}
			fvCxSpace.onclick=function(){
				hideCxMenu();
				if(hrf.indexOf("javascript:")==0){
					eval(hrf);//href was showWindow call
				} else {
					showWindow("indexSales.aspx?content=boothSales&designator=" + boothArea.boothNumber,"vbooth","630","800");
				}
			}
		} else {
			//attendee mode or else already rented
			fvCxSpace.style.display="none";
		}
		
		// **position it
		var newX=parseInt(dv.style.left) + (evt.layerX?evt.layerX:evt.offsetX);
		var newY=parseInt(dv.style.top) + (evt.layerY?evt.layerY:evt.offsetY);
		fvCxMenu.style.left=newX + "px";
		fvCxMenu.style.top=newY + "px";
		//if tip runs off the screen to the right,
		// then flip it to left of mouseposition
		var viewWidth = document.body.clientWidth + document.body.scrollLeft;
		var viewHeight = document.body.clientHeight + document.body.scrollTop;
		if((ECI.getOffsetLeft(fvCxMenu) + fvCxMenu.offsetWidth) > viewWidth){
			fvCxMenu.style.left = newX-fvCxMenu.offsetWidth+"px";
		}
		if((ECI.getOffsetTop(fvCxMenu) + fvCxMenu.offsetHeight) > viewHeight){
			fvCxMenu.style.top = newY-fvCxMenu.offsetHeight+"px";
		}
	} catch(e){
		fvCxMenu.style.display="none";
	}
		
}
function hideCxMenu(){
	ECI.getElement("fvCxMenu").style.display="none";
	ECI.getElement("fvCxVboothRow").className="popmenuItem";
	ECI.getElement("fvCxTagRow").className="popmenuItem";
	ECI.getElement("fvCxUntagRow").className="popmenuItem";
	ECI.getElement("fvCxSpaceRow").className="popmenuItem";
}
// ********************