/** Ross Apps JavaScript        *
 * must be loaded after jQuery. */

/** jQuery extensions */

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
    

/** Utility Class */

function RossUtils() { }

RossUtils.prototype = new Object();

RossUtils.addWaiting = function(element) {
  $(element).wrap('<div></div>').prepend('<div id="waiting"></div>');
  $("#waiting").height($(element).height());
  $("#waiting").width($(element).width()); 
}

RossUtils.addUserMenu = function() {
  $("#headerContent").append('<div id="userMenuContainer"></div>');
  $("#userMenuContainer").html($("#userMenu")).addClass("ui-corner-bottom");
  var first_width = $("div#userMenuContainer").width() - $("#userMenu ul").width();
  $("#userMenu li:first").width(first_width + $("#userMenu li:first").width() - $("#userMenu li").length );
}

RossUtils.preventWidow = function(string) {
  var wordArray = string.split(" ");
  wordArray[wordArray.length - 2] += "&nbsp;" + wordArray[wordArray.length - 1];
  wordArray.pop();
  return wordArray.join(" ");
}


/** Top Menu Drop Down */

function DropdownMenu(subMenuLinks, current) {
  this.subMenuLinks = subMenuLinks;
  this.current = this.isValidMenuItem(current) ? this.filterEmptyString(current) : null;
  this.down = false;
  this.up = true;
  this.downing = false;
  this.upping = false;
  this.delayedEvent;
  
  $('#topMenu a').hover(this.buttonHover(), this.buttonOff());
  $('#navBar').hover(this.navBarHover(), this.navBarOff());
  
  if (this.current) {
    this.makeActive(this.current);
  }
}

DropdownMenu.prototype = new Object();

DropdownMenu.prototype.buttonHover = function() {
  var self = this;
  return function() {
    self.clearRollover();
    $(this).html('<img src="/imgs//buttons/' + this.id + '.png" />');
    if (self.subMenuLinks[this.id] && self.subMenuLinks[this.id].length > 0) {
      $('#dropMenu p').html(self.getSublinks(this.id));
      if ($(this).position().left < 400) { 
        $('#dropMenu p').css('text-align', 'left');
      } else {
        $('#dropMenu p').css('text-align', 'right');
      }
      if (self.up || self.upping) {
        self.lowerDropMenu();
      }
    } else {
      if (self.downing || self.down) {
        self.raiseDropMenu();
      }
    }
  }
}
    
DropdownMenu.prototype.buttonOff = function() {
  var self = this;
  return function() { }
}

DropdownMenu.prototype.navBarHover = function() {
  var self = this;
  return function() {
    clearTimeout(self.delayedEvent);
  }
}
    
DropdownMenu.prototype.navBarOff = function() {
  var self = this;
  return function() {
    clearTimeout(self.delayedEvent);
    self.delayedEvent = setTimeout(function() {
        if (self.current) {
          self.makeActive(self.current);
        } else {
          self.raiseDropMenu();
        }
        self.clearRollover();
      }, 500);
  }
}

DropdownMenu.prototype.lowerDropMenu = function() {
  if (this.upping) {
    $('#dropMenu').stop(true, false);
    this.upping = false;
  }
  this.preDowning();
  var self = this;
  $('#dropMenu').show('slide', { direction: 'up' }, 750, function() { self.postDown(); });
}
    
DropdownMenu.prototype.raiseDropMenu = function() {
  if (!this.upping && !this.up) {
    if (this.downing) {
      $('#dropMenu').stop(true, false);
      this.downing = false;
    }
    this.preUpping();
    var self = this;
    $('#dropMenu').hide('slide', { direction: 'up' }, 1250, function() { self.postUp(); });
  }
}
    
DropdownMenu.prototype.preDowning = function() {
  this.downing = true;
  this.up = false;
}
    
DropdownMenu.prototype.postDown = function() {
  this.downing = false;
  this.down = true;
  $('#dropMenu').css({'position' : 'static', 'top' : '0'});
}
    
DropdownMenu.prototype.preUpping = function() {
  this.upping = true;
  this.down = false;
}
    
DropdownMenu.prototype.postUp = function() {
  this.upping = false;
  this.up = true;
  $('#dropMenu').css('top', '0');
}
    
DropdownMenu.prototype.getSublinks = function(item) {
  var markup = "";
  for (i = 0;i < this.subMenuLinks[item].length;i++) {
    markup += '<a';
    for (attr in this.subMenuLinks[item][i][1]) {
      markup += ' ' + attr + '="' + this.subMenuLinks[item][i][1][attr] + '"'; 
    }
    markup += '>' + this.subMenuLinks[item][i][0] + '</a>';
    if (i < this.subMenuLinks[item].length -1) {
      markup += '&nbsp;&nbsp; | &nbsp;&nbsp;';
    }
  }
  return markup;
}

DropdownMenu.prototype.makeActive = function(menu_id) {
  if (this.isValidMenuItem(menu_id)) {
    $("#" + menu_id).html('<img src="/imgs//buttons/' + menu_id + '.png" />');
    if (this.subMenuLinks[menu_id] && this.subMenuLinks[menu_id].length > 0) {
      $('#dropMenu p').html(this.getSublinks(menu_id));
      if ($("#" + menu_id).position().left < 400) { 
        $('#dropMenu p').css('text-align', 'left');
      } else {
        $('#dropMenu p').css('text-align', 'right');
      }
      if (this.upping) {
        this.lowerDropMenu();
      } else if (this.downing) {
        $('#dropMenu').stop(true, true);
        this.downing = false;
      } else {
        $('#dropMenu').show();
      }
      this.up = false;
      this.down = true;
    }
  }
}

DropdownMenu.prototype.clearRollover = function() {
  if (this.current && this) {
    $('#topMenu > a:not(#' + this.current + ')').empty();
  } else {
    $('#topMenu > a').empty();
  }
}

DropdownMenu.prototype.filterEmptyString = function(string) {
  return (string && string.replace(/\s/g, "") !== "" ? string : null)
}

DropdownMenu.prototype.isValidMenuItem = function(string) {
  return this.subMenuLinks[string] != null;
}

/** Home Page Slide Show */

function ExcSlideShow() {
  this.nextSlide;
  this.slideIntervalTime = 5000;
  this.crossFadeTime = 1000;
  this.cleanupTime = 250;
  
  var self = this;
  
  /* attach hover to latest spotlight */
  $("#featuredPhoto").hover(function() {
        self.stopSlide();
        //self.spotlight(this, 500);
      },
      function() {
        self.runSlide();
      }
    );
    
  $("#featured #latest li").hover(function() {
        self.stopSlide();
        self.spotlight(this, 500);
      },
      function() {
        self.runSlide();
      }
    );
    
  /** featured arrows */
  $("#featured div.arrowRight").hover(function() {
        self.stopSlide();
        $(this).css("background-image", 'url("/imgs//buttons/arrowRight-on.png")').css("cursor", "pointer"); 
      },
      function() {
        $(this).css("background-image", 'url("/imgs//buttons/arrowRight.png")').css("cursor", "auto");
        self.runSlide();
      }
    ).click(function() {
        self.incrumentSpotlight(500);
      }
    );
  
  $("#featured div.arrowLeft").hover(function() {
        self.stopSlide();
        $(this).css("background-image", 'url("/imgs//buttons/arrowLeft-on.png")').css("cursor", "pointer"); 
      },
      function() {
        $(this).css("background-image", 'url("/imgs//buttons/arrowLeft.png")').css("cursor", "auto");
        self.runSlide();
      }
    ).click(function() {
        self.decrementSpotlight(500);
      }
    );
    
  /* attach hover to latest notes */
  $("ul#ofNoteImages > li").hover(function() {
        $("div.headline h2 > a", $(this).parent().parent()).attr('title', $("a", this).attr('title'));
        $("div.headline h2 > a", $(this).parent().parent()).attr('href', $("a", this).attr('href'));
        $("div.headline h2 > a", $(this).parent().parent()).text($("a", this).attr('title'));
        $(this).append($("img#notePointer"));
        $(this).siblings().stop(true, true).removeClass("current", 700);
        $(this).stop(true, true).addClass("current", 300);
      },
      function() { }
    );
    
  /* attach over to department hightlights, while we're at it */
  $("ul.featured > li").hover(function() {
        $(this).parent().children().stop(true, true).removeClass("current");
        $(this).stop(true, true).addClass("current");
        $("div.photo", $(this).parent().parent()).append($("a.departmentPhoto", this).clone().fadeIn(function() { 
            $(this).prevAll().remove(); }));
      },
      function() { }
    );
  
  this.runSlide();
  
  jQuery.preLoadImages("/imgs//buttons/arrowLeft.png",
                       "/imgs//buttons/arrowRight.png",
                       "/imgs//buttons/arrowLeft-on.png",
                       "/imgs//buttons/arrowRight-on.png");
}

ExcSlideShow.prototype = new Object();

ExcSlideShow.prototype.runSlide = function() {
  if (this.nextSlide) { this.stopSlide(); }
  var self = this;
  this.nextSlide = setInterval(function() { self.incrumentSpotlight(); }, self.slideIntervalTime);
}

ExcSlideShow.prototype.stopSlide = function() {
  clearInterval(this.nextSlide);
}

ExcSlideShow.prototype.spotlight = function(item, crossFadeTime) {
  if (!crossFadeTime) { crossFadeTime = this.crossFadeTime; }
  $(item).siblings().stop(true, true).removeClass("current", this.cleanupTime);
  $(item).stop(true, true).addClass("current", crossFadeTime);
  $("#featuredPhoto").append($("a.featuredPhoto", item).clone().fadeIn(crossFadeTime, 
      function() { $(this).prevAll().remove(); }));
  $("#featured .headline > h2").append($("a.featuredHeader", item).clone().fadeIn(crossFadeTime/1.5,
      function() { $(this).prevAll().remove(); }));  
}

ExcSlideShow.prototype.incrumentSpotlight = function(crossFadeTime) {
  if (!crossFadeTime) { crossFadeTime = this.crossFadeTime; }
  var currentItem = $("ul#latest > li.current").last()
  if (currentItem.get(0) == currentItem.parent().children().last().get(0)) {
    this.spotlight(currentItem.parent().children().first(), crossFadeTime);
  } else {
    this.spotlight(currentItem.next(), crossFadeTime);
  }
}

ExcSlideShow.prototype.decrementSpotlight = function(crossFadeTime) {
  if (!crossFadeTime) { crossFadeTime = this.crossFadeTime; }
  var currentItem = $("ul#latest > li.current").last()
  if (currentItem.get(0) == currentItem.parent().children().first().get(0)) {
    this.spotlight(currentItem.parent().children().last(), crossFadeTime);
  } else {
    this.spotlight(currentItem.prev(), crossFadeTime);
  }
}