(function(win){
	function Load()
	{
		ShowLoader("Loading schedule");
		var url = "widget/widgetschedule/WidgetPlan.asp?siteid=" + GetValue(document.ScheduleForm.ScheduleSiteCmb) + "&year=" + document.ScheduleForm.ScheduleYearEdit.value;
		$.ajax({
			url: url,
			type: "post",
			async: true,
			cache: false,
			success: function(data) {
				HideLoader();
				$("#PlanContent").html(data);
			},
			error: function(xhr) {
				HideLoader();
				alert(xhr.responseText);
			}
		});
	}

	function SchedulePreviousYear()
	{
		var currentYear = parseInt(document.ScheduleForm.ScheduleYearEdit.value);
		document.ScheduleForm.ScheduleYearEdit.value = currentYear - 1;
		document.ScheduleForm.ScheduleYearEndEdit.value = currentYear;
		LoadSchedulePlan();
	}

	function ScheduleNextYear()
	{
		var currentYear = parseInt(document.ScheduleForm.ScheduleYearEdit.value);
		document.ScheduleForm.ScheduleYearEdit.value = currentYear + 1;
		document.ScheduleForm.ScheduleYearEndEdit.value = currentYear + 2;
		LoadSchedulePlan();
	}

	/******** Job Details *********/
	function OpenJobDialog(id)
	{
		var url = "api/JS_Dashboard_GetJobDetail.asp?id=" + id;
		$.ajax({
			url: url,
			type: "get",
			async: true,
			cache: false,
			success: function(data) {
				var job = JSON.parse(data);

				if(job.Session == 1)
				{
					var source = "";

					source += "<div class=\"SectionTitle\">Job: " + job.JobID + "</div>";

					source += "<div class=\"row\">";

					source += "<div class=\"column\">";
					source += "<div>";
					source += "<label>Required Date</label>";
					source += "</div>";
					source += "<div>";
					source += "<label class=\"bold\">" + job.RequiredDate + "</label>";
					source += "</div>";
					source += "</div>";

					source += "<div class=\"column\">";
					source += "<div>";
					source += "<label>Completed Date</label>";
					source += "</div>";
					source += "<div>";
					source += "<label class=\"bold\">" + job.CompletedDate + "</label>";
					source += "</div>";
					source += "</div>";

					source += "<div class=\"clear\"></div>";
					source += "</div>";

					source += "<div class=\"row\">";

					source += "<div class=\"column\">";
					source += "<div>";
					source += "<label>Job Cost</label>";
					source += "</div>";
					source += "<div>";
					source += "<label class=\"bold\">" + FormatCurrency(job.JobCost) + "</label>";
					source += "</div>";
					source += "</div>";

					source += "<div class=\"column\">";
					source += "<div>";
					source += "<label>Job Status</label>";
					source += "</div>";
					source += "<div>";
					source += "<label class=\"bold\">" + job.JobStatus + "</label>";
					source += "</div>";
					source += "</div>";

					source += "<div class=\"clear\"></div>";
					source += "</div>";

					source += "<div class=\"row\">";
					
					source += "<table cellpadding=\"6\" cellspacing=\"0\" width=\"100%\" border=\"0\">";
					source += "<tr class=\"WidgetGridHeaderLine\">";
					source += "<th align=\"left\" colspan=\"2\">Document Name</th>";
					source += "<th align=\"left\">Category</th>";
					source += "</tr>";

					if(job.Document.length > 0)
					{
						for(var i = 0; i < job.Document.length; i++)
						{
								source += "<tr class=\"WidgetGridData\">";
								source += "<td><a href=\"GetDocument.asp?id=" + job.Document[i].DocumentID + "\" title=\"Download\" target=\"_blank\"><img src=\"images/icon_pdf.png\" width=\"32\" height=\"32\" alt=\"Download\"/></a></td>";
								source += "<td><a href=\"GetDocument.asp?id=" + job.Document[i].DocumentID + "\" title=\"Download\" target=\"_blank\">" + job.Document[i].DocumentName + "</a></td>";
								source += "<td>" + job.Document[i].Category + "</td>";
								source += "</tr>";
						}
					}
					else
					{
						source += "<tr class=\"WidgetGridData\"><td colspan=\"3\" align=\"center\">[Document not available]</td></tr>";
					}
					source += "</table>";
					source += "</div>";

					$("#JobContent").html(source);
				}
				else
				{
					location.href = "Logon.asp";
				}
			},
			error: function(xhr) {
				alert(xhr.responseText);
			}
		});

		ShowBack();
		$("#JobDialog").show();
	}

	function CloseJobDialog()
	{
		HideBack();
		$("#JobDialog").hide();
	}

	win.widgetschedule_onload = Load;
	win.LoadSchedulePlan = Load;
	win.SchedulePreviousYear = SchedulePreviousYear;
	win.ScheduleNextYear = ScheduleNextYear;
	win.OpenJobDialog = OpenJobDialog;
	win.CloseJobDialog = CloseJobDialog;
})(window);