var jsVoucherCartManager = Class.create ();
jsVoucherCartManager.prototype = {

	isPopover: true,
	cartContentDomId: "voucherShoppingCartSummary",

	text_adding: "",
	text_added: "",
	text_deleteCartItem: "",
	url_addVoucher: "",
	url_tickIcon: "",
	url_noticeIcon: "",

	html_cartLinkHtml: "",
	html_cartSummaryHtml: "",

	// constructor
	initialize: function () {
		return true;
	},

	// 
	set_cart_content_dom_id: function (cartContentDomId) {
		this.cartContentDomId = cartContentDomId;
		return true;
	},

	// record if the cart is in a popover or not so that it can act differently where necessary
	set_is_popover: function (isPopover) {
		this.isPopover = isPopover;
		return true;
	},

	// specify the text that this object is to render
	set_static_text: function (text_adding, text_added, text_deleteCartItem, url_addVoucher, url_tickIcon, url_noticeIcon) {
		this.text_adding = text_adding;
		this.text_added = text_added;
		this.text_deleteCartItem = text_deleteCartItem;
		this.url_addVoucher = url_addVoucher;
		this.url_tickIcon = url_tickIcon;
		this.url_noticeIcon = url_noticeIcon;
		return true;
	},

	// specify the text that this object is to render
	set_dynamic_text: function () {

		return true;
	},

	set_cart_summary_html: function  (html_cartSummaryHtml, html_cartLinkHtml) {

		// update the cart summary html
		this.html_cartSummaryHtml = html_cartSummaryHtml;
		if (this.isPopover) {
			if (jsMessageManager.is_message_visible ("cartSummary"))
				this.show_cart_summary ();
		}
		else
			this.show_cart_summary ();

		// update the cart summary link html
		this.html_cartLinkHtml = html_cartLinkHtml;

		if (this.isPopover)
			this.show_cart_summary_link ();


		return true;
	},
/**
	observe_voucher_popover_forms: function (domId) {
		if ($(domId)) {
			var newDiv = new Element ("div").update ($(domId).innerHTML);	// create the div and set it's innerHTML to the html given
			var tempElements = newDiv.descendants ();
			for (var count = 0; count < tempElements.length; count++) {
				var value = tempElements[count].readAttribute ("id");
				if ((value != "") && (value != null)) {
					if (value.indexOf ("form_diningVoucherAdd") >= 0) {
						eval ("temp = function (event) { event.stop (); jsVoucherCartManager.add_dining_voucher_to_cart (" + /\d+/.exec (value) + "); };");
						$(value).observe ("submit", temp);
					}
				}
			}
			return true;
		}
		return false;
	},

	add_dining_voucher_to_cart: function (voucher2DiningTypeId) {

		jsMessageManager.set_place_holder ("adding" + voucher2DiningTypeId, "link_diningVoucherAddButton" + voucher2DiningTypeId, { xSourcePoint: "right", xDestPoint: "left", xPixelOffset: 8, yPixelOffset: -6 });
		jsMessageManager.set_display_effects ("adding" + voucher2DiningTypeId, "Appear", { duration: 0.4 }, "DropOut", { duration: 0.5 });
		jsMessageManager.reset_messages ("adding" + voucher2DiningTypeId);
		jsMessageManager.show_message ("adding" + voucher2DiningTypeId, this.text_adding);

		// send the request to add the item to the shopping cart
		var requestParameters = new Object ();
		requestParameters.action = "addVoucher";
		requestParameters.voucher2DiningTypeId = voucher2DiningTypeId;
		requestParameters.quantity = $("diningVoucherQty" + voucher2DiningTypeId).value;
		gc_ajaxManager.request (
			this.url_addVoucher,
			{	method: "post",
				parameters: requestParameters },
			"addVoucherToCart" + voucher2DiningTypeId
		);
		return true;
	},
/**/
	// observe a form on index.php that lets a user "add" vouchers to their cart
	observe_restaurant_vouchers_form: function (domId, voucherType, restaurantId) {
		$(domId).observe ('submit', function (event) { event.stop (); jsVoucherCartManager.submit_voucher_form (domId, voucherType, restaurantId); } );
	},

	// submit a form on index.php that lets a user "add" vouchers to their cart
	submit_voucher_form: function (domId, voucherType, restaurantId) {

		if (voucherType == 'diningGeneric') {
			jsMessageManager.set_place_holder ("addingDiningGeneric", "link_diningGenericAddButton", { xSourcePoint: "right", xDestPoint: "left", xPixelOffset: 8, yPixelOffset: -6 });
			jsMessageManager.set_display_effects ("addingDiningGeneric", "Appear", { duration: 0.4 }, "DropOut", { duration: 0.5 });
			jsMessageManager.reset_messages ("addingDiningGeneric");
			jsMessageManager.show_message ("addingDiningGeneric", this.text_adding);
		}
		else if (voucherType == 'diningRestaurant') {
			jsMessageManager.set_place_holder ("addingDiningRestaurant" + restaurantId, "link_diningRestaurantAddButton" + restaurantId, { xSourcePoint: "right", xDestPoint: "left", xPixelOffset: 8, yPixelOffset: -6 });
			jsMessageManager.set_display_effects ("addingDiningRestaurant" + restaurantId, "Appear", { duration: 0.4 }, "DropOut", { duration: 0.5 });
			jsMessageManager.reset_messages ("addingDiningRestaurant" + restaurantId);
			jsMessageManager.show_message ("addingDiningRestaurant" + restaurantId, this.text_adding);
		}

		var requestParameters = $(domId).serialize (true);
		requestParameters.action = "addVoucher";
		requestParameters.voucherType = voucherType;
		requestParameters.restaurantId = restaurantId;
		requestParameters.isPopover = this.isPopover;
		gc_ajaxManager.request (
			this.url_addVoucher,
			{	method: "post",
				parameters: requestParameters },
			"addVoucherToCart" + restaurantId
		);
		return true;
	},

	show_voucher_added_message: function (voucherType, restaurantId) {
		if (voucherType == 'diningGeneric')
			jsMessageManager.show_message ("addingDiningGeneric", "<div style='float: left; margin: 0px; padding: 0px;'><img src='" + this.url_tickIcon + "' /></div><div style='float: left; padding: 0px; margin: 1px 0px 0px 10px;'>" + this.text_added + "</div>", 1500);
		else if (voucherType == 'diningRestaurant')
			jsMessageManager.show_message ("addingDiningRestaurant" + restaurantId, "<div style='float: left; margin: 0px; padding: 0px;'><img src='" + this.url_tickIcon + "' /></div><div style='float: left; padding: 0px; margin: 1px 0px 0px 10px;'>" + this.text_added + "</div>", 1500);
		return true;
	},

	show_voucher_error_message: function (voucherType, restaurantId, message) {
		if (voucherType == 'diningGeneric')
			jsMessageManager.show_message ("addingDiningGeneric", "<div style='float: left; margin: 0px; padding: 0px;'><img src='" + this.url_noticeIcon + "' /></div><div style='float: left; padding: 0px; margin: 1px 0px 0px 10px;'>" + message + "</div>", 3500);
		else if (voucherType == 'diningRestaurant')
			jsMessageManager.show_message ("addingDiningRestaurant" + restaurantId, "<div style='float: left; margin: 0px; padding: 0px;'><img src='" + this.url_noticeIcon + "' /></div><div style='float: left; padding: 0px; margin: 1px 0px 0px 10px;'>" + message + "</div>", 3500);
		return true;
	},








	fetch_shopping_cart: function () {

		var requestParameters = new Object ();
		requestParameters.isPopover = this.isPopover;
		gc_ajaxManager.request (
			this.url_addVoucher,
			{	method: "post",
				parameters: requestParameters },
			"fetchShoppingCart"
		);
		return true;
	},

	show_cart_summary_link: function () {
		if ((this.isPopover) && ($("link_cartSummary"))) {
			$("link_cartSummary").innerHTML = this.html_cartLinkHtml;

			$("link_cartSummary").stopObserving ();
			if (this.html_cartLinkHtml != '')
				$("link_cartSummary").observe ("click", function () { jsVoucherCartManager.show_cart_summary (); });
		}
	},

	show_cart_summary: function () {

		if (this.isPopover) {
			if ($("link_cartSummary")) {

				jsMessageManager.set_place_holder ("cartSummary", "link_cartSummary", { xSourcePoint: "right", xDestPoint: "right", xPixelOffset: 8, yPixelOffset: -6 });
				jsMessageManager.set_display_effects ("cartSummary", "Appear", { duration: 0.4 }, "DropOut", { duration: 0.5 });
				jsMessageManager.reset_messages ("cartSummary");
				jsMessageManager.show_message ("cartSummary", this.html_cartSummaryHtml);
			}

			if ($("link_closeCart"))
				$("link_closeCart").observe ("click", function () { jsMessageManager.hide_message_block ("cartSummary"); } );
		}
		else
			 $(this.cartContentDomId).replace (this.html_cartSummaryHtml);

		if ($(this.cartContentDomId))
			this.observe_cart_links ();

		return true;
	},

	observe_cart_links: function () {
		if ($(this.cartContentDomId)) {
			var newDiv = new Element ("div").update ($(this.cartContentDomId).innerHTML);	// create the div and set it's innerHTML to the html given
			var tempElements = newDiv.descendants ();
			for (var count = 0; count < tempElements.length; count++) {
				var value = tempElements[count].readAttribute ("id");
				if ((value != "") && (value != null)) {
					if (value.indexOf ("link_deleteCartItem") >= 0) {
						eval ("temp = function () { jsVoucherCartManager.delete_cart_item (" + /\d+/.exec (value) + "); };");
						eval ("temp = function () { jsVoucherCartManager.delete_cart_item (" + /\d+/.exec (value) + "); };");
						$(value).stopObserving ();
						$(value).observe ("click", temp);
					}
				}
			}
			return true;
		}
		return false;
	},

/**
	render_shopping_cart: function () {

		var requestParameters = new Object ();
		requestParameters.isPopover = this.isPopover;
		gc_ajaxManager.request (
			this.url_addVoucher,
			{	method: "post",
				parameters: requestParameters },
			"renderShoppingCart"
		);
		return true;
	},
/**/

	delete_cart_item: function (cartItemId) {
		if (confirm (this.text_deleteCartItem)) {
			// send the request to remove the item from the shopping cart
			var requestParameters = new Object ();
			requestParameters.action = "deleteCartItem";
			requestParameters.cartItemId = cartItemId;
			requestParameters.isPopover = this.isPopover;
			gc_ajaxManager.request (
				this.url_addVoucher,
				{	method: "post",
					parameters: requestParameters },
				"deleteCartItem" + cartItemId
			);
		}
		return true;
	}
}
