﻿/// <reference path="~/scripts/jsonhelper.js" />
/// <reference path="~/scripts/conf/ui.js" />

ui.cart = {

	items: [],

	// --------------------------------------------------------

	init: function() {
		//console.info("ui.cart init");

		this.items = $.cookies.get('cart');

		if (!this.items) {
			this.items = [];
		} else {
			$.each(this.items, function(index, item) {
				ui.cart.addRow(item);
			});
		}

		$("div#cart div.bottom a.delete_all").click(ui.cart.removeAllHotels);

		if (ui.cart.isEventRequestFormPage()) {
			$("div#cart div.bottom button").button({
				icons: { primary: 'ui-icon-triangle-1-w', secondary: 'ui-icon-empty' },
				label: "Tilføj flere"
			});
		} else {
			$("div#cart div.bottom button").button({
				icons: { primary: 'ui-icon-empty', secondary: 'ui-icon-triangle-1-e' }
			})
			.children(".ui-button-text").css("padding-left", "1em")
			.children(".ui-button-icon-primary").remove();
		}

		$("div#cart div.bottom button").css("width", "170px").css("text-align", "left").click(function(event) {
			event.preventDefault();
			ui.cart.submitCart();
			$(this).removeClass("ui-state-focus");
		});

		ui.cart.updateButtons();

		ui.cart.createDialog();
	},

    // --------------------------------------------------------

    isEventRequestFormPage: function() {
        return location.href.toLowerCase().indexOf(LINK_FACTORY_EVENT_REQUEST) != -1;
    },

	// --------------------------------------------------------

	addRow: function(hotel) {

		var tr = $("<tr class=\"id_" + hotel.Id + "\"><td>" + hotel.Name + "</td><td style=\"width: 10px;\"><a href=\"#\" title=\"Slet fra listen\"><img src=\"/assets/images/conf/icon_false.png\" alt=\"Slet fra listen\" /></a></td></tr>");

		$('td:last-child a', tr).click(function() { ui.cart.removeHotel(hotel); return false; });

		$("div#cart div.content table").append(tr);

		ui.cart.updateButtons();
	},

	// --------------------------------------------------------

	removeHotel: function(hotel, force) {
		if (force == true || confirm("Vil du fjerne " + hotel.Name + " fra listen?")) {
			$("div#cart div.content table tr.id_" + hotel.Id).remove();

			ui.cart.items.splice($.inArray(hotel, ui.cart.items), 1);

			$.cookies.set('cart', ui.cart.items);

			if (ui.search)
				ui.search.hotelRemovedFromCart(hotel);

			// HotelDetailView.ascx
			if (typeof hotelRemovedFromCart == 'function')
				hotelRemovedFromCart(hotel);

			ui.cart.updateButtons();
		}
	},

	// --------------------------------------------------------

	removeAllHotels: function(force) {

		//ui.cart.showDialog("Vil du fjerne alt fra listen?");

		if (force == true || confirm("Vil du fjerne alt fra listen?")) {
			$("div#cart div.content table tr").remove();

			ui.cart.items = [];

			$.cookies.set('cart', ui.cart.items);

			if (ui.search)
				ui.search.allHotelsRemovedFromCart();

			// HotelDetailView.ascx
			if (typeof allHotelsRemovedFromCart == 'function')
				allHotelsRemovedFromCart();

			ui.cart.updateButtons();
		}

		return false;
	},

	// --------------------------------------------------------

	addToCart: function(hotel) {
		//console.info("ui.cart addToCart");

		var exists = false;
		$.each(this.items, function(index, item) {
			if (hotel.Id == item.Id) {
				exists = true;
				return false;
			}
		});

		if (exists)
			return false;

		this.items.push({ Id: hotel.Id, Name: hotel.Name });

		$.cookies.set('cart', this.items);

		ui.cart.addRow(hotel);

		return true;
	},

	// --------------------------------------------------------

	isHotelInCart: function(id) {
		return $("div#cart div.content table tr.id_" + id).length != 0;
	},

	// --------------------------------------------------------

	updateButtons: function() {
		var elems = $("div#cart div.bottom a, div#cart div.bottom button");
		var info = $("div#cart div.bottom div");
		if (ui.cart.items.length == 0) {
			elems.hide();
			info.show();
		} else {
			elems.show();
			info.hide();
		}
	},

	// --------------------------------------------------------

	submitCart: function() {
	    if (ui.cart.isEventRequestFormPage())
			location.href = "/";
		else
		    location.href = LINK_FACTORY_EVENT_REQUEST;
	},

	// --------------------------------------------------------

	getHotelIds: function() {
		return jQuery.map(this.items, function(hotel) { return hotel.Id; });
	},

	// --------------------------------------------------------

	createDialog: function() {
		$("#dialog-cart-confirm").dialog({
			resizable: false,
			height: 150,
			modal: true,
			autoOpen: false,
			buttons: {
				'Ja': function() {
					$(this).dialog('close');
				},
				'Nej': function() {
					$(this).dialog('close');
				}
			}
		});
	},
	// --------------------------------------------------------

	showDialog: function(text) {
		var t = "<p><span class=\"ui-icon ui-icon-alert\" style=\"float:left; margin:0 7px 20px 0;\"></span>" + text + "</p>";

		$("#dialog-cart-confirm").html(t).dialog('open');
	}
};
