/* 
tools.js    v.1.0

The latest version is available at
http://www.molooga.de

Created 08.06.2006 by H.Matthias (Web: http://www.molooga.de )
Last modified: 08.06.2006

JavaScript collection of useful, cross browser capable routines
Currently provides methods to
- detect windows size
- detect mouse position

*/

var docEl = (typeof document.compatMode!="undefined" && document.compatMode!="BackCompat")?"documentElement":"body";
var mouseX=0, mouseY=0;

function mousePos(e) {
	mouseX=e?e.pageX:window.event.x;
	mouseY=e?e.pageY:window.event.y;

	// for ie add scroll position
	if(document.all&&!document.captureEvents) {
		mouseX+=document[docEl].scrollLeft;
		mouseY+=document[docEl].scrollTop;
  }
	if (document.layers) routeEvent(e); // for NS4
	mouseMove();
}

function click(e) {
  if(!e) e = window.event;
  if((e.type && e.type == "contextmenu") || (e.button && e.button == 2) || (e.which && e.which == 3)) {
    rightClick();
    return false;
  }else{
  	leftClick();
  }	
}

if (document.layers){ //NS4
	document.captureEvents(Event.MOUSEDOWN);
	document.captureEvents(Event.MOUSEMOVE);
}	
document.onmousemove = mousePos;
document.onmousedown = click;
document.oncontextmenu = new Function("return false;");


// event driven functions
// need to be redefined in javascript application, which uses this tool library
function mouseMove(){

}
function leftClick(){

}
function rightClick(){

}


// get mouse coordinates	
function getMouseX(){
	return mouseX;
}	

function getMouseY(){
	return mouseY;
}


// get window size information
function getWindowWidth(win){
    if(win == undefined) win = window;
    if(win.innerWidth) return win.innerWidth;
    else{
        if(win.document.documentElement && win.document.documentElement.clientWidth) 
        	return win.document.documentElement.clientWidth;
        return win.document.body.offsetWidth;
    }
}

function getWindowHeight(win){
    if(win==undefined) win=window;
    if(win.innerHeight) return win.innerHeight;
    else{
        if(win.document.documentElement && win.document.documentElement.clientHeight) 
	       return win.document.documentElement.clientHeight;
        return win.document.body.offsetHeight;
    }
}


// set window size
function setWindowWidth(v,win){
    if(win == undefined) win = window;
    if(win.innerWidth) win.innerWidth=v;
    else{
        if(win.document.documentElement && win.document.documentElement.clientWidth) 
        	win.document.documentElement.clientWidth=v;
        win.document.body.offsetWidth=v;
    }
}

function setWindowHeight(v,win){
    if(win==undefined) win=window;
    if(win.innerHeight) win.innerHeight=v;
    else{
        if(win.document.documentElement && win.document.documentElement.clientHeight) 
	       win.document.documentElement.clientHeight=v;
        win.document.body.offsetHeight=v;
    }
}

function getObjectWidth(id){
	obj = document.getElementById(id);
	if(obj){
		return obj.clientWidth;
	}else{
		return -1;
	}		
}	

function getObjectHeight(id){
	obj = document.getElementById(id);
	if(obj){
		return obj.clientHeight;
	}else{
		return -1;
	}		
}

function getObjectX(id){
	obj = document.getElementById(id);
	if(obj){
		if(obj.offsetLeft){
			return obj.offsetLeft;
		}else{
			return obj.clientLeft;
		}	
	}else{
		return -1;
	}		
}

function getObjectY(id){
	obj = document.getElementById(id);
	if(obj){
		if(obj.offsetLeft){
			return obj.offsetTop;
		}else{
			return obj.clientTop;
		}	
	}else{
		return -1;
	}		
}
