
$(function() {
	_.firstInit();
});

_ = {
	s: undefined,
	w: undefined,
	k: undefined,
	l: undefined,
	
	d: true,
	x: undefined,
	
	a: undefined,
	c: undefined,
	
	firstInit: function() {
		_.s = false;
		_.w = false;
		_.k = new Array();
		_.l = document.location.host.length + 7;
		_.a = new Array();
		_.c = 0;
		_.r = 0;
		
		$.historyInit(_.doBack);
		_.init('pageContainer');
		$("script").each(function() {
			if ($(this).attr("id"))
				_.add($(this).attr("id"));
		});
		_.run();
		if (document.location.hash)
			if (document.location.hash.substr(1,1)=='/')
				_.doClickH(document.location.hash.substr(1));
	},

	init: function(a) {
		document.cookie = 'nojs=0; path=/';
		$("#"+a).find("a").click(function() {
			if (this.href.substr(0,_.l) != document.location.href.substr(0,_.l))
				return true;
			if (this.className.match("no_ajax"))
				return true;
			return _.doClick(this.href, "GET", "", this.className);
		});
		$("#"+a).find("form").submit(function() {
			if (this.className.match("no_ajax"))
				return true;
			return _.doSubmit(this);
		});
		$("#"+a).find("a.onload_reload").click();
	},

	doClickH: function(h, c) {
		_.doClick(document.location.href.substr(0,_.l) + h, "GET", "", c);
	},
	doClick: function(h, t, d, c) {
		if (_.w) {
			_.k.push(new Array(h,t,d,c));
			return false;
		}
		_.w = true;
		if (h.indexOf('#')>-1)
			h = h.substr(0, h.indexOf('#'));
		m=false;
		e=false;
		if (c) {
			m = c.match("moduleid_([a-zA-Z0-9_]+)");
			if (m)
				m=m[1];
			e = c.match("effect_([a-zA-Z0-9_]+)");
			if (e)
				e=e[1];
		}
		_.s = false;
		if (m) {
			ho=h+"/_module_id:" + m;
		} else {
			ho=h;
			m = "pageMain";
		}
		if (_.r==0) {
			if (e) {
				if ($.trim($("#"+m).text())!="") {
					_.d=false;
					$("#"+m).hide(e, 1000, _.effectDone);
				} else {
					$("#"+m).hide();
				}
			} else {
				_.d=false;
				$("#"+m).fadeTo("fast", 0.3, _.effectDone);
			}
			if ($("#dobrocloader").length > 0) {
				$("#dobrocloader").css("position","absolute");
				$("#dobrocloader").css("top", ( $(window).height() -  $("#dobrocloader").height() ) / 2+$(window).scrollTop() + "px");
				$("#dobrocloader").css("left", ( $(window).width() -  $("#dobrocloader").width() ) / 2+$(window).scrollLeft() + "px");
				$("#dobrocloader").show();
			}
		}
		$.ajax({
			url: (ho + '/_ajax/_tab:' + current_tab),
			async:false, 
			type: t, 
			data: d,
			error: function(x, t, e) {
				alert('Unknown error.'+e);
			},
			success: function(x) {
				_.x=new Array(x, h, m, e);
				_.doSuccess();
			}
		});
		return false;
	},
	effectDone: function() {
		_.d = true;
	},
	doSuccess: function() {
		if (_.d) {
			x = _.x[0];
			h = _.x[1];
			m = _.x[2];
			e = _.x[3];
			$(x).find("module").each(function() {
				if ($(this).attr("id")=='error')
					alert($(this).text());
				else if ($(this).attr("id")=='pageTitle')
					document.title=$(this).text();
				else if ($(this).attr("id")=='current_tab') {
					current_tab=$(this).text();
					_.doClick(h,t,d,c);
				} else if ($(this).attr("id")=='pageCss') {
					index = 0;
					while (index>-1) {
						f = $(this).text().substr(index).match("href='([^']+)'");
						if (f==null)
							index=-1;
						else {
							f=f[1];
							o = document.createElement("link");
							o.href = f;
							o.rel = "stylesheet";
							o.type = "text/css";
							document.getElementsByTagName('head')[0].appendChild(o);
							index=index+$(this).text().substr(index).indexOf(f)+1;
						}
					}
				} else if ($(this).attr("id")=='pageJs') {
					$("script").each(function() {
						this.d=true;	
					});	
					index = 0;
					while (index>-1) {
						f = $(this).text().substr(index).match("src='([^']+)'( id='([^']+)')?");
						if (f==null)
							index=-1;
						else {
							id=f[3];
							f=f[1];
							sure = true;
							$("script").each(function() {
								if (this.src.substr(this.src.length - f.length) == f) {
									sure = false;
									this.d = false;
								}
							});
							if (sure) {
								o = document.createElement("script");
								o.src = f;
								o.type = "text/javascript";
								if (id) {
									o.id = id;
									_.add(id);
								}
								document.getElementsByTagName('head')[0].appendChild(o);
							}
							index=index+$(this).text().substr(index).indexOf(f)+1;
						}
					}
					$("script").each(function() {
						if (this.d && this.src) {
							if ($(this).attr("id"))
								_.del($(this).attr("id"));
							this.parentNode.removeChild(this);
						}
					});
				} else {
					$('#'+$(this).attr("id")).html($(this).text());
					_.init($(this).attr("id"));
				}
			});
			document.location.hash=h.substr(_.l);
			$.historyInit(_.doBack);
			_.w = false;
			_.r = _.r + 1;
			if (_.k.length>0) {
				n = _.k.shift();
				_.doClick(n[0], n[1], n[2], n[3]);
			}
			_.r = _.r - 1;
			if (_.r==0) {
				_.run();
				if (e) {
					_.d = false;
					$("#" + m).show(e, 1000, _.effectDone);
				} else
					$("#" + m).fadeTo("fast", 1);
			  if ($("#dobrocloader").length > 0) $("#dobrocloader").hide();
			}
		} else {
			setTimeout("_.doSuccess()", 10);
		}
	},
	doSubmit: function(f) {
		if ($(f).find("input[type=file]").length) return true;
		$(f).find("input[type=submit]").each(function () {
				this.disabled=true;
		});
		var a=$(f).attr("action");
		if (a=="") {
			if (document.location.hash)
				a="http://" + document.location.host + document.location.hash.substr(1);
			else
				a=document.location.href.substr(0, document.location.href.length - document.location.hash.length);
  	} else {
			if (a.substr(0,1)=='/') {
				a="http://" + document.location.host + a;
			}
		}
		return _.doClick(a, "POST", $(f).serialize(), f.className);
	},
  
	doBack: function(h) {
		if (_.s) 
			_.doClickH(h);
		else
			_.s = true;
	},

	add: function(f) {
		_.a[_.c]=f;
		_.c++;
	},
	del: function(f) {
		for (var i = 0; i <_.c; i++)
			if (_.a[i]==f)
				break;
		if (i==_.c)
			return;
		_.c--;
		while (i<_.c) {
			_.a[i]=_.a[i+1];
			i++;
		}
		_.del(f);
	},
	run: function(i) {
		if (i == undefined) {
			i=0;
//			clearTimeout();
		}
		if (_.a == undefined) 
			return;
		while(i < _.c) {
			if (typeof(window[_.a[i].substr(4)])!="function") {
				setTimeout('_.run('+i+');', 100);
				i = _.c;
			} else {
				window[_.a[i].substr(4)]();
				i++;
			}
		}
	}
};
