var time_to_hide_submenu = 0;
var no_shop_selected = true;


function redirectToShop(url) {
// 	alert("url: "+ url);
	window.location.href = url;
}



function incKategorieMenu() {
    if (!document.getElementById("kategorie_menu_table"))
        return false;
    
    var menu_table = document.getElementById("kategorie_menu_table");
    var menu_items = menu_table.getElementsByTagName("li");
    
    for (var i = 0; i < menu_items.length; i++) {
        if (menu_items[i].className != "between") {
            
            menu_items[i].onmouseover = function() {
                clearTimeout(time_to_hide_submenu);
                hideAllSubmenu();
                
                var submenu_num = this.id.substr(4);
                addClass(this, "active");
                
                if (submenu_num == "0" && !no_shop_selected) {
                    showSubmenu(submenu_num, true);
                } else if (submenu_num != "0") {
                    showSubmenu(submenu_num, true);
                }
                
                if (submenu_num == "0") {
                    this.style.textDecoration = "underline";
                }
                
                if ( (submenu_num == "0" && !no_shop_selected) || submenu_num != "0" ) {
                    
//                     $('#content_obchod').css("opacity", 0.4);
//                     $('#obchod_list').css("opacity", 0.4);
                    $('#content_obchod').stop(true, false).animate({ opacity: 0.3 }, 200);
                    $('#obchod_list').stop(true, false).animate({ opacity: 0.3 }, 200);
                }
                
                
            }
            menu_items[i].onmouseout = function() {
                var submenu_num = this.id.substr(4);
                removeClass(this, "active");
                showSubmenu(submenu_num, false);

                if (submenu_num == "0") {
                    this.style.textDecoration = "none";
                }

//                 $('#content_obchod').css("opacity", 1);
//                 $('#obchod_list').css("opacity", 1);
                $('#content_obchod').animate({ opacity: 1 }, 200, function(){ repairFont("content_obchod"); });
                $('#obchod_list').animate({ opacity: 1 }, 200, function(){ repairFont("obchod_list"); });

            }
        }
    }
    
    setSubmenu(document.getElementById("obchod_list"));
    
}



function showSubmenu(submenu_num, show) {
    
    if (document.getElementById("kat_submenu_"+ submenu_num)) {
        var kat_item = document.getElementById("kat_"+ submenu_num);
        var submenu = document.getElementById("kat_submenu_"+ submenu_num);
        
        setSubmenu(submenu);
        
        submenu.onmouseover = function() {
            clearTimeout(time_to_hide_submenu);

            this.style.display = "block";
            addClass(kat_item, "active");

//             $('#content_obchod').css("opacity", 0.4);
//             $('#obchod_list').css("opacity", 0.4);
            $('#content_obchod').stop(true, false).animate({ opacity: 0.3 }, 200);
            $('#obchod_list').stop(true, false).animate({ opacity: 0.3 }, 200);

        }
        submenu.onmouseout = function() {
            var elem = this;
            
            time_to_hide_submenu = setTimeout(function(){
                elem.style.display = "none";
                removeClass(kat_item, "active");
                
                var kats_active = getElementsByClass("active", document.getElementById("kategorie_menu_table"), "td");
                
                for (var i = 0; i < kats_active.length; i++) {
                    removeClass(kats_active[i], "active");
                }
                
//                 $('#content_obchod').css("opacity", 1);
//                 $('#obchod_list').css("opacity", 1);
                $('#content_obchod').animate({ opacity: 1 }, 200, function(){ repairFont("content_obchod"); });
                $('#obchod_list').animate({ opacity: 1 }, 200, function(){ repairFont("obchod_list"); });
                
            }, 50);
        }
        
        if (show) {
            submenu.style.display = "block";
        } else {
            submenu.style.display = "none";
        }
        
    }
    
}


function setSubmenu(submenu_elem) {
    var items = submenu_elem.getElementsByTagName("span");
    
    for (var i = 0; i < items.length; i++) {
        if (items[i].className != "title") {
            items[i].onmouseover = function() {
                this.style.textDecoration = "underline";
            }
            items[i].onmouseout = function() {
                this.style.textDecoration = "none";
            }

            items[i].onclick = function() {
                var obchod_id = this.id.substr(7);
                
                showShop(obchod_id, true);
            }

        }
    }
    
}


function hideAllSubmenu() {


    // skryjeme všechny divy se submenu
    var shop_guide = document.getElementById("shop_guide");
    var allSubMenu = getElementsByClass("kategorie_submenu", shop_guide, "div");
    
    for (var i = 0; i < allSubMenu.length; i++) {
        allSubMenu[i].style.display = "none";
    }
    
    
    // nastavíme položky menu na neaktivní
    var menu_table = document.getElementById("kategorie_menu_table");
    var menu_items = menu_table.getElementsByTagName("li");
    
    for (var i = 0; i < menu_items.length; i++) {
        if (menu_items[i].className != "between") {
            removeClass(menu_items[i], "active");
        }
    }
    
    
}


function incSearchBox() {
    var search_box = document.getElementById("obchody_search");
    var input = search_box.getElementsByTagName("input")[0];
    var button = search_box.getElementsByTagName("img")[0];
    
    input.onkeyup = function(e) {
      	if (!e) e = window.event;
      	
      	if (e.keyCode == 13)
            searchShop(input.value);
    }

    button.onclick = function() {
        searchShop(input.value);
    }
    
}

function searchShop(search_string, modify_flash, after_search) {

	if (typeof modify_flash == "undefined") {
		modify_flash = true;
	}

    ajax("/templates/ajax.search_obchod.php", "word="+ search_string, function(response, http){
        
        var obchod_list = document.getElementById("obchod_list");
        var content_obchod = document.getElementById("content_obchod");
        
        var result = eval("("+ response +")");
        
        if (result.rows == 1) {
            content_obchod.style.display = "block";
            obchod_list.style.display = "none";
            
            if (modify_flash) {
	            showShop(result.obchod_id, true);
			}
            
            no_shop_selected = true;

        } else {
            content_obchod.style.display = "none";
            obchod_list.style.display = "block";
            
//             alert("1");
            
            obchod_list.innerHTML = result.html;
            
//             alert("2");

            if (modify_flash) {
// 	            alert("3");
            	showAllFloorsOnMap();
            }
            
            no_shop_selected = false;
        }

//         alert("4");
        
        setSubmenu(obchod_list);
        
		if (typeof after_search != "undefined") {
			after_search();
		}
    });
}


function showShopsFor(floor) {
	searchShop("all_shops_"+ floor, false, function(){
// 		$('#obchod_list td:not(.selected)').animate({ opacity: 0.5 }, 200, function(){ repairFont("obchod_list"); });
		$('#obchod_list td:not(.selected) ul li').css("font-weight", "normal");
	});
	
}


function selectShopOnMap(patro, obchod_id) {
    if (obchod_id.indexOf(".") != false) {
        obchod_id = obchod_id.replace(".", "_");
    }

    var flash_element = $.browser.msie ? window["obchody_mapa"] : document["obchody_mapa"];
    
    if (flash_element)
        flash_element.showShop(patro, obchod_id);
}


function showAllFloorsOnMap() {

    var flash_element = $.browser.msie ? window["obchody_mapa"] : document["obchody_mapa"];
    
    if (flash_element)
        flash_element.showFloors();
}


function showShop(obchod_id, selectOnMap) {
    if (typeof selectOnMap == "undefined")
        selectOnMap = false;
    
    
    if (obchod_id.indexOf("_") != false) {
        obchod_id = obchod_id.replace("_", ".");
    }
    
    
    hideAllSubmenu()

    $('#content_obchod').animate({
        opacity: 0
    }, 200, function() {
        loadShop(obchod_id);
    });

    var loadShop = function(obchod_id) {
    
        var httpp = getHTTPObject();
    
        httpp.onreadystatechange = function() {
          if (httpp.readyState == 4) {
              var obchod = eval("("+ httpp.responseText +")");
              
              if (!obchod) {
                  document.getElementById("obchod_list").style.display = "block";
                  document.getElementById("content_obchod").style.display = "none";
                  
                  document.getElementById("obchod_list").innerHTML = "<div style='padding: 20px;'>Zde není žádný obchod.</div>";
                  
                  return false;
              }
              
              document.getElementById("obchod_list").style.display = "none";
              document.getElementById("content_obchod").style.display = "block";

              var content_obchod_image = document.getElementById("content_obchod_image");

              if (obchod.logo != "" && obchod.logo != null) {
                  content_obchod_image.style.display = "block";
                  content_obchod_image.src = "/"+ obchod.logo;
              } else {
                  content_obchod_image.style.display = "none";
              }

              document.getElementById("content_obchod_title").innerHTML = obchod.nazev;
              document.getElementById("content_obchod_text").innerHTML = obchod.sortiment;
              document.getElementById("content_obchod_opening").innerHTML = obchod.oteviraci_doba;
              document.getElementById("content_obchod_floor").innerHTML = obchod.nazev_patra;
              document.getElementById("content_obchod_contact").innerHTML = obchod.kontakt;

              document.getElementById("vernostni_program").style.display = "none";
              document.getElementById("virtualni_prohlidka").style.display = "none";
              document.getElementById("virtualni_prohlidka_flash").innerHTML = "";

              if (parseInt(obchod.virtualni_prohlidka_zobrazit) == 1) {
//                   document.getElementById("button_virtualni_prohlidka").innerHTML = '<img src="/images/flash_obchody/button_virtualni-prohlidka.gif" alt="" id="button_virtualni-prohlidka" />';
                  document.getElementById("button_virtualni_prohlidka").style.display = "block";
              } else {
                  document.getElementById("button_virtualni_prohlidka").style.display = "none";
              }

              
              $('#content_obchod').animate({ opacity: 1 }, 200, function() {
                  repairFont("content_obchod");
                  
                  if (obchod.vernostni_program == 1) {  // zobrazíme ikonu vernostního programu
                      $('#vernostni_program').slideDown(700);
                  }
                  
                  if (selectOnMap) {
                      selectShopOnMap(obchod.id_patra, obchod.cislo_obchodu);
                  }
                  
                  if (parseInt(obchod.virtualni_prohlidka_zobrazit) == 1) { // nastavíme button virtuální prohlídky
                      var button = document.getElementById("button_virtualni_prohlidka");
                      
                      button.onclick = function() {
                          
                          var flash_object = '  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="PurePlayer" width="600" height="435" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab#version=9,0,0,0"><param name="movie" value="/virtualni-prohlidka/PurePlayer.swf" /><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="flashvars" value="panorama=/virtualni-prohlidka/'+ obchod.virtualni_prohlidka +'.ivp" /><embed src="/virtualni-prohlidka/PurePlayer.swf" width="600" height="435" name="PurePlayer" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" flashvars="panorama=/virtualni-prohlidka/'+ obchod.virtualni_prohlidka +'.ivp" pluginspage="http://www.adobe.com/go/getflashplayer"></embed></object>';
                          
                          var fo = new FlashObject("/virtualni-prohlidka/PurePlayer.swf", "virtualni_prohlidka_flash", "600", "435", 9);
                          if (fo.installedVer.major >= 9){

                              document.getElementById("virtualni_prohlidka_flash").style.display = "block";
                              document.getElementById("virtualni_prohlidka_noflash").style.display = "none";
                              document.getElementById("virtualni_prohlidka_table").style.display = "block";

//                               fo.addParam("allowScriptAccess", "sameDomain");
//                               fo.addParam("quality", "high");
//                               fo.addParam("devicefont", "true");
//                               fo.addParam("bgcolor", "#ffffff");
//                               fo.addParam("wmode", "transparent");
//                               fo.addParam("movie", "PurePlayer.swf");
//                               fo.addParam("flashVars", "panorama=/virtualni-prohlidka/"+ obchod.virtualni_prohlidka +".ivp")
//                               fo.write("virtualni_prohlidka_flash");
                              
                              document.getElementById("virtualni_prohlidka_flash").innerHTML = flash_object;
                              
                          } else {
                              document.getElementById("virtualni_prohlidka_flash").style.display = "none";
                              document.getElementById("virtualni_prohlidka_noflash").style.display = "block";
                              document.getElementById("virtualni_prohlidka_table").style.display = "none";
                          }

                          $('#virtualni_prohlidka').slideDown(500);

                          var targetOffset = $("virtualni_prohlidka").offset().top;
                          
//                           alert(targetOffset);
//                           alert($("content_obchod").offset().top);
                          
                          $('html,body').animate({scrollTop: 2000}, 1000);

                          
                      }
                  }
                  
              });
              
              no_shop_selected = false;
              
          }
        }
    
        httpp.open('POST', "/templates/ajax.get_shop.php", true);
        httpp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        httpp.send("id="+obchod_id);
        
    }


    
}


function repairFont(elem_id) {
    if($.browser.msie) {  // v IE se po efektu zkostrbatí písmo, tímhle se zase vyhladí
        document.getElementById(elem_id).style.removeAttribute("filter", false);
    }
}


function ajax(script, vars, fn) {

    var httpp = getHTTPObject();

    httpp.onreadystatechange = function() {
      if (httpp.readyState == 4) {
          fn(httpp.responseText, httpp);
      }
    }

    httpp.open('POST', script, true);
    httpp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    httpp.send(vars);
}






// vrátí pole elementu nalezených podle class
function getElementsByClass( searchClass, domNode, tagName) {
  	if (domNode == null) domNode = document;
  	if (tagName == null) tagName = '*';
  	
    var el = new Array();
  	var tags = domNode.getElementsByTagName(tagName);
  	var tcl = " "+searchClass+" ";
  	
    for(i=0,j=0; i<tags.length; i++) {
    		var test = " " + tags[i].className + " ";
    		if (test.indexOf(tcl) != -1)
    			 el[j++] = tags[i];
  	}
  	
  	return el;
}


function targetBlank() {
    var elements = getElementsByClass("target_blank");
    
    for (var i = 0; i < elements.length; i++) {
        elements[i].onclick = function(e) {
            window.open(this.href);
            return preventDefaultAction(e);
        }
    }
}


function preventDefaultAction(e) {
  if (e && e.preventDefault) {
      e.preventDefault;
  } else {
      window.event.returnValue = false;
  }
  return false;
}



function ajaxExecute(idecko, script, vars, action) {
    vars = vars || null;

    var httpp = getHTTPObject();

    httpp.onreadystatechange = function() {
      if (httpp.readyState == 4) {
      
        if (idecko != "") {
            document.getElementById(idecko).innerHTML = httpp.responseText;
        } else {
            return httpp.responseText;
        }
        
        if (typeof action != "undefined") {
            return action();
        }

      }
    }
    
    httpp.open('POST', script, true);
    httpp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    httpp.send(vars);
    
    
}



// ajax object
function getHTTPObject() {
  var request = false;
    try {
      request = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (err2) {
      try {
        request = new ActiveXObject('Microsoft.XMLHTTP');
      }
      catch (err3) {
		    try {
          request = new XMLHttpRequest();
		    }
		    catch (err1) {
          request = false;
		    }
      }
    }
    return request;
}





function hasClass(ele,cls) {
	return ele.className.match( new RegExp('(\\s|^)'+cls+'(\\s|$)') );
}

function addClass(ele,cls) {
	if (!hasClass(ele,cls)) {
      ele.className += " "+cls;
  }
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className = ele.className.replace(reg,' ');
	}
}
