// JavaScript Document
//---------------------------------------------------------------Global variables
var isCss, isW3c, isIe4, isNn4, isIe6css;
var stopPoint =  0;
var intervalID = 0;
var corFact = {top:-1, left:-1};
var alerts = "";
//---------------------------------------------------------------Object detection


	//alert("initing");
	if (document.images) {
		isCss = (document.body && document.body.style)? true : false;
		isW3c = (document.getElementById)? true : false;
		isIe4 = (document.all)? true : false;
		isNn4 = (document.layers)? true : false;
		isIe6css = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0)? true : false;
		}


var isNav, isIE
var coll = ""
var styleObj = ""
	
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true
	} else {
		isIE = true
		coll = "all."
		styleObj = ".style"
	}
}

	//alert ("CSS = "+isCss+"\nW3C = "+isW3c+"\nIE4 = "+isIe4+"\nNN4 = "+isNn4+"\nIE6CSS = "+isIe6css+"\nNav = "+isNav+"\nIE = "+isIE);
	
//---------------------------------------------------------------Event Handler to intiDhtmApi

//window.onload = initDhtmApi;
window.onresize = handleResize;

//---------------------------------------------------------------NN4 layer detection

function seekLayer (doc, name) {
	var theObj;
	for (var i = 0; i < doc.layers.length; i++){
		if (doc.layers[i].name == name) {
			theObj = doc.layers[i];
			break;
		}
//------Dive into layers if need be
		if (doc.layers[i].document.layers.length > 0) {
			theObj = seekLayer(document.layers[i].document, name);
		}
	}
	return theObj;
}

//---------------------------------------------------------------Convert obj name string/reference into valid elem obj reference

function getRawObj(obj) {
	var theObj;
	if (typeof obj == "string"){
		if (isW3c) {
			if(alerts=="on"){alert("isW3c");}
			theObj = document.getElementById(obj);
		} else if (isIe4) {
			if(alerts=="on"){alert("isIe4");}
			theObj = document.all(obj);
		} else if (isNn4) {
			if(alerts=="on"){alert("isNN4");}
			theObj = seekLayer(document, obj);
		} 
		
	} else {
		//alert("Last Else of GetRawObj");
		theObj = obj;
	}
		//alert (theObj)
	return theObj;
}

//---------------------------------------------------------------Convert obj name string/reference into valid style/Nn4 layer reference

function getObjStyle(obj) {
	var theObj = getRawObj(obj);
	if (theObj) {
		theObj = theObj.style;
	}
	return theObj;
}

//---------------------------------------------------------------Position object at specific x/y coordinate

function shiftTo(obj, x, y) {
	theObj=getObjStyle(obj);
	if (theObj) {
		if (isNn4) {
			alert ("Nn4");
			theObj.moveTo(x,y);
		} else {
			var units = (typeof theObj.left == "string")? "px" : 0;
			theObj.left = x + units;
			theObj.top = y + units;
		}
	}
}

//-----------------------------------------------------------------Move object x/y pixels

function shiftBy(obj, deltaX, deltaY){
	if (alerts=="on"){alert(deltaX + " - " + deltaY);}
	var theObj = getObjStyle(obj);
	if (theObj) {
		if (isCss) {
			var units = (typeof theObj.left == "string")? "px" : 0;
			theObj.left = getObjX(obj) + deltaX + units;
			theObj.top = getObjY(obj) + deltaY + units;
		} else if (isNn4) {
			theObj.moveby(deltaX, deltaY);
		}
	}
}

//-----------------------------------------------------------------set zIndex

function setZindex(obj, zOrder) {
	var theObj = getObjStyle(obj);
	if (theObj) {
		theObj.zIndex = zOrder;
	}
}

//-----------------------------------------------------------------set Background colour of object

function setBgCol(obj, col) {
	var theObj = getObjStyle(obj);
	if (theObj) {
		if (isNn4) {
			theObj.bgColor = col
		} else if (isCss) {
			theObj.backgroundColor = color;
		}
	}
}

//-----------------------------------------------------------------Hide Object

function hide(obj) {
	var theObj = getObjStyle(obj);
	if (theObj) {
		theObj.visibility = "hidden";
	}
}

//-----------------------------------------------------------------Show Object

function show(obj) {
	var theObj = getObjStyle(obj);
	if (theObj) {
		theObj.visibility = "visible";
	}
}

//----------------------------------------------------------------Get x coordinate of object

function getObjX(obj) {
	var elem = getRawObj(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		var result = cssDecl.getPropertyValue("left");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.left;
	} else if (elem.style) {
		result = elem.style.left;
	} else if (isNn4) {
		result = elem.left;
	}
	return parseInt(result);
}

//----------------------------------------------------------------Get y coordinate of object

function getObjY(obj) {
	var elem = getRawObj(obj);
	var result = 0;
	if (document.defaultView) {
		var style = document.defaultView;
		var cssDecl = style.getComputedStyle(elem, "");
		var result = cssDecl.getPropertyValue("top");
	} else if (elem.currentStyle) {
		result = elem.currentStyle.top;
	} else if (elem.style) {
		result = elem.style.top;
	} else if (isNn4) {
		result = elem.top;
	}
	return parseInt(result);
}

//-----------------------------------------------------------------Get width of Object

function getObjWidth(obj) {
	var elem = getRawObj(obj);
	var result = 0;
	if (elem.offsetWidth) {
		if (elem.scrollWidth && (elem.offsetWidth != elem.scrollWidth)) {
			result = elem.scrollWidth;
		} else {
			result = elem.offsetWidth;
		}
	} else if (elem.clip && elem.clip.width) {
		result = elem.clip.width;
	} else if (elem.style && elem.style.pixelWidth){
		result = elem.style.pixelWidth;
	}
	return parseInt(result);
}

//-----------------------------------------------------------------Get height of object

function getObjHeight(obj) {
	var elem = getRawObj(obj);
	var result = 0;
	if (elem.offsetHeight) {
		result = elem.offsetHeight;
	} else if (elem.clip && elem.clip.height) {
		result = elem.clip.height;
	} else if (elem.style && elem.style.pixelHeight){
		result = elem.style.pixelHeight;
	}
	return parseInt(result);
}

//------------------------------------------------------------------Available space in browser (Width)

function getScreenWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (isIe6css) {
		return document.body.parentElement.clientWidth;
	}else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth
	}
	return 0;
}

//------------------------------------------------------------------Available space in browser (Height)

function getScreenHeight() {
	
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (isIe6css) {
		return document.body.parentElement.clientHeight;
	}else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
}

//------------------------------------------------------------------Center Object

function centerObj(objName) {
		//alert("got object " + objName)
		 var obj = getRawObj(objName);
		 if (corFact.top == -1){
		 	if ((typeof obj.offsetTop == "number") && obj.offsetTop > 0) {
			   corFact.top = obj.offsetTop;
			   corFact.left = obj.offsetLeft;
			 } else {
			   corFact.top = 0;
			   corFact.left = 0;
			 }
			if (obj.offsetWidth && obj.scrollWidth) {
			   if (obj.offsetWidth != obj.scrollWidth) {
			   	  obj.style.width = obj.scrollWidth;
			   }
			}
		 }
		 //alert (getObjWidth(obj) + "   " + getObjHeight(obj));
		 //alert (getScreenWidth() + "   " + getScreenHeight());
		 var x = Math.round((getScreenWidth()/2) - (getObjWidth(obj)/2));
		 var y = Math.round((getScreenHeight()/2) - (getObjHeight(obj)/2));
		 shiftTo(obj, x - corFact.left, y - corFact.top);
		 show(obj);
}
//------------------------------------------------------------------Flying Objects
function startGlide(objN) {
   		
		var imgs = new Array();
		var imgs = objN.split(",");
		var objName = imgs[0]
		var Tangle = imgs[1]
		var stopP = imgs[2]
		var SX = imgs[3]
		var SY = imgs[4]
		var edge = imgs[5]
		var imgNo = imgs[6]
		if(alerts=="on"){alert(objName + " " + Tangle + " " + stopP + " " + SY + " " + SX)}
		if(alerts=="on"){alert ("caught Glide start " + objName);}
		 var obj = getRawObj(objName);
		if(alerts=="on"){alert ("cRaw Obj = " + obj);} 
		 if (corFact.top == -1){
		 	if ((typeof obj.offsetTop == "number") && obj.offsetTop > 0) {
			   corFact.top = obj.offsetTop;
			   corFact.left = obj.offsetLeft;
			 } else {
			   corFact.top = 0;
			   corFact.left = 0;
			 }
			if (obj.offsetWidth && obj.scrollWidth) {
			   if (obj.offsetWidth != obj.scrollWidth) {
			   	  obj.style.width = obj.scrollWidth;
			   }
			}
		 }
		 
		  if(edge=="L"){
		  	stopPoint = Math.round(getScreenWidth()*stopP);
		  	var startX = Math.round(getScreenWidth()*SX) - getObjWidth(obj)
		  	var startY = Math.round(getScreenHeight()*SY) - (getObjHeight(obj)/2)
		  }else if (edge=="B"){
		  	stopPoint = Math.round(getScreenHeight()*stopP);
		  	var startX = Math.round(getScreenWidth()*SX) - getObjWidth(obj)
		  	var startY = Math.round(getScreenHeight()*SY) + (getObjHeight(obj)/2)
																		  
		  }
		  
		  if(alerts=="on"){alert ("stopPoint = " + stopPoint);};
		  if(alerts=="on"){alert("Glide Obj = " + obj + "startX = " + startX + "startY = " + startY);}
		  shiftTo(obj, startX, startY);
		  show(obj);
		  if(alerts=="on"){alert("Setting Interval")}
		  intervalID = setInterval("glideToCenter('" + objName + "','" + Tangle + "','" + imgNo + "','" + startY + "','" + edge + "')", 7);
}


function glideToCenter(objName,angle,imgNo,startY,edge) {
		  if(alerts=="on"){alert("glide angle" + angle)}
		  if(alerts=="on"){alert("Gliding to center");}
		  if (alerts=="on"){alert(edge);}
		  var obj = getRawObj(objName);
		  var objX = getObjX(obj);
		  var objY = getObjY(obj);
		  if(edge == "L"){
		  	var jumpX = Math.round((stopPoint - objX)/6);
    		if(angle==0){
				var jumpY = 0;
			}else{
				var jumpY = Math.round((jumpX)/angle);
			}
		  }else if (edge == "B"){
			  var jumpY = (Math.round((objY - stopPoint)/6))*-1;
    		if(angle==0){
				var jumpX = 0;
			}else{
				var jumpX = Math.round((jumpX)/angle);
			}
			  if (alerts=="on"){alert("B - X = " + jumpX + " Y = " + jumpY);}
		  }
		  
		  if (alerts=="on"){alert("jumping X - " + jumpX + "   Y - " + jumpY + " Object X - " + objX + " Object Y - " + getObjY(obj));}
		  
		  shiftBy(obj, jumpX, jumpY);
		  if (edge=="L"){
		  	if (getObjX(obj) >= (stopPoint - 5)){
				  //shiftTo(obj, stopPoint, Math.round(stopPoint/2.22))
			  	clearInterval(intervalID);
			  	if(alerts=="on"){alert("Interval Cleared");}
			  	imgSlide(imgNo);
		  	}
		  }else if(edge=="B"){
		  	if (getObjY(obj) <= (stopPoint + 5)){
				  //shiftTo(obj, stopPoint, Math.round(stopPoint/2.22))
			  	clearInterval(intervalID);
			  	if(alerts=="on"){alert("Interval Cleared");}
			  	imgSlide(imgNo);
		  	}
		  }
		  
		  
		  
		  
		  
}

function getMouseXY(e) {
  if (isIe4) {
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {
    tempX = e.pageX
    tempY = e.pageY
  }  
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
}



//------------------------------------------------------------------Handling for CSS-P redraw bug in NN4

function handleResize() {
		location.reload();
}
