(function($){
  /* Prevent default actions on main navigation links */
  $("nav ul li a").click(function(event){event.preventDefault();});
  
  /* Call functions needed for tabs */
  callTabs();
  frontTabs();
  closeTabs();
  clearTabs();
  initDropdowns();
})(window.jQuery);

function callTabs(){
  /* Event for all subnavigation links to open tabs */
  $("nav ul li.menu-item a").click(function(event){
    event.preventDefault();
    
    var clicked = $(this).attr("href");
    
    $(this).addClass("opened");
    
    if($(this).attr("title")){
      if($(this).attr("rev")){}
      else{
        /* If there is not already a rev attribute, make an ajax call to appropriate page */
        $.ajax({ 
          url: clicked, 
          context: $(this), 
          success: function(data){
            /* Grab the title from the link, and strip spaces */
            var tabTitle = $(this).attr("title");
            tabTitle = tabTitle.replace(/\s+/g,'');        
            
            /* Add rev attribute so event can't be fired again */
                $(this).attr("rev", "clicked " + tabTitle + "Tab");
                
                /* Append the content box to the content body, and assign it a random position */
                var newTab = $("<div class='new_tab' id='" + tabTitle + "Box'>" + data + "</div>");
                var closeLink = "<a href='#' class='close_tab' id='" + tabTitle + "Tab' title='" + tabTitle + "'>close</a>";
            
                
                
                newTab.appendTo("#content");
                $(closeLink).appendTo(newTab);

                var l = Math.floor(Math.random() * 1024 - 320);                
//                var l = Math.floor(Math.random() * $("#content").width() - 320);
                var t = Math.floor(Math.random() * 30);
                
                if(l < 0){
                  newTab.css("left", 14);
                }
                else{
                  newTab.css("left", l);
                }
                newTab.css("top", t);
                
                //console.log(l, t);
                $(".new_tab").css("z-index",99999);
//                $(".new_tab").topZIndex( { increment: 1 } );

                
                /* Make the newly created instance draggable */
                $("#" + tabTitle + "Box").draggable({ stack: ".new_tab", cursor: "move" });
              },
              error: function(){
                $(this).removeClass("opened");
              }
            });
      }
    }
  });
}

function frontTabs(){
    $(".new_tab").live("click", function(){
      $(this).topZIndex( { increment: 1 } );
    });
 }

function closeTabs(){
  /* Close content tab, and remove rev attribute from appropriate link in navigation */
  $(".close_tab").live('click', function(event){
    event.preventDefault();
    
    var tabId = $(this).attr("id");
    
    $('a[rev=clicked '+tabId+']').removeClass();
    $('a[rev=clicked '+tabId+']').removeAttr("rev");    
    
    $(this).parent().remove();
  });
}

function clearTabs(){
  /* Clear all tabs, and rev attributes on the page */
  $("#clear_tabs").click(function(event){
    event.preventDefault();
    
    $("nav ul li a").each(function(i){
      $(this).removeAttr("rev");
      $(this).removeClass();
    });
    
    $(".new_tab").each(function(i){
      $(this).remove();
      $(this).removeClass();
    });
  });
}

function initDropdowns(){
  $("nav ul li").hover(function(){
    $(this).addClass("hover");
    $('ul:first',this).css('visibility', 'visible');
  }, function(){
    $(this).removeClass("hover");
    $('ul:first',this).css('visibility', 'hidden');
  });
  
  $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
}

