//Menu
var selectedButton = null;
var showedMenu = new Array(10);
var overMenuId = "";
var iTimerID = -1;

function ShowMenu(source, topMenu, level) {
    //if (!topMenu) window.alert(source.id);
    var menuId = ((source.id.substring(0, 3) == "btn") ? "mn" + source.id.substring(3) : source.id);
    var menu = document.getElementById(menuId);
    HideMenu2(level);
    if (menu == null) return;
    var obj = source;
    // zobrazene menu jiz nezobrazovat

    overMenuId = menuId;
    if (overMenuId == source.id)
        return;

    // skryti menu zobrazenych menu
    var tmpObj;
    var tmpArray = new Array();

    if (obj != null) {
        var left = 0;
        var top = 0;
        var flagHorizontal = false;
        while (obj != null) {
            left += obj.offsetLeft;
            top += obj.offsetTop;
            obj = obj.offsetParent;
        }

        if (left + source.offsetWidth + menu.offsetWidth > document.body.offsetWidth) {
            left = left - menu.offsetWidth + source.offsetWidth + 1;
        }
        else {
            left = left + ((topMenu) ? 0 : source.offsetWidth + 0) + 1
        }

        if (level > 0) {
            left = left - 0;
        }
        mnWidth = menu.offsetWidth;
        left += 0;
        menu.style.left = left + 'px'; // + ((topMenu)?0:source.offsetWidth+3) + 1;
        menu.style.top = top + 'px';
        if (source.attributes['hv'] != null)
            if (source.attributes['hv'].nodeValue == 'h')
            flagHorizontal = true;
        if (flagHorizontal)
            menu.style.left = (left + source.offsetWidth + 0) + 'px';
        else
            menu.style.top = (top + ((topMenu) ? source.offsetHeight + 3 : 0)) + 'px';
        menu.style.visibility = '';
        menu.style.width = mnWidth;
    }
    //window.alert(source.style.paddingLeft)
    //window.alert(left);
    //window.alert(menu.style.left);
    showedMenu[level] = menu;

    //	source.className = "selected";
    try {
        for (i = 0; i < source.parentElement.parentElement.cells.length; i++)
            source.parentElement.parentElement.cells[i].className = "selected";
    }
    catch (e) { }
}
//*********************************************************************************************
//*********************************************************************************************
function HideMenu(source, hide) {
    var menuId = ((source.id.substring(0, 3) == "btn") ? "mn" + source.id.substring(3) : source.id);
    var menu = document.getElementById(menuId);
    if (menu == null) return;
    var tmpArray = new Array();

    AddMessage('SetTimeOut');
    window.clearTimeout(iTimerID);
    iTimerID = window.setTimeout("HideMenu2(0);", 1000);
    //window.alert(iTimerID);
}

function HideMenu2(toLevel) {
    var tmpObj;
    var am = new Array(10);
    if (iTimerID != -1) {
        window.clearTimeout(iTimerID);
        AddMessage('TimeOut vynulovan.' + iTimerID);
    }
    AddMessage('Hide ' + toLevel);
    for (i = showedMenu.length - 1; i >= 0; i--) {
        tmpObj = showedMenu[i];
        if (tmpObj != null) {
            if (i >= toLevel) {
                tmpObj.style.visibility = 'hidden';
                tmpObj.className = "";
            }
            else {
                am[i] = tmpObj;
                //window.alert(tmpObj.id);
            }
        }
    }
    showedMenu = am;
}

function AddMessage(s) {
    return;
    var o = document.getElementById('xxx');
    o.value = s + '\n\r' + o.value;
}

function SetRowSelected(row, selected) {
    //	row.className = ((selected)?'selected':'');
    for (i = 0; i < row.cells.length; i++) {
        row.cells[i].className = ((selected) ? 'selected' : '');
    }
}
function getLeft(o) {

    if (o == null) {
        return 0;
    }
    else {
        return o.offsetLeft + getLeft(o.offsetParent);
    }
}
function getTop(o) {
    if (o == null)
        return 0;
    else
        return o.offsetTop + getTop(o.offsetParent);
}

function igmenu_getTopPos(element) {
    var y = 0;
    var parent = element;
    while (parent != null) {
        if (parent.tagName == "BODY")
            break;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return y;
}

function igmenu_getLeftPos(element) {
    var x = 0;
    var parent = element;
    while (parent != null) {
        if (parent.tagName == "BODY")
            break;
        x += parent.offsetLeft;
        parent = parent.offsetParent;
    }
    return x;
}

function f_clientWidth() {
    return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
    return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
    return f_filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
    return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

var ShowPanelPriceQtyPrev = null;
function ShowPanelPriceQty(pButton, pID, pAlign) {
    var p = document.getElementById(pID);
    if (p == null)
        return;

    var pTop = 0;
    var pLeft = 0;
    
    p.style.display = (p.style.display == 'none' ? '' : 'none');
    var y = getTop(pButton);
    //p.style.top = igmenu_getTopPos(pButton); p.style.left = igmenu_getLeftPos(pButton) + pButton.offsetWidth + 10;
    pTop = y + pButton.offsetHeight + 10;
    //p.style.left = igmenu_getLeftPos(pButton);
    if (pAlign == 'right')
        pLeft = getLeft(pButton) - (p.offsetWidth - pButton.offsetWidth) + 2;
    else
        pLeft = getLeft(pButton);

    var scrt = f_scrollTop();
    var docheight = f_clientHeight();

    if (y - scrt > 400) {
        pTop = y - p.offsetHeight - 10;
    }

    p.style.left = pLeft + 'px';
    p.style.top = pTop + 'px';


    if (ShowPanelPriceQtyPrev != null && ShowPanelPriceQtyPrev != p) {
        ShowPanelPriceQtyPrev.style.display = 'none';
    }

    ShowPanelPriceQtyPrev = p;
}
function ChoosePriceQty(pAnchor, pName) 
{
    var q = document.getElementsByName(pName);
    if (q == null)
        return;
    if (q.length == 0)
        return;

    q.item(0).value = pAnchor.innerHTML;
    if (ShowPanelPriceQtyPrev != null)
        ShowPanelPriceQtyPrev.style.display = 'none';
}
var menuImg = '';  
var flagIni = false;
var level;
function menumini(pImg, pArray)
{
  menuImg = pImg;
  flagIni = false;
  level = pArray;    
  itemini(document.getElementById('panelmenu'));
}
//*********************************************************
function itemini(obj)
{
	if (obj == null)	return;
	if (obj.tagName == null) return;
	var i=0;
	if (obj.tagName.toUpperCase() == 'DIV')
		for(i=0;i<obj.childNodes.length;i++)
			itemini(obj.childNodes[i]);
	if (obj.tagName.toUpperCase() == 'A')
	{
		if(obj.href == '' || obj.href == null)
			obj.href="#";
	}
}
//*********************************************************
function itemimage(iid, iimg)
{
	var opm = document.getElementById('x' + iid);
	if (opm == null) return;
	
	var pm = menuImg + iimg;
	opm.src = pm;
}
//*********************************************************
function itemopen(iid, ilevel)
{
	var o = document.getElementById(iid);
	var oam = document.getElementById('menuactive');
	
	if (oam.value == '') oam.value = ';';
	oam.value = oam.value + iid + ';';
	
	if (o == null) return;
	itemimage(iid, 'im1.gif');
	itemclose(level[ilevel], ilevel);
	
	level[ilevel] = iid;
}
//*********************************************************
function itemclose(iid, ilevel)
{
	var o = document.getElementById(iid);
	if  (o == null) return;
	var oam = document.getElementById('menuactive');
	
	if (oam.value == '') oam.value = ';';
	
	oam.value = oam.value.replace(';' + iid + ';', ';');
	oam.value = oam.value.replace(';;', ';');
	
	//window.alert(iid);
	o.style.display = 'none';
	
	itemimage(iid, 'im0.gif');
	level[ilevel] = '';
}
//*********************************************************
function itemurl(oh, iid) {
  if (flagIni) {
    oh.href = '#null';
    return;
  }
  if (oh == null) {
    return false;
  }
  //window.alert(oh.attributes["href"].value);
  var am = '';
  var url = '/productlist.aspx?pix_id=' + iid.substring(2, iid.length);
  var oam = document.getElementById('menuactive');
  if (oh.href.substring(oh.href.length - 5, oh.href.length) != '#null') {
    var oAttrib = oh.attributes["spec"];
    if (oAttrib != null)
      url += '&spec=' + oAttrib.nodeValue;
    //oh.href += '&menuactive=' + oam.value;
    //url += '&menuactive=' + oam.value;
    url += '&menuactive=;'
    for (i = 0; i < 10; i++) 
      if (level[i] != '') 
        am += level[i] + ";";
    url += am;
    flagIni = true;
  }
  if (oh.attributes['href'].value == '#')
  {
    oh.href = url;
  }
  //setCookie('activemenu', am, 1);
  //window.alert(url);
}
//*********************************************************
function smi(oh, iid, bClose, ilevel) {
  var o = document.getElementById(iid);
  if (o == null) {
    //window.alert(oh.outerText);
    //itemopen(iid, ilevel);
    itemurl(oh, iid)
    return false;
  }
  //return;
  if (!bClose && o.style.display == '')
    return;

  o.style.display = (o.style.display == '' ? 'none' : '');
  var oam = document.getElementById('menuactive');
  if (oam.value == '')
    oam.value = ';';
  if (o.style.display == 'none') {
    itemclose(iid, ilevel);
  }
  else {
    itemopen(iid, ilevel);
  }
  return false;
}
//*********************************************************
function setCookie(c_name, value, expiredays) 
  {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  var val = c_name + "=" + escape(value);
  if (expiredays != null)  
    val += ";expires=" + exdate.toUTCString();

  document.cookie = val;
}
//***********************************************************
//************************ NAVIGATOR ************************
//***********************************************************
var oprev = null;
function showitematr(oclick)
{
	
	if (oprev != null && oprev != oclick)
	{
		ChangeItem(oprev, true);
	}
	ChangeItem(oclick, false);
	oprev = oclick;
	return false;
}
function ChangeItem(obj, bClose)
{
	var r;
	var tb = obj.parentNode;
	var rvisible = '';
	while (tb.tagName.toUpperCase() != 'TABLE')
	{
		tb = tb.parentNode;
	}
	for(i=0;i<tb.rows.length;i++)
	{
		r = tb.rows[i];
		rvisible = (r.style.display == 'none' && !bClose ? '' : 'none');
		
		if (TestSelected(r))
			rvisible = '';
		r.style.display = rvisible;
		//window.alert(r.tagName);
	}
}
function TestSelected(row) {
  var ol = row.getElementsByTagName('INPUT');
  if (ol == null)
    return false;
  if (ol.length == 0)
    return false;

  if (ol[0].checked == true)
    return true;

  return false;
}

function getAbsolutePath() {
  return document.getElementById('ApplicationRoot').href;
}
function FindRowsSetFocus() {
  var ff = document.getElementById('ffind');
  if (ff != null) {
    ff.focus();
  }
}
FindRowsSetFocus();

function FindRows(ptext, ptable, pfrom, pstep) {
  var t = document.getElementById(ptable);
  var r;
  var f = ptext.value.split(" ");
  var fi = "";
  var flag1 = false;
  var flag2 = false;
  for (i = pfrom; i < t.rows.length; i = i + pstep) {
    flag1 = true;
    for (j = 0; j < f.length; j++) {
      fi = f[j].toUpperCase();
      if (fi != "") {
        flag2 = false;
        for (istep = i; istep < t.rows.length && istep < i + pstep; istep++) {
          r = t.rows[istep];
          if (r.innerHTML.toUpperCase().indexOf(fi) >= 0)
            flag2 = true;
        }
        if (!flag2)
          flag1 = false;
      }

    }
    for (istep = i; istep < t.rows.length && istep < i + pstep; istep++)
      t.rows[istep].style.display = (flag1 ? '' : 'none');
  }
}
//***********************************************************
//************************ BASKET ***************************
//***********************************************************

function AddToBasket(pobj, ppro_id) {
  if (document.getElementById('panelbasket') == null)
    return true;

  var x = new pxrequest();
  x.loader = new pxwindow();
  x.loader.winelement = document.getElementById('imgloader');
  x.onload = ShowBasket;

  x.getData(getAbsolutePath() + '/servisx.aspx?cls=basket&pro_id=' + ppro_id + '&qty=1');
  return false;
}
function CopyNodeById(pFrom, pTo) {
  var elf = document.getElementById(pFrom);
  var elt = document.getElementById(pTo);

  if (elf == null || elt == null)
    elt.innerHTML = '';
  else
    elt.innerHTML = elf.innerHTML;
}
function ShowWindowById(pMsg) {
  var elw = new pxwindow();
  elw.winelement = document.getElementById('wwindow');
  CopyNodeById(pMsg, 'wwindowbody');
  elw.open();
  elw.movepercentage(50, 50);
}
function ShowBasket(pj) {
  var el = document.getElementById('panelbasket');

  if (el == null)
    return;
  el.innerHTML = pj.request.responseText;

  ShowWindowById('msgx');
}

//***********************************************************
//********************* IMG GALLERY *************************
//***********************************************************
function ImgGallery(ppro_id, pindex) {
  var x = new pxrequest();
  x.loader = new pxwindow();
  x.imgdefault = pindex;
  x.loader.winelement = document.getElementById('imgloader');
  x.onload = ImgGalleryShow;
  x.getData(getAbsolutePath() + '/servisx.aspx?cls=imggallery&pro_id=' + ppro_id);
  return false;
}
function ImgGalleryShow(pj) {
  var w = ShowWindowByRequest(pj);
  w.onkeydown = ImgGalleryNextImage;

  ImgGalleryChange(document.getElementById('imga' + pj.imgdefault));
  ImgGalleryNextImage(null, 0);
}
function ImgGalleryChange(pimg) {
  if (pimg == null)
    return;
  document.getElementById('imgbig').src = pimg.attributes['srcbig'].nodeValue; 
  ImgGalleryNextImage(null, 0);
}
function ImgGalleryNextImage(ev, key) {
  var imgmain = document.getElementById('imgbig');

  if (imgmain == null)
    return;


  var img = null; var img1 = null; var img2 = null; var img3 = null;
  for (i = 0; i < imgmain.parentNode.childNodes.length; i++) {
    img = imgmain.parentNode.childNodes.item(i);
    if (!img.attributes)
      continue;

    img.style.border = '1px solid white';
    if (img.attributes && img.attributes['id'] != null && img.attributes['id'].nodeValue.substr(0, 4) == 'imga') {
      if (img2 != null && img3 == null)
        img3 = img;


      if (imgmain.attributes['src'].nodeValue.indexOf(img.attributes['srcbig'].nodeValue) >= 0)
        img2 = img;

      if (img2 == null)
        img1 = img;


    }
  }
  if (ev != null) {
    if (img3 != null && key == 39) {
      imgmain.src = img3.attributes['srcbig'].nodeValue;
      img2 = img3;
    }
    if (img1 != null && key == 37) {
      imgmain.src = img1.attributes['srcbig'].nodeValue;
      img2 = img1;
    }
  }
  if (img2 != null)
    img2.style.border = '1px dotted #d3d3d3';

}

//***********************************************************
//************************** AJAX ***************************
//***********************************************************
function ShowWindowByRequest(pj) {
  var elw = new pxwindow();

  elw.winelement_set(document.getElementById('wwindow'));

  document.getElementById('wwindowbody').innerHTML = pj.request.responseText; ;
  
  elw.open();
  elw.movepercentage(50, 50);

  return elw;
}
