function $d(elem){
	if(typeof elem != "undefined")
		return true;
	else
		return false;
}

function utils(oKaMap){
	this.kaMap = oKaMap;
	this.kaMap.utils = this;
}

// return the element on which event took place
utils.prototype.getEventSrc = function(e){
	e = (e)?e:((event)?event:null);
	if(e == null)
		return null;
	var src = (e.target)?e.target:((e.srcElement)?e.srcElement:e);
	return src;
}

utils.prototype.createTable = function(id, rows, cols){
        var table = document.createElement("table");
	if(id != null)
		table.id = id;                  
				                                                        
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);                 
													                                      
	var tableContent = new Array();
	for(var i = 0; i < rows; i++){
		tr = document.createElement("tr");
		tbody.appendChild(tr);
		tableContent.push(tr);
		for(var j = 0; j < cols; j++){
			td = document.createElement("td");
			tr.appendChild(td); 
			tableContent[tableContent.length-1][j] = td;
		}
	}
	return [table, tableContent];               
}

utils.prototype.createButton = function(){
	try{
		var button = document.createElement("<input type='button'>");
	}
	catch(e){
		var button = document.createElement("input");
		button.type = "button";
	}
	return button;
}

// make a field sensible to enter key : execute onclick (or onmousedown/up)
// function of the trigger when you press enter in elem
utils.prototype.listenToEnter = function(elem, trigger){
	elem = $(elem);
	trigger = $(trigger);
	if(elem == null || trigger == null)
		return false;

	if($d(trigger.onclick))
		elem.onkeyup = function(e){if(isReturn(e)) trigger.onclick()};
	if($d(trigger.click))
		elem.onkeyup = function(e){if(isReturn(e)) trigger.click()};
	else if($d(trigger.onmousedown))
		elem.onkeyup = function(e){if(isReturn(e))trigger.onmousedown()};
	else if($d(trigger.onmouseup)) 
		elem.onkeyup = function(e){if(isReturn(e))trigger.onmouseup()};
	else if($d(trigger.launchSearchFct))
		elem.onkeyup = function(e){if(isReturn(e)) trigger.launchSearchFct()};
	else
		return false;
		
	function isReturn(e){
		e = (e)?e:((event)?event:null);
		var code = e.charCode || e.keyCode;
		if(code == Event.KEY_RETURN)
			return true;
		else
			return false;
	}
	return true;
}


utils.prototype.insertAfter = function(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}

// op is over 100
utils.prototype.setOpacity = function(elem, op){
	elem = $(elem);
	elem.style.opacity = op/100;
	elem.style.mozOpacity = op/100;
	elem.style.filter = "Alpha(opacity="+op+")";
}

// return a boolean, tell us if click was a leftclick
utils.prototype.isLeftClick = function(e) {
	var rightclick;
	if (e.which)
		rightclick = (e.which == 3);
	else if (e.button)
		rightclick = (e.button == 2);
	return rightclick;
}


utils.prototype.getPageSize = function(){
        var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ 
		// all but Explorer Mac
 		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else { 
		// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { 
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { 
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	}
	else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	}
	else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

// used with a mouse click on map :
// return an object with the coords in geo and pixel relatively to theInsideLayer
utils.prototype.getGeoAndPixCoords = function(e){
	e = (e)?e:((event)?event:null);
	var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
	var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));

	// adjust pix position (from kaTool)
	var obj = this.kaMap.domObj;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (obj) {
        offsetLeft += parseInt(obj.offsetLeft);
        offsetTop += parseInt(obj.offsetTop);
        obj = obj.offsetParent;
    }
	var pX = parseInt(this.kaMap.theInsideLayer.style.left) + offsetLeft - this.kaMap.xOrigin - x;
    var pY = parseInt(this.kaMap.theInsideLayer.style.top) + offsetTop - this.kaMap.yOrigin - y;

	var geoCoords = this.kaMap.pixToGeo(pX, pY);
	
	var itop = (-pY - this.kaMap.yOrigin);
	var ileft = (-pX - this.kaMap.xOrigin);
	
	return {geo : {x : geoCoords[0], y : geoCoords[1]}, pix : {x : ileft, y : itop}};
};
