$(function(){
  // Variables
  var leftLink = true;
  var leftInfo = true;
  
  // Calendar - hover date
  $(".merket a").each(function(){
    
    // Mouse events
    $(this)
    .mouseover(function(e)
    {      
      leftLink = false;
      
      var $linkDate = $(this).attr("title");
      var url = "/kalenderhendelser?dato=" + $linkDate;
      
      var position = $(this).position();
      var x = position.left;
      var y = position.top;      
      
      $("#calendarInfo").html("...laster...").show();
      
      // If date is given
      if (url != "/kalenderhendelser?dato=") 
      { 
        openCalendarInfo($(this), url, x, y);        
      }    
    })
    .mouseout(function()
    {
      leftLink = true;
      closeCalendarInfo();
      //$(this).toggleClass("active");
      //setTimeout( function(){ $("#calendarInfo").stop().hide(); }, 500);    
    });
  
  });
  
  $("#calendarInfo")
  .mouseover(function(){ leftInfo = false; })
  .mouseout(function(){ 
    leftInfo = true; 
    closeCalendarInfo();
  });
  
  // Open calendarInfo
  function openCalendarInfo(parent, url, x, y)
  {
    // Fjerner active in link
    $(".merket a").each(function(){ $(this).removeClass("active"); });    
    parent.addClass("active");
    
    // Ajax call
    $.get(url, function(data){ $( "#calendarInfo").stop().html(data).show(); });
    $("#calendarInfo").css({left: (x - 172), top: (y + 19)}); 
  }
  
  function closeCalendarInfo()
  {
      setTimeout( function()
      { 
        if(leftLink && leftInfo)
        { 
          $("#calendarInfo").stop().html("").hide();
          
          // Fjerner active in link
          $(".merket a").each(function(){ $(this).removeClass("active"); });
        }         
      }, 100);      
  }
  
});


(function($){
  // RETURN TAG NAME  
  $.fn.tagName = function() { return this.get(0).tagName.toLowerCase(); }
  
  // CONTAINS INGNORE CASE
  $.expr[":"].containsIgnoreCase = function(e,i,m){return $(e).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;};
  
})(jQuery)

