﻿var TICKER_Pos;
var TICKER_Init;
var TICKER_tmr;
var TICKER_top;
var TICKER_top2;
var TICKER_decr;
var TICKER_JOBS;
var TICKER_Delay=7;

function TICKER_Initialise()
{
  if(!__EntityExists("Jobs"))
  {
    if(!__ReqMade)
    {
      __ReqMade=true;
      TICKER_GetJobs();
    }
    else
    {
      TICKER_ReqFailure();
    }    
  }
  else
  {
    TICKER_JOBS=__SelectNodes(__dataXml,"Job");
    if(TICKER_JOBS!=null&&TICKER_JOBS.length>0){
      TICKER_Pos=0;
      TICKER_Init=false;
      TICKER_PopulateJobList();
      TICKER_DisplayJob();
      document.getElementById(TICKER_ObjId).style.display="block";
    }
  }
}

function TICKER_GetJobs()
{
  var reqData="<getJobs />";
  __MakeRequest(TICKER_ID,reqData,TICKER_ReturnFromGettingJobs,TICKER_ReqFailure);
}

function TICKER_ReturnFromGettingJobs()
{
  __AddEntity(__SelectSingleNode(__Resp,"Jobs"));
  TICKER_Initialise();
}

function TICKER_ReqFailure(){}

function TICKER_PopulateJobList()
{
  var listHolder=document.getElementById("TickerJobList");
  var jobCount=TICKER_JOBS.length;    
  if(jobCount>1)
  {
    for(var i=1;i<jobCount;i++)
    {
      TICKER_AddJobToList(listHolder,i);
    }
    TICKER_AddJobToList(listHolder,0);
    if(jobCount>13){
      listHolder.style.height="338px";
    }
    else{
      listHolder.style.height=((jobCount-1)*26+1)+"px";
    }
    listHolder.style.display="block";
  }
  else
  {
    listHolder.style.display="none";
  }
}
function TICKER_AddJobToList(listHolder,i)
{
  var listDiv=document.createElement("div");
  listDiv.innerHTML=__NodeValue(TICKER_JOBS[i],"JobTitle");  
  listDiv.className="ListItem";
  listDiv.onmouseover=function(){this.className='ListItemOver';}
  listDiv.onmouseout=function(){this.className='ListItem';}
  listDiv.title="Click to view this vacancy";
  jobId=__NodeValue(TICKER_JOBS[i],"ItemId");
  if(TICKER_ViewJobURL!=""){
    var url=TICKER_ViewJobURL+"?jobId="+jobId;
    listDiv.onclick=function(){window.location=url};
  }  
  listHolder.appendChild(listDiv);
}

function TICKER_DisplayJob()
{
  if(TICKER_tmr!=null)
  {
    window.clearInterval(TICKER_tmr);
    TICKER_tmr=null;
  }  
  var currJob=TICKER_JOBS[TICKER_Pos];
  TICKER_Pos=TICKER_Pos+1;
  if(TICKER_Pos==TICKER_JOBS.length){
    TICKER_Pos=0;
  }
  if(currJob){  
    var suffix;
    if(TICKER_Init){
      suffix="2";
    }
    else{
      suffix="1";
    }
    var location=__NodeValue(currJob,"LocationFree");
    if(location==""){
      location=__NodeValue(currJob,"Town");
    }
    var desc=__NodeValue(currJob,"JobDescription");
    if(desc.length>90){
      desc=desc.substr(0,90)+"<b>... <i>(more)</i></b>";
    }    
    document.getElementById("JobTitle"+suffix).innerHTML=__NodeValue(currJob,"JobTitle");
    document.getElementById("Location"+suffix).innerHTML=location;
    document.getElementById("Description"+suffix).innerHTML=desc;
    document.getElementById("Salary"+suffix).innerHTML="Salary: "+TICKER_GetSalary(currJob)+TICKER_GetJobType(currJob);
    if(TICKER_ViewJobURL!=""){
      document.getElementById("JobLink"+suffix).href=TICKER_ViewJobURL+"?jobId="+__NodeValue(currJob,"ItemId");
    }
    else{
      document.getElementById("JobLink"+suffix).href="";
    }
    if(TICKER_Init)
    {
      TICKER_top=0;
      TICKER_top2=0;
      TICKER_decr=10;
      TICKER_tmr=window.setInterval(TICKER_Scroll,50);
    }
    else
    {
      TICKER_tmr=window.setInterval(TICKER_DisplayJob,TICKER_Delay*1000);
      TICKER_Init=true;
    }    
  }   
}

function TICKER_Scroll()
{
  window.clearInterval(TICKER_tmr);
  TICKER_tmr=null;
  if(TICKER_top>-106)
  {
    TICKER_top=TICKER_top-TICKER_decr;
    if(TICKER_top<-106){
      TICKER_top=-106;
    }
    TICKER_decr=TICKER_decr*1.025;
    document.getElementById("Job1").style.top=TICKER_top+"px";
    document.getElementById("Job2").style.top=TICKER_top+"px";
    TICKER_ScrollList();
    TICKER_tmr=window.setInterval(TICKER_Scroll,50);
  }
  else
  {
    document.getElementById("JobTitle1").innerHTML=document.getElementById("JobTitle2").innerHTML;
    document.getElementById("Location1").innerHTML=document.getElementById("Location2").innerHTML;
    document.getElementById("Description1").innerHTML=document.getElementById("Description2").innerHTML;
    document.getElementById("Salary1").innerHTML=document.getElementById("Salary2").innerHTML;
    document.getElementById("JobLink1").href=document.getElementById("JobLink2").href;
    document.getElementById("Job1").style.top="0px";
    document.getElementById("Job2").style.top="0px";    
    TICKER_tmr=window.setInterval(TICKER_DisplayJob,TICKER_Delay*1000);
  }
}

function TICKER_ScrollList()
{
  if(TICKER_top2!=999){
    var listHolder=document.getElementById("TickerJobList");
    if(listHolder.style.display!="none"){
      var jobList=listHolder.getElementsByTagName("div");
      if(TICKER_top2>-26){
        TICKER_top2=TICKER_top2-5;
        if(TICKER_top2<-26){    
          TICKER_top2=-26;
        }      
        for(var i=0;i<jobList.length;i++){
          jobList[i].style.top=TICKER_top2+"px";
        }
      }
      else{
        for(var i=0;i<jobList.length;i++){
          jobList[i].style.top="0px";
        }
        var topJob=jobList[0];
        listHolder.removeChild(topJob);
        listHolder.appendChild(topJob);
        TICKER_top2=999;
      }
    }
    else{
      TICKER_top2=999;
    }
  }
}

function TICKER_GetJobType(job)
{
  var duration=__NodeValue(job,"JobType");
  if(duration.substr(0,7)=="001.001"){
    return " (Permanent or Contract)";
  }
  else{
    if(duration.substr(0,7)=="001.002"){
      return " (Contract/Temporary)";
    }
    else{
      if(duration.substr(0,7)=="001.003"){
        return " (Permanent/Full Time)";
      }
      else{
        return "";
      }
    }
  }
}

function TICKER_GetSalary(job)
{
  var rate;
  var rateFree=__NodeValue(job,"RateFree");
  var period=__NodeValue(job,"SalaryPeriod")*1;
  var min=__NodeValue(job,"SalaryMin")*1;
  var max=__NodeValue(job,"SalaryMax")*1;
  if(rateFree!=""){
    rate=rateFree;
  }
  else{
    if(min==0&&max==0){
      rate="Contact us for more details";
    }
    else{
      if(min>0){
        if(period==1||period==2){
          rate="From "+TICKER_FormatCurrency(min,true);
        }
        else{
          rate="From "+TICKER_FormatCurrency(min,false);
        }        
      }
      if(max>0){
        if(rate==""){
          rate="Up to ";
        }
        else{
          rate=rate+" to ";
        }
      }
      if(period==1||period==2){
        rate=rate+TICKER_FormatCurrency(max,true);
      }
      else{
        rate=rate+TICKER_FormatCurrency(max,false);
      }
      switch(period){
        case 1:
          rate=rate+" per hour";
          break;
        case 2:
          rate=rate+" per day";
          break;
        case 3:
          rate=rate+" per week";
          break;
        case 4:
          rate=rate+" per month";
          break;
        case 5:
          rate=rate+" per annum";
          break;
      }
    }
  }
  return(rate);
}
function TICKER_FormatCurrency(num,incDecs)
{
  num=num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  {
    num="0";
  }
  sign=(num==(num = Math.abs(num)));
  num=Math.floor(num*100+0.50000000001);
  cents=num%100;
  num=Math.floor(num/100).toString();
  if(cents<10)
  {
    cents="0"+cents;
  }
  for(var i=0;i<Math.floor((num.length-(1+i))/3);i++)
  {
    num=num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
  }
  if(incDecs)
  {
    return(((sign)?'':'-')+'£'+num+'.'+cents);
  }
  else
  {
    return(((sign)?'':'-')+'£'+num);
  }  
}







