
function gid(id){
	return document.getElementById(id);
};

function cel(name) { return document.createElement(name); };

function scrollToElem(elem){ window.scrollTo(findPosX(elem),findPosY(elem)); };

function urlEncode (string) { return encodeURIComponent(string); };

function urlDecode (string) { return decodeURIComponent(string); };

function getPageSize() {
	var x = (document.documentElement.clientWidth && browser.client!='op') ? document.documentElement.clientWidth:document.body.clientWidth;
  var y = (document.documentElement.clientHeight && browser.client!='op') ? document.documentElement.clientHeight:document.body.clientHeight;
	if ((browser.klient=='saf') || (browser.klient=='kon')) {
			y = window.innerHeight
	}
	return { width:x,height:y }
};

function dbg(text) {
	var dE = gid('debugText');
	dE.innerHTML = text + '<br/>' + dE.innerHTML;
	dE.style.display = 'block';
}

function pageWidth() {
	return window.innerWidth != null ? window.innerWidth : (document.body != null ? document.body.clientWidth : null);
}

function pageHeight() {
	return window.innerHeight != null ? window.innerHeight : (document.body != null ? document.body.clientHeight : null);
}

function setParentCoords(x, y)
{
    setStaticArrowCoordPos(arrowCrosshair, x, y);

	var win = window.opener ? window.opener : window.dialogArguments;
	// Setup window openerer
	if (!window.opener)
		window.opener = win;

	if (!window.opener)
	    return;

    if (window.opener.setMapCoords) window.opener.setMapCoords(x, y);
}

// buble params
var showBubleTimer = false;
var hideBubleTimer = false;
var bubleVisible = false;
var bubleWidth = 300;
var bubleHeight = 250;

function showBuble(x, y, id) {
    hideBubleTimer = false;
	if (!showBubleTimer && !bubleVisible) {
        showBubleTimer = true;
        setTimeout("showBubleOnTimer("+ x + ", " + y + ", '" + id +"' );", 1000);
	}
}

function showBubleOnTimer(x, y, id) {
	if (showBubleTimer) {
		bubleOpen(x, y, id);
	}
}

function hideBuble() {
    showBubleTimer = false;
    if (!hideBubleTimer && bubleVisible) {
    	setTimeout("hideBubleOnTimer();", 1000);
    	hideBubleTimer = true;
    }
}

function bubleOver() {
    hideBubleTimer = false;
}

function bubleOpen(event)
{
    var event = events.getEvent(event);

	var elemId = events.getElement(event).id;
	gid('objectDetail').innerHTML = '';
	var objectId = elemId.substr(9);

    events.stopEvent(event);
    var objectArr = null;
	if ((objectArr = getMapObjectById(objectId)) == null) {
		return false;
	}

    var x = objectArr[0] + objectArr[3];
	var y = objectArr[1] + objectArr[3];
	
	var bD = gid('objectBuble');
	
	if (x + bubleWidth > mc.sx) {
		x = x - bubleWidth;
	}
	
	if (y + bubleHeight > mc.sy) {
		y = y - bubleHeight;
	}

	bD.style.left = x + 'px';
	bD.style.top = y + 'px';
	bD.style.display = 'block';
	bubleVisible = true;

	gid('objectBubleInfo').innerHTML = '';
	//showHideBubleTab('objectBubleInfo');
	xajax_mapShowObjectBubleDetail(mc, objectId);
	showBubleTimer = false;
	hideBubleTimer = false;
	
	return false;
}

function bubleClose() {
    var bD = gid('objectBuble');
	bD.style.display = 'none';
	bubleVisible = false;
	showBubleTimer = false;
	hideBubleTimer = false;
	
	return false;
}

function hideBubleOnTimer() {
	if (hideBubleTimer) {
        bubleClose();
	}
}

function showHideBubleTab(id) {
	if (id == 'objectBubleInfo') {
		gid('objectBubleInfo').style.display = 'block';
		gid('objectBubleDescription').style.display = 'none';
		gid('objectBublePhoto').style.display = 'none';
	} else if (id == 'objectBubleDescription') {
        gid('objectBubleInfo').style.display = 'none';
		gid('objectBubleDescription').style.display = 'block';
		gid('objectBublePhoto').style.display = 'none';
	} else if (id == 'objectBublePhoto') {
        gid('objectBubleInfo').style.display = 'none';
		gid('objectBubleDescription').style.display = 'none';
		gid('objectBublePhoto').style.display = 'block';
	}
	
	return false;
}



// class to store browser information such as
// platform - lin, mac, win, oth
// client   - geck, op, ie
// version  - 0 - 3
function cBrowser() {
	this.platform = '';
	this.client = '';
	this.version = '';
	this.getPlatform = function() {
		var txt=navigator.userAgent;
		if(txt.indexOf('X11')!=-1) {
			return'lin';
		}
		else if(txt.indexOf('Mac')!=-1) {
			return'mac';
		}
		else if(txt.indexOf('Win')!=-1) {
			return'win';
		}
		else {
			return'oth';
		}
	}
	this.getClient = function() {
		var txt=navigator.userAgent;
		if(document.addEventListener&&!window.opera&&(txt.indexOf('KHTML')==-1)) {
			return'geck';
		}
		else if(document.addEventListener&&window.opera) {
			return'op';
		}
		else if(document.attachEvent&&!window.opera) {
			return'ie';
		}
		else if(txt.indexOf('KHTML')!=-1) {
			if(txt.indexOf('Safari')!=-1) {
				return'saf';
			}
			else {
				return'kon';
			}
		}
		else {
			return'oth';
		}
	}
	this.getVersion = function() {
		var txt=navigator.userAgent;
		if(browser.client=='ie') {
			var st=txt.indexOf('MSIE');
			var ln=8;
			var num=parseFloat(txt.substr(st+5,3));
			if(num<5.5) {
				return 1;
			}
			else if(num<5.9) {
				return 2;
			}
			else if(num>5.9) {
				return 3;
			}
		}
		else if(browser.client=='op') {
			var test=txt.indexOf('Opera');
			var ver=txt.substr(test+6,5);
			num=parseInt(ver,10)>7?1:0;
			return num;
		}
		else if(browser.client=='kon') {
			var st=txt.indexOf('KHTML/');
			var ln=6;
			var num=parseFloat(txt.substr(st+6,4));
			return num;
		}
		else {
			return 0;
		}
	}
	// retrieves all the information about client browser
	this.getBrowser = function() {
		this.platform=this.getPlatform();
		this.client=this.getClient();
		this.version=this.getVersion();
	}
}



// Class used to handle all events - adding, removing, stoping, etc.
function cEvent() {
	this.getEvent = function() {
		e = arguments[0];
		e = e?e:window.event;
		return e;
	}
	// returns element on which event was triggered
	this.getElement = function(event) {
		e = window['events'].getEvent(event);
		if(!e.currentTarget) {
			e.currentTarget = e.srcElement;
		}
		return e.currentTarget;
	}
	// adds event handler f for element e for event type type
	// capture defines if the event is trigger in capture phase or bubbling phase
	this.addEvent = function (e, type, f, capture) {
		if (document.addEventListener)	{
			if (window.opera && (e==window)) {
				e=document;
			}
			e.addEventListener(type,f,capture);
		}
		else if (document.attachEvent) {
			e.attachEvent('on'+type,f);
		}
	}
	// removes event handler f for element e and type type
	this.removeEvent = function(e, type, f, capture) {
		if (document.removeEventListener) {
			if (window.opera && (e==window)) {
				e=document;
			}
			e.removeEventListener(type,f,capture);
		}
		else if (document.detachEvent) {
			e.detachEvent('on'+type,f);
		}
	}
	// stops event propagation in bubbling phase
	this.stopEvent = function(event) {
		var e=window['events'].getEvent(event);
		
		if(e.stopPropagation) {
			e.stopPropagation()
		}
		else {
			e.cancelBubble=true;
		}
	}
	/**
	 * Finds the first node with the given tagName, starting from the node the event was triggered on; traverses the DOM upwards
	 */
  this.findElement = function(event, tagName) {
    var element = events.getElement(event);
    while (element.parentNode && (!element.tagName ||
        (element.tagName.toUpperCase() != tagName.toUpperCase())))
      element = element.parentNode;
    return element;
  }
	// prevents execution of a default action defined for the event
	this.preventDefault=function(event) {
		e=window['events'].getEvent(event);
		if(e.preventDefault) {
			e.preventDefault();
		}
		else {
			e.returnValue=false;
		}
	}
}
