MG.namespace("MG.widget.JobPosting");
MG.widget.JobPosting = (function() {
	
	var templates = {
		job_opp : [
			'<div class="job-cycle modal-content-wrapper">',
				'<div style="display:none;" id="{id}" class="meta-id"></div>',
				'<h3>{job_name}</h3>',
				'<div class="job-wrapper">' +
					'<div class="job-column">' +
						'<dl>' +
							'<dt>Posted</dt>' +
							'<dd>{date_modified}</dd>' +
							'<dt>Compensation</dt>' +
							'<dd>{compensation}</dd>' +
							'<dt>Employment Type</dt>' +
							'<dd>{employment_type_enum}</dd>' +
						'</dl>' +
					'</div>' +
					'<div class="job-column">' +
						'<dl>' +
							'<dt>Location</dt>' +
							'<dd>{location}</dd>' +
							'<dt>Industry</dt>' +
							'<dd>{job_industry_enum}</dd>' +
							'<dt>Category</dt>' +
							'<dd>{job_category_enum}</dd>' +
							'<dt>Reference Number</dt>' +
							'<dd>{job_number}</dd>' +
						'</dl>' +
					'</div>' +
					'<div class="job-description">' +
						'<strong>Job Description</strong>' +
						'<p>{job_description}</p>' +
					'</div>' +
				'</div>',
			'</div>'
		].join("")
	};
	
	/**
	 * Shows a modal job description
	 */
	function showJob(id, showApply) {
		MG.util.Modal.showSaving();
		$.ajax(MG.util.Async.getConfig({
			data : {
				action	: 'getJob',
				job_id	: id
			},
			success	: function(json) {
				MG.util.Modal.hideModal();
				var o = $.parseJSON(json);
				var t = new MG.util.Template(templates.job_opp);
				var buttons = {
					'Close' : function() {
						$(this).dialog("close");
					}
				}
				
				if(showApply === true) {
					buttons['Apply for Position'] = function() {
						MG.widget.Applicant.applyForPosition($(this).find(".meta-id").attr("id"));
					}
				}
				
				$("#modal_content").empty();
				t.applyValues(o);
				t.append("modal_content");
				
				$("#modal_content").dialog({
					title	: 'Position Details',
					modal	: true,
					width	: 650,
					buttons	: buttons
				});
			}
		}));
	}
	
	/**
	 * Hides the job posting overlay/modal
	 */
	function hideJob() {
		$("#modal_content").dialog('close');
		$("#jobs").cycle('resume');
	}
	
	
	var jobsCycleWrapper = $("#jobs");
	/**
	 * Starts the jquery-cycle carousel of job postings
	 */
	function startJobCycle() {
		var c = $("#jobs").cycle({
			fx : 'scrollLeft',
			pauseOnPagerHover : 1,
			before : function(currentSlide, nextSlide, options, forwardFlag) {
				jobsCycleWrapper.height($(nextSlide).height() + 10 +  "px");
			}
		});
	
		$(".job-cycle-control-wrapper").click(function(e) {
			var t = $(e.target);
			
			if(t.hasClass("prev")) {
				$("#jobs").cycle('prev');
			} else if(t.hasClass('next')) {
				$("#jobs").cycle('next');
			} else if(t.hasClass('pause') || t.hasClass('play')) {
				$("#jobs").cycle('toggle');
				t.toggleClass('play').toggleClass('pause');
			} else if(t.hasClass('apply')) {
				showJob(e.target.id, true);
			}
		});
	}
	
	MG.registerBoot(startJobCycle);
	
	return {
		hideJob				: hideJob,
		showJob				: showJob
	}
})();
