$(document).ready(function(){ setup() });

var preload = new Array();
var tim;

function changeSearchDropdown( disc, sector, line1, line2, lst )
{
  sector.options.length = 0;
  var optId = -1;
  if ( disc!=null && typeof(disc)!="undefined" )
  {
    optId = disc.options[disc.selectedIndex].value;
  }
  if ( optId>0 )
  {
    sector.options[sector.options.length] = new Option( sectornames[optId], optId );
    var arr = lst[optId];
    for ( i=0; typeof(arr)!="undefined" && i<arr.length; i++ )
    {
    	sector.options[sector.options.length] = new Option( " - " + arr[i][1], arr[i][0] );
    }
  }
  else
  {
    sector.options[sector.options.length] = new Option( line1, "" );
    sector.options[sector.options.length] = new Option( line2, "" );
  }
}
function Vacancy( empType, vacancyId, jobCode, jobTitle, location, salary )
{
  this.empType      = empType;
  this.vacancyId    = vacancyId;
  this.handle       = this.empType + "_" + this.vacancyId;
  this.url          = "/html/vacancies/view/index.jsp?vacancyId="+this.vacancyId;
  this.jobCode      = jobCode;
  this.jobTitle     = jobTitle;
  this.location     = location;
  this.salary       = salary;
  this.display      = document.createElement("div");
  this.display.id   = this.handle;
  $(this.display).html( "<a href=\""+this.url+"\" onmouseover=\"featured.pause();\" onmouseout=\"featured.go();\" class=\"title\">"    + this.jobTitle + "</a>\n" +
                        "<a href=\""+this.url+"\" onmouseover=\"featured.pause();\" onmouseout=\"featured.go();\" class=\"location\">" + this.location + "</a>\n" +
                        "<a href=\""+this.url+"\" onmouseover=\"featured.pause();\" onmouseout=\"featured.go();\" class=\"more\">"     + "more &gt;" + "</a>\n" +
                        "<a href=\""+this.url+"\" onmouseover=\"featured.pause();\" onmouseout=\"featured.go();\" class=\"salary\">"     + this.salary + "</a>\n" );
}
function VacancyList()
{
  this.jobs = new Array();
  this.lastDisplayed = "";
  this.add  = function( vacancy )
  {
    this.jobs[this.jobs.length] = vacancy;
  }
  this.getIndex = function( handle )
  {
    for ( var i=0; i<this.jobs.length; i++ )
    {
      if ( this.jobs[i].handle == handle )
      {
        return i;
      }
    }
    return -1;
  }
}
function FeaturedJobs()
{
  this.show = 2;
  this.jobs = new Array();
  this.timeout = null;
  this.getFeatured = function( empType )
  {
    var feat = this.jobs[empType];
    if ( typeof(feat)=="undefined" )
    {
      feat = new VacancyList();
      this.jobs[empType] = feat;
    }
    return feat;
  }
  this.addVacancy = function( empType, vacancyId, jobCode, jobTitle, location, salary )
  {
    this.getFeatured( empType ).add( new Vacancy( empType, vacancyId, jobCode, jobTitle, location, salary ) );
  }
  this.go = function()
  {
    if ( this.timeout==null )
    {
      this.timeout = setTimeout( "featured.scroll()", 4000 );
    }
  }
  this.pause = function()
  {
    clearTimeout( this.timeout );
    this.timeout = null;
  }
  this.scroll = function()
  {
    if ( this.getFeatured( "Permanent" ).jobs.length>0 )
    {
      $("#panelPermanent").show();
      this.scrollType( "Permanent" );
    }
    if ( this.getFeatured( "Temporary" ).jobs.length>0 )
    {
      $("#panelTemporary").show();
      this.scrollType( "Temporary" );
    }
    this.pause();
    this.go();
  }
  this.scrollType = function( handle )
  {
    var jobs = featured.getFeatured(handle);
    if ( jobs.jobs.length>0 )
    {
      if ( $("#featured"+handle+">div>div").size()>0 )
      {
        $($("#featured"+handle+">div>div:visible").get(0)).slideUp( new function() { featured.addAll( handle ); } );
      }
      else
      {
        this.addAll( handle );
      }
    }
  }
  this.addAll = function( handle )
  {
    var jobs = featured.getFeatured(handle);
    if ( jobs.jobs.length>0 )
    {
      $("#featured"+handle+">div>div").remove("div:hidden");
      for( var i=0; i<jobs.jobs.length; i++ )
      {
        if ( $("#featured"+handle+">div>#"+jobs.jobs[i].handle).size()<=0 )
        {
          $("#featured"+handle+">div").append( $(jobs.jobs[i].display).clone() );
        }
      }
    }
  }
}
var featured = new FeaturedJobs();

function setup()
{
  setupImageSwap("a.imgswap:not(.active)");
  setupImageSwap("input:image.imgswap:not(.active)");
  setupImageSwap("img.imgswap:not(.active)");
  $("#candidates input").focus(        function() { if( $(this).val()=='email' || $(this).val()=='password' ) $(this).val("") } );
  $("#menu ul li ul").each(            function() { $(this).parent().mouseover( function() { clearTimeout(tim); $(this).find("ul").fadeIn(150) } ) } );
  $("#menu ul li ul").each(            function() { $(this).parent().mouseout(  function() { clearTimeout(tim); tim = setTimeout("$(\"#menu>ul>li>ul\").fadeOut(150);",100) } ) } );
  $("#sectors ul li ul li").mouseover( function() { $(this).stop(); $(this).animate( { "marginLeft":"10px", "marginRight":"0px" } ); } );
  $("#sectors ul li ul li").mouseout(  function() { $(this).stop(); $(this).animate( { "marginLeft":"0px",  "marginRight":"10px"  } );  } );
  $("#splash #menu ul li").mouseover(  function() { $(this).stop(); $(this).animate( { "marginLeft":"10px", "marginRight":"0px" } ); } );
  $("#splash #menu ul li").mouseout(   function() { $(this).stop(); $(this).animate( { "marginLeft":"0px",  "marginRight":"10px"  } );  } );
  $("#links_left h2").mouseover(       function() { $("#links_left h2:not(#"+this.id+")").removeClass("active"); $("#links_left h2#"+this.id).addClass("active"); $("#links_right>div:not(#"+this.id+"_view):visible").hide(); $("#links_right>div#"+this.id+"_view:hidden").show(); } );
}


function setupImageSwap( search )
{
  $(search).each(
    function()
    {
      var img = this;
      if ( $(this).find("img").size()>0 )
      {
        var img = $(this).find("img").get(0);
      }
      var path = img.src.substring( 0, img.src.lastIndexOf("/")+1 );
      var name = img.src.substring( img.src.lastIndexOf("/")+1, img.src.length );
      var id   = name.substring( 0, name.lastIndexOf(".") );
      var ext  = name.substring( name.lastIndexOf("."), name.length );
      preload[id+"_"]  = new Image(); preload[id+"_"].src  = path+id+ext;
      preload[id+"_R"] = new Image(); preload[id+"_R"].src = path+id+"R"+ext;
      $(this).mouseover( function() { img.src = preload[id+"_R"].src; } );
      $(this).mouseout ( function() { img.src = preload[id+"_"].src; } );
    } );
}