﻿   
$(document).ready(function(){
    //load in the first set of jobs (for the first item in the drop down box    
    $.get('/WebServices/JobHTML.aspx',{sector:getSector(), count:10, SectorID:getSector()},function(data) { 
            $('#rotatorContainer').html(data);
            resetJobs();
            setSector();
        }
    );
    /****
	*Load in the new sector jobs - passes through sectorid
	****/
    $('#theSectors').change (function(){
        $.get('/WebServices/JobHTML.aspx',{sector:this.options[this.selectedIndex].value, count:10, SectorID:getSector()},function(data) { 
                $('#rotatorContainer').html(data);
                resetJobs();
            }
        );  
    });
	/****
	*End of new sector jobs code
	****/
});

function isSector() {
    var sSector = new String(document.location);
    var iEndSector = sSector.indexOf("/browse");
    if (iEndSector != -1) {
        return true;
    }
    else {
        return false;
    }
}

function getSector() {
    var iSector = 0;
    if (isSector) {
    var aSector  = new String(document.location).split("/");
    var i = 0;
        for (i;i<=aSector.length-1;i++) {
            if (aSector[i] == "browse") {
                sSector = aSector[i-1];
                var theSectors = document.getElementById('theSectors')
                var ii = 0;
                for (ii=0;ii<theSectors.options.length;ii++) {
                    if (theSectors.options[ii].id == sSector) {
                        iSector = theSectors.options[ii].value;
                    }
                }    
            } 
        }        
    }
    return iSector;
}

function setSector() {
    document.getElementById('theSectors').value = getSector();
}

/****
* Job rotator code - should be a call to resetJobs() in the document.ready function
****/
var t;
//hide all items apart from the first one
function resetJobs(){ 
    //check if the pause button exists on the page
    if (document.getElementById('pause') != null){
        var i;
        //pause the rotator
        pause(false);
      //  $('div#rotatorItem'+i).show();
       // resetLink('rotatorItem'+i);
        document.getElementById('rotatorItem0').setAttribute('class','selected');
        resetLink('rotatorItem0');
        for (i=1;i<10;i++){
            currItem = document.getElementById('rotatorItem'+i);
            if (currItem != null){
                $('div#rotatorItem'+i).hide();
                document.getElementById('rotatorItem'+i).removeAttribute('class');
            }
        }
        //start the rotator again
        pause(true);
    }
}

function resetLink(sID){
    var divJob = document.getElementById(sID); 
    var ancMore = document.getElementById('ancMore');
    if (divJob.getAttribute("name") != null) {
        ancMore.setAttribute("href",  "/" + divJob.getAttribute("name"));
    }
    else {
        ancMore.setAttribute("href",  "javascript:;");
    }
}

//go to the next item, this will loop back to 0 once the final item has been shown
//takes the pause parameter to say whether this has been manually triggered or triggered by the 
//auto rotator
function gotoNext(bPause){
    var i;
    var currItem;
    for (i=0;i<10;i++){
        currItem = document.getElementById('rotatorItem'+i);
        if (currItem != null){
       
            if (currItem.getAttribute('class')=='selected'){
	            currItem.removeAttribute('class');
	            $('div#rotatorItem'+i).hide();
	            if ((i==9) || (document.getElementById('rotatorItem'+(i+1))==null)){
	                document.getElementById('rotatorItem0').setAttribute('class','selected');
	                $('div#rotatorItem0').show();
	                resetLink('rotatorItem0');
	            }else{
	                $('div#rotatorItem'+(i+1)).show();
	                resetLink('rotatorItem'+(i+1));
	                document.getElementById('rotatorItem'+(i+1)).setAttribute('class','selected');
	                i=10;
	            }

            }
        }	        
    }   
    if(!bPause){
        t=setTimeout("gotoNext()",5000);
    }
}
//goes to the previous job... when it has reached 0 it will search for the highest number less than 10 that exists and show that.
function gotoPrev(){
	    var i;
	    var j;
        var currItem;
	    for (i=0;i<10;i++){
	        currItem = document.getElementById('rotatorItem'+i);
	        if (currItem != null){
	       
	            if (currItem.getAttribute('class')=='selected'){
    	            currItem.removeAttribute('class');
                    $('div#rotatorItem'+i).hide();
    	            if ((i==0) || (document.getElementById('rotatorItem'+(i-1))==null)){
    	                //loop backwards through the div numbers 9 to 0 checking if they exist          
    	                for (j=9;j>-1;j--){
    	                    if (document.getElementById('rotatorItem'+j) != null){
    	                        $('div#rotatorItem'+j).show();
    	                        resetLink('rotatorItem'+j);
    	                        document.getElementById('rotatorItem'+j).setAttribute('class','selected');
    	                        i=10;
    	                        j=-1;
    	                    }
    	                }
    	            }else{
    	                $('div#rotatorItem'+(i-1)).show();
    	                resetLink('rotatorItem'+(i-1));
    	                document.getElementById('rotatorItem'+(i-1)).setAttribute('class','selected');
    	                i=10;
    	            }
    
                }
            }	        
	    }
}

// pause button - also triggered by next and prev buttons (to pause the rotation)
function pause(bManual){
    //detect whether we are currently in pause or play mode based on the alt text of the button
    if (document.getElementById('pause').childNodes[0].getAttribute("alt") == "Pause"){
        clearTimeout(t);
        document.getElementById('pause').innerHTML = '<img src="/img/Refresh_home.gif" alt="Forward" />'
    }else if(bManual==true){
        t=setTimeout("gotoNext(false)",5000);
        document.getElementById('pause').innerHTML = '<img src="/img/iconPause.gif" alt="Pause" />'
    }
}
/****
* End of Job Rotator code
****/