/**
 * Shield jQuery plug-in.
 * @author Tibor Balogh
 */
(function($){
	var
		at = '&#'+'6'+'4;',
		sp = '//', h;

	$.fn.shield = function(){
		return this.each(function(){
			if (h = $(this.firstChild).html()){
				var
					items = h.split(sp);
					title = items[2]? items[2] : items[4]+at+items[5]+'.'+items[6];
				$(this).html(['<a href="',String.fromCharCode(109, 97, 105, 108, 116, 111, 58),items[3],String.fromCharCode(32,60),items[4],at,items[5],'.',items[6],String.fromCharCode(62,34,62),title,'</a>'].join(''));
			}
		});
	}
})(jQuery);

/**
 * Dimenzió adatok beállítása az adott elemhez.
 * $element.get(0).dimension : {height,width,top,left}
 */
(function($){
	$.fn.setDimension = function(){
		return this.each(function(i, ee){
			var
				e = $(this),
				d = e.css("display"),
				v = e.css("visibility");

			e.css({"display": "block", "visibility": "hidden"});
			ee.dimension = {
				height : e.height(),
				width : e.width(),
				top : e.offset().top,
				left : e.offset().left
			};
			e.css({"display": d, "visibility": v});
		});
	};
})(jQuery);

/**
 * Alapértelmezett JS könyvtár kinyerése.
 */
(function($){
	var
		basefile = 'jq.js',
		baseurl;

	$.getBaseUrl = function(jsfile){
		if (!baseurl || jsfile){
			var js = jsfile || basefile;
			baseurl = $('script[src*="'+js+'"]:first').attr('src');
			baseurl = baseurl.substring(0, baseurl.indexOf(js));
		}
		return baseurl;
	};
})(jQuery);

/**
 * CSS fájl behúzása.
 */
(function($){
	$.addCSS = function(css){
		$('head').append('<link rel="stylesheet" type="text/css" href="'+css+'" media="screen" />');
	};
	$.addJS = function(js){
		$('head').append('<script type="text/javascript" src="'+js+'"></script>');
	};
})(jQuery);

/**
 * Egérpozíció lekérdezése.
 * @return {x, y}
 */
(function($){
	$.mousePosition = function(e){
		return {
			x : (e.clientX + document.documentElement.scrollLeft),
			y : (e.clientY + document.documentElement.scrollTop)
		};
	};
})(jQuery);

/**
 * Porting the Crockford's JSON object to jQuery plug-in.
 * @author Crockford
 */
(function($){
	$.JSON = {
		copyright: '(c)2005 JSON.org',
		license: 'http://www.crockford.com/JSON/license.html',
		toString: function(v) {
			var a = [];
			g(v);
			return a.join('');

			function e(s) {
				a.push(s);
			};

			function g(x) {
				var c, i, l, v;

				switch (typeof x) {
				case 'object':
					if (x) {
						if (x instanceof Array) {
							e('[');
							l = a.length;
							for (i = 0; i < x.length; i += 1) {
								v = x[i];
								if (typeof v != 'undefined' && typeof v != 'function') {
									if (l < a.length) {
										e(',');
									}
									g(v);
								}
							}
							e(']');
							return;
						} else if (typeof x.valueOf == 'function') {
							e('{');
							l = a.length;
							for (i in x) {
								v = x[i];
								if (typeof v != 'undefined' && typeof v != 'function' && (!v || typeof v != 'object' || typeof v.valueOf == 'function')) {
									if (l < a.length) {
										e(',');
									}
									g(i);
									e(':');
									g(v);
								}
							}
							return e('}');
						}
					}
					e('null');
					return;
				case 'number':
					e(isFinite(x) ? +x : 'null');
					return;
				case 'string':
					l = x.length;
					e('"');
					for (i = 0; i < l; i += 1) {
						c = x.charAt(i);
						if (c >= ' ') {
							if (c == '\\' || c == '"') {
								e('\\');
							}
							e(c);
						} else {
							switch (c) {
							case '\b':
								e('\\b');
								break;
							case '\f':
								e('\\f');
								break;
							case '\n':
								e('\\n');
								break;
							case '\r':
								e('\\r');
								break;
							case '\t':
								e('\\t');
								break;
							default:
								c = c.charCodeAt();
								e('\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16));
							}
						}
					}
					e('"');
					return;
				case 'boolean':
					e(String(x));
					return;
				}
				e('null');
			};
		},
		parse: function(text) {
			return (/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) &&
				eval('(' + text + ')');
		}
	};
})(jQuery);


/**
 * Storage jQuery plug-in.
 * set: $.Storage("foo", 1);
 * set: $.Storage("foo", 1, expirationInSeconds);
 * get: foo = $.Storage("foo");
 * delete: $.Storage("foo", null);
 * @author Tibor Balogh
 */
(function($){
	var
		id = location.host,
		val = { //Interface
			get : function(n){},
			set : function(n, v, e){},
			remove : function(n){},
			setValue : function(v, e){
				return {"value":v, "exp":parseInt(new Date().getTime() / 1000 + e)};
			},
			getValue : function(n, d){
				if (d && (!d.exp || d.exp >= parseInt(new Date().getTime() / 1000)))
					return d.value;
				this.remove(n);
			}
		}, s;

	$.Storage = s = function(n, v, e){
		if (v === undefined) return val.get(n);
		if (v === null) val.remove(n);
		else val.set(n, v, e);
	};

	s.init = function(){
		if (window.localStorage){
			$.extend(val, {
			get: function(n){
				return this.getValue(n, $.JSON.parse(localStorage[n]));
			},
			set: function(n, v, e){
				localStorage[n] = $.JSON.toString(this.setValue(v, e));
			},
			remove: function(n){
				if (localStorage[n] !== undefined)
					delete localStorage[n];
			}
			});
			s.type = "localStorage";
		}
		else if (window.globalStorage){
			$.extend(val, {
			get: function(n){
				return this.getValue(n, $.JSON.parse(globalStorage[id].getItem(n)));
			},
			set: function(n, v, e){
				globalStorage[id].setItem(n, $.JSON.toString(this.setValue(v, e)));
			},
			remove: function(n){
				globalStorage[id].removeItem(n)
			}
			});
			s.type = "globalStorage";
		}
		else if ($.browser.msie){
			var v = $('<var/>')[0];
			$.extend(v, {
			get: function(n){
				return this.getValue(n, val.getAttribute(n));
			},
			set: function(n, v, e){
				this.setAttribute(n, val.setValue(v, e));
				this.save(id);
			},
			remove: function(n){
				this.removeAttribute(n);
				this.save(id);
			}}).addBehavior('#default#userData');

			$(document).ready(function(){
				$(v).hide().appendTo('body')[0].load(id);
			});
			s.type = "userData";
		}
		else {
			var ls = {};
			$.extend(val, {
			get: function(n){
				return this.getValue(n, ls[n]);
			},
			set: function(n, v, e){
				ls[n] = this.setValue(v, e);
			},
			remove: function(n){
				delete ls[n];
			}
			});
			s.type = "coreJavaScript";
		}
	};
	s.init();
})(jQuery);

/**
 * Cookie.
 * Cookie jQuery plug-in.
 * set: $.Cookie("foo", 1);
 * get: foo = $.Cookie("foo");
 * delete: $.Cookie("foo", null);
 * @param string cookie name
 * @param int cookie expiration in seconds
 * @author Tibor Balogh
 */
(function($){
	$.Cookie = function(name, value, exp, path, domain, secure){
		if (value === undefined){
			var m = document.cookie.match('\\b'+name+'=([^;]*)');
			return m && unescape(m[1]);
		}
		if (value === null){
			value = '';
			exp = -1;
		}
		var
			p = (path? "; path="+escape(path):''),
			d = (domain? "; domain="+escape(domain):''),
			s = (secure? "; secure":''),
			e = '';

		if (exp){
			e = new Date();
			e.setTime( e.getTime() + (exp * 1000) );
			e = "; expires="+e.toGMTString();
		}
		document.cookie = name+"="+escape(value)+e+p+d+s;
	};
})(jQuery);

/**
 * PNG Alpha fixation.
 * Internet Explorer png alpha jQuery plug-in.
 * using: $("img").iePNGFix();
 * $("div.transparent").iePNGFixBg();
 * $.iePNGFix.children = true;
 * $("div.images").iePNGFix();
 * $("body").iePNGFixBg();
 * @author Tibor Balgoh
 */
(function($){
	//Public properties
	$.iePNGFix = {
		spcImg : '/main_gui/images/space.gif',
		children : false
	};

	//Public methods
	$.fn.iePNGFix = function(settings){
		if (!IE || $.browser.version>=7){ return this; }
		$.extend(s, $.iePNGFix, settings);

		if (s.children){ $('img', this).each(img); }
		return this.each(img);
	};

	$.fn.iePNGFixBg = function(s){
		if (!IE || $.browser.version>=7){ return this; }
		s = $.extend({}, $.iePNGFix, s);

		if (s.children){ $('*', this).each(bgImg); }
		return this.each(bgImg);
	};

	//Private properties
	//The jQuery browser detection's fundament is the user agent string, It's not a valide checking, now we fix the IE detection.
	var
		IE = /*@cc_on !@*/false,
		s={}, m,
		rxImg = /.*\.png$/i,
		rxBgd = /^url\(["']?(.*\.png)["']?\)$/i;

	//Private methods
	function img(i, e){
		if ((m = e.src) && e.src.match(rxImg)){
			e.src = s.spcImg;
			e.style.filter += 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image, src="'+m[0]+'")';
		}
	}

	function bgImg(i, e){
		if (m = $(e).css('backgroundImage').match(rxBgd)){
			e.style.backgroundImage = 'none';
			e.style.filter += 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=crop, src="'+m[1]+'")';
		}
	}
})(jQuery);

/**
 * Adott szöveg kiigazítása megfelelő hosszúságra.
 * @param string Alapszöveg.
 * @param int Visszaadandő szöveg teljes hossza.
 * @param string Ismétlődő kitöltő karakter. Alapértelmezetten '0'.
 */
(function ($){
	$.sprint = function (s, len, c){
		var d = len-((s && s.length) || 0), r=""; c = c || "0";

		if (d > 0){ for (; d; --d){ r += c; }}
		return r+s;
	}
})(jQuery);

/**
 * Egyszerű scrollozás adott elemhez.
 */
(function($){
	$.scroll = {
		speed: "slow"
	};
	$.fn.scroll = function(direction, element){
		return this.each(function(){
			var anim = {};

			if (/left/.test(direction))
				anim.scrollLeft = element.offsetLeft || 0;
			else if (/right/.test(direction))
				anim.scrollLeft = element? element.offsetLeft + element.clientWidth - this.clientWidth : this.scrollWidth - this.offsetWidth;

			if (/top/.test(direction))
				anim.scrollTop = element.offsetTop || 0;
			else if (/bottom/.test(direction))
				anim.scrollTop = element? element.offsetTop + element.clientHeight - this.clientHeight : this.scrollTop - this.offsetHeight;

			$(this).animate(anim, $.scroll.speed);
		});
	}
})(jQuery);
