var window_checking;
var tab_open = false;
var lastWindowHeight = 0;
var lastHash;

  /*****************************************************************************/
  document.observe("dom:loaded", function() {

    window_checking = setInterval("check_window_size()",100);
    if(navigator.appVersion.indexOf("MSIE 7.0")==-1) {
      $$(".menu a, .intro a, .myinfo a, .sleva_banner a").invoke("observe","click",function(e){load_tab(this);}).each(function(el){el.href=el.href.replace(/^(.*)\/([^\/]*)$/,"$1#$2");});
    }

    if(location.href.search(/#.+$/)!=-1){
      a = $$(".menu a").find(function(el){return (el.href.indexOf(location.hash)!=-1);})
      if(!a && location.hash.search(/^dum\//)){
        load_tab(location.hash.replace(/^#/,""));
      } else {
        load_tab(a);
      }
    } else {
      if(location.href.replace(location.protocol+"//"+location.host+"/","").replace(/^#/,"")==""){
        tab_open = false;
        anim = $$(".intro").first();
        new Effect.SlideDown(anim, {delay:1, duration:4, transition: Effect.Transitions.spring});
      } else {
        tab_open = true;
        $$(".stripe, .page_content").invoke("removeClassName","hidden");
        fix_scene_layout(0);
      }
    }

    $$("#libeznice4-header img").invoke("observe","click",function(e){
      tab_open = false;
      location.hash = "";
      $$(".stripe, .page_content").invoke("addClassName","hidden");
      fix_scene_layout(getWindowHeight());

      anim = $$(".intro").first();
      new Effect.SlideDown(anim, {delay:2, duration:2, transition: Effect.Transitions.spring});
    });
    
    if(sleva_element = $("velikonocni_sleva")){
      new Effect.Appear(sleva_element,{delay:1,duration:2,transition: Effect.Transitions.spring});
    }


  });

  /*****************************************************************************/
  function load_tab(a){
    $$(".menu a").invoke("removeClassName","active");
    if(typeof(a)=="object"){
      a.blur();
      a.addClassName("active");
      url = a.href.replace(/#/,"")+"?ajax";
      mylastHash = a.hash.replace(/^#/,"");
    } else
    if(typeof(a)=="string"){
      url = a.replace(/^#/,"") + "?ajax";
      mylastHash = a.replace(/^#/,"");
    } else {return false;}
    tab_open = true;
    $$(".intro").first().hide();
  
    fix_scene_layout(0);
    $$(".stripe, .page_content").invoke("removeClassName","hidden");
    $("tab_content").update("<img id='spinner' src='im/spinner.gif'>");

//     if(navigator.appVersion.indexOf("MSIE 7.0")!=-1) {
      new Ajax.Updater("tab_content",url,{evalScripts:true});
//     } else {
//       location.href = url.replace(/\?ajax/,"");
//     }
  

    lastHash = mylastHash;
    location.hash = lastHash;
//     console.log(lastHash+", "+location.hash);
  }

  /*****************************************************************************/
  function check_window_size(){
//     var currentWindowHeight = getWindowHeight();
//     if(currentWindowHeight != lastWindowHeight){
//       fix_scene_layout(currentWindowHeight);
//       lastWindowHeight = currentWindowHeight;
//     }
    var currentHash = location.hash.replace(/^#/,"");
    if(currentHash != lastHash && currentHash!=""){
      load_tab(currentHash);
      lastHash = currentHash;
    } 
  }
  
  /*****************************************************************************/
  function fix_scene_layout(window_height){
  if(location.search=="?dev" && tab_open) {return;}
    var tab_height = 80; 
    var max_top = 672 - tab_height;
    var min_top = 191;
      min_top = 110;
    if(tab_open){max_top = min_top;}

    offsets = $("front").cumulativeOffset();
    tabs_top = offsets[1];
    var new_tabs_top = window_height-tab_height;
    if(new_tabs_top < min_top){
      new_tabs_top = min_top;
    }
    if(new_tabs_top > max_top){
      new_tabs_top = max_top;
    }
    $("front").setStyle({top:new_tabs_top+'px'});
    
    if(tab_open){
//       page_content = $$(".page_content").first().removeClassName("hidden");
      page_content = $("pg1").removeClassName("hidden");
      var page_content_height = getWindowHeight() - page_content.cumulativeOffset()[1] - 20;
      if(page_content_height > (672-min_top-38)){page_content_height = 672-min_top-38;}
      page_content.setStyle({height:page_content_height+'px'});
    }
  }
  
  /*****************************************************************************/
  function getWindowHeight(){
    if (self.innerHeight)
      return self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
      return document.documentElement.clientHeight;
    else if (document.body)
      return document.body.clientHeight;
    else return 0;
  }


  function perform(obj){
    mya = $(obj).down(".lnk");
    mya.href=mya.protocol+"//"+mya.host+"#"+mya.pathname.replace(/^\//,"");
    load_tab(mya);
    location.hash = mya.hash;
  }

