$(document).ready(function(){//return;

  //open external links and PDF's in a new window
  $("a").each(function(){
    if( $(this).attr('href') ){
      href = $(this).attr('href');
      base = basedir.split('http://').join('').split('https://').join('');
      if( (href.match('http://') || href.match('https://')) && (!href.match(base) || href.match(/\.pdf|\.jpg|\.gif|\.doc/i)) ){
        $(this).attr('target','_blank');
      }
    }
  });
  
  assignEvents();
  
  //fix checkbox/radio inputs
  $("input[type='checkbox']").addClass("checkbox");
  $("input[type='radio']").addClass("radio");
  
  //assign defaults to the form based on labels
  $("form.valuelabels label").each(function(){
    label = $(this).html();
    selector = "#"+$(this).attr("for");
    if($(selector).attr('type')!='password') {
      createValueLabel(selector, label);
    }
  });
  
  //enhance subnav
  $("#subnav li.selected li.selected a:first").addClass("sub-select");
  $("#subnav li.selected li.selected li.selected a:first").addClass("sub-sub-select");
  
  //fade in home images
  $("#home-images img").fadeTo(1,0).load(fadeInHomeImage(1));
  
  if(!ltie7){
  
    //apply CSS for menu rollovers
    $("#page_1 #content").css({ height: "230px" });
    $("#content_1")
      .css({ height: "200px" })
      .find("div.section").css({ zIndex: "2", position: "absolute", height: "250px" })
      .find("div").css({ height: "250px" });
    for(i=1;i<=4;i++) { $("#content_1 div.s-"+i).css({ marginLeft: (232*(i-1))+"px" }); }
    
    //assign rollover events to each section
    $("#content_1 div.section")
      .mouseover(function(){
        $(this).find("div").css({ background: "#fff" });
        $(this).stop().animate({ paddingTop: "0px", height: "480px" }, 200, "swing");
        $(this).find("ul").stop().fadeTo(200, 1);
      })
      .mouseout(function(){
        $(this).stop().animate({ paddingTop: "230px", height: "250px" }, 500, "swing", function(){
          $(this).find("div").css({ background: "transparent" });
        });
        $(this).find("ul").stop().fadeTo(500, 0);
      })
      .find("ul").fadeTo(1, 0);
  }
 
});

function fadeInHomeImage (key){//$(this).data('key')
  if(key>4) return;
  $("#home-images img.img-"+key).fadeTo(200,0,function(){
    $("#home-images img.img-"+key).fadeTo(500,1,fadeInHomeImage(++key));
  });
}
 
function createValueLabel (selector, defaultValue){
  $(selector).data("default", defaultValue);
  $(selector)
    .bind("focus blur", function(){
      value = $(this).val();
      if(value==defaultValue){ $(this).val("") }
      if(!value){ $(this).val(defaultValue); }
    })
    .val(defaultValue);
  $(selector).trigger("focus").trigger("blur");
}

function assignEvents() {
  
  //form field events
  $("button:enabled, input:enabled, textarea:enabled")
    .mouseover(function(){ $(this).addClass("hover") })
    .mouseout(function(){ $(this).removeClass("hover") })
    .focus(function(){ $(this).addClass("selected") })
    .blur(function(){ $(this).removeClass("selected") });
  
  //confirmation alerts
  $(".confirm").click(function(){
    action = $(this).attr("title").substr(0,1).toLowerCase() + $(this).attr("title").substr(1);
    if( !confirm("Are you sure you want to "+action+"?") ){ return false; }
  });
}