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 - 2;
	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 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, _title) {
	var email = formular.email.value;
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return true;

	xmlhttp.open('GET', '/index.php?ma=ajax&sid=aemail&p=' + profile + '&l=' + language + '&e=' + email);
	xmlhttp.onreadystatechange = function() {
		anonym_email_r(xmlhttp, _title);
	};

	xmlhttp.send(content);

	return false;
}

function anonym_email_r(xmlhttp, _title) {
	if (xmlhttp.readyState == 4) {
		var message = xmlhttp.responseXML.getElementsByTagName('text')[0].firstChild.data
		var status = xmlhttp.responseXML.getElementsByTagName('status')[0].firstChild.data

		SWindow.Open(505, 0, message, 'message_win ' + (status == 1 ? 'swin_good' : 'swin_bad'), _title);
	}
}

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, "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, "arr");
			SJEL.AddClass(this, "arr2");
			this.ref.Close();
		}
	}

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

function add_messages(_id, _classes) {
	var messa = SJEL.$(_id);
	SWindow.Open(505, 0, messa, 'message_win' + _classes);
}

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 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 ajaxLogin(_sl, _t) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	if (!xmlhttp)
		return true;

	xmlhttp.open('GET', '/index.php?ma=ajax&sid=login&link=' + _sl);
	xmlhttp.onreadystatechange = function() {
		ajaxLoginShowBox(xmlhttp, _t);
	};

	xmlhttp.send();

	return false;
}

function ajaxLoginShowBox(_x, _t) {
	if (_x.readyState == 4) {
		SWindow.Open(505, 0, _x.responseText, '', _t);
	}
}

function createSuperfilterToolbar(_open, _opentext, _closetext) {
	var main = $('#sfblock');
	var table = $('#sftable');

	if (!_open)
		table.css({display: 'none'});

	var a = $('<a>').appendTo(main);
	a.attr('href', '#');
	a.text(_open ? _closetext : _opentext);
	a.css({position: 'absolute', right: '8px', bottom: '4px'});

	var isopened = !_open;

	a.bind('click', function(event) {
		event.preventDefault();
		table.toggle();

		a.text(isopened ? _closetext : _opentext);

		isopened = !isopened;
	});

	/*block.appendChild(a);
	main.appendChild(a);*/
}

function createBrandFilterToolbar(_open, _opentext, _closetext) {
	var main = $('#brandbox');
	var box = $('#closebox', main);
	var div = $('<div>').appendTo(main);
	div.css({'text-align': 'right', 'margin-top' : '5px'});

	if (!_open)
		box.css({display: 'none'});

	var a = $('<a>').appendTo(div);
	a.attr('href', '#');
	a.text(_open ? _closetext : _opentext);

	var isopened = !_open;

	a.bind('click', function(event) {
		event.preventDefault();
		box.toggle();

		a.text(isopened ? _closetext : _opentext);

		isopened = !isopened;
	});
}

function removeTextInInputField() {
	var length = arguments.length;
	var obj;

	for (var n = 0; n < length; n++) {
		$(arguments[n]).bind('focus', function(){this.value = ''; $(this).unbind('focus')});
	}
}

(function($) {
$.fn.jMenuSelector = function(o) {
    var o = $.extend({
        link: null,
        openClass: '',
        menu: null
    }, o || {});

    return this.each(function() {
		if (o.menu == null || o.link == null)
			return;

        var obj = $(this);
        var menu = $(o.menu, obj);
        var link = $(o.link, obj);
        var isopen = true;

        if (o.openClass != '' && menu.hasClass(o.openClass))
        	isopen = false;

        link.css({'cursor': 'pointer', 'padding-left': '20px', 'background': 'url(\'/images/menu/' + (isopen ? 'minus' : 'plus') + '.gif\') 3px 50% no-repeat'});

        if (!isopen)
        	menu.css({'display': 'none'});

        link.bind('click', function() {
        	menu.slideToggle();

        	isopen = !isopen;

        	link.css({'background': 'url(\'/images/menu/' + (isopen ? 'minus' : 'plus') + '.gif\') 3px 50% no-repeat'});
        });
    });
};
})(jQuery);
