﻿	//max pagination value

	var currQueryJobReg;
    var currQueryJobFunc;
    var currPage = 0;

	var pages = 0;
	var items = 0;

	function job(func, reg, jtitle, division, loc, lnk, isnew, key)
	{
	    //Argument ref: 
	    //job function, region, function text, job title, division, location, link, isnew, iskey
	   
        this.f = func;
        this.r = reg;
        
        switch(func)
            {
            case 2:
                this.fxtext = "Customer Service";
                break;
            case 3: 
                this.fxtext = "Finance";
                break;
            case 4:
                this.fxtext = "General Management";
                break;
            case 5: 
                this.fxtext = "IT/Internet";
                break;
            case 6:
                this.fxtext = "Marketing";
                break;
            case 7: 
                this.fxtext = "Operations";
                break;
            case 8:
                this.fxtext = "Sales";
                break;
            case 9: 
                this.fxtext = "Teaching/Academic";
                break;
            case 10:
                this.fxtext = "Temporary opportunities";
                break;
        }
        
        switch(reg)
            {
            case 1:
                this.rgtext = "Asia Pacific excl. China";
                break;            
            case 2:
                this.rgtext = "Europe";
                break;
            case 3: 
                this.rgtext = "International";
                break;
            case 4:
                this.rgtext = "Latin America";
                break;
            case 5: 
                this.rgtext = "Middle East and Africa";
                break;
            case 6:
                this.rgtext = "North America";
                break;
            case 7: 
                this.rgtext = "China";
                break;
            case 9: 
                this.rgtext = "Others";
                break;
        }
        
        this.jtitle = jtitle;
        this.division = division;       
        
        this.loc = loc;
        this.lnk = lnk;
        
        this.isnew = isnew;
        this.iskey = key;   
	}
 
  
  //Exec the query against array
  function doQuery(jobReg, jobFunc)
	{
	    if (!jobReg || !jobFunc)
	    {
	        return;
	    }
	    
		currentJobs = new Array();
		currQueryJobReg = jobReg;
		currQueryJobFunc = jobFunc;
		items = 0;
		pages = 0;
        currPage = 0;
        
		var str = "";
		var paginationMaxItems = 15;
		
		var jobTemplate = "<h5>#SECTIONHEADING#</h5>\n";
		jobTemplate += "<div class=\"job\"><strong><a href=\"#LINK#\">#JOB#</a></strong></div>";
		
		var jobTemplateALL = "<h5>#SECTIONHEADING#</h5>\n<h6>#REGIONHEADING#</h6>\n";
		jobTemplateALL += "<div class=\"job\"><strong><a href=\"#LINK#\">#JOB#</a></strong></div>";

		var jobPage = 0;
		var lastJobPage = -999;
		var jobGroups = ",";
		var jobRegionGroups = ",";
		
		for (var i=0; i<jobs.length; i++)
		{
		    //Display for Specific Region and specific or all Functions---------------------------------------------
			if (((jobs[i].r == jobReg)) && ((jobs[i].f == jobFunc)||(jobFunc==-1)))
			{								
				var newJobStr = jobTemplate;
				jobPage = Math.round(((items+1)/paginationMaxItems)+0.49)-1;//counts from zero for exact page number like all good scripts do
				
				//Check for a new Page
				if (jobPage!=lastJobPage)
				{
					//page div and maintian section heading
					    newJobStr = "<div id=\"jobsPage"+jobPage+"\" style=\"display:none;\">\n"+newJobStr;  
                        newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].fxtext);

					    if (jobPage>0)
					    {
						    //close last div
						    newJobStr = "</div>\n" + newJobStr;
					    }
						
					lastJobPage=jobPage;
				}
				//Check for a new Section
				if (jobGroups.indexOf(","+jobs[i].f+",")==-1)
				{
					newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].fxtext);
					
					jobGroups += ","+jobs[i].f+",";
				} else {
					//Not a new section, just remove heading tag
					newJobStr = newJobStr.replace("<h5>#SECTIONHEADING#</h5>\n","");
				}
				
				var job_str = jobs[i].jtitle;
				
				if (jobs[i].division != ""){
				job_str += ", " + jobs[i].division;
				}
				
				if (jobs[i].loc != ""){
				job_str += ", " + jobs[i].loc;
				}
				
				newJobStr = newJobStr.replace("#JOB#",job_str);
				newJobStr = newJobStr.replace("#LINK#",jobs[i].lnk);
				

				str += newJobStr;//line break to ease it up
				items++;
			}
			
			//Display for all Regions and specific Function---------------------------------------------
			if (((jobReg==-1)) && ((jobs[i].f == jobFunc))){
			    var newJobStr = jobTemplate;
				jobPage = Math.round(((items+1)/paginationMaxItems)+0.49)-1;//counts from zero for exact page number like all good scripts do
				
				//Check for a new Page
				if (jobPage!=lastJobPage)
				{
					//page div and maintian section heading
					    newJobStr = "<div id=\"jobsPage"+jobPage+"\" style=\"display:none;\">\n"+newJobStr;  
                        newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].rgtext);

					    if (jobPage>0)
					    {
						    //close last div
						    newJobStr = "</div>\n" + newJobStr;
					    }
						
					lastJobPage=jobPage;
				}
				//Check for a new region
				if (jobGroups.indexOf(","+jobs[i].r+",")==-1)
				{
					newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].rgtext);
					
					jobGroups += ","+jobs[i].r+",";
				} else {
					//Not a new region, just remove heading tag
					newJobStr = newJobStr.replace("<h5>#SECTIONHEADING#</h5>\n","");
				}
				
				var job_str = jobs[i].jtitle;
				
				if (jobs[i].division != ""){
				job_str += ", " + jobs[i].division;
				}
				
				if (jobs[i].loc != ""){
				job_str += ", " + jobs[i].loc;
				}
				
				newJobStr = newJobStr.replace("#JOB#",job_str);
				newJobStr = newJobStr.replace("#LINK#",jobs[i].lnk);
				

				str += newJobStr;//line break to ease it up
				items++;
			}

            //Display for all Regions and specific Function---------------------------------------------
			if (((jobReg==-1)) && ((jobFunc==-1))){	
			    var newJobStr = jobTemplateALL;
				jobPage = Math.round(((items+1)/paginationMaxItems)+0.49)-1;//counts from zero for exact page number like all good scripts do
				
				//Check for a new Page
				if (jobPage!=lastJobPage)
				{
					//page div and maintian section heading
					    newJobStr = "<div id=\"jobsPage"+jobPage+"\" style=\"display:none;\">\n"+newJobStr;  
                        newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].fxtext);
                        newJobStr = newJobStr.replace("#REGIONHEADING#",jobs[i].rgtext);

					    if (jobPage>0)
					    {
						    //close last div
						    newJobStr = "</div>\n" + newJobStr;
					    }
						
					lastJobPage=jobPage;
				}
				//Check for a new Section
				if (jobGroups.indexOf(","+jobs[i].f+",")==-1)
				{
					newJobStr = newJobStr.replace("#SECTIONHEADING#",jobs[i].fxtext);
					jobGroups += ","+jobs[i].f+",";
					jobRegionGroups = ",";
				} 
				else {
					//Not a new section, just remove heading tag
					newJobStr = newJobStr.replace("<h5>#SECTIONHEADING#</h5>\n","");
				}
				
				//Check for new region
				if(jobRegionGroups.indexOf(","+jobs[i].r+",")==-1){
				    newJobStr = newJobStr.replace("#REGIONHEADING#",jobs[i].rgtext);
					jobRegionGroups += ","+jobs[i].r+",";
				}
				else {
					//Not a new section, just remove heading tag
					newJobStr = newJobStr.replace("<h6>#REGIONHEADING#</h6>\n","");
				}

				
				var job_str = jobs[i].jtitle;
				
				if (jobs[i].division != ""){
				job_str += ", " + jobs[i].division;
				}
				
				if (jobs[i].loc != ""){
				job_str += ", " + jobs[i].loc;
				}
				
				newJobStr = newJobStr.replace("#JOB#",job_str);
				newJobStr = newJobStr.replace("#LINK#",jobs[i].lnk);
				

				str += newJobStr;//line break to ease it up
				items++;
			}		
		}

		if (items==0) 
		{
		    str="<div class=\"noMatch\"><br /><strong><i>Sorry,</i> we couldn't find anything to match your choice.<br />Please try again.</strong></div>";
		}
		pages = Math.round((items/paginationMaxItems)+0.49); //NOTE starts counting from 1!	
		//display_jobs
		$("#jobs").html(str);
		
		// Make pagination links
		$("#paginationPages").html("");		
		
		if (pages>1)
		{
			var pstr="";
			for (var i=0; i<pages; i++)
			{
				pstr += "<span id=\"jobsPageSelector"+i+"\">";
				pstr +="<a href=\"#\" onclick=\"jobs_openPage("+i+")\">"+(i+1)+"</a></span> ";
				if (i+1<pages) pstr +="| ";
			}
			$("#paginationPages").html(pstr);
		    $("#paginationPages").css("display","inline"); 
		
		} else {
		    
		   setStyle("nextButton","display","none");
		   setStyle("prevButton","display","none");
		   setStyle("paginationPages","display","none");
		}
		//show current page 
		 
		jobs_openPage(currPage);
	}

	// Change pagination actions	
	function jobs_openPage(p)
	{  	
		$(document).ready(function(){	
		    alternateColors($("#jobsPage" + p),"div","#FFFFFF","#EEEEEE");
		    $("#jobsPage" + p).css({"border-bottom":"1px solid #999999"});		
		});
		
		$("#backButton").css("display","inline");
	    if (pages>0)
	    {
            $("#jobsPageSelector" + currPage).css({ fontWeight:"normal" });    
		    $("#jobsPage" + currPage).css("display","none");	
		    
		    currPage = p;
		    currPage = checkPageVal(currPage);

		   
		    $("#jobsPageSelector" + p).css({ fontWeight:"bolder"});    
		    $("#jobsPage" + currPage).fadeIn("slow");

		    //show prev/next buttons if we should have them
		    if (pages>1)
		    {
			    //Pagination is on, so toggle the buttons based on current page number, you may need to change the 'block' value to whatever works with your display mode style
			   $("#nextButton").css("display","inline"); 
			   $("#prevButton").css("display","inline"); 
			   

			   if (currPage >= (pages-1)){
			         
		  	         $("#nextButton").css("display","none");
			    }
			    if (currPage <= 0){					
			        $("#prevButton").css("display","none"); 
		        }
		    }
		}
	}

	function click_shiftPage(s)
	{
		jobs_openPage(currPage+s);
		$(document).ready(function(){		
		alternateColors($("#jobsPage" + currPage),"div","#FFFFFF","#EEEEEE");
		});
	}
	
	function checkPageVal(p)
	{
		if (p<0) return 0;
		if (p>pages) return pages-1;
		else return p;
	}
	
	//replace this with the jquery thing if working
	function setStyle(_id,_att,_val)
	{
	    eval("document.getElementById(\""+_id+"\").style."+_att+"=\""+_val+"\"");
	}
	
	function alternateColors(parent, children, even, odd){
            $(parent).find(children + ":nth-child(odd)").css({"background-color":even, "border-top":"1px dotted #999999"});
            $(parent).find(children + ":nth-child(even)").css({"background-color":odd, "border-top":"1px dotted #999999"});       
    }
