﻿/// <reference path="../../jquery-1.3.2-vsdoc.js" />
/// <reference path="../../dsbo.js" />
/// <reference path="../../dsbo/dsbo.ajax-extensions.js" />
/// <reference path="../../dsbo/dsbo.numeric-extensions.js" />
/// <reference path="../../dsbo/dsbo.string-extensions.js" />

/*
	Updates to this file must be run through the compressor at:
	http://javascriptcompressor.com/
	save output to savejobs.min.js
*/

/*global $, jQuery, document, window */

jQuery.Namespace.Register('jQuery.DSBO.Search');

(function($) {
	$('a.favorite').each(function(i, el) {
		var id = $(el).attr("id").split("_");
		$(el).click(function(e) {
			if ($(el).hasClass('selected')) {
				$(el).removeClass('selected');
				$.DSBO.Search.DeleteJob(this, id[1], $(el).text(), "");
			}
			else {
				$(el).addClass('selected');
				$.DSBO.Search.SaveJob(this, id[1], $(el).text(), "");
			}
			return false;
		});
	});
})(jQuery);

jQuery.DSBO.Search.SaveJob = function(what, id, title, url) {
	var values = $.format("&id={0}&title={1}&url={2}", id, title, url);
	var data = $.format('{0}{1}', $.getAntiForgeryToken(), values);
	$.postJSON('/Search/Save/Job', data, function(result) {
		if (result === false) {
			$(what).removeClass('selected');
			alert("There was a problem saving this job");
		}
	});
};

jQuery.DSBO.Search.DeleteJob = function(what, id, title, url) {
	alert('delete');
	var values = $.format("&id={0}&title={1}&url={2}", id, title, url);
	var data = $.format('{0}{1}', $.getAntiForgeryToken(), values);
	$.postJSON('/Search/Delete/Job', data, function(result) {
		if (result === false) {
			$(what).addClass('selected');
			alert("There was a problem deleting this job");
		}
	});
};
