// No conflict is used so that menus will will work when other script libs are loaded
// Replace all "$" shortcuts with keyword "jQuery" when adding new jQuery statements.
jQuery.noConflict();

var loadTasks = new Array(); // Sigh, need for legacy functions

// Do NOT use window.onload - it can't be invoked twice
// Use jQuery(document).ready instead
jQuery(document).ready(function(){
  
  // Call any functions that were not updated to jQuery
  // These need to be called before everything else due to menu compatability
  legacyFunctions();
   
  // This will setup the left hand navigation
  jQuery("#sideNav").treeview({
    collapsed: true,
    animated: (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) ? false : "fast", // this will turn animations off for IE6
    persist: "manual"
  });
  
  var positionMenu = function() {
    var dropDown = jQuery(this);
    var windowOb = jQuery(window);
    
    var mWidth  = dropDown.width(); 
    var mLeft   = jQuery(dropDown.parent().get(0)).offset().left;
    var wWidth  = windowOb.width();
    var wScroll = windowOb.scrollLeft();
    var pLeft = mLeft;
    
    dropDown.css("margin-left", "0px");
    if(((mLeft-wScroll) + mWidth) > (wWidth)){
      var pWidth = dropDown.parent().width();
      var marginLeft = ((mWidth - pWidth) * -1);
      pLeft = pLeft + pWidth;
      var bOffset = parseInt(dropDown.css("border-left-width")) + parseInt(dropDown.css("border-right-width"));
      marginLeft = marginLeft - bOffset;
      if((pLeft - wScroll) > wWidth){
        marginLeft = marginLeft + ((wWidth+wScroll) - pLeft);
      }
      dropDown.css("margin-left", (marginLeft + "px"));
    }
    else if(wScroll > pLeft){
      dropDown.css("margin-left", ((wScroll - pLeft) + "px"));
    }
  };

  // This will setup the drop down menus
  jQuery('ul#topLevelNav').superfish({
    autoArrows: false,
    animation: {opacity:'show',height:'show'},
    dropShadows: jQuery.browser.msie ? false : true, // IE browsers will not see shadows under menus
    delay: 800,
    disableHI: false,
    speed: ((jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) ? 0 : 'fast'), // Turn off animations for IE6
    onBeforeShow:  positionMenu
  });

  // IE6 Hack to make sure menus float above select boxes
  if(jQuery.fn.bgiframe){
    jQuery(".topLevelNavItem ul").bgiframe();
  }
  
  // This sets up lightbox effects
  if(jQuery.fn.lightBox){ // Test if lightbox library has been loaded
    // Look for and anchors with the lightbox rel and activate them
    jQuery('a[rel~="lightbox"]').lightBox({
      imageLoading:  '/images/admin/lightbox/lightbox-ico-loading.gif',
      imageBtnClose: '/images/admin/lightbox/lightbox-btn-close.gif',
      imageBtnPrev:  '/images/admin/lightbox/lightbox-btn-prev.gif',
      imageBtnNext:  '/images/admin/lightbox/lightbox-btn-next.gif'
    });
  }
  
  // This sets page tracking for outbound clicks in Google Analytics
  jQuery('#contentArea a').each(function() {
    var anchor = jQuery(this);
    var href = anchor.attr("href") ? anchor.attr("href") : '';
    if((href.match(/^https?:\/\//)) && (href.indexOf(document.domain) == -1)){
      anchor.attr({
        rel: "external",
        onclick: ("javascript: pageTracker._trackPageview('/outgoing/"+(this.href.replace(/^https?:\/\//, ''))+"');")
      });
      anchor.addClass('external');
    }
    else{
      // This will track clicks on downloadable files in Google Analytics
      if(href.match(/\.(pdf|doc|docx|xls|xlsx|zip|exe|ppt|swf|rtf|txt|dot|avi|wmv|mp3|mpeg|wav)$/i)){
        var path = this.pathname;
        if(path.charAt(0) != '/'){
           path = '/' + path;
        }
        anchor.attr({ 
          onclick: ("javascript: pageTracker._trackPageview('"+path+"');")
        });
      }
    }
  });
  
});


// Legacy functions below - these should be removed over time (not expanded or used!!)

// Horrible legacy load function
function registerLoadTask(fn) {
  loadTasks.push(fn);
}

// Non updated functions can be reloaded here
function legacyFunctions () {
  // Sigh, this needs to remain until the menus can be rewritten
  for (var i=0; i < loadTasks.length; i++) {
    switch(typeof loadTasks[i]) {
      case 'string':
        eval(loadTasks[i]);
        break;
      case 'function':
        loadTasks[i]();
        break;
    }
  };
  
  // Load the multi-page JS if it is detected
  if(typeof window.pagerInit == 'function') {
    pagerInit();
  }
}

// This sets the class="context" for the menu
// This does not need to be called if class="selected" is present
function xcSet(m, c) {
  var lastArg = arguments[arguments.length-1];
  var query = '#'+lastArg;
  var menu = jQuery(query);
  while(menu.not("a") && menu.children().length > 0){
    menu = menu.children(':first');
  }
  if(menu.is("a")){
    menu.addClass('context');
  }
}

// This sets class="selected"
function SetActive(m,l,c) {
  var query = '#'+l+'>a';
  jQuery(query).addClass('selected');
}

