function send_xmlhttprequest(obsluha, method, url, content, headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return false;

	xmlhttp.open(method, url);

	if (obsluha != null)
		xmlhttp.onreadystatechange = function() {
			obsluha(xmlhttp);
		};

	if (headers) {
		for (var key in headers)
			xmlhttp.setRequestHeader(key, headers[key]);
	}

	xmlhttp.send(content);
	return true;
}

function open_popup(dokument, sirka, vyska, scrollbars) {
	so = screen.width;
	vo = screen.height;
	zleva = ((so - sirka) / 2) - 13;
	shora = 120;
	popup_win = window.open(dokument,"popup_okno","width="+sirka+",height="+vyska+",left="+zleva+",top="+shora+",location=0,menubar=0,resizable=1,scrollbars="+scrollbars+",status=0,titlebar=0,toolbar=0");
	popup_win.focus();
}

function open_popup2(dokument, sirka, vyska, title) {
	var iframe = SJEL.CE("iframe");
	iframe.style.visibility = "hidden";  // schovame iframe, protoze behem loadingu je tam bile pozadi, ktereho se nejde zbavit
	iframe.src = dokument;
	iframe.width = sirka - 20;
	iframe.height = vyska - 78;
	iframe.setAttribute("frameborder", 0);  // ie8 to potrebuje, jinak vykresluje ramecek
	// az se iframe nahraje, tak ho zobrazime
	SJEL.AddEvent(iframe, "load", function(_e){var th = SJEL.$ET(_e); th.style.visibility = "visible";});
	SWindow.Open(sirka, vyska, iframe, 'popup_swin', title);
}

function Show(id, show) {
  if (show)
    document.getElementById(id).style.visibility='visible';
  else
    document.getElementById(id).style.visibility='hidden';
}

function mute(m) {
    if ((m == "on") || (m == "off"))
        send_xmlhttprequest(null, "GET", "/index.php?ma=amute&mute=" + m);
}


function reload_confirm_image() {
	var d = new Date();
	var obj = document.getElementById('confirm_data_image_image');

	if (obj)
		obj.src = '/index.php?ma=cnumber&new&time=' + d.getTime();
}

function change_confirm_style(second) {
	var obj1 = document.getElementById('confirm_data_image');
	var obj2 = document.getElementById('confirm_data_flash');

	if (second) {
		obj1.style.display = 'none';
		obj2.style.display = 'block';
	} else {
		obj2.style.display = 'none';
		obj1.style.display = 'block';
	}
}

function on_off(_id) {
    var elem = document.getElementById(_id);

    if (elem == null)
        return;

    if (elem.style.display == "none")
        elem.style.display = "block";
    else
        elem.style.display = "none";
}

function anonym_email(formular, profile, language) {
	var email = formular.email.value;

	if (!send_xmlhttprequest(anonym_email_r, 'GET', '/index.php?ma=ajax&sid=aemail&p=' + profile + '&l=' + language + '&e=' + email)) {
		return true;
	}

	return false;
}

function anonym_email_r(xmlhttp) {
	if (xmlhttp.readyState == 4)
		SWindow.Open(505, 0, xmlhttp.responseText);
}

function add_to_compare(m, p) {
	if (send_xmlhttprequest(add_to_compare_r, 'GET', '/index.php?ma=ajax&sid=compare&m=' + m + '&idp=' + p))
		return false;
	else
		return true;
}

function add_to_compare_r(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var el = document.getElementById('compare_l');

		if (el) {
			el.innerHTML = xmlhttp.responseXML.getElementsByTagName('count')[0].firstChild.data;
		}


		SWindow.Open(505, 0, xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data);
	}
}

function Vyjizdec (_w, _t, _c, _t2, _ro) {
	/*if (SJEL.ie) {
		this.w = SJEL.$(_w);
		SJEL.RemoveClass(this.w, "nojs");
		var ms = new MenuSwitcher(_w, '', true);
		ms.AddPair(_t, _c);
		return;
	}*/
	this.w = SJEL.$(_w);
	this.t = SJEL.$(_t);
	this.c = SJEL.$(_c);
	this.t2 = null;
	if ((_t2 != undefined) && (_t2 != null))
		this.t2 = SJEL.$(_t2);

	this.rol = _ro||false;
	if (this.rol) {
		SJEL.SStyle(this.c, {display: "block"});
		this.cHe = this.c.offsetHeight;
		SJEL.SStyle(this.c, {display: "none"});
	}

	if (!this.w || !this.t || !this.c)
		return;

	var cc = this.c;
    this.mo = new SJEL.Morph();
    this.ch = 0;
    this.o = false;  // otevreny

	SJEL.RemoveClass(this.w, "nojs");

	this.Close = function () {
		if (this.rol)
			this.mo.Init(this.c, {opacity: 0.0, height: "0px"}, 280);
		else
			this.mo.Init(this.c, {opacity: 0.0}, 280);

		this.mo.OnMorphFinished(function(){SJEL.SStyle(cc, {display: "none"});});
		this.mo.Morph();
		this.o = false;
	}

	this.t.ref = this;
	this.t.onclick = function () {
		if (!this.ref.o) {
			SJEL.AddClass(this.ref.w, "arr");
			SJEL.RemoveClass(this.ref.w, "arr2");
			/*SJEL.AddClass(this, "arr");
			SJEL.RemoveClass(this, "arr2");*/
			SJEL.SStyle(this.ref.c, {display: "block", opacity: 0.0, overflow: "hidden"});
			if (this.ref.rol) {
				SJEL.SStyle(this.ref.c, {height: "0px"});
				this.ref.mo.Init(this.ref.c, {opacity: 1.0, height: this.ref.cHe + "px"}, 280);
			} else
				this.ref.mo.Init(this.ref.c, {opacity: 1.0}, 280);
			this.ref.mo.OnMorphFinished(function(){});
			this.ref.mo.Morph();
			this.ref.o = true;
		} else {
			SJEL.RemoveClass(this.ref.w, "arr");
			SJEL.AddClass(this.ref.w, "arr2");
			/*SJEL.RemoveClass(this, "arr");
			SJEL.AddClass(this, "arr2");*/
			this.ref.Close();
		}
		
		return false;
	}

	if (this.t2 != null) {
		this.t2.ref = this;
		this.t2.onclick = function () {
			if (this.ref.o)
				this.ref.Close();
		}
	}
}

function add_messages(_id, _titleId) {
	var messa = SJEL.$(_id);
	var title = SJEL.$(_titleId);

	var bad = (SJEL.$A(messa, "class", "bad_message") != "");
	var clazz = bad ? "swin_bad" : "swin_good";

	SWindow.Open(505, 0, messa, 'message_win ' + clazz, title);
}

function ie6_hover(_id, _cl) {
	if (SJEL.ie != 6)
		return;

	var cl = SJEL.$A(_id, "class", _cl);
	for (var i = 0; i < cl.length; i++) {
		cl[i].onmouseover = function() {
			SJEL.AddClass(this, _cl + "hover");
		}
		cl[i].onmouseout = function() {
			SJEL.RemoveClass(this, _cl + "hover");
		}
	}
}

function product_click(_id, _cl) {
	var cl = SJEL.$A(_id, "class", _cl), a = null;
	for (var i = 0; i < cl.length; i++) {
		var a = SJEL.$T(cl[i], "a")[0];
		cl[i].href = a.href;
		cl[i].onclick = function() {
			window.location = this.href;
		};
	}
}

function row_highlight(_id) {
    this.wr = SJEL.$(_id);

    this.set_events = function (_elem) {
        _elem.onblur = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
            SJEL.RemoveClass(n, "highlight");
        }
        _elem.onfocus = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
        	if (!SJEL.ClassExists(n, "highlight")) {
				this.classAdded = true;
            	SJEL.AddClass(n, "highlight");
            }
        }
        _elem.onclick = function () {
        	var n = this.parentNode.parentNode;
        	if (n.tagName == "TD")
        		n = n.parentNode;
        	if (!SJEL.ClassExists(n, "highlight")) {
				this.classAdded = true;
	            SJEL.AddClass(n, "highlight");
	        }
        }
    }

    this.inputs = SJEL.$T(this.wr, "INPUT");
    this.selects = SJEL.$T(this.wr, "SELECT");

    for (var i = 0; i < this.inputs.length; i++)
        if ((this.inputs[i].type == "text") || (this.inputs[i].type == "password"))
            this.set_events(this.inputs[i]);

    for (var i = 0; i < this.selects.length; i++)
        this.set_events(this.selects[i]);
}

function show_box(_hide, _click) {
	var ref = this;
	this.h = SJEL.$(_hide);
	this.c = SJEL.$(_click);
	this.v = true;

	if ((this.h == null) || (this.c == null)) {
		alert('show_box error');
		return;
	}

	this.Show = function(_vis) {
		this.v = _vis;
		if (!this.v) {
			SJEL.SStyle(this.h, {display: "none"});
			if (!SJEL.ClassExists(this.c.parentNode, "hidenos"))
				SJEL.AddClass(this.c.parentNode, "hidenos");
		} else {
			SJEL.SStyle(this.h, {display: "block"});
			SJEL.RemoveClass(this.c.parentNode, "hidenos");
		}
	}

	this.Show(this.v);

	this.c.onclick = function() {
		if (this.checked) {
			SJEL.SStyle(ref.h, {display: "block"});
			SJEL.RemoveClass(this.parentNode, "hidenos");
		} else {
			SJEL.SStyle(ref.h, {display: "none"});
			if (!SJEL.ClassExists(this.parentNode, "hidenos"))
				SJEL.AddClass(this.parentNode, "hidenos");
		}
	}
}

function open_close_project(_opened) {
	var ref = this;
	this.opened = _opened || false;
	this.open = SJEL.$("project_open");
	this.close = SJEL.$("project_close");
	this.prb = SJEL.$("project_box");
	this.prbHeight = this.prb.offsetHeight;
	this.pr = SJEL.$("project");
	this.prHeight = this.pr.offsetHeight;
	this.pri = SJEL.$("project_in");
	this.priHeight = this.pri.offsetHeight;
	this.max = 254;
	this.speed = 250;
	this.morph = new SJEL.Morph();
	
	if (this.opened) {
		SJEL.SStyle(ref.prb, {height: ref.prbHeight + this.max + "px"});
		SJEL.SStyle(ref.pr, {height: ref.prHeight + this.max + "px"});
		SJEL.SStyle(ref.pri, {height: ref.priHeight + this.max + "px"});
	}
	
	this.show = function(_dt) {
		if (ref.opened)
			return;
			
		var h = _dt * ref.max;
		SJEL.SStyle(ref.prb, {height: ref.prbHeight + h + "px"});
		SJEL.SStyle(ref.pr, {height: ref.prHeight + h + "px"});
		SJEL.SStyle(ref.pri, {height: ref.priHeight + h + "px"});
	}

	this.hide = function(_dt) {
		var h = (1.0 - _dt) * ref.max;
		SJEL.SStyle(ref.prb, {height: ref.prbHeight + h + "px"});
		SJEL.SStyle(ref.pr, {height: ref.prHeight + h + "px"});
		SJEL.SStyle(ref.pri, {height: ref.priHeight + h + "px"});
	}

	this.open.onclick = function() {
		ref.morph.Init(ref.show, null, ref.speed);
		ref.morph.Morph();
		ref.morph.OnMorphFinished(function(){ref.opened = true});
	}	

	this.close.onclick = function() {
		ref.morph.Init(ref.hide, null, ref.speed);
		ref.morph.Morph();
		ref.morph.OnMorphFinished(function(){ref.opened = false});
	}
}

function add_reg_tooltip(_elId, _butClass, _infoClass, _tooltip) {
	this.el = SJEL.$(_elId);
	if (!this.el)
		return;
		
	this.buts = SJEL.$A(this.el, "class", _butClass);
	this.infos = SJEL.$A(this.el, "class", _infoClass);
	
	if (this.buts.length != this.infos.length) {
		alert("add_reg_tooltip error: buts.length != infos.length");
		return;
	}
	
	for (var i = 0; i < this.buts.length; i++) {
		new _tooltip(this.buts[i], this.infos[i], "right", -10);
	}
}
