// jQueryId: jobs.js 11375 2008-11-11 07:13:00Z twilson $
var jobCatNav, jobsNav;


var nav = {
  
  init : function(){

    // JOB CATEGORIES
    if(jQuery("#showAllJobCategoriesList")){
      jobCatNav = {
          link   : jQuery("#showAllJobCategoriesLink"),
          box    : jQuery("#showAllJobCategoriesList"),
          on     : false,
          hideTxt : "Hide All Job Categories",
          showTxt : "Show All Job Categories",
          upArrow : "/global/images/hide_all_jobs_ico.gif",
          downArrow : "/global/images/show_all_jobs_ico.gif",
          swapBG : true,
          height : typeof(jobCatNavHeight) != "undefined" ? jobCatNavHeight :  350
      }
      jobCatNav.link.attr('href', 'javascript:nav.toggle(jobCatNav);');
      jobCatNav.box.height(0);
    }

    // AL JOBS
    if(jQuery("#showAllJobsList")){
      jobsNav = {
          link   : jQuery("#showAllJobsLink"),
          box    : jQuery("#showAllJobsList"),
          on     : false,
          hideTxt : "Hide All Jobs",
          showTxt : "Show All Jobs",
          upArrow : "/global/images/hide_all_jobs_ico.gif",
          downArrow : "/global/images/show_all_jobs_ico.gif",
          swapBG : true,
          height : 385,
	  jobsList : JobsList()
      }
      jobsNav.link.attr('href', 'javascript:nav.toggle(jobsNav);');
      jobsNav.box.height(0);
    }
  },
  
    toggle : function(obj){
        if(obj.on){
            this.scrollUp(obj);
            obj.on = false;
        }else{
            obj.link.html(obj.hideTxt);

            if(obj.swapBG)
                obj.link.css('backgroundImage', 'url(' + obj.upArrow + ')');
                
            obj.on = true;
            this.scrollDown(obj);
        }
    },
  
    scrollDown : function(obj){
        obj.box.css('display', 'block');
        
        obj.box.animate({
            height: obj.height
        });
    },

    scrollUp : function(obj){
    
        obj.box.animate({
            height: 0,
            callback: function(e) {
                var obj = jQuery(this);
                obj.css('display', 'none');
            }
        });

        obj.link.html(obj.showTxt);
        
        if(obj.swapBG)
            obj.link.css('backgroundImage', 'url(' + obj.downArrow + ')');
            
        obj.on = false;
    }
}

function JobsList()
{
    var cats = [
                'PriorityJobs', 'OfficersAndManagement', 'TradesAndApprenticeships', 'LogisticsHospitalityAndSupport', 'Aviation',
                'HealthCareAndScience', 'HealthScienceChaplaincy', 'BusinessAndAdministration', 'BusinessAdminEducation', 'Engineering', 
                'ITAndEducation', 'CommunicationITIntelligence', 'CombatAndSecurity'
               ];
    var canXhr = true;

    return {
        show: function(service, category, categoryIds, jobId)
        {
            if (canXhr)
            {
                var elem = jQuery('#' + category);
                for (cat in cats)
                {
                    linkElem = jQuery('#link_'+cats[cat]);
                    var tempElem = jQuery('#' + cats[cat]);
                    if (tempElem.attr('id') == elem.attr('id'))
                    {
                        linkElem.addClass('selected');
                        
                        if (elem.html().trim() == '')
                        {
                            // we haven't got this category yet, so go get it via ajax
                            var postVars;
                            if (cats[cat] !== 'PriorityJobs')
                                postVars = "service=" + service + "&category=" + category ;
                            else
                                postVars = "&priority=1&service=" + service + "&category=" + category + "&job=" + jobId + '&categoryIds=' + categoryIds;
                                
                            if (window.location.href.indexOf('https') >= 0)
                                postVars = postVars + '&RequiresSecure=1';
                                
                            canXhr = false;
                            jQuery.post("/global/templates/ajax_categoryjobs.aspx?" + postVars, "", 
                                function(o) {
                                    elem.html(o);
                                    elem.css('display', 'block');
                                    canXhr = true;
                                }
                            );
                        }
                        else
                        {
                            // we've already got this category, so show it
                            elem.css('display', 'block');
                        }
                    }
                    else
                    {
                        // not the category we clicked on - hide it
                        tempElem.css('display', 'none');
                        linkElem.removeClass('selected');                        
                    }
                }
            }
        }
    };
}


