// on the home page, even out the boxes
//jQuery(window).load(function(){
//  if( jQuery("#HomeContent").length < 1 ) return;
//  
//  var elements = ["#HomeContent #Calendar", "#HomeContent #Tabs"];
//  
//  var i, maxBottom = 0, delta, box;
//  for( i in elements ) {
//    elements[i] = { el: jQuery(elements[i]) };
//    elements[i].pos = elements[i].el.position();
//    elements[i].height = elements[i].el.height();
//    elements[i].bottom = elements[i].pos.top + elements[i].el.outerHeight(true);
//    if( elements[i].bottom > maxBottom ) maxBottom = elements[i].bottom;
//  }
//  for( i in elements ) {
//    if( elements[i].bottom < maxBottom ) {
//      delta = maxBottom - elements[i].bottom;
//      elements[i].el.height( elements[i].height + delta );
//      box = elements[i].el.children("").last();
//      if( box.length ) {
//        box.height( box.height() + delta );
//      }
//    }
//  }
//});


// jQuery callback that fires at DOM-ready
jQuery(function($){
  
  // Tabs
  if( $().tabs ) {
    $('.tabs').tabs();
  }
  
  // Accordion
  if( $().accordion ) {
    $("#accordion").accordion({ header: "h3" });
  }
  
  //hover states on the static widgets
  $('#dialog_link, ul#icons li').hover(
    function() { $(this).addClass('ui-state-hover'); }, 
    function() { $(this).removeClass('ui-state-hover'); }
  );
  
  // install the feature area functionality on the home page
  var panelZIndex = 800;
  $("#homepage-features")
    .addClass("hpf-active")
    .click(function(e){
        // figure out what fragment the link is pointing at
        var href = (($(e.target).closest("a").get(0) || {}).href || "").replace(/^.*(#[-\w]+)$/, "$1");
        if( href.substr(0,1) !== "#" ) return;
        
        // find the panel (that is not current) with that ID
        var $panel = $(href+".hpf-panel",this);
        if( $panel.length === 0 ) return true;
        else if( $panel.is(".hpf-current") ) return false;
        
        // change the current panel
        $(".hpf-panel.hpf-current",this).removeClass("hpf-current");
        $panel.addClass("hpf-current");
        e.preventDefault();
      })
    .children(".hpf-panel").each(function(){
        var imgName = this.id.replace("feature-","tab-bg-");
        for( var i=1; i<=4; i++ ) {
          var cls = "feature-" + i;
          $(this).append('<a href="#'+cls+'" class="hpf-selector '+cls+((i===1) ? " hpf-first" : "")+'">'+i+'</a>');
        }
        $("."+this.id,this).addClass("hpf-current");
        $(this).css("z-index",panelZIndex--).prepend('<a href="#'+this.id+'" class="hpf-tab"><img src="./files/pagelayoutimages/'+imgName+'.png"></a>');
      })
      .first().addClass("hpf-current");
  
  // patch around IE's broken hover behavior for the navigation flyouts
  if (window.attachEvent) {
    $("#nav>li").each(function() {
      this.onmouseover = function() {
        this.className += " over";
      }
      this.onmouseout = function() {
        this.className = this.className.replace(new RegExp(" over\\b"), "");
      }
    });
  }
  
  /* ---------------
   * install the code to allow the login field labels to float over their
   * respective fields (to act as placeholders)
   * 
   */
  function toggleLabel( id, visible ) {
    $("label.overlabel-apply[for='" + id + "']").toggleClass("hidden", !visible);
  }
  
  $("label.overlabel[for]").each(function(){
    var id = $(this).attr("for");
    var fld = $("#" + id);
    
    // if the label's for doesn't point to a real field, skip
    if( !id || fld.length === 0 ) return;
    
    // activate the label
    $(this).addClass("overlabel-apply").removeClass("overlabel");
    
    // hide the label if the field isn't empty
    if( fld.val() ) toggleLabel(id, false);
    
    // install the necessary event handlers
    $(this).click(function(){ fld.focus(); });
    fld.focus(function(){ toggleLabel(this.id, false); });
    fld.blur(function(){ if( !$(this).val() ) toggleLabel(this.id, true); });
  });
  
});


