﻿/// <reference path="/Scripts/jquery.intellisense.js"/>

$(document).ready(function() {
	App.bind();
});

var App =
{
	aboutToHideNav: false,
	timer: null,
	isIE6: ($.browser.msie && $.browser.version < 7),
	isIE7: ($.browser.msie && $.browser.version == "7.0"),

	bind: function() {
		// popup menu ie7 fix */
		if (App.isIE7) {
			App.bindIE7Fixes();
		}
		App.bindSpecialOffers();
		App.bindNavEvents();
	},

	bindIE7Fixes: function() {
		$("#Footer ul li").each(function() {
			$(this).css("background-position", "left 6px");
		});
	},

	bindSpecialOffers: function() {
		// setup ul.tabs to work as tabs for each div directly under div.panes
		$("ul.ExclusiveOffersTabs").tabs("div.ExclusiveOffersPanels > div.OfferPanel");
	},

	bindNavEvents: function() {
		$("#Nav ul li.NavItem").mouseenter(function() {
			$("#Nav ul li.NavItem .FlyoutWrapper").hide();
			$(this).children(".FlyoutWrapper").show();
			clearTimeout(App.timer);
			App.aboutToHideNav = false;
		});
		$("#Nav ul li .FlyoutWrapper").mouseenter(function() {
			clearTimeout(App.timer);
			App.aboutToHideNav = false;
		});
		$("#Nav ul li.NavItem").mouseleave(function() {
			if (!App.aboutToHideNav) {
				App.aboutToHideNav = true;
				clearTimeout(App.timer);
				App.timer = setTimeout(App.hideNav, 3000);
			}
		});
	},

	hideNav: function() {
		if (App.aboutToHideNav) {
			$("#Nav ul li .FlyoutWrapper").fadeOut(500);
		}
	},

	getPageDimensions: function() {
		var dimensions = { width: 0, height: 0 };
		if (App.isIE6) {
			if (document.body.scrollHeight > document.body.offsetHeight) {
				dimensions.width = document.body.scrollWidth;
				dimensions.height = document.body.scrollHeight;
			}
			else {
				dimensions.width = document.body.offsetWidth;
				dimensions.height = document.body.offsetHeight;
			}
		}
		else {
			var doc = $(document);
			dimensions.width = doc.width();
			dimensions.height = doc.height();
		}
		return dimensions;
	},

	openWindow: function openWindow(url, name, width, height, resizable, scrollbars, statusbar, menubar, toolbar) {
		var win = null;
		var optionString = "";
		if (width) optionString += "width=" + width + ",";
		if (height) optionString += "height=" + height + ",";
		if (resizable) optionString += "resizable=1,";
		if (scrollbars) optionString += "scrollbars=1,";
		if (statusbar) optionString += "status=1,";
		if (menubar) optionString += "menubar=1,";
		if (toolbar) optionString += "toolbar=1,";
		if (optionString != "") optionString = optionString.substr(0, optionString.length - 1);
		win = window.open(url, name, optionString);
		if (win) win.focus();
		return win;
	},

	printPage: function() {
		if (window.print) window.print();
		else alert("Please select \"Print...\" from the File menu.");
	}
}

jQuery.fn.center = function() {
	this.css("position", "absolute");
	this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
	this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
	return this;
}

function rgb2hex(rgb) {
	function hex(x) {
		return parseInt(x).toString(16);
	}
	if (rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)) {
		rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}
}

function launchBooking() {
	var bookingURL = "https://booking.ihotelier.com/istay/istay.jsp?HotelID=73179&language=11";
	if (window._gaq)
	{
		_gaq.push(['_link', bookingURL]);
	}
	App.openWindow(bookingURL, "booking", 1000, 700, 0, 0);
}
