var header = {};
header.fn = {};
header.cst = {};
var navHover = false;
var MenuAjaxUrl="http://www.125pix.com/sandbox/ajaxmenu/";

header.fn.showHeaderTab = function (jLink) {
  if (jLink.find("div.header-tab-content").length == 0 || !jLink.data("loaded")) {
      jLink.find(".header-tab-content").remove();
      jLink.append(' <div class="header-tab-content layer" style="display:none"> </div> ');
      navHover = true;
      jLinkID = jLink.find("a.tab-link").attr("id").split("cat-");
      if (!navHover) {
          header.fn.ajaxRequest(jLink, jLinkID)
      } else {
          jLink.find("div.header-tab-content").show();
          header.cst.timerAjax = setTimeout("header.fn.ajaxRequest(jLink,jLinkID);", 200)
      }
  } else {
      jLink.find("div.header-tab-content").show()
  }
};

header.fn.showHeaderTabs = function () {
        $input = $("select:visible");
        var $tabs = $(".VMnavigation .tab").data("loaded", false),
            $nav = $(".VMnavigation"),
            jFocusElt = "button,a,input,textarea,select",
            $escapeMenuLink = $nav.prevUntil(".VMnavigation").find(jFocusElt).last(),
            timer;
        var navHeight = $nav.height();
        if (navHeight > 70) {
            $nav.find(">li:last-child").hide()
        }
        if (!window.orientation) {
            $tabs.bind("mouseenter", function () {
                jLink = $(this);
                if (!jLink.hasClass("home") && !jLink.hasClass("disabled")) {
                    jLink.addClass("opened");
                    if (!navHover) {
                        timer = setTimeout("header.fn.showHeaderTab(jLink);", 500)
                    } else {
                        timer = 0.5;
                        header.fn.showHeaderTab(jLink, true)
                    }
                }
            });
            $tabs.find("a").bind("focus", function () {
                jLink = $(this).parent("li");
                $tabs.removeClass("opened");
                $tabs.find("div.header-tab-content").hide();
                if (!jLink.hasClass("home") && !jLink.hasClass("disabled")) {
                    jLink.addClass("opened");
                    header.fn.showHeaderTab(jLink, true)
                }
            });
            $escapeMenuLink.bind("focus", function () {
                $tabs.removeClass("opened");
                $tabs.find("div.header-tab-content").hide()
            });
            $(".VMnavigation .tab").bind("mouseleave", function () {
                clearTimeout(timer);
                clearTimeout(header.cst.timerAjax);
                jLink = $(this);
                jLink.removeClass("opened");
                jLink.parent().find("div.header-tab-content").hide()
            });
            $tabs.find("a").bind("focus", function () {})
        } else {
            $tabs.find(".tab-link").unbind("click");
            $tabs.find(".tab-link").bind("click", function (e) {
                if (!$(this).parent().hasClass("services")) {
                    e.preventDefault()
                }
                jLink = $(this).parent();
                jLink.parent().find("div.header-tab-content").css("display", "none");
                if (!jLink.hasClass("home") && !jLink.hasClass("opened")) {
                    $tabs.removeClass("opened");
                    jLink.addClass("opened");
                    header.fn.showHeaderTab(jLink, true)
                } else {
                    jLink.removeClass("opened")
                }
            })
        }
        $nav.bind("mouseleave", function () {
            navHover = false
        })
    };

header.fn.ajaxRequest = function (jLink) {
        $.ajax({
            url: MenuAjaxUrl+'id'+jLinkID[1]+'.html',
            cache: true,
            dataType: "html",
            success: function (data) {
                jLink.find(".header-tab-content").html($(data).html()).show();
                jLink.data("loaded", true);
                jLink.find("a:last").bind("blur", function () {
                    jLink.removeClass("opened");
                    jLink.find("div.header-tab-content").hide()
                })
            }
        })
    };
$(document).ready(function(){
header.fn.showHeaderTabs();
})

