// Functions used to determine the position/dimension of elements and the client/browser/window

// Get position of mouse from event
function getMouseXY(e)
{
	//ShowDebug("IN getMouseXY");
	if (!e) e=window.event;
	var mouseX=0,mouseY=0;
	if(e)
	{
		//ShowDebug("check page e.pageX = " + e.pageX + " e.pageY = " + e.pageY);
		if(e.pageX || e.pageY)
		{
			
			//ShowDebug("use e.pageX = " + e.pageX + " pageY = "+ e.pageY)
			mouseX = parseInt(e.pageX+10);
			mouseY = parseInt(e.pageY);
		}
		else if(e.clientX || e.clientY)
		{
			var scroll = getScrollPosition();
			//ShowDebug("mouseX = clientX " + e.clientX + " scroll.x " + scroll.x + " clientY " + e.clientY + " scroll.y " + scroll.y)
			mouseX = parseInt(e.clientX)+10 + scroll.x;
			mouseY = parseInt(e.clientY) + scroll.y;	
		}
	}
	var mouse = {
		'x' : mouseX,
		'y' : mouseY
	}
	//ShowDebug("In getMouse x = " + mouseX + " y = " + mouseY);
	return mouse;

}

// get dimensions of viewport
function getViewportSize()
{
	//ShowDebug("In getViewportSize");
	var width=0,height=0;

	if (typeof window.innerWidth != 'undefined') {
		width = window.innerWidth;
		height = window.innerHeight;
		//ShowDebug("use window.innerWidth/Height")
	}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
		//ShowDebug("use document.documentElement.clientWidth/Height")
	}else{
		width = document.getElementsByTagName('body')[0].clientWidth;
		height = document.getElementsByTagName('body')[0].clientHeight;
		//ShowDebug("use document.getElementsByTagName('boyd')[0].clientWidth/Height")
	}
	//ShowDebug("return width = " + width + " height + " +height);
	var size = {
		"width" : width,
		"height" : height
	}	
	return size;
}


// get scroll position
function getScrollPosition(){
	var x=0,y=0;
	//ShowDebug("document.body.scrollLeft = " + document.body.scrollLeft + " document.body.scrollTop = " + document.body.scrollTop);
	if( typeof( window.pageYOffset ) == 'number' ) {
		// Most browsers apart from IE
		x = window.pageXOffset;
        y = window.pageYOffset;
		ShowDebug("GetScrollPosition - use window.pageOffset x = "+ x + ", y = "+ y);
	} else if( typeof(document.documentElement) != 'undefined' && (document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode and some other browsers
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
		ShowDebug("GetScrollPosition -use document.documentElement.scroll x = "+ x + ", y = "+ y);
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant browsers
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
		ShowDebug("GetScrollPosition - use document.body.scroll x = "+ x + ", y = "+ y);
	} 

	var position = {
		'x' : x,
        'y' : y
    }

	return position;
}

// find the current x,y top,left position of an element
function findPosition( obj ) {
	ShowDebug("in find position typeof(obj) = " + typeof(obj) + " obj.id = " + obj.id)
	var position,posX=0,posY=0;
	if(obj){	
		ShowDebug("tagname = " + obj.tagName + " parent tag = "+ obj.offsetParent.tagName + " typeof obj.offsetParent = "+ typeof(obj.offsetParent))
		if( typeof( obj.offsetParent ) != 'undefined' ) {
			for( var posX = 0, posY = 0; obj; obj = obj.offsetParent ) {
				ShowDebug("obj = " + obj.tagName + " obj.id = "+ obj.id + " typeof(obj) = " + typeof(obj))
				posX += obj.offsetLeft;
				posY += obj.offsetTop;
			}
			if(obj)
				ShowDebug("exit for on = " + obj.tagName + " typeof = " + typeof(obj));
		} else {			
			posX = obj.x;
			posY = obj.y;
		}
		ShowDebug("Position of object is x = " +posX + " y = "+ posY);
	}
	position = {
		"x":posX,
		"y":posY
	}
	ShowDebug("no object reference return x = " + posX + " y = " + posY)
	return position;
}

// get an elements style property css/inline
function getElementStyle(elemID, IEStyleProp, CSSStyleProp, win) {
	if(!win) win = window;
	doc = win.document;
	var elem = getEl(elemID,doc);	
	if (elem.currentStyle) {
		return elem.currentStyle[IEStyleProp];
	}else if (win.getComputedStyle) {
		var compStyle = win.getComputedStyle(elem, "");
		return compStyle.getPropertyValue(CSSStyleProp);
	}
	return "";
}

// RJR - function to convert em to px incase a size has been set in em
function ConvertEmToPX(em, win)
{
	if(!win){win = window}
	var doc = win.document;
	var fs, fspx, elem;
	// we work out how much 1em is from the fontsize
	if(doc.body.fontSize){
		fs = doc.body.fontSize;
	}else{
		elem = doc.body;
		if (elem.currentStyle) {					
			fs = elem.currentStyle["fontSize"];							
		}else if (win.getComputedStyle) {
			var compStyle = win.getComputedStyle(elem, "");					
			fs = compStyle.getPropertyValue("font-size");
		}					
	}
	//if in points needs pixels
	fpt = fs.toString()
	pt = fpt.substring(fpt.length-2,fpt.length)				
	if(pt=="pt"){					
		fspx = 1.3333*parseInt(fs)
	}else if(pt=="px"){
		fspx = parseFloat(fs)
	}				
	return fspx*em
}

// Returns the dimensions of an object if they can be calculated
function getDimensions(id){
	ShowDebug("CALL getDimensions = " + id)
	var width = getWidth(id);
	var height = getHeight(id);
	var dimensions = {
		"width" : width,
		"height" : height
	}
	return dimensions;
}

//try to get the width of an element in px
function getWidth(id)
{
	//ShowDebug("in getWidth = " + id)
	var width=0;
	var el = getEl(id);
	//ShowDebug("typeof = " + typeof(el) + " el.type = " + el.type)
	//ShowDebug("scrollWidth = " + el.scrollWidth +" innerWidth = " + el.innerWidth + " offset = " + el.offsetWidth + " document.layers["+id+"].offsetWidth = " + document.layers[id].offsetWidth + " document.layers[id].width = " + document.layers[id].width)
	if(!el){return 0;}
	var elstyle = (el.style)?el.style:el;
	if(Browser.dom==3){
		width = parseInt(el.document.width);
		//ShowDebug("got layers width = " + width)
	}
	//ShowDebug("el.width = " + el.width + " el.style.width = "+ el.style.width + " el.pixelWidth = " + el.pixelWidth + " el.style.pixelWidth = " + el.style.pixelWidth + " el.offsetWidth = "+ el.offsetWidth + " getElementStyleID(width) = " + getElementStyle(id, "width", "width") + " and thats all folks!!")
	if(typeof(width)!='number' || width==0){
		//ShowDebug("get from el.width if possible = " + el.width)	
		if (el.width){
			//ShowDebug("get from el.width = " + el.width);
			width = parseInt(el.width);
		}
		if((typeof(width)!='number'||width==0)&&el.style){
			//ShowDebug("get from el.style.width");
			width = parseInt(el.style.width);
		}
	}
	//ShowDebug("width = " + width)
	if(	typeof(width)!='number' || width==0){
		//ShowDebug("get width from el.style.pixelWidth")
		width = (el.style)?el.style.pixelWidth:el.pixelWidth;
	}
	if(!width||parseInt(width)==0){
		width= getElementStyle(id, "width", "width")		
		//ShowDebug("from getElementStyle = " + width)
	}
	s=width.toString().toLowerCase().replace(/\d+/,"")
	if(s=="auto"||s=="%"||!width||parseInt(width==0)){
		width = el.offsetWidth;
		//ShowDebug("use offset = "+ width )
	}else if(s=="pt"){
		width = 1.3333*parseInt(width)
		//ShowDebug("is pt = " + s.toLowerCase() + " convert to px = " + width)
	}else if(s=="em"){
		width = ConvertEmToPX(parseInt(width))
		//ShowDebug("is em = " + s.toLowerCase() + " convert to px = " + width)
	}
	//ShowDebug("return " + width)
	return width;
}

//try to get the width of an element in px
function getHeight(id,bPad)
{
	//ShowDebug("in getHeight = " + id)
	var height=0;var padTop=0,padBot=0,padding=0;
	var px="px";
	var el = getEl(id);
	if(!el){return 0;}
	if((Browser&&Browser.dom==3)||(!Browser&document.layers)){
		//ShowDebug("get from el.document.height")
		height = el.document.height;
		px="";
	}
	if(typeof(height)!='number' || height==0){
		//ShowDebug("get from el.style.height")
		height = el.style ? el.style.height : el.height;		
	}
	//ShowDebug("height = "+ height);
	if(bPad){
		//ShowDebug("want to add padding if any set");
		padTop = getElementStyle(el.id, "padding-top", "padding-top");
		padBot = getElementStyle(el.id, "padding-bottom", "padding-bottom");
		//ShowDebug("padding-top = " + padTop + " padding-bottom = " + padBot);
		padding = parseInt(padTop)+parseInt(padBot);
		//ShowDebug("total padding = " + padding);
	}
	if(!height||parseInt(height)==0){
		height= getElementStyle(el.id, "height", "height")		
		//ShowDebug("from getElementStyle = " + height)
	}
	s=height.toString().toLowerCase().replace(/\d+/,"")
	//ShowDebug("s = " + s);
	if(s=="auto"||s=="%"||!height||parseInt(height==0)){
		height = el.offsetHeight;
		//ShowDebug("use offset = "+ height)
	}else if(s=="pt"){
		height = 1.3333*parseInt(height)
		//ShowDebug("is pt = " + s.toLowerCase() + " convert to px = " + height)
	}else if(s=="em"){
		height = ConvertEmToPX(parseInt(height))
		//ShowDebug("is em = " + s.toLowerCase() + " convert to px = " + height)
	}
	//ShowDebug("return height=" + parseInt(height)+" padding="+padding+" total="+(parseInt(height)+padding+px));
	return parseInt(height)+padding+px;
}
