  //////////////////////////
 // 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) && agent.indexOf('like gecko') == -1));
	this.ie = (agent.indexOf("msie") != -1);
	this.o = (agent.indexOf("opera") != -1);
	this.safari = (agent.indexOf("safari") != -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));
};


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

var jgtc = {
	rootURL: null,
	rootImgURL: null,

	getComputedStyle: function(e) { return GetComputedStyle(e); },
	openPopup: function(htmname, winname, w, h, params) { throw "not implemented yet."; },
	setCookie: function(name, value, days, path, domain, secure) { setCookie(name, value, days, path, domain, secure); },
	getCookie: function(name, defVal) { getCookie(name, defVal); },
	captureEvent: function(element, event, fn, useCapture) { CaptureEvent(element, event, fn, useCapture); },
	releaseEvent: function(element, event, fn, useCapture) { ReleaseEvent(element, event, fn, useCapture); },
	discardEvent: function(event) { DiscardEvent(event); },
	getBounds: function(element) { return GetBounds(element); }
}

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;

/*
	if(is.ie) {
		console.log(e.currentStyle.getAttribute('paddingRight', 0));
		console.log(e.currentStyle);
	} else {
		console.dir(window.getComputedStyle(e, null).getPropertyCSSValue('padding-right'));
		console.log(window.getComputedStyle(e, null).getPropertyCSSValue('padding-right').getFloatValue(5));
	}
*/

	do {
		x += e.offsetLeft;
		y += e.offsetTop;
		if(is.ie && e.nodeName == 'TD') { // IE hack, nem biztos, hogy jó.
			x += e.clientLeft;
			y += e.clientTop;
		}
		e = e.offsetParent;
	} while(e != null);

	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;
				jgtc.captureEvent(a, "mouseover", chg(img, on), true);
				jgtc.captureEvent(a, "mouseout", chg(img, off), true);
			}
		}
	}
}

jgtc.captureEvent(window, "load", function() { new AutoButtons(); }, true);

jgtc.captureEvent(window, "load", function() {
	with(document.getElementsByTagName("script")) {
		for(var i = 0; i < length; i++) {
			var s = item(i);
			var r = s.src.match(/^(.*\/)?common\.js$/);
			if(r != null) {
				jgtc.rootURL = (r[1] == '' || r[1] == null) ? './' : r[1];
				jgtc.rootImgURL = jgtc.rootURL + '../admin/img/';
				break;
			}
		}
	}
}, true);

/**
 * JSONML funkciók, némi kiegészítéssel.
 */
function jsonML(jsonml, doc) {
	if(typeof doc == 'undefined') doc = document;

	if(jsonml instanceof Array && jsonml.length > 0) {
		// Element
		var tag = jsonml[0].toUpperCase();
		var e;
		e = doc.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(typeof ad.constructor.prototype[a] == 'undefined') {
//						e.setAttribute(a, ad[a]);
						e[a] = ad[a];
//						wxlog.log("attr0: " + a + ": " + e[a]);
					}
				}
				i = 2;
			}

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

			for(; i < jsonml.length; i++) {
				e1.appendChild(jsonML(jsonml[i]));
			}
		}
		return e;
	} else {
		// Text
		return doc.createTextNode(String(jsonml));
	}
}

/*
 * Tween helper
 */
/**
 *  eventfn = function(x, mode);
 *    x - az érték a pillanatnyi időpontban
 *    mode - 0: tick, 1: első tick, 2: utolsó tick
 */
function HTTween(eventFn, tween, time, step) {
	this.tstep = step || 40; // 25fps
	this.ttime = time || 200;
	this.eventFn = eventFn;
	this.tween = this.tweens[tween] || this.tweens.linear;
}

HTTween.prototype = {
	running: false,
	tweens: {
		'linear': function(x) { return x; },
		'sinoidal': function(x) { return 0.5 - Math.cos(x * Math.PI) / 2; }
	},

	_event: function(now, mode) {
		now = this.tween(now);
		now = this._x0 + (this._x1 - this._x0) * now;

//		console.log('now: %d %d', now, mode);

		this.eventFn(now, mode);
	},

	start: function(start, end, time) {
		var _this = this;

		time = time || this.ttime;

		if(this.running) {
			var n = this._x0 + (this._x1 - this._x0) * _now();
			if(n < start && n < end || n > start && n > end) {
				console.error("invalid now value: %d, %d, %d -> %d, %d, %d at %d / %d", this._x0, this._x1, this.ttime, start, end, time, _now(), this._t1 - this._t0);
				return;
			}
			this._t0 = this._t1 - time * (n - start) / (end - start);
		}

		this.ttime = time;
		this._x0 = start;
		this._x1 = end;

		if(this.running) return;

		this._t0 = this._t1 = new Date().getTime();

		function _now() {
			var now = (_this._t1 - _this._t0) / _this.ttime;
			if(now < 0) {
				console.error("invalid now value: %d at %d / %d, t1: %d, t0: %d", now, this._t1 - this._t0, _this.ttime, this._t1, this._t0);
				return;
			}
			return now > 1 ? 1 : now;
		}

		function _tick() {
			if(!_this.running) return; // cancelled...

			_this._t1 = new Date().getTime();
			var now = _now();

			if(now == 1) {
				_this._event(1, 2);
				_this.running = false;
			} else {
				setTimeout(_tick, _this.tstep);
				_this._event(now, 0);
			}
		}

		setTimeout(_tick, this.tstep);
		this._event(0, 1);
		this.running = true;

		return this;
	},

	cancel: function() {
		if(this.running) {
			this.running = false;
			this._event(1, 2);
		}

		return this;
	}
}
