s1JS.Widgets = {};

s1JS.Widgets.Base = Base.extend({
    
    constructor:function($id)
    {
        this.$id = $id;      
    }
    
});

s1JS.Widgets.ResultsBase = s1JS.Widgets.Base.extend({

    default_results:10,
    searches:{},
    
    constructor:function($id)
    {
        this.base($id);
        this.searches = {}; // seems to be that all objects that inherit this share it..
    },
    
    loadSavedState:function()
    {
        // Parse the saved state of the widget (if it exists)
        var state = this.$id.attr('options');
        if(state)
        {
            var searches = state.split(':');
            for(var i =0; i < searches.length; i++)
            {
                var params = searches[i].split('=');
                this.searches[params[0]] = params[1];
            }
        }   
    },  

    getState:function()
    {
        var tmp = [];
        // target: search_id=num_results:search_id2=num_results2..etc.
        for(var prop in this.searches)
        {
            tmp.push(prop + '=' + this.searches[prop]);
        }
        return tmp.join(':');
    },  
    
    setEvents:function()
    {
        var this_widget = this;
        
        // SHOW MORE RESULTS BUTTON
        $('.results-add', this.$id).click(function() { 
            var $results = $(this).parent().parent().next();
            $('tr:hidden:first', $results).show();
            var id = this.href.match(/#(\w+)/)[1];
            this_widget.searches[id]++;                        
            return false;
        });       

        // SHOW LESS RESULTS BUTTON
        $('.results-remove', this.$id).click(function() { 
            var $results = $(this).parent().parent().next();
            $('tr:visible:last', $results).hide();
            var id = this.href.match(/#(\w+)/)[1];
            this_widget.searches[id]--;
            return false;
        });         
        
        
        // let's go through each search and show them the number of options they asked for.
        // if they don't have a record of the search, set it to the default
        $('.widget-results', this.$id).each(function() {
            var raw_id = this.id;
            var id = raw_id.match(/res_(\w+)/)[1];            
            if(!this_widget.searches[id])
            {
                this_widget.searches[id] = this_widget.default_results;
            }
            
            var num_to_show = (this_widget.searches[id] > 0) ? this_widget.searches[id] - 1 : 0;
            $('tr:gt(' + num_to_show  + ')', this).hide();
        });      
    }

});

/*
 *   Application Management Widget
 */
s1JS.Widgets.ApplicationManagement = s1JS.Widgets.ResultsBase.extend({

	
	setEvents:function()
    {    
        this.base();
		
		$('#closingRemindersOn').click(function() {       
			$('#save-application-settings').trigger('click');           
        });
		
		$('#closingRemindersOff').click(function() {       
			$('#save-application-settings').trigger('click');           
        });
        
        $('#save-application-settings').click(function() {           
			
			var url = '/myaccount/stored-jobs-change/?js=1&closing_reminder=';                        
            url += ($('#closingRemindersOn').is(':checked')) ? 'Y' : 'N';   
		    $.get(url, function(data){
				if($('#closingRemindersOn').is(':checked')){
				  	$('#closingReminderStatus').addClass('on');
					$('#closingReminderStatus').html('on');
				  }
				  else {
				  	$('#closingReminderStatus').removeClass('on');
					$('#closingReminderStatus').html('off');
				  }
			}			  
  			);			

            return false;            
        });
    }

});

/*
 *   Career Profile Widget
 */
s1JS.Widgets.CareerProfile = s1JS.Widgets.Base.extend({
    
    setEvents:function()
    {
        if(document.getElementById('cp-dropdown'))
        {           
            var $drop = $('#cp-dropdown');
            var default_text = $drop.html();
            
            var base = '/myaccount/set-profile-status/';
            var data = [{ text: 'live', val: 'live', selected: (default_text.match('live')) },
                        { text: 'anonymous', val: 'anonymous', selected: (default_text.match('anonymous')) },                        
                        { text: 'suspended', val: 'suspended', selected: (default_text.match('suspended')) }];    
                        
            var sel = document.createElement('SELECT');  
            var sel_ind = 0;
            for(var i = 0; i < data.length; i++)
            {              
                sel_ind = data[i].selected ? i : sel_ind;
                $(sel).append('<option value="' + data[i].val + '">' + data[i].text + '</option>');
            }
            sel.selectedIndex = sel_ind;               
            $drop.html(sel);            
            
            $('select', $drop).change(function() { 
                var $this = $(this);
                var url = '/myaccount/set-profile-status/?status=' + $this.val();
                url += '&t=' + new Date().getTime();
                
                var sel = this;
                var sel_ind = this.selectedIndex;                
                $this.append('<option value="">updating...</option>');
                this.selectedIndex = this.options.length - 1;
                
                $.get(url, function() {
                    sel.options.length = sel.options.length - 1;
                    sel.selectedIndex = sel_ind;
                });
            });            

        }
    }

});

/*
 *   The Job Alerts widget
 */
s1JS.Widgets.JobAlerts = s1JS.Widgets.ResultsBase.extend({ 
    
    setEvents:function()
    {   
        // Syntax for SUPER calls in Base
        this.base();        
       
        $('.alerts-email-dropdown').each(function() { 
            var default_text = this.innerHTML;
            var base = $('a', this).attr('href');
            
            var data = [{ text: 'Immediately', link: base + '&js=1&email_freq=Every hour', selected: (default_text.match('Every hour')) },
                        { text: 'Every day', link: base + '&js=1&email_freq=Every day', selected: (default_text.match('Every day')) },
                        { text: 'Every 3 days', link: base + '&js=1&email_freq=Every 3 days', selected: (default_text.match('Every 3 days')) },                        
                        { text: 'Every week', link: base + '&js=1&email_freq=Every week', selected: (default_text.match('Every week')) },                        
                        { text: 'Every month', link: base + '&js=1&email_freq=Every month', selected: (default_text.match('Every month')) }, 
                        { text: 'Never', link: base + '&js=1&email_freq=Never', selected: (default_text.match('Never')) }];
                        
            var sel = document.createElement('SELECT');  
            var sel_ind = 0;
            for(var i = 0; i < data.length; i++)
            {              
                sel_ind = data[i].selected ? i : sel_ind;
                $(sel).append('<option value="' + data[i].link + '">' + data[i].text + '</option>');
            }
            sel.selectedIndex = sel_ind;               
            $(this).html(sel);
            
            $('select', this).change(function() { 
                var $this = $(this);
                var url = $this.val();
                url += '&t=' + new Date().getTime();
                
                var sel = this;
                var sel_ind = this.selectedIndex;                
                $this.append('<option value="">updating...</option>');
                this.selectedIndex = this.options.length - 1;
                
                $.get(url, function() {
                    sel.options.length = sel.options.length - 1;
                    sel.selectedIndex = sel_ind;
                });
            });

        });
        
         /* CHANGE SMS FREQUENCY */     
        $('.alerts-sms-dropdown').each(function() { 
            var default_text = this.innerHTML;
            var base = $('a', this).attr('href');

            var data = [{ text: 'Max 5 per week', link: base + '&js=1&sms_freq=5', selected: (default_text.match('5 per')) },
                        { text: 'Max 10 per week', link: base + '&js=1&sms_freq=10', selected: (default_text.match('10 per')) },                        
                        { text: 'Max 25 per week', link: base + '&js=1&sms_freq=25', selected: (default_text.match('25 per')) },                        
                        { text: 'Max 35 per week', link: base + '&js=1&sms_freq=35', selected: (default_text.match('35 per')) },                                              
                        { text: 'Never', link: base + '&js=1&sms_freq=Never', selected: (default_text.match('Never')) }];
                        
            var sel = document.createElement('SELECT');  
            var sel_ind = 0;
            for(var i = 0; i < data.length; i++)
            {              
                sel_ind = data[i].selected ? i : sel_ind;
                $(sel).append('<option value="' + data[i].link + '">' + data[i].text + '</option>');
            }
            sel.selectedIndex = sel_ind;               
            $(this).html(sel);
            
            $('select', this).change(function() { 
                var $this = $(this);
                var url = $this.val();
                url += '&t=' + new Date().getTime();
                
                var sel = this;
                var sel_ind = this.selectedIndex;                
                $this.append('<option value="">updating...</option>');
                this.selectedIndex = this.options.length - 1;
                
                $.get(url, function(data) {
                    if(data == 'unconfirmed')
                    {
                        var full = url;
                        full = full.replace(/&?js=1/,'');
                        window.location.href = full + '&from=main';
                    }
                    else
                    {
                        sel.options.length = sel.options.length - 1;
                        sel.selectedIndex = sel_ind;
                    }                

                });
            });                        
        });        

        /* ADD JOB ALERT FORM */
        $('#add-job-alert').click(function() {
            $('#widget-job-alert-form').slideDown();
            return false;
        });    
        
        $('#core-skill-select').change(function() { 
            var core_skill = $(this).val();
            var dest_list = document.getElementById('jobtype');
            dest_list.options.length = 0;
            dest_list.disabled = false;
            
            if(!core_skill)
            {
                dest_list.options[dest_list.options.length] = new Option('Please select a Core Skill...', '');                  
                dest_list.disabled = true;
                return 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
            {
                dest_list.options[dest_list.options.length] = new Option('None available', '');            
                dest_list.disabled = true;
            }
        });

    }
    
});
