$(document).ready(function () {
	
	(function () {
		var BDOT = function () {
			var lineage = [];
			(function (a) {
				if (a.parents("ul").length) {
					lineage.push({ anchor: a, title: a.text(), section: a.parents("ul").eq(0) });
					arguments.callee(a.parents("ul").parent().children("a").eq(0));
				}
			})($("#nav a[href=" + window.location.pathname + "]").eq(0));
			this.lineage = lineage;
			
			this.pageTitle = $("#nav-quaternary h2");
			this.contentPrimary = $("#content-primary");
			this.contentSecondary = $("#content-secondary");
			this.subPages = this.contentPrimary.find(".pages > div");
			
			if (!this.pageTitle.text()) {
				if (this.lineage.length == 4) {
					this.pageTitle.text(this.lineage[1].title);
					this.pageTitle.after(this.lineage[0].section);
				} else if (this.lineage.length == 3) {
					this.pageTitle.text(this.lineage[0].title);
					this.pageTitle.after(this.lineage[0].anchor.next());
				} else {
					this.pageTitle.text(document.title);
				}
			}
		};
		
		BDOT.prototype.followDeeplink = function (newLocation, historyData) {
			if (window.location.pathname.indexOf('bermuda_music_festival.aspx') !== -1 && newLocation == 'music-festival-hotels') {
			// Remove when music festival promotion is over... this is a special case.  Deep-linking on this site, needs to be rethought
			// as it is handled very generically...
			$(function () {
			$('#bmfmenu a[href=#hotels]').click();
			});

			} else {
				// Get tab by matching hash to anchor href & get page by matching hash to div id
				var targetTab = $("ul.tabs").find('a[href="#' + newLocation + '"]');
				var targetPage = newLocation == "" ? this.subPages.eq(0) : this.contentPrimary.find("#" + newLocation.replace(/[\/]/g,""));

				// Turn off any current tabs/pages
				this.contentPrimary.find("#page-title").text(targetPage.attr("data-title"));
				this.contentPrimary.find("ul.tabs a").removeClass("active");
				this.subPages.hide();

				// Turn on targets
				targetTab.addClass("active");
				targetPage.show();
			}
		};
		BDOT.prototype.updateWeather = function (c, f) {
            $(".weather .weather-temperatures .celsius").text(c);
            $(".weather .weather-temperatures .fahrenheit").text(f);
		};
		
		window.BDOT = new BDOT();
	})();
	
    // Deeplinking
    dhtmlHistory.addListener(function (n, h) { BDOT.followDeeplink(n, h); });
	BDOT.followDeeplink(window.location.hash.replace("#","") || "");
	
	// Behavioral classes
	$('.js-prompt-text').focus(function () {
		if ($(this).val() == $(this).attr('title'))
			$(this).val('');
	});
	$('.js-prompt-text').blur(function () {
		if ($(this).val() == '')
			$(this).val($(this).attr('title'));
	});
    
    /* IE Patching
    ===================================================== */    
    if ($.browser.msie) {
        if (parseInt($.browser.version) == 6) {
            $("#nav li").hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
        }
        if (parseInt($.browser.version) <= 8) {
            $("li:last-child").each(function () { $(this).addClass("last-child"); });
            $("li:first-child").each(function () { $(this).addClass("first-child"); });
        }
    }  
});

// Google - RSH
window.dhtmlHistory.create({
	toJSON: function (o) { return $.json.serialize(o); },
	fromJSON: function (s) { return $.json.deserialize(s); }
});
dhtmlHistory.initialize();
