// JavaScript Document
s1JS.Jobs.indexPage = 
{

	
	init:function()
    {
        var s = s1JS.Jobs.indexPage; // shorthand
        s.loadUserData();
        $('#coreSkillSelect').change(s.changeSpecialisms);
        s1JS.Jobs.featuredEmployers(12, 'li');
        s.rotateHomePic(3);
        s.deleteCookies();
    },
    loadUserData:function()
    {
        if(s1JS.Jobs.loggedIn())
        {     
            $('#personalisation').html('<div class="personalisation-loader"><img src="/ui/images/general/ajax-loading.gif" id="ajax-loader" style="padding-top: 40px;"/><h2 style="margin: 0pt;">Loading personalised content...</h2></div>');
            var d = new Date();
            $.getJSON("/myaccount/user-query/?t=" + d.getTime()  , function(data) {              
                if(data && data.logged_in)
                {
                    s1JS.Jobs.indexPage.parseUserData(data);
                }            
            });        
        }
        else
        {
            $('#crm-message').html('Welcome. <a href="/myaccount/sign-in.shtml?return_to=/" class="sign-in-link">Sign in</a> or <a href="/myaccount/register/">set up an account</a> to enjoy the <a href="/myaccount/register/">benefits of membership</a>.');
        }
    },
    parseUserData:function(data)
    {
        var name = ' ' + data.name || '';
        if(data.search_count != 0)
        {
            var job_or_jobs = data.search_count == 1 ? 'job' : 'jobs';
            var summary = data.search_summary || ' jobs';
            summary = summary.replace(/jobs/, job_or_jobs );  

            $('#crm-message').html('Hello' + name + ', this week we have <a href="/search/results.cgi?' + data.search_query + '&stored=1">' + data.search_count + ' new</a> ' + summary + '.');
        }
        else
        {
            $('#crm-message').html('Hello' + name + ', welcome to s1jobs.');
        }
        
        var myTemplate = new s1JS.PerlTemplates({url:'/homepage-personalisation.tmpl', data:data});           
        $('#personalisation').html(myTemplate.getContent());
        $('#personalisation').parent().addClass('active');
        s1JS.Jobs.indexPage.setWidgetEvents();
    },
    setWidgetEvents:function()
    {
        var widget = new s1JS.Widgets.JobAlerts($('#ja'));
        widget.loadSavedState();
        widget.setEvents();  

        widget = new s1JS.Widgets.ApplicationManagement($('#am'));
        widget.loadSavedState();
        widget.setEvents();
        
        widget = new s1JS.Widgets.CareerProfile($('#cp'));
        widget.setEvents();

        /*
         * 
         *      CV AND DOCUMENTS WIDGET
         *  
         */ 
        $('#show-doc-form').click(function() { 
            $('#widget-document-add-form').slideDown();
            return false;
        });
        $('#cancel-doc-form').click(function() { 
            $('#widget-document-add-form').slideUp();
            return false;
        });
        
        var docValidator = new s1JS.Jobs.Validator(document.getElementById('document-add-form'));
        docValidator.addRequired(['new_doc']);
        docValidator.validateOnSubmit();    
    
    },
    changeSpecialisms:function(e)
    {
        var core_skill_option = this.options[this.selectedIndex];
        var core_skill = core_skill_option.value;

        var dest_list = $('#jobTypeSelect')[0];
        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(core_skill.match(/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(core_skill_option.hasClass('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;
            }
        }
    },
    rotateHomePic:function(num_of_homepage_pics)
    {
        if (!$('#content')) return false;
        var pos = Math.floor(Math.random()*num_of_homepage_pics)+1;
        $('#content').addClass("pic0"+pos);
        $('#content').removeClass("pic00");
    },
    deleteCookies:function()
    {
        document.cookie = 'search=; expires=Fri, 28-Sep-1984 00:00:00 GMT';
    }
	
	
	
			
			
			
			
	
};

s1JS.Jobs.addLoader(s1JS.Jobs.indexPage.init);




$(document).ready(function(){

	
	$('#post-a-job').hover(
      function () {
        $(this).addClass('forceCursor');
      }, 
      function () {
        $(this).removeClass('forceCursor');
      }
    );
	
	
	$('#post-a-job').click(function() {
			
			var link = $(this).children("h2").children("a").attr("href");
			if (link == undefined){
				alert('No link');
				return false;
				}
			else
				{
					window.location.href = link;
				}	
    });
	

	// News panel dynamic loading
	// loads a generic or locally specific news panel depending on what the location check script returns
	var includeNewsFileName;

		      includeNewsFileName = "/ssi/" + "_home-news.html";
		
		
		      $.ajax({
				 url: includeNewsFileName,
				 contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
				 success: function(data) {			
				     $('#loadingHomeNewsPlaceholder').hide(); // hide the loading message
				     $('#homeNewsDataWrapper').hide(); // pre-hide the news content container
				     $('#homeNewsDataWrapper').html(data); // load the news data into the content container
				     $('#homeNewsDataWrapper').fadeIn(); // fade the news container into view for a smooth loading effect
				 }
			     });
		
	
	
	
	// Featured employers panel dynamic loading
	// loads the generic or locally specific featured employers depending on what the location check script returns
	var includeRecruitersFileName = "/ssi/featured-employer.html";

	$.ajax({
		  url: includeRecruitersFileName,
		  success: function(data) {
			$('#recruiter-logo-holder').hide(); // pre-hide the featured employer content container
			$('#recruiter-logo-holder').html(data); // load the featured employer data into the content container
			$('#recruiter-logo-holder').fadeIn(); // fade the news container into view for a smooth loading effect
			s1JS.Jobs.featuredEmployers(12, 'li');
		  }
		});


});



