﻿/// <reference path="../Edentity.Global.js" />
/// <reference path="../External/jquery-1.4.1-vsdoc.js" />
/// <reference path="StoreLocatorHeader.ascx.js" />
/// <reference path="../Shoppers.Cookie.js" />

Edentity.RegisterNamespace("Shoppers.Controls.LocatorBar");


(function(SLB, $) {

	var StoreLocatorArea = null;
	var pnlLocatorBar = null;

	SLB.OnInit = function(pnlLocatorBarID, PostalCodeLabel) {

		if (GetPostalCode().length > 0) {

			var temp = GetPostalCode();
			temp = temp.slice(0, 3) + " " + temp.slice(3);
			$('#PostalCode').val(temp);
		}

		pnlLocatorBar = $("#" + pnlLocatorBarID);
		StoreLocatorArea = $("div.StoreLocatorHeader");
        /*
		// Close this when clicking elsewhere on page
		$(document).click(function(e) {
			var el = e.target;
			if (StoreLocatorArea.parent().parent().parent().find('*').index(el) >= 0 || $(el).is("img.Search")) {
				return;
			}
			else {
				Shoppers.Controls.LocatorBar.OnClose();
				//                var go = $("img.Search", pnlLocatorBar);
				//                go.removeAttr("disabled");
				//                go.prev().removeAttr("disabled");
				//                go.prev().prev().removeAttr("disabled");
			}
		});

		$('input[type=text]', pnlLocatorBar)
        .Watermark(PostalCodeLabel)
        .keypress(function(e) {
        	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        		$('img.Search', pnlLocatorBar).click();
        		return false;
        	} else {
        		return true;
        	}
        });
        */
	};

	SLB.OnSearch = function(sender) {
		var go = $(sender);
		var postalcodeStr = go.prev().prev().val().replace(/^\s+|\s+$/g, "");
		var rePostalCode = /^[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z]\s*\d[a-ceghj-npr-tv-z]\d$/i;

		if (postalcodeStr.length > 0 && postalcodeStr != go.attr('watermark') && rePostalCode.test(postalcodeStr)) {
	
			if (location.protocol.toLowerCase() == 'https:') {
				var url = go.attr("locatorurl") + "?pc=" + postalcodeStr;
				var sh = $.trim(go.prev().val());
				if (sh.length > 0) {
					url += "&sh=" + sh;
				}
				window.location = url;
				return;
			}
		
			go.attr("disabled", true);
			go.prev().attr("disabled", true);
			go.prev().prev().attr("disabled", true);
			Shoppers.Controls.LocatorBar.OnShow(postalcodeStr, go.prev().val());
		}
	};

	SLB.OnFooterInit = function(pnlLocatorFooterID, PostalCodeLabel) {
        
        alert($("#" + pnlLocatorFooterID + " :text.PostalCode").length);
        $("#" + pnlLocatorFooterID + " :text.PostalCode").Watermark(PostalCodeLabel);

//        .keypress(function(e) {
//        	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
//        		$("#" + pnlLocatorFooterID + " img.Search").click();
//        		return false;
//        	} else {
//        		return true;
//        	}
//        });
	};

	SLB.OnFooterSearch = function(sender) {
		var go = $(sender);
		var postalcodeStr = go.prev().val().replace(/^\s+|\s+$/g, "");
		var rePostalCode = /^[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z]\s*\d[a-ceghj-npr-tv-z]\d$/i;

		if (postalcodeStr.length > 0 && postalcodeStr != go.attr('watermark') && rePostalCode.test(postalcodeStr)) {
			var url = go.attr("locatorurl") + "?pc=" + postalcodeStr;
			var sh = $.trim(go.prev().prev().val());
			if (sh.length > 0) {
				url += "&sh=" + sh;
			}
			window.location = url;
		}
	};

	SLB.OnClose = function() {
		StoreLocatorArea.slideUp("fast");
		var go = $("img.Search", pnlLocatorBar);
		go.removeAttr("disabled");
		go.prev().removeAttr("disabled");
		go.prev().prev().removeAttr("disabled");
	};

	SLB.OnShow = function(postalCode, storeHours) {
		//Expand locator window
		StoreLocatorArea.slideDown("fast");

		if (typeof Shoppers.Controls.StoreLocatorHeader.Search === 'function') {
			Shoppers.Controls.StoreLocatorHeader.Search(postalCode, storeHours);
		}
	};

	function GetPostalCode() {
		try {
			return Shoppers.Cookie.Get("eFlyerPostalCode");
		} catch (e) {
			return '';
		}
	}

})(Shoppers.Controls.LocatorBar, jQuery);


