// JavaScript Document
s1JS.Jobs.Public = {};

s1JS.Jobs.Public.indexPage = 
{
    init:function()
    {
        var s = s1JS.Jobs.Public.indexPage; // shorthand

        var list = s1JS.Dom.getByClass('button', 'A', s1JS.Dom.get('s1psclear'));    

        for(var i = 0; i < list.length; i++)
        {
            s1JS.Event.addListener(list[i], 'click', s.blurFix);    
        }

        s1JS.Event.addListener('industry', 'change', s.changeSpecialisms);
    },
    blurFix:function(e)
    {
        this.blur();    
    },
    fixLinks:function()
    {
        var anchors = document.getElementsByTagName('A');
        
        for(var j = 0; j < anchors.length; j++)
        {        
            if(anchors[j].getAttribute('href') && anchors[j].getAttribute('rel') == 'external')
            {
                anchors[j].target = '_blank';
            }
        }        
    },
    changeSpecialisms:function(e)
    {
        var core_skill_option = this.options[this.selectedIndex];
        var core_skill = core_skill_option.value;

        var dest_list = s1JS.Dom.get('jobTypeSelect');
        dest_list.disabled = false;
        dest_list.options.length = 0;

        if(!core_skill)
        {
            dest_list.options[dest_list.options.length] = new Option('Please select a core skill', '');            
            dest_list.disabled = true;           
        }

        var specialisms = s1JS.Jobs.js.coreSkills[core_skill];   

        if(specialisms)
        {
            dest_list.options[dest_list.options.length] = new Option('All', '');

            for(var i=0; i < specialisms.length; i++) 
            {
               dest_list.options[dest_list.options.length] = new Option(specialisms[i], specialisms[i]);
            }                
        }
        else
        {
            if(s1JS.Dom.hasClass(core_skill_option, 'specialism'))
            {
                // SPECIAL CASE: specialisms are in the core_skill list in some sections
                var csSpec = core_skill.split('|');

                var real_core_skill = csSpec[0];
                var specialism = csSpec[1];

                // select this core skill in the list
                for(var i = 0; i < this.options.length; i++)
                {
                    if(this.options[i].value == real_core_skill)
                    {
                       this.options.selectedIndex = i;
                    }
                }

                specialisms = s1JS.Jobs.js.coreSkills[real_core_skill];  
                dest_list.options[dest_list.options.length] = new Option('All', '');

                var newSelIndex = 0;
                for(var i=0; i < specialisms.length; i++) 
                {
                    var display_spec = specialisms[i];

                    if(specialisms[i] == specialism) 
                    { 
                        newSelIndex = i + 1; 

                        if(s1JS.Dom.hasClass(core_skill_option, 'changedName'))
                        {
                            display_spec = core_skill_option.text;
                        }
                    }
                    
                    dest_list.options[dest_list.options.length] = new Option(display_spec, specialisms[i]);                    
                    
                }

                dest_list.selectedIndex = newSelIndex;
                   
            }
            else
            {
                dest_list.options[dest_list.options.length] = new Option('None available', '');            
                dest_list.disabled = true;
            }
        }
    }
};

s1JS.Event.onElementReady('s1pssearch', s1JS.Jobs.Public.indexPage.init);
s1JS.Event.onElementReady('s1psfooter', s1JS.Jobs.Public.indexPage.fixLinks);

