(function($) {
	$.extend({
		URLEncode: function(c) {
			var o = '';
			var x = 0;
			c = c.toString();
			var r = /(^[a-zA-Z0-9_.]*)/;
			while(x < c.length) {
				var m=r.exec(c.substr(x));
				if(m != null && m.length > 1 && m[1] != '') {
					o += m[1];
					x += m[1].length;
				} else {
					if(c[x] == ' ') {
						o += '+';
					} else {
						var d = c.charCodeAt(x);
						var h = d.toString(16);
						o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
					}
					x++;
				}
			}
			return o;
		},
		URLDecode: function(s) {
			var o = s;
			var binVal, t;
			var r = /(%[^%]{2})/;
			while((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
				b = parseInt(m[1].substr(1), 16);
				t = String.fromCharCode(b);
				o = o.replace(m[1], t);
			}
			return o;
		}
	});
})(jQuery);

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while(c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		if(c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
}

$(document).ready(function() {

	$("#company").bind("change", function() {
		if($(this).val().length) {
			$(this).parents("form").submit();
		}
	});
	
	$("input.error").bind("keyup", function() {
		if($(this).val().length) {
			$(this).removeClass("error");
			$(this).bind("blur", function() {
				if(!$(this).val().length) {
					$(this).addClass("error");
				}
			});
		}
	});

	$("#fill-details-from-cookie").click(function() {
		var lead_cookie = readCookie("Lead");
		var cookies     = lead_cookie.split("&");
		for(var i = 0; i < cookies.length; i++) {
			var cookie_key   = "" + $.URLDecode(cookies[i].substring(0, cookies[i].indexOf("="))).replace(/\+/g, " ");
			var cookie_value = "" + $.URLDecode(cookies[i].substring(cookies[i].indexOf("=") + 1)).replace(/\+/g, " ");
			$("#" + cookie_key).val(cookie_value);
		}
		$(this).unbind("click");
		return false;
	});

	$("#top-franchises ul.franchises li:gt(3)").hide();
	
	$("#top-franchises a.arrow").bind("click", function() {
		$(this).siblings("ul.franchises").children("li:visible").fadeOut("fast", function() {
			$(this).remove().appendTo($("#top-franchises ul.franchises")).hide();
			$("#top-franchises ul.franchises li:lt(4)").fadeIn();
		});
		return false;
	});

	$("#request-list-large input.franchise-request-checkbox").change(function() {
		if(!$(this).attr("checked")) {
			//$(this).parents("li").fadeOut("medium", function() {
			$(this).parents("li").hide("drop", {}, 500, function() {
				$(this).remove();
			});
		}
	});

	function updateRequestStatus(text) {
		$("#basket-status").fadeOut("fast", function() {
			$(this).children("p").remove();
			$(this).html(text);
			$(this).fadeIn("fast");
			if(text.indexOf("empty") === -1) {
				$("#open-request-list").fadeIn("fast");
			} else {
				$("#open-request-list").fadeOut("fast");
			}
		});
	}

	$("input.franchise-request-checkbox").bind("change", function() {

		var franchise = $(this).val()
		  , action    = $(this).attr("checked") ? "add" : "delete";

		$.post("/request_list.asp", { "franchise" : franchise, "action" : action, "nocache" : Math.round(new Date().getTime() / 1000) }, function(data) {
			updateRequestStatus(data);
		});

	});

	$("#request-list-container a.remove").live("click", function() {
		
		var franchise = $(this).attr("rel")
		  , item      = $(this).parents("li");

		$.post("/request_list.asp", { "franchise" : franchise, "action" : "delete", "nocache" : Math.round(new Date().getTime() / 1000) }, function(data) {
			item.slideUp("fast", function() {
				item.remove();
			});
			$.get("/request_list.asp", { "status" : "1", "nocache" : Math.round(new Date().getTime() / 1000) }, function(data) {
				updateRequestStatus(data);
				$("#request-list-large input.franchise-request-checkbox[value=" + franchise + "]").parents("li").hide("drop", {}, 500, function() {
					$(this).remove();
				});
				$("input.franchise-request-checkbox[value=" + franchise + "]").removeAttr("checked");
			});
		});

		return false;

	});

	function showOverlay() {

		var w = "100%"
		  , h = $(document).height();

		$("#overlay").css("height", h).fadeTo("fast", 0.75, function() {
			showPopup();
		});

	}

	function hideOverlay() {
		$("#overlay").fadeOut("fast");
	}

	function showPopup() {
		$("#popup-list").fadeIn("fast");
	}

	function hidePopup() {
		$("#popup-list").fadeOut("fast", function() {
			hideOverlay();	
		});
	}

	$("#open-request-list").bind("click", function() {
		$.get("/request_list.asp", { "nocache" : Math.round(new Date().getTime() / 1000) }, function(data) {
			$("#request-list-container").html(data);
			showOverlay();
		});
		return false;
	});

	$("#close-list").bind("click", function() {
		hidePopup();
		return false;
	});

});