var CGMPlayer = null;
var CGMLastSelectedHs = null;
var CGMFlashFrequency = 750;
var CGMHotspotStyle = 4; // cgm player porperty
var CGMTimeout = null;
var CGMHintCache;
var CGMOpeningFile = false;
var CGMCurrentFile = null;
var CGMPotentialItem4AnOrder = null;
var CGMInitZoomSize = 50;
// hotspot fill color follows below r,g,b
var CGMR = 255;
var CGMG = 60;
var CGMB = 0;
var CGMLastHS4Moving = null;
function CGMSelectedPart(nMouseBtn,lRefID,strName,strViewPortName,strViewPortFile) { // HsHit
	if(strName && strName != "") {
		if(nMouseBtn == 1 || nMouseBtn == 2) { // Hotspot clicked
			if(nMouseBtn == 2) {
				CGMPotentialItem4AnOrder = strName;
				CGMCreateMenu();					
			}
			CGMDeselectPart(null);
			CGMLastSelectedHs = strName;
			CGMPlayer.Iso3HighlightObject("name("+strName+",all)", CGMHotspotStyle, CGMRGB(CGMR,CGMG,CGMB));
			belenusHome.eventHandler("HotspotsSelected","HotspotID0="+strName+"&Exclusive=true"); // clicked on Hotspot
		} else if(nMouseBtn == 0) { // MouseOver Hotspot
			CGMDisposeMenu();
			CGMPlayer.Iso3SetObjectTip ("name("+strName+")",CGMGetHintText(strName));
		} 
	} else {
		CGMDisposeMenu();
		CGMPotentialItem4AnOrder = null;
	}
}

function CGMSelectPart(params) {
	var paramArr = belenusHome.queryStringToArray(params);
	var HotspotID = paramArr['HotspotID0'];
	try {
		if(CGMPlayer != null) {
			var didSelect = false;
			if(paramArr["Exclusive"] == "true")
				CGMDeselectPart(null);
				CGMPlayer.Iso3HighlightObject("name("+CGMLastSelectedHs+",all)", CGMHotspotStyle, CGMRGB(CGMR,CGMG,CGMB));
			for(var i = 0; i < CGMPlayer.GetObjectCount(); i++)
				if(CGMPlayer.GetObjectName(i) == HotspotID) {
					CGMLastSelectedHs = (CGMLastSelectedHs!=null) ? CGMLastSelectedHs+","+HotspotID : HotspotID;
					didSelect = true;
					CGMPlayer.Iso3HighlightObject("name("+CGMPlayer.GetObjectName(i)+",all)", CGMHotspotStyle, CGMRGB(CGMR,CGMG,CGMB));
					CGMMoveHSInCanvas(HotspotID);
					break;
				}
			CGMPlayer.DisposeObjectList();
			if(!didSelect) lastSelectedHs = null;
		} else {
			alert("CGMPlayer not started!");
		}
		//alert(params+"\n\nCGMLastSelectedHs:"+CGMLastSelectedHs);
	} catch(e) { alert("CGMPlayer error message: "+e.message); }
}

function CGMDeselectPart(HotspotID) {
	try {
		if(HotspotID == null) { // deselect all 
			if(CGMLastSelectedHs == null)return;
			var cgmlshs = CGMLastSelectedHs.split(",");
			var newCgmlshs = new Array();
			for(xy in cgmlshs)
				CGMPlayer.Iso3HighlightObject("name("+cgmlshs[xy]+",all)", 0, CGMRGB(0,0,0));
			CGMLastSelectedHs = null;
		} else { // deselect servel hotspot
			var cgmlshs = CGMLastSelectedHs.split(",");
			var newCgmlshs = new Array();
			for(xy in cgmlshs)
				if(cgmlshs[xy] != HotspotID && cgmlshs[xy] !="")
					newCgmlshs.push(cgmlshs[xy]);
			CGMLastSelectedHs = newCgmlshs.join(",");
			CGMPlayer.Iso3HighlightObject("name("+HotspotID+",all)", 0, CGMRGB(0,0,0));	
		}
	}catch(e) {
		alert("CGMDeselectPart() Error: " +e.message);
	}
}

function CGMInitFinished(CGMPlayerObj) {
	CGMPlayer = CGMPlayerObj;
	CGMPlayer.SetZoomRange(50,175);
	CGMPlayer.ConfigEvents( 1+2+4+32+1024 );
	CGMLastSelectedHs = null;
	CGMClearTimeout();
	CGMBlink();
	CGMHintCache = new Array();
	CGMOpeningFile=true;
}

function CGMRGB( r, g, b ) {
	return ( r + g*256 + b*65536 );
}

function CGMMoveHSInCanvas(hsName) {
  	if(CGMLastHS4Moving == hsName) return;
	if(CGMLastSelectedHs!=null && CGMLastSelectedHs.split(",").length>1) {
		if(CGMPlayer.Viewsize != CGMInitZoomSize)
			CGMPlayer.Viewsize = CGMInitZoomSize;
		CGMPlayer.OffsetX=0;
		CGMPlayer.OffsetY=CGMPlayer.ExtentY;
	} else {
		var shouldMove = CGMPlayer.Viewsize;
		if(shouldMove > 125) {
		  var isZoom = CGMPlayer.Viewsize;
		  CGMPlayer.SetViewPort("$OBJ_"+hsName);
		  CGMPlayer.Viewsize = isZoom;
		}	
	}
	CGMLastHS4Moving = hsName;
}

var CGMCurrentFlashState = 0;

function CGMBlink() { //} function justComment() {
	if(CGMPlayer != null && CGMLastSelectedHs!=null) 
		if(CGMLastSelectedHs.indexOf(",")==-1) {
			CGMPlayer.Iso3HighlightObject("name("+CGMLastSelectedHs+",all)", CGMCurrentFlashState = (CGMCurrentFlashState == CGMHotspotStyle) ? 0 : CGMHotspotStyle, CGMRGB(CGMR,CGMG,CGMB));
		} else if(CGMLastSelectedHs.indexOf(",") >0) {
			CGMPlayer.Iso3HighlightObject("name("+CGMLastSelectedHs.split(",")[0]+",all)", CGMHotspotStyle, CGMRGB(CGMR,CGMG,CGMB));
		}
	CGMTimeout = setTimeout('CGMBlink()',CGMFlashFrequency);
}

function CGMClearTimeout() {
	if(CGMTimeout != null) clearTimeout(CGMTimeout);
}

function CGMGetHintText(strName) {
	try {
		if(typeof CGMHintCache[strName] != "undefined") 
			return CGMHintCache[strName];
		else {
			var itemPath = belenusHome.getProperty("Table", "PartsByField", "Field=posno&Value="+strName);
			if(itemPath==null) {
				CGMHintCache[strName] = strName;
				return strName
			}
			var itemData = belenusHome.getProperty("Table","PartData","ItemPath="+belenusHome.getQueryField(itemPath,"ItemPath0"));
			if(itemData && itemData != "") {
				var CGMParamArray = belenusHome.queryStringToArray(itemData);
				CGMHintCache[strName] = CGMParamArray[belenusHome.partnoField]+"\n"+CGMParamArray[belenusHome.descriptionField];
			} else CGMHintCache[strName] = strName;
			return CGMHintCache[strName];	
		}
	} catch(e) {
		alert("CGMGetHintText Error: "+e.message);
	}
}

function CGMFileHasChanged(fileName) {
	if(CGMCurrentFile != fileName) {
		CGMLastHS4Moving = null;
		CGMInitZoomSize = CGMPlayer.Viewsize;
		CGMLastSelectedHs = null;
		CGMHintCache = new Array();
		CGMCurrentFile = fileName
		fileName = fileName.substring(0,fileName.lastIndexOf("["));
		fileName += ".cgm";
		if(CGMOpeningFile==false)   {
			belenusHome.sendMessage("Table","ChangeImage","Label="+fileName);
		}
		CGMOpeningFile=false;
	}
}

function CGMContextMenuItemHit(item,title) {
	if(item==1) 
		CGMPlayer.GoHome();
	else if(item==2) 
		belenusHome.eventHandler("AddSelectionToBasket", null);	
}

function CGMCreateMenu() {
	CGMPlayer.DisposeContextMenu();
	CGMPlayer.AddContextMenuItem( ""+belenusHome.ld['resetToInitialView'], true );
	CGMPlayer.AddContextMenuItem( ""+belenusHome.ld['orderCurrentPart'], true );
	CGMPlayer.CreateContextMenu();
}

function CGMDisposeMenu() {
	CGMPlayer.DisposeContextMenu();
	CGMPlayer.AddContextMenuItem( ""+belenusHome.ld['resetToInitialView'], true );
	CGMPlayer.CreateContextMenu();
}