  //////////////////////////
 // The global variables //
//////////////////////////
var is = new function() {
	var agent = navigator.userAgent.toLowerCase();
	this.ns = ((agent.indexOf('mozilla') != -1) && ((agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)));
	this.ie = (agent.indexOf("msie") != -1);
	this.o = (agent.indexOf("opera") != -1);
	var nav = navigator.appVersion.toLowerCase();
	this.ie6 = (this.ie && (nav.indexOf('msie 6.0') != -1));
	this.ie7 = (this.ie && (nav.indexOf('msie 7.0') != -1));
};

////////////////////// DEBUG //////////////////////
function PO(objname, dest) {
	function out(s0) {
		if(io) {
			var div = document.createElement("p");
			div.innerHTML = s;
			dest.appendChild(div);
		} else {
			alert(s);
		}
	}

	var io = false;
	if(dest) {
		if(typeof dest == "string") dest = document.getElementById(dest);
		io = true;
	}

	var s = "[["+objname+"]] ->";
	var eo;
	if(typeof objname == "object") {
		eo = objname;
	} else {
		try {
			eo = eval(objname);
		} catch(e) {
			out("Initial error: " + e);
			return;
		}
	}
	var re = new RegExp('\n', 'g');
	var re_amp = new RegExp('&', 'g');
	var re_lt = new RegExp('<', 'g');
	var re_gt = new RegExp('<', 'g');
	var re_quot = new RegExp('"', 'g');

	var j = 0;
	for(var i in eo) {
		var x;
		try {
			x = eo[i];
		} catch(e) {
			x = "err: " + e;
		}
		var ss = "" + new String(x);
		ss = new String(ss).replace(re, ' ');
		if(ss.length > (io ? 128 : 64)) ss = ss.substr(0, io ? 125 : 61) + '...';
		if(io) {
			ss = ss.replace(re_amp, '&amp;').replace(re_lt, '&lt;').replace(re_gt, '&gt;').replace(re_quot, '&quot;');
			s = s + i + '=<code>' + ss + '</code> ';
		} else {
			s = s + i + '=' + ss + '   ';
		}

		if(j == 3) {
			s += io ? '<br/>' : '\n';
			j = 0;
		} else {
			j++;
		}
	}

	out(s);
}

////////////////// END DEBUG //////////////////////

  ///////////////////////////
 // Nagyon Common rutinok //
///////////////////////////
function nop() {
}

function GetComputedStyle(e) {
	return is.ie ? e.currentStyle : window.getComputedStyle(e, null);
}

function GetBounds(e) {
	var x = 0;
	var y = 0;
	var w = e.offsetWidth;
	var h = e.offsetHeight;

	while(e != null) {
		x += e.offsetLeft;
		y += e.offsetTop;
/*		if(is.ie) {
			webxPrintLog("|-" + e.nodeName + (e.id ? '#' + e.id : '') + (e.className ? '.' + e.className : '') + ' (' + e.offsetLeft + ';' + e.offsetTop + ';' + e.clientLeft + ';' + e.clientTop + ')');
		} */
		if(is.ie && e.nodeName == 'TD') { // IE hack, nem biztos, hogy jó.
			x += e.clientLeft;
			y += e.clientTop;
		}
		e = e.offsetParent;
	}

//	webxPrintlnLog();
	return { "x": x, "y": y, "w": w, "h": h };
}

function OverElement(ss, x, y) {
	var pos = GetBounds(ss);
	var x0 = pos.x
	var x1 = x0 + pos.w;
	var y0 = pos.y;
	var y1 = y0 + pos.h;
	return x > x0 && x < x1 && y > y0 && y < y1;
}

function openPopup(htmname, winname, xs, ys, plus) {
	return window.open(htmname, winname, "width="+xs+",height="+ys+",screenX=1,screenY=1,status=0,resizable=0"+plus);
}

function openPopupN(htmname, winname, xs, ys, plus) {
	var w = window.open(htmname, winname, "width="+xs+",height="+ys+",screenX=1,screenY=1,status=0,resizable=0"+plus);
	w.focus();
}

function setCookie(name, value, days, path, domain, secure) {
	var c = name + "=" + escape(value);
	if(days && days >= 0) {
		var d = new Date();
		d.setTime(d.getTime()+(days*24*60*60*1000));
		c += "; expires=" + d.toGMTString();
	}
	if(path) c += "; path=" + path;
	if(domain) c += "; domain=" + domain;
	if(secure) c += "; secure";
	document.cookie = c;
}

function getCookie(name, defVal) {
	if(typeof defVal == "undefined") defVal = null;

	var idx = document.cookie.indexOf(name + '=');
	if(idx == -1) return defVal;
	value = document.cookie.substring(idx + name.length + 1);
	var end = value.indexOf(';');
	if(end == -1) end = value.length;
	value = unescape(value.substring(0, end));
	return value;
}

function CaptureEvent(element, event, fn, useCapture) {
	if(is.ie) {
		element.attachEvent("on" + event, fn);
	} else {
		element.addEventListener(event, fn, useCapture);
	}
}

function ReleaseEvent(element, event, fn, useCapture) {
	if(is.ie) {
		element.detachEvent("on" + event, fn);
	} else {
		element.removeEventListener(event, fn, useCapture);
	}
}

function DiscardEvent(event) {
	if(is.ie) {
		event.cancelBubble = true;
		event.returnValue = false;
	} else {
		event.preventDefault();
	}
}

Object.prototype.clone = function(deep) {
	var o = new this.constructor();
	for(i in this) {
		var e = this[i];
		var t = typeof e;
		if(t == 'function' && this.constructor.prototype[i]) continue;
		else if(deep && t == 'object') o[i] = e.clone();
		else o[i] = e;
	}
	return o;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
};

if(!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(src, fromIdx) {
		for(var max = this.length, i = fromIdx ? (fromIndex < 0 ? Math.max(max - fromIndex, 0) : fromIndex) : 0; i < max; i++) {
			if(this[i] === src) return i;
		}
		return -1;
	}
}

/**
 * Automatikus hájlájtos gombok kezelése.
 */
function AutoButtons() {
	var _re_autoButtons = new RegExp("\\.(jpg|jpeg|gif|png)$", 'i');

	function chg(img, src) {
		return function() {
			img.src = src;
		}
	}

	with(document.images) {
		for(var i = 0; i < length; i++) {
			var img = item(i);
			if(img.id.substring(0, 5) == "abtn_" && img.parentNode.nodeName == 'A') {
				var off = img.src;
				var on = off.replace(_re_autoButtons, "-over.$1");
				var a = img.parentNode;
				CaptureEvent(a, "mouseover", chg(img, on), true);
				CaptureEvent(a, "mouseout", chg(img, off), true);
			}
		}
	}
}

CaptureEvent(window, "load", function() { new AutoButtons(); }, true);

/**
 * JSONML funkciók, némi kiegészítéssel.
 */
function jsonML(jsonml) {
//	wxlog.log("JSONML: " + jsonml.toJSONString());

	var t = typeof jsonml;
	if(jsonml instanceof Array && jsonml.length > 0) {
		// Element
		var tag = jsonml[0].toUpperCase();
		var e;
		e = document.createElement(tag);
		if(jsonml.length > 1) {
			var i = 1;
			var ad = jsonml[1];
			if(typeof ad == 'object' && !(ad instanceof Array)) {
				// attr
				for(var a in ad) {
					if(a == 'element') {
						if(ad.element != null && typeof ad.element == 'object') {
							ad.element[ad.elementId ? ad.elementId : 'element'] = e;
						} else {
							ad.element = e;
						}
					} else if(a == 'elementId') {
						// Az "element" property feldolgozásánál kell.
					} else if(a == 'class') {
						e.className = ad[a];
					} else if(a == 'style') {
						if(is.ie) e.style.cssText = ad[a];
						else e.setAttribute('style', ad[a]);
					} else if(!ad.constructor.prototype[a]) {
//						e.setAttribute(a, ad[a]);
						e[a] = ad[a];
//						wxlog.log("attr0: " + a + ": " + e[a]);
					}
				}
				i = 2;
			}

			var e1;
			if(tag == "TABLE") {
				e1 = document.createElement("TBODY");
				e.appendChild(e1);
			} else {
				e1 = e;
			}

			for(; i < jsonml.length; i++) {
				e1.appendChild(jsonML(jsonml[i]));
			}
		}
		return e;
	} else if(t == "string" || t == "number" || t == "boolean" || (jsonml instanceof String) || (jsonml instanceof Boolean) || (jsonml instanceof Number)) {
		// Text
		return document.createTextNode(jsonml);
	}

	var e = "Hibás jsonml! Nem megfelelö tipus: " + t;
	if(typeof console != 'undefined') {
		console.error(e);
		console.trace();
	}
	throw new Error(e);
}

function imagePopup(img, width, height) {
	var win = window.open(null, "rollstar_pic", 'location=no,menubar=no,status=no,toolbar=no,width=' + width + ',height=' + height + ',resizable=no,scrollbars=no');
	var d = win.document;
	d.write('<html><head><title>Kép</title></head><body style="margin: 0px;"><a href="javascript:window.close();">');
	d.write('<img src="' + img.href + '" width="' + width + '" height="' + height + '" border="0" alt=""/>');
	d.write('</a></body></html>');
	d.close();
	return false;
}

function openGallery(gname, link) {
	var href;
	if(typeof link != 'undefined') href = link.href;
	else href = '/galery.php?galery[id]=' + gname;
	window.open(href, '_blank', 'width=810,height=658,resizable=0,scrollbars=0');

	return false; // onclick miatt.
}

function ajanlatkeresChange(sel) {
	var trtelepules = document.getElementById('trtelepules');
	var x = document.getElementById('rollstar[telepules]');
	var y = document.getElementById('rollstar[telepulesdiv]');
	if (sel.value == 1) {
		//trtelepules.style.display = 'table-row';
		x.style.display = 'inline';
		y.style.display = 'inline';
	} else {
		//trtelepules.style.display = 'none';
		x.style.display = 'none';
		y.style.display = 'none';
	}
}

