﻿JOBLIST = {};

JOBLIST.saveLinkMode = {
	Add: {
		value: 0,
		image: "/global/images/jobSave.png"
	},
	Remove: {
		value: 1,
		image: "/global/images/jobRemove.png"
	}
};

JOBLIST.saveLinkModeDark = {
	Add: {
		value: 0,
		image: "/global/images/jobSave_dark.png"
	},
	Remove: {
		value: 1,
		image: "/global/images/jobRemove_dark.png"
	}
}

JOBLIST.saveJobLink = function (buttonId, jobId, entryTypeId, status, saveLinkMode) {
    // private variables
    var button;
    
    entryTypeId = entryTypeId || ''; 
    
    saveLinkMode = saveLinkMode || JOBLIST.saveLinkMode;

    var toggle = function(){
        //Make an async request to toggle the status of the current job
        var postVars ='jobid='+jobId+'&entrytypeid='+entryTypeId;
        if (window.location.href.indexOf('https') >= 0)
            postVars = postVars + '&RequiresSecure=1';
            
        jQuery.post("/global/savedjobs/ajax_job_toggle.aspx", postVars, success);
	};

	var success = function() {
	    // Update button
        if (status === saveLinkMode.Add) {
			status = saveLinkMode.Remove;
		}
		else {
			status = saveLinkMode.Add;
		}
		button.attr('src', status.image);
	};
    	
    var init = function() {
        // save reference to the button
        button = jQuery('#' + buttonId);
        
        // add click handler
        button.click(function(e) {
            toggle();
            if (e.preventDefault) {
                e.preventDefault();
            }
            return false;
        });
        
        // dont need it any more
        buttonId = null;
    }(); // gets called right away

    return {
        // Public stuff
        toggle: toggle,
        getJobId: function() { return jobId; },
        getEntryTypeId: function() { return entryTypeId; }
    };    
};

