// String.trim emuláció
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.ltrim = function() { return this.replace(/^\s+/g, ''); }
String.prototype.rtrim = function() { return this.replace(/^\s+$/g, ''); }
String.prototype.trimall = function() { return this.replace(/\s+/g, ''); }
String.prototype.unspace = function() { return this.replace(/\s+/g, ' '); }

/**
* Browser version detection
*/
var ns4 = false; // to be set later...
var ieversion = 0, opversion = 0, koversion = 0, saversion = 0, ua = navigator.userAgent.toLowerCase();

if (ua.indexOf("msie")!=-1 && ua.indexOf("opera")==-1){
temp=ua.split("msie");
ieversion=parseFloat(temp[1]);
}
else if (ua.indexOf("opera")!=-1){
temp=ua.split("opera");
temp[1] = temp[1].replace("/","");
opversion=parseFloat(temp[1]);
} 

else if (ua.indexOf("safari")!=-1) {
	temp=ua.split("version");
	temp[1] = temp[1].replace("/","");
	saversion = parseFloat(temp[1]);
}
else if (ua.indexOf("konqueror")!=-1) {
	temp=ua.split("khtml");
	temp[1] = temp[1].replace("/","");
	koversion = parseFloat(temp[1]);
}

/*******************
* Common functions *
********************/
function get(el) {
	return (typeof el == "string") ? window.document.getElementById(el) : el;
}
function collectionToArray(col) {
	a = new Array();
	for (i = 0; i < col.length; i++)
		a[a.length] = col[i];
	return a;
}
function addListener(el, ev, li) {
	if (!(el = get(el))) return;
	if (el.addEventListener) {
		el.addEventListener(ev, li, false); 
	} else if (el.attachEvent){
		el.attachEvent("on" + ev, li);
	}
}
function hasClassName(el,cn) {
	if (!(el = get(el))) return;
	var elementClassName = el.className;
	return (elementClassName.length > 0 && (elementClassName == cn ||
	 new RegExp("(^|\\s)" + cn + "(\\s|$)").test(elementClassName)));
}
function addClassName(el, cn) {
	if (!(el = get(el))) return;
	if (!hasClassName(el, cn))
	  el.className += (el.className ? ' ' : '') + cn;
	return el;
}
function removeClassName(el, cn) {
	if (!(el = get(el))) return;
	  el.className = el.className.replace(new RegExp("(^|\\s+)" + cn + "(\\s+|$)"), ' ');
	return el;
}
function addClassName_(el, cn) { // for use in hover.htc
if (!(el = get(el))) return;
	if (!hasClassName(el, cn)) {
		if (!el.oldClassName) el.oldClassName = el.className ? el.className : '';
		el.className += (el.className ? ' ' + el.oldClassName + '_' + cn + ' ' : '') + cn;
	}
	return el;
}
function removeClassName_(el, cn) { // for use in hover.htc
	if (!(el = get(el))) return;
	el.className = el.className.replace(new RegExp("(^|\\s+)"+ el.oldClassName + "_" + cn + "(\\s+|$)"), ' ');
	el.className = el.className.replace(new RegExp("(^|\\s+)" + cn + "(\\s+|$)"), ' ');
	return el;
}
function removeChildrenFromNode(node) {
	if (node === undefined || node === null) {
		return;
	}
	var len = node.childNodes.length;
	while (node.hasChildNodes()) {
		node.removeChild(node.firstChild);
	}
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function getElementHeight(Elem) {
	
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	}
	var elem = get(Elem);
	
	if (elem.style.pixelHeight && typeof elem.style.pixelHeight != "undefined") { 
		xPos = elem.style.pixelHeight;
	} else {
		xPos = elem.offsetHeight;
		
	}
	//trace(xPos);
	return xPos;

}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		var elem = get(Elem);
		if (elem.style.pixelWidth) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// ActiveX Object Activation
var activateObjects = function() {
	if (ieversion) {
		ns = document.getElementsByTagName('noscript');
		for (i in ns) {
			var n = ns[i];
				if (n.className=='obj') {
				//var flashHTML = n.innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&').toString();
				var flashHTML = n.innerHTML.replace(/<\!\[endif\]-->/i,'').replace(/<\!--\[if IE\]>/i,'');
				var holder = document.createElement('span');
				holder.style.display = 'inline';
				holder.style.position = 'static';
				holder.innerHTML = flashHTML;
				n.parentNode.insertBefore(holder, n);
			}
		}
	}
}

// Flash Detection
var flashinstalled = 0;
var flashversion = 0;
var MSDetect = "false";
var detectFlash = function() {
	if (navigator.plugins && navigator.plugins.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if (x) {
			flashinstalled = 2;
			if (x.description) {
				y = x.description;
				flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
		else
			flashinstalled = 1;
		if (navigator.plugins["Shockwave Flash 2.0"]) {
			flashinstalled = 2;
			flashversion = 2;
		}
	}
	else if (navigator.mimeTypes && navigator.mimeTypes.length) {
		var x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin) flashinstalled = 2;
		else flashinstalled = 1;
	}
	else MSDetect = "true";
}
// end of Flash Detection js part


/*----------------------
* End Common functions *
-----------------------*/

var setMenuEvents = function() {
	body  = document.getElementsByTagName("body").item(0);
	var menu = get('mainmenu');
	window.menuItems = menu.getElementsByTagName('li');
	//window.menuItems = collectionToArray(menu.getElementsByTagName('dl'));
	//window.menuItems = menuItems.concat(collectionToArray(menu.getElementsByTagName('li')));
	
	//window.menuItems = menuItems;
	window.t = new Array();
	
	var smenu = get('submenuholder');
	window.subMenu = smenu.getElementsByTagName('div');
	
	for (i=0; i<menuItems.length; i++) {
		if (get('sub'+(i+1))) {
			window.menuItems[i].subMenu = get('sub'+(i+1));
			window.menuItems[i].menuHeight = getElementHeight(menuItems[i].subMenu);
			
			window.menuItems[i].menuTop = -30;
			window.menuItems[i].menuBottom = menuItems[i].menuTop-menuItems[i].menuHeight-5;
			window.menuItems[i].subMenu.style.bottom = menuItems[i].menuBottom+'px';
			window.menuItems[i].subMenu.style.display = 'none';
			window.menuItems[i].i = i;
			window.menuItems[i].y = menuItems[i].menuBottom;
			window.menuItems[i].delay = 0;
			//menuItems[i].t = false;
			
			//trace(menuItems[i].menuHeight);
			window.menuItems[i].move = function(i,dir) {
				//Number:i = i;
				ths = window.menuItems[i];
				
				/*trace('this.i['+i+'] = '+window.t[i]+'... y='+window.menuItems[i].y);*/
				//if (window.delay < 0 || dir=='down') {
					
				
				if (dir=='down') {
					speed = 8;
					dest = ths.menuBottom;
					isondest = (ths.y < dest+1);

				} else {
					speed = 5;
					dest = ths.menuTop;
					isondest = (ths.y > dest-1);
					ths.subMenu.style.display = 'block';
				}
				
				if (window.active == ths) {
					
					if (window.delay < 1 || dir=='up') {
						window.moving = true;
						ths.y += (dest-ths.y)/speed;
					} else window.delay-=10;
	
						if (isondest) {
							window.moving = false;
							ths.y = dest;
							if (dir=='down') {
								clearInterval(window.t[i]);
								ths.subMenu.style.display = 'none';
							} else {
								window.delay = 99999999;
								clearInterval(window.t[i]);
								var f = function(){ths.move(i,'down')}
								window.t[i] = setInterval(f,20);
							}
						}
						ths.subMenu.style.bottom = Math.round(ths.y)+'px';/**/
					
				} else {
					speed = 8;
					dest = ths.menuBottom;
					ths.y += (dest-ths.y)/speed;
					if (ths.y < dest+1) {
						ths.y = dest;
							clearInterval(window.t[i]);
							ths.subMenu.style.display = 'none';
							//window.delay = 0;
					}
					ths.subMenu.style.bottom = Math.round(ths.y)+'px';
				}
				//trace(window.moving+" "+window.delay);
			}
			//window.menuItems[i].onmouseover = function () { this.subMenu.style.bottom = this.menuTop+'px'; trace(this.menuHeight)/**/}
			window.menuItems[i].onmouseover = window.menuItems[i].onfocus = function () { 
				//this.subMenu.style.bottom = -this.menuHeight+'px'
				var i = this.i;
				window.active = this;
				window.delay = 0;
				clearInterval(window.t[i]);
				var f = function(){window.menuItems[i].move(i,'up',0)}
				window.t[i] = setInterval(f,20);
				//window.t[this.i] = setInterval(function(i){trace(i)},500,this.i);
			}
			window.menuItems[i].onmouseout = window.menuItems[i].onblur = function () { 
				//this.subMenu.style.bottom = -this.menuHeight+'px'
				var i = this.i;
				if (!window.moving) {
					window.delay = 500;
					clearInterval(window.t[i]);
					var f = function(){window.menuItems[i].move(i,'down')}
					window.t[i] = setInterval(f,20);
				}


				//window.t[this.i] = setInterval(function(i){trace(i)},500,this.i);
			}
			/*window.menuItems[i].onclick = function () {
				this.onmouseover();
				return false;
			}*/
		}
	}
	for (i=0; i<subMenu.length; i++) {
		subMenu[i].onmouseover = function() {if (!window.moving) window.delay=99999999;/**/}
		subMenu[i].onmouseout = function() {window.delay=500;}
		subMenu[i].onclick = function() {window.mnuclk=true;}
	}
	body.onclick = function() {
		if (!window.mnuclk) window.active=false;
		window.mnuclk=false;
	}
}


//=========//
// Startup //
//=========//
function addLoadEvent(func) {
	if (!document.getElementById | !document.getElementsByTagName) return;
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
			oldonload();
			func();
		}
	}
}
var startLoad = function() {
	//detectFlash();
	setMenuEvents();
	activateObjects();
}
addLoadEvent(startLoad);
