/**************************************
* Jonas Raoni Soares Silva
* http://www.joninhas.ath.cx
**************************************/

hitTest = function(o, l){
	function getOffset(o){
		for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
			o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
		return r.r += r.l, r.b += r.t, r;
	}
	for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i;
		b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
		&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
	return j ? !!r.length : r;
};


function get_left(obj,id)
{
	var tmp = id;
	var pos_left = 0;
	if (obj.offsetParent) { 
		while (obj.offsetParent) {
			pos_left += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else {
	  	if (obj.x) {
	  		pos_left += obj.x;
	  	}
	}
		
	if ((tmp.clientWidth + pos_left) >= (document.body.clientWidth + document.documentElement.scrollLeft)) {
		pos_left = ((document.body.clientWidth + document.documentElement.scrollLeft) - tmp.clientWidth - 5);
	}

  return pos_left;
}

function get_top(obj)
{
	var pos_top = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			pos_top += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else {
		if (obj.y) {
			pos_top += obj.y;
		}
	}
	
	return pos_top;
}

function highlight_head_line(state, id) {
	/* zvyrazni barevne boxik nad kterym mam mys */
	if (state && (state != '') && id && (id != '')) {
		var linka = document.getElementById('h_linka_div_' + id);
		if (linka) {
			if (state == 'on') {
				linka.className = 'h_linka h_linka_active_' + id;
			} else if (state == 'off') {
				linka.className = 'h_linka';
			}
		}
	}
}

function show_popup(obj, id, pics, url, title) {
	var tmp = document.getElementById(id);
	tmp.style.top = get_top(obj)+obj.offsetHeight-1+"px";
	tmp.style.left = get_left(obj,tmp)+"px";
	var tmplogo = document.getElementById(id + "_logo");
	var $target = "_self";

	if (pics && (pics!='')) {
		if ((pics=='22') || (pics=='29') || (pics=='30') || (pics=='32') || (pics=='35') || (pics=='42') || (pics=='44') || (pics=='46')) {
			$target = "_blank";
		}
		//if (pics=='32') {
			//if (title!='') {
				//tmplogo.innerHTML = '<a href="http://ad.123shop.cz/adclick.php?bannerid=445&zoneid=0&source=&dest=http://www.kralovna.cz/" target="_self"><img src="http://ad.123shop.cz/adimage.php?filename=logo_menu_32.gif&contenttype=gif" width="104" height="33" alt="" title="'+title+'" border="0" hspace="10" vspace="10"></a><div id="beacon_445" style="position: absolute; left: 0px; top: 0px; visibility: hidden;"><img src="http://ad.123shop.cz/adlog.php?bannerid=445&clientid=146&zoneid=0&source=&block=0&capping=0&cb=8e60d15184c829920a00769da3ef3076" width="0" height="0" alt="" style="width: 0px; height: 0px;"></div>';
			//} else {
				//tmplogo.innerHTML = '<a href="http://ad.123shop.cz/adclick.php?bannerid=445&zoneid=0&source=&dest=http://www.kralovna.cz/" target="_self"><img src="http://ad.123shop.cz/adimage.php?filename=logo_menu_32.gif&contenttype=gif" width="104" height="33" alt="" title="" border="0" hspace="10" vspace="10"></a><div id="beacon_445" style="position: absolute; left: 0px; top: 0px; visibility: hidden;"><img src="http://ad.123shop.cz/adlog.php?bannerid=445&clientid=146&zoneid=0&source=&block=0&capping=0&cb=8e60d15184c829920a00769da3ef3076" width="0" height="0" alt="" style="width: 0px; height: 0px;"></div>';
			//}
		//} else { 
			if (url!='') {
				//if (title!='') {
					//tmplogo.innerHTML = '<a href="'+url+'" title="'+title+'"><img src="http://img.123shop.cz/img/css/shops-loga/logo_menu_'+pics+'.gif" hspace="10" vspace="10"></a>';
				//} else {
					tmplogo.innerHTML = '<a href="'+url+'" target="'+$target+'"><img src="http://img.123shop.cz/img/css/shops-loga/logo_menu_'+pics+'.gif" hspace="10" vspace="10"></a>';
				//}
			} else {
				tmplogo.innerHTML = '<img src="http://img.123shop.cz/img/css/shops-loga/logo_menu_'+pics+'.gif" hspace="10" vspace="10">';
			}
		//}
	}
	tmp.style.visibility = "visible";

	tmp.style.display = "block";
	if(!tmp.ieFix)
		tmp.ieFix = hitTest(tmp, document.getElementsByTagName("select"));
	for(var i = tmp.ieFix.length; i; tmp.ieFix[--i].style.visibility = "hidden");
}

function hide_popup(id) {
	var tmp = document.getElementById(id);
	tmp.style.visibility = "hidden";

	tmp.style.display = "block";
	if(!tmp.ieFix)
		tmp.ieFix = hitTest(tmp, document.getElementsByTagName("select"));
	for(var i = tmp.ieFix.length; i; tmp.ieFix[--i].style.visibility = "visible");
}

function popup_over(obj) {
	obj.style.visibility = 'visible';
	if(!obj.ieFix)
		obj.ieFix = hitTest(obj, document.getElementsByTagName("select"));
	for(var i = obj.ieFix.length; i; obj.ieFix[--i].style.visibility = "hidden");
}

// fix, aby mi to neborilo design v hlavicce pri zmenseni okna
function changeResize() {
	/*var bar = document.getElementById('h_bar3');
	var box = document.getElementById('h_box');
	if (bar && box) {
		if (document.body.clientWidth > 780) {
			box.style.display = "block";
			bar.style.width = "460px";
		} else {
			box.style.display = "none";
			bar.style.width = "260px";
		}
	}*/
	if (p_popups) {
		var i = 0;
		for (i = 0; i < p_popups.length; i++) {
			var popup = p_popups[i];
			if (popup != "") {
				var tmp = document.getElementById(popup);
				if (tmp) {
					tmp.style.top = "10px";
					tmp.style.left = "0px";
				}
			}
		}
	}
}

// funkce pro donuceni zapsani poznamky - napr. pri ruseni polozky objednavky
function poznamka(url, vychozi, allownull) { 
  var p;
  var def_poznamka = vychozi;
  if (!vychozi) {
  	def_poznamka = '';
  }
  p = prompt("Zadejte poznámku: ", def_poznamka);
  
  if (p == null) {
    return false;
  } else if (p == "") {
  	if (!allownull || allownull == 'no') {
    	alert("Musíte zapsat poznámku!!!");
    	return false;
    } else {
    	// je povolena prazdna poznamka
    	adresa = url;
    	window.location = adresa;
    	return true;
    }
  } else {
    adresa = url;
    if (url.indexOf("?") == -1) {
      adresa = adresa + "?msg=" + p;
    } else {
      adresa = adresa.substring(0, url.indexOf("?") + 1) + "msg=" + p + "&" + adresa.substring(url.indexOf("?") + 1);
    }
    window.location = adresa;
    return false;
  }
}

//funkce pro donuceni zapsani poctu pridelenych ks zbozi
function pridelZbozi(url, povoleny_pocet_low, povoleny_pocet_high, vychozi, plusminus) { 
  var p;
  var def_poznamka = vychozi;
  if (!vychozi) {
  	def_poznamka = '0';
  }  
  p = prompt("Zadejte počet přidělovaných ks (" + povoleny_pocet_low + " - " + povoleny_pocet_high + "): ", def_poznamka);
  
  if (p == null) {
    return false;
  } else if (p == "") {
	alert("Musíte zadat počet přidělovaných ks (" + povoleny_pocet_low + " - " + povoleny_pocet_high + ")!!!");
	return false;
  } else if (isNaN(parseInt(p))) {
	alert("Počet přidělovaných ks musí být číslo!!!");
	return false;
  } else if ((parseInt(p) < parseInt(povoleny_pocet_low)) || (parseInt(p) > parseInt(povoleny_pocet_high))) { 
	alert("Musíte zadat počet přidělovaných ks v povoleném intervalu (" + povoleny_pocet_low + " - " + povoleny_pocet_high + ")!!!");
	return false;  
  } else {
    adresa = url;
    if (url.indexOf("?") == -1) {
      adresa = adresa + "?pridelks=" + p + "&msg=Požadavek na přidělení zboží (" + p + " ks)";
    } else {
      adresa = adresa.substring(0, url.indexOf("?") + 1) + "pridelks=" + p + "&msg=Požadavek na přidělení zboží (" + p + " ks)" + "&" + adresa.substring(url.indexOf("?") + 1);
    }
    window.location = adresa;
    return false;
  }
}

// funkce pro  uvery a pujcky menu
function changeLinkBack(el, state) {
	if (el) {
		if (state == 'on') {
			el.style.background = 'url("http://img.123shop.cz/img/css/uvery-pujcky/back_menu_links_sel.gif")';
		} else if (state == 'off') {
			el.style.background = 'url("http://img.123shop.cz/img/css/uvery-pujcky/back_menu_links.gif")';
		}
	}
	return true;
}

function showremarks(trow, vis) {
	if (trow) {
		var visible;
		if(navigator.appName == 'Microsoft Internet Explorer') { /* jen pro IE */
	    	visible = 'block';
		} else { /* ostatni prohlizece */
			visible = 'table-row';
		}     
	
		if(vis == 'off') {
	    	document.getElementById(trow).style.display = 'none';
		} else if(vis == 'on') {
	    	document.getElementById(trow).style.display = visible;
		}
	}
}

function showDiv(whichdiv, show) {
    var el = document.getElementById(whichdiv);
    if (el) {
        if (show) {
            el.style.display = 'block';
        } else {
            el.style.display = 'none';
        }
    }
}

function showHideDiv(source, whichdiv, editsourcetext) {
    var el = document.getElementById(whichdiv);
    if (el) {
        if (el.style.display != 'none') {
            showDiv(whichdiv, false);
            if (editsourcetext) {
            	source.innerHTML = '[&amp;nbsp;&gt;&amp;nbsp;]';
            }
        } else {
            showDiv(whichdiv, true);
            if (editsourcetext) {
            	source.innerHTML = '[&amp;nbsp;&lt;&amp;nbsp;]';
            }
        }
    }
}

// constants to define the title of the alert and button text.
var CUST_ALERT_BUTTON_TEXT = "Ok";

function createCustomAlert(title, txt) {
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H3 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h3"));
	h1.appendChild(d.createTextNode(title));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = txt;
	
	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(CUST_ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }
}

var CUST_OK_BUTTON_TEXT = "Ok";
var CUST_CLOSE_BUTTON_TEXT = "Zavřít";

function createObjPolDuvodyRollAlert(title, url, key, vychozi, allownull) {
	// shortcut reference to the document object
	var d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	var mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	var alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H3 element as the title bar
	var h3 = alertObj.appendChild(d.createElement("h3"));
	h3.appendChild(d.createTextNode(title));

	// create a paragraph element to contain the txt argument
	var msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = "Vyberte ze seznamu níže důvod provedení Vámi zvolené akce.<br /><br />";
	
	var form = alertObj.appendChild(d.createElement("form"));
	var sel = form.appendChild(d.createElement("select"));
	sel.setAttribute("id", "duvodyRollSelect");
	sel.setAttribute("style", "margin-left: 10px;");
	
	var option = d.createElement("option");
    option.text = "Vyberte jeden z důvodů: ";
    option.value = "-1";
    option.setAttribute("style", "font-weight: bold; background-color: #e0e0e0; border-bottom: 2px #404040 dashed;");
    try {
        sel.add(option, null); //Standard
    } catch(error) {
        sel.add(option); // IE only
    }
    
    var i;
    for (i = 0; i < objPolDuvodyRoll_array.length; i++) {
		if (objPolDuvodyRoll_array[i][1] == key) {
        	var dynoption = d.createElement("option");
		    dynoption.text = objPolDuvodyRoll_array[i][3];
		    dynoption.value = objPolDuvodyRoll_array[i][0];
		    try {
		        sel.add(dynoption, null); //Standard
		    } catch(error) {
		        sel.add(dynoption); // IE only
		    }
        }
    }
    	
	// create an anchor element to use as the confirmation button.
	var ok_btn = alertObj.appendChild(d.createElement("a"));
	ok_btn.id = "confirmBtn";
	ok_btn.setAttribute("style", "margin: 15px 5px 5px 520px; display: inline-block;");
	ok_btn.appendChild(d.createTextNode(CUST_OK_BUTTON_TEXT));
	ok_btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	ok_btn.onclick = function() { confirmCustomAlert(url, vychozi, allownull); }
	
	// create an anchor element to use as the storno button.
	var storno_btn = alertObj.appendChild(d.createElement("a"));
	storno_btn.id = "closeBtn";
	storno_btn.setAttribute("style", "margin: 15px 5px 5px 5px; display: inline-block;");
	storno_btn.appendChild(d.createTextNode(CUST_CLOSE_BUTTON_TEXT));
	storno_btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	storno_btn.onclick = function() { removeCustomAlert(); return false; }
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

function confirmCustomAlert(url, vychozi, allownull) {
	var duvodyRollSelect = document.getElementById("duvodyRollSelect");
	
	if (duvodyRollSelect) {
	  	if (!allownull || allownull == 'no') {
	  		var m = "", v = "-1", so = duvodyRollSelect.options[duvodyRollSelect.selectedIndex];
	  		
			m = so.text;
	  		v = so.value;
	  		
	  		if (v != "-1") {
	    		adresa = url;
			    if (url.indexOf("?") == -1) {
			    	adresa = adresa + "?msg=" + m + "&duvodId=" + encodeURIComponent(v);
			    } else {
			    	adresa = adresa.substring(0, url.indexOf("?") + 1) + "msg=" + m + "&duvodId=" + encodeURIComponent(v) + "&" + adresa.substring(url.indexOf("?") + 1);
			    }
			    
			    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
			    //alert("adresa: " + adresa);
			    window.location = adresa;
			    return false;
	  		} else {
	    		alert("Musíte zvolit důvod!!!");
	    		return false;
	    	} 
	    } else {
	    	// je povolena prazdna poznamka
	    	adresa = url;
	    	window.location = adresa;
	    	return true;
		}
	} else {
		// nejaky problem se selectem duvodu - zavolam klasickou funkci pro zadani poznamky
		document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
		return poznamka(url, vychozi, allownull);
	}
}


