
if (!lm) {
	var lm = {};
}

lm.holidays = {};

function hiddenFormSubmit() {
    var currentCity = $(".current").find('a').attr('title');
    document.getElementById('city').value= currentCity;
    document.forms[0].submit()
}

(function($) {

	// holiday details pricing tabs
	lm.holidays.pricing = function() {
        var showChildHeader = false;
        $(".priceGuide tbody tr").each(function(i, item) {
            if(i == 0) {
                $(item).removeClass("hide");
            }

            if (this.className && !this.className.match('hide')
                     && this.innerHTML.match("childrenPrice")) {
                $("#childrenPriceTH").show();
                showChildHeader = true;
            } else if (!showChildHeader) {
                $("#childrenPriceTH").hide();
            }
        });

        $(".priceTabs a").click(function(event) {
            $(".priceTabs li").each(function(i, item) {
                $(item).removeClass("current");
            });
            $(this).parents("li").addClass("current");
            $(".priceGuide tbody tr.pricing").each(function(i, item) {
                if (!$(item).addClass("hide")) {
                    $(item).addClass("hide");
                }
            });

            $("#" + this.className).removeClass("hide");

            if ($("#" + this.className).html().match("childrenPrice")){
                 $("#childrenPriceTH").show();
            } else {
                $("#childrenPriceTH").hide();
            }
            event.preventDefault();
        });
	};

	// holiday navigation dropdowns
	lm.holidays.nav = function() {

		var selects = $('#holiday_regions, #holiday_countries, #holiday_cities'),
			currentPath = location.href,
			basePath = '/holidays/',
			urlParams,
			selectedNav;

		// extract current selected options as an array from URL
		urlParams = currentPath.match(/(.*\/holidays\/)([^?]+)/);
		if (urlParams) {
			basePath = urlParams[1];
	 		selectedNav = urlParams[2].replace(/\/$|\.html$/, '').split('/');
		}

		selects.cascadingSelects(holidays_data, {
			headings: ['All Destinations', 'All Countries', 'All Cities'],
			selected: selectedNav
		});

		$('#holiday_submit').click(function() {
			var path = [], selectedValue;
			for (var i = 0; i < selects.length; i++) {
				selectedValue = $(selects[i]).val();
				if (selectedValue == '') {
					break;
				} else {
					path.push(selectedValue);
				}
			}
			if (path.length) {
				location.href = basePath + path.join('/') + '.html';
			} else {
				alert("Please select a destination");
			}
		});

	};

	// cascading selects jQuery plugin
	$.fn.cascadingSelects = function (data, options) {

		if (!this.length || !data) {
			return;
		}

		var init = true, state = [], select = [];
		var settings = $.extend({}, $.fn.cascadingSelects.defaults, options);
		function updateSelect(pos, data) {
			var output = '<option value="">' + (settings.headings[pos] || settings.heading) + '</option>';
			for (var i = 0; i < data.length; i++) {
				if (data[i]) {
					output += '<option value="' + data[i].value + '"' + ((init && settings.selected[pos] == data[i].value) ? ' selected="selected"' : '') + '>' + data[i].text + '</option>';
				}
			}
			select[pos].html(output);
		}

		this.each(function(pos, obj) {
			select[pos] = $(obj);
			select[pos].change(function() {
				state[pos] = select[pos].attr('selectedIndex') - 1;
				var selectData = data,
					next = pos + 1;
				for (var i = 0; i <= pos; i++) {
					selectData = (selectData[state[i]] && selectData[state[i]].children) ? selectData[state[i]].children : [];
				}
				if (select[next]) {
					updateSelect(next, selectData);
					select[next].change();
				}
			});
		});

		updateSelect(0, data);
		select[0].change();
		init = false;

		return this;

	};

	$.fn.cascadingSelects.defaults = {
		heading: 'Please select',
		headings: [],
		selected: []
	}

	function setCookie(key, value, days) {
		var expiry = "";
		if (days) {
			expiry = new Date();
			expiry.setDate(expiry.getDate() + days);
			expiry = ";expires=" + expiry.toUTCString();
		}
		document.cookie = (key + "=" + escape(value) + expiry + ';path=/');
	}

	function getCookie(key) {
		var result;
		if (document.cookie.length > 0) {
			result = document.cookie.match(new RegExp(key + "=([^;]+);?"));
		}
		return result && unescape(result[1]);
	}

	function getUrl(param) {
		var result = window.location.href.match(new RegExp("[?&]" + param + "=([^&#]+)"));
		return result && result[1];
	}

	function manageTracking() {
		var key = 'lmautsid',
			value = getUrl('tsid');
		if (value) {
			setCookie(key, value, 30);
		} else {
			value = getCookie(key);
		}
        if (value) {
            value = value.toUpperCase().substr(0, 2);
            $('.tsid').text(value);
            $('#tsid').val(value);
        }
	}

	$(document).ready(function() {
		manageTracking();
	});

})(jQuery);
