var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25226180-1']);
_gaq.push(['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

var overlayVisible = false;
var overlayShowTimer;
var overlayHideTimer;

$(document).ready(function() {
  $("a.lightbox").nyroModal({galleryCounts:null,callbacks:{afterShowCont:lightboxInitTitle}});
  if(!$.browser.msie || $.browser.version>=8) {
    menuInit();
  }
});

function lightboxInitTitle() {
  if($(".nyroModalTitle").html()!='') {
    $(".nyroModalTitle").width($(".nyroModalTitle").parent().width()).slideDown(200);
  }
}

function menuInit() {
  $("#navi ul a").not("#navi ul ul a").each(function(i){
    $(this).attr("id","navPoint"+i);
  }).mouseenter(function(i){
    menuOver(this);
  }).mouseleave(menuOut);
  $("#overlay,#overlayArrow").mouseenter(function(){
    clearTimeout(overlayHideTimer);
  }).mouseleave(menuOut);
  $("#overlay").css({"opacity":0,"width":"707px"}).show();
  $("#overlay .overlayPage").each(function(){
    $(this).width($(this).innerWidth()).attr("rev",$(this).innerWidth()).attr("rel",$(this).innerHeight()).hide();
  });
}

function menuOver(t) {
  var i = $(t).attr("id").split('navPoint').join('');
  clearTimeout(overlayShowTimer);
  clearTimeout(overlayHideTimer);
  overlayShowTimer = setTimeout("menuShow("+i+");",200);
}

function menuOut() {
  clearTimeout(overlayShowTimer);
  if(overlayVisible) {
    overlayHideTimer = setTimeout("menuHide();",500);
  }
}

function menuShow(i) {
  var h = Math.round($("#navPoint"+i).position().top)-100;
  if($("#overlayPage"+i).length) {
    if(overlayVisible) {
      var w = menuRezise(i);
      $("#overlay .overlayPage[id!='overlayPage"+i+"']").fadeOut(200);
      $("#overlayPage"+i).show().animate({"opacity":1},200);
      if(h>=$("#overlayPage"+i).height()) {
        $("#overlay").animate({top:h-$("#overlayPage"+i).height()+30},100,'linear');
      } else {
        $("#overlay").animate({top:0},100,'linear');
      }
      $("#overlayArrow").animate({top:h},300);
    } else {
      var w = menuRezise(i);
      $("#overlayPage"+i).css("opacity",1).show();
      if(h>=$("#overlay").height()) {
        $("#overlay").css("top",h-$("#overlay").height()+50);
      } else {
        $("#overlay").css("top",0);
      }
      $("#overlayArrow").css("top",h).show().animate({opacity:1,width:'21px',left:'-21px'},200);
      $("#overlay").css("width",0).show().animate({opacity:1,width:Math.round(w/2)},100,'linear').animate({width:w},100,'linear',function(){
        $(this).find("div[class^='shadow']").fadeIn(150);
      });
      overlayVisible = true;
    }
  } else {
    menuHide();
  }
}

function menuHide() {
  $("#overlay div[class^='shadow']").fadeOut(100);
  $("#overlay").animate({width:'353px'},100,'linear').animate({opacity:0,width:0},100,'linear',function(){
    $(this).hide().find(".overlayPage").hide();
  });
  $("#overlayArrow").animate({opacity:0,width:0,left:0},200,'linear',function(){ $(this).hide(); });
  overlayVisible = false;
}

function menuRezise(i) {
  var w = $("#overlayPage"+i).attr("rev");
  var h = $("#overlayPage"+i).attr("rel");
  if(overlayVisible) {
    $("#overlay").animate({width:(w*1)+25,height:(h*1)+20},200);
  } else {
    $("#overlay").height((h*1)+20);
  }
  return (w*1)+25;
}

