/*
--------------------------------------------------

Marriott Vacation Club
Site-Wide Scripts [application.js]

Joe Morrow [joe.morrow@acquitygroup.com]
3/24/2010

Copyright (c) 2008-2010 Acquity Group LLC

--------------------------------------------------
*/


/* Load all required functions on DOM load */
var EXPLORE_PATH = "/vacation-resorts/index.shtml";

dojo.require("dijit.Dialog");
dojo.require("dojo.NodeList-traverse");

dojo.addOnLoad(function() {
	ClearDefault.initialize();
	HoverOver.initialize();
	PrintLink.initialize();
	Tabs.initialize();
	UtilityNavPopouts.initialize();
	RegionSelect.initialize();
});


// Navigates up the DOM tree, starting at node until a parent with a class className is found. If no parent exists, null is returned.
function findParentByClass(node, className) {

	if (node.parentNode.tagName == "HTML") {
		return null;
	}
	else if (dojo.hasClass(node.parentNode, className)) {
		return node.parentNode;
	}
	else {
		return findParentByClass(node.parentNode, className);
	}
}


// Clears the default value in all text inputs when user selects them. Returns to default value if user enters nothing.
var ClearDefault = {
	initialize: function() {
		dojo.query('input[type="text"], input[type="password"]').onfocus(function() {
			if(this.value == this.defaultValue) {
				this.value = '';
			}
		}).onblur(function() {
			if(!this.value.length) {
				this.value = this.defaultValue;
			}
		});
	}
};


var RegionSelect = {
	initialize: function() {
		// if not on explore page, do not register this event
		if (dojo.query("#mapUtility").length == 0) {
			dojo.query("#regionSelect").onchange(
				function(e) {
					window.location = EXPLORE_PATH + "#region-" + e.target.value;
				}			
			);
		}
	}
}


var HoverOver = {
	initialize: function() {
		dojo.query(".mapResort").
			onmouseover(function() {
				dojo.addClass(this,"hoverOver");
			}).
			onmouseout(function() {
				dojo.removeClass(this,"hoverOver");
			});
	}
}

var PrintLink = {
	initialize: function() {
		var printPage = dojo.query("a.print");
			printPage.onclick(function(e) {
				return window.print(e);
			});
		}
}


var Tabs = {
	tabItems: "ul.tabs li",
	tabLinks: ".tabLink",
	tabContainers: "div.tabContent",
	initialize: function() {
		var tabLinks = dojo.query(Tabs.tabItems + " a");
		// add the click handler
		tabLinks.onclick(function(e) {
			Tabs.selectTab(this);
			return preventHyperlink(e);
		});
		// also add this handler, to keep browsers from following the links
		tabLinks.forEach(function(item) {
			if (!dojo.attr(item, "onclick"))
				dojo.attr(item, "onclick", "return false;");
		});

		
	},
	selectTab: function(el, contextID) {
		// Scrub the inputs for the element's HTML DOM node and the closest parent element with a valid ID 
		var elNode = dojo.query(el)[0];
		var elParent = elNode;
		while (elParent && !dojo.byId(contextID)) {
			elParent = elParent.parentNode;
			if (elParent)
				contextID = (elParent.id) ? elParent.id : contextID;
		}
		contextID = (dojo.byId(contextID)) ? "#" + contextID + " " : "";

		// IE returns the fully-qualified URL instead of the contents of the href attribute, so correct for this.
		var elNodeHref = dojo.attr(elNode, "href");
		var elNodeTarget = elNodeHref.substr(elNodeHref.lastIndexOf("#"));

		// update classes on the tab items
		dojo.query(contextID + Tabs.tabItems + ".selected").removeClass("selected");
		dojo.addClass(elNode.parentNode, "selected");

		// update classes on the tab contents
		dojo.query(contextID + Tabs.tabContainers + ".selected").addClass("hidden").removeClass("selected");
		dojo.query(contextID + elNodeTarget).removeClass("hidden").addClass("selected");
	}
}

function homeTab(tabNumber) {
	var dialogID = "content";
	Tabs.selectTab("#" + dialogID + " ul.tabs li:nth-child(" + tabNumber + ") a", dialogID);
}


// Launch a dialog, presetting to a particular tab if set
function showDialog(dialogID, tabNumber) {
	dojo.query("body").addClass("modalOn");
	var dialog = dijit.byId(dialogID);
	dialog.connect(dialog, "onCancel", modalOff);
	dialog.show(); 
	if (tabNumber)
		Tabs.selectTab("#" + dialogID + " ul.tabs li:nth-child(" + tabNumber + ") a", dialogID);
	return dialog; 
}

function modalOff()
{
	dojo.query("body").removeClass("modalOn")
}

// Launch a dialog with a virtual tour
function showDialogTour(dialogID, tabNumber, container) {
	var dialog = showDialog(dialogID, tabNumber);
	hideFloorPlanInstructions("floorPlanInstructions");
	unloadFloorPlan("floorPlanSWF");
	esAddPlayer(container);
	dialog.connect(dialog, "hide", function(e) {es_closeResort();});
}


// Launch a dialog with a photo gallery
function showDialogGallery(dialogID, tabNumber, galleryID, album, image) {
	var dialog = showDialog(dialogID, tabNumber);
	loadAlbum(galleryID, album, image);
	dialog.connect(dialog, "hide", function(e) {unloadAlbum(galleryID);});
}


// Launch a dialog with a floor plan
function showDialogFloorPlan(dialogID, tabNumber, floorPlanID, floorPlanSWF) {
	var dialog = showDialog(dialogID, tabNumber);
	es_closeResort();
	showFloorPlanInstructions("floorPlanInstructions");
	loadFloorPlan(floorPlanID, floorPlanSWF);
	dialog.connect(dialog, "hide", function(e) {unloadFloorPlan(floorPlanID);});
}


// Turn on the floor plan-specific instructions
function showFloorPlanInstructions(instructionID) {
	if (dojo.hasClass(instructionID, "hidden"))
		dojo.removeClass(instructionID, "hidden");
}


// Turn off the floor plan-specific instructions
function hideFloorPlanInstructions(instructionID) {
	if (!dojo.hasClass(instructionID, "hidden"))
		dojo.addClass(instructionID, "hidden");
}


// Load a new floor plan from a select field
function selectFloorPlan(thisNode, floorPlanID) {
	if (thisNode.value)
		loadFloorPlan(floorPlanID, thisNode.value);
}


/* Developer Note 4/16/2010:
1.  Need to work around the use of ".nav li.selected" here, to fix the unintended deselection of the secondary nav menu.
*/ 
var UtilityNavPopouts = {
	containers: ".specialTip.selected, .nav li.selected",
	initialize: function() {
		//UtilityNavPopouts.startWatchForClick();
		var triggers = dojo.query(".trigger");
		triggers.
			onclick(function() {
				dojo.query(UtilityNavPopouts.containers).removeClass("selected");
				dojo.addClass(this.parentNode, "selected");
				setTimeout(UtilityNavPopouts.startWatchForClick, 100);
			});
		triggers.forEach(function(el){
			//override href, onclick event returning false in the dojo registered event is not sufficient
			dojo.attr(el, "href", "javascript:;");
		})
	},
	startWatchForClick: function() {
		var listener = dojo.connect(dojo.query("body")[0], "onclick",null, function(e) {
			if (dojo.query(e.target).parents(".pop").length == 0) {
				if (!dojo.hasClass(e.target,"trigger")) {
					dojo.query(UtilityNavPopouts.containers).removeClass("selected");
				}
				UtilityNavPopouts.stopWatchForClick();
			}
		});
		UtilityNavPopouts.bodyClickListener = listener;
	},
	stopWatchForClick: function() {
		dojo.disconnect(UtilityNavPopouts.bodyClickListener);
		UtilityNavPopouts.bodyClickListener = null;
	}
}

// Load the Photo Gallery SWF, if needed, or switch to a new album or gallery.  Note that the "image" value should be zero to load the initial state.
function loadAlbum(id, album, image) {
	var swf = document.getElementById(id);
	if (swf)
		swf.loadAlbum(album, image);
	else {
		var idPlaceholder = id + "placeholder";
		var swfPlaceholder = document.getElementById(idPlaceholder);
 		if (swfPlaceholder)
			swfobject.embedSWF("/common/cms/mvc/flash/gallery.swf", idPlaceholder, "700", "408", "9.0.0", "/common/cms/mvc/flash/expressInstall.swf", {album: album, image: image}, {allowscriptaccess: "always"}, {id: id, name: id});
	}
}


// Unload the Photo Gallery SWF, if needed.  Called when closing the dialog.
function unloadAlbum(id) {
	var swf = document.getElementById(id);
	if (swf) {
		var swfParent = findParentByClass(swf, "gallery");
		if (swfParent) {
			var swfPlaceholder = document.createElement("div");
			swfPlaceholder.setAttribute("id", id + "placeholder");
			swfParent.appendChild(swfPlaceholder);
			swfobject.removeSWF(id);
		}
	}
	return true;
}


// Floorplan helper.  Note that the "floorplan" value must be a root-relative path to the appropriate SWF.
function loadFloorPlan(id, floorplan) {
	var swf = document.getElementById(id);
	if (swf)
		swf.loadFloorplan(floorplan);
	else {
		var idPlaceholder = id + "Placeholder";
		var swfPlaceholder = document.getElementById(idPlaceholder);
		if (swfPlaceholder)
			swfobject.embedSWF("/common/cms/mvc/flash/floorplan.swf", idPlaceholder, "698", "415", "9.0.0", "/common/cms/mvc/flash/expressInstall.swf", {floorplan: floorplan}, {allowscriptaccess: "always"}, {id: id, name: id});
	}
}


// Unload the Floor Plan SWF, if needed.  Called when closing the dialog.
function unloadFloorPlan(id) {
	var swf = document.getElementById(id);
	if (swf) {
		var swfParent = findParentByClass(swf, "floorPlan");
		if (swfParent) {
			var swfPlaceholder = document.createElement("div");
			swfPlaceholder.setAttribute("id", id + "Placeholder");
			swfParent.insertBefore(swfPlaceholder,document.getElementById("foorPlanFooter"));
			swfobject.removeSWF(id);
		}
	}
	return true;
}


// Turn off the autohiding alt content, which can cause the layout to jump.
/* Developer Note 5/12/2010:
1.  Somehow, in FF3 this seems to break the EveryScape player, which itself is almost completely undocumented.
*/
//swfobject.switchOffAutoHideShow();


// IE still follows hyperlinks even with onclick attributes that return false.  This function, when called within the function called by onclick, will stop this behavior in a variety of ways.
function preventHyperlink(event) {
	if (event) {
		if (typeof(event.preventDefault) != "undefined")
			event.preventDefault();	// W3C
		else
			event.returnValue = false; // IE
	}
	return false;
}

// Everyscape virtual tour helper elements
