﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"/>

(function (window) {
	var constructor = function () {

		var instance = {
			searchPrice: function () {
				var postalCode = $('#txtZipCode').val();
				var heatingOil = $('#chkHeatingOil').attr('checked');
				var propane = $('#chkPropane').attr('checked');

				$.ajax({
					type: "POST",
					url: "/Default.aspx/SearchPrices",
					data: "{postalCode: '" + postalCode + "', heatingOil: " + heatingOil + ", propane: " + propane + "}",
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					success: function (msg) {
						if (msg.d.HeatingOil == null)
							$('#divPrices #trHeatingOil').hide();
						else {
							$('#divPrices #trHeatingOil td:nth-child(2)').html(msg.d.HeatingOil);
							$('#divPrices #trHeatingOil').show();
						}

						if (msg.d.Propane == null)
							$('#divPrices #trPropane').hide();
						else {
							$('#divPrices #trPropane td:nth-child(2)').html(msg.d.Propane);
							$('#divPrices #trPropane').show();
						}

						$('#divPrices').show();
						$('#divPriceSearch').hide();
						$('#divPriceSearching').hide();
					},
					error: function (error) {
						alert("Failed to retrieve prices - " + error.status + ": " + error.statusText);
						$('#divPrices').hide();
						$('#divPriceSearch').show();
						$('#divPriceSearching').hide();
					}
				});
			}
		}

		return instance;
	}

	// Expose global singleton
	window.Default = new constructor();
})(window);

$(function () {
	// Initialize the display of the default images
	$('.Panel1').show();
	$('.Panel2').hide();
	$('.Panel3').hide();

	$('#SignUp').hover(function () {
		$('.Panel1').show();
		$('.Panel2').hide();
		$('.Panel3').hide();
	});

	$('#Order').hover(function () {
		$('.Panel1').hide();
		$('.Panel2').show();
		$('.Panel3').hide();

	});

	$('#Delivery').hover(function () {
		$('.Panel1').hide();
		$('.Panel2').hide();
		$('.Panel3').show();
	});

	$('#imgPriceSearch').click(function () {
		$('#divPrices').hide();
		$('#divPriceSearch').hide();
		$('#divPriceSearching').html('').show();
		setTimeout(function () {
			$('#divPriceSearching').html('Searching. Please wait...');
		}, 500);
		Default.searchPrice();
	});

	$('#divSearchAgain').click(function () {
		$('#divPrices').hide();
		$('#divPriceSearch').show();
		$('#divPriceSearching').hide();
	});

});
