﻿/// <reference path="json2.js" />
/// <reference path="enviro.js" />
/// <reference path="jquery/jquery-1.3.2.js" />
/// <reference path="jquery/jquery.ajaxdotnet.3.js" />
/// <reference path="jquery/jquery.hash.1.js" />
/// <reference path="jquery/jquery.pan.js" />
/// <reference path="jquery/jquery.history.js" />
/// <reference path="jquery/jquery.cookie.js" />
/// <reference path="swfobject-redux.js" />
/// <reference path="pca_finder.js" />
/// <reference path="pages/home.js" />
/// <reference path="pages/section.js" />
/// <reference path="pages/video.js" />
/// <reference path="pages/contact.js" />

// Document Ready
$(document).ready(function() {
	if ($.browser.msie) {
		setInterval(checkTitle, 500);
	}

	var hash = $(document).hash();
	if ((!hash && entry.home) || /home/.test(hash)) {
		loadFlash();
	}
	else {
		loadTemplateAssets();
	}

	$.history.init(history_load);

	logEvent("entry", entry.section, entry.page);
});

// Navigation/History
var goTo = function(section, page) {
	var hash = (section ? section : "home") + (page ? "/" + page : "");
	$.history.load(hash);

	logEvent("nav", hash);
};

var history_load = function(hash) {
	var section = null;
	var page = null;
	var envOnly = false;

	if (hash && hash != "") {
		var arr = hash.split("/");
		section = arr[0];
		page = (arr.length > 1 ? arr[1] : null);
	}
	else {
		section = entry.section;
		page = entry.page;
		envOnly = true;
	}

	pageLoad(section, page, envOnly);
};

var pageLoad = function(section, page, envOnly) {
	var current = $(document).data("current.page");
	var next = null;

	switch (section) {
		case "facilities":
		case "apartments":
		case "visit":
		case "plans":
		case "team":
			next = $section;
			break;
		case "video":
			next = $video;
			break;
		case "contact":
			next = $contact;
			break;
		default:
			next = $home;
			break;
	}

	if (next) {
		if ($section.isLoaded() && current && current.type == $section.type && next.type == $section.type) {
			next.show(section, page);
		}
		else {
			if (current) {
				current.hide();
			}

			next.load(section, page, envOnly);
		}
	}

	$(document).data("current.page", next);
};

var nav_click = function(evt) {
	evt.preventDefault();
	var parent = $(this).parent();

	if (parent.hasClass("speaker")) {
		var music = document["music"] || window["music"];
		if (parent.hasClass("on")) {
			parent.removeClass("on");
			music.playMusic();
		}
		else {
			parent.addClass("on");
			music.stopMusic();
		}
	}
	else {
		var section = parent.attr("class");
		goTo(section);
	}
};

// Helpers
var changeTitle = function(title) {
	if ($.browser.msie) {
		$(document).data("title.template", title);
	}
	document.title = title;
};

var checkTitle = function() {
	if (/#/.test(document.title)) {
		var title = $(document).data("title.template");
		if (!title) {
			if (typeof r_page == "undefined") {
				title = r_section.MetaTitle;
			}
			else {
				title = r_page.MetaTitle;
			}
		}
		document.title = title;
	}
};

var flashIsGood = function() {
	return $so.hasFlashPlayerVersion("9");
};

var flashCanUpgrade = function() {
	return $so.hasFlashPlayerVersion("6.0.65");
};

var addMusic = function(flash) {
	if ($("#music").length > 0) {
		return;
	}
	
	flash = (flash === undefined ? true : flash);
	if (flash) {
		$so.createSWF({
			data: "/flash/mk3.swf",
			width: 1,
			height: 1
		}, {
			menu: false,
			wmode: "transparent",
			loop: false,
			quality: "high"
		}, $("<div id=\"music\"></div>").appendTo(document.body)[0]);

		$("#nav .speaker a").attr("title", "Speaker On/Off");
	}
	else {
		$("#nav .speaker").addClass("noflash").find("a").attr("title", "Speaker Disabled");
	}
};

var loadReveal = function(flash) {
	flash = (flash === undefined ? true : flash);

	var reveal = $("<div id=\"reveal\">"
		+ "<div id=\"reveal_flash\" /></div>").appendTo("#frame");

//	var reveal = $("<div id=\"reveal\"><a href=\"/thats-the-point/\" class=\"ribbon\">"
//		+ "Click to Register now for your Invitation to the Premier Launch, 30th & 31st May"
//		+ "</a><a id=\"entryLink\" href=\"#\">To go straight to The Point click here.</a>"
//		+ "<div id=\"reveal_flash\" /></div>").appendTo("#frame");

	if (flash) {
		$so.createSWF({
			data: "/flash/reveal02.swf",
			width: 950,
			height: 550
		}, {
			menu: false,
			wmode: "transparent",
			loop: false,
			quality: "high",
			flashvars: ""
		}, "reveal_flash");
	}
	else {
		reveal.css("background-image", "url(/images/reveal.jpg)");
//		$("#entryLink").click(function(evt) { evt.preventDefault(); removeReveal(); });
		setTimeout(function() {
			reveal.fadeOut("slow", function() {
				removeReveal();
			});
		}, 4000);
	}
};

var loadTemplateAssets = function() {
	if ($.browser.msie && $.browser.version < 7) {
		// IE6 filters screw up.
		$("#nav_back, #nav, #logo").css({
			visibility: "visible"
		});
	}
	else if ($.browser.chrome || $.browser.msie) {
		// C, IE7 and IE8 don't fade nicely.
		$("#logo, #nav").css({
			display: "block"
		});
	}
	else {
		$("#logo, #nav").fadeIn();
	}

	$("#nav a").click(nav_click);
	addMusic(flashIsGood());
};

var removeReveal = function() {
	var reveal = $("#reveal");

	// Assets need to be loaded before reveal is hidden, otherwise S3.2.1 (Win) crashes.
	loadTemplateAssets();
	reveal.css({
		display: "none"
	});

	// Reveal needs to be removed on timeout, otherwise C crashes.
	setTimeout(function() {
		reveal.remove();
	}, 15000);
};

var loadFlash = function() {
	if (flashIsGood()) {
		$(document.body).addClass("flash");
		addMusic();
		loadReveal();
	}
	else if (flashCanUpgrade() && $.cookie("dontask.flash") != "yes") {
		var movie = "/flash/expressInstall.swf";
		var fv = "MMredirectURL=" + window.location
			+ "&MMplayerType=" + ($.browser.msie ? "ActiveX" : "PlugIn")
			+ "&MMdoctitle=" + document.title;
		var flashExpress = $("<div id=\"flash_express\"><div id=\"flash_express_container\"></div></div>").appendTo("#frame");

		$so.createSWF({
			data: movie,
			width: 950,
			height: 550
		}, {
			menu: false,
			wmode: "transparent",
			loop: false,
			quality: "high",
			flashvars: fv
		}, "flash_express_container");
	}
	else {
		loadReveal(false);
		$(".speaker").addClass("noflash").find("a").attr("title", "Speaker Disabled");
	}
};

// Flash Handlers
var swfobject = function() {
	return {
		expressInstallCallback: function() {
			$("#flash_express").remove();

			var useFlash = flashIsGood();
			if (useFlash) {
				$(document.body).addClass("flash");
			}

			addMusic(useFlash);
			loadReveal(useFlash);

			$.cookie("dontask.flash", "yes");
		}
	}
} ();

var revealStart = function(source) {
	$("#reveal").css("background", "none");
};

var flashEnd = function(source) {
	switch (source) {
		case "reveal":
			removeReveal();
			break;
	}
};

var flashStateChange = function(state) {
};

var tooltipNav = function(section, page) {
	logEvent("tooltips", section, page);
	goTo(section, page);
};

// Ajax Calls
var getSection = function(section, success, error, context) {
	$.ajaxDotNet("/Service.asmx/GetSectionByCode", {
		verb: "GET",
		data: {
			sectionCode: section
		},
		success: success,
		error: error,
		context: context
	});
};