var movieName = "accordion";
var currentNavigationElement;
var index;

jQuery(document).ready(function($){

	// deactivate all subnav links
	$("#subnav li a, #subsubnav li a").click(function(){
		return false;
	});
	
	// re-activate some links that should work
	$(".subnav-5 a").click(function(){
		location.href = $(this).attr("href");
		return false;
	});

	// turn on links that should open popups from nav
	$(".page-item-132 a").click(function(){
		quickPop($(this).attr("href"));
		return false;
	});
	




	
	// enable spine links - about
	$(".subnav-3 a").click(function(){
		index = $(".subnav-3 a").index(this);
		changeSpine($(this), ".subnav-3", index);
		return false;
	});
	// set first child item to selected
	if ($(".subnav-3 li").exists()) {
		// set currently selected element, so that selected state can be properly set
		currentNavigationElement = ".subnav-3";
		$(".subnav-3 li:first").addClass("current_page_item");
	}
	
	// feature
	$(".subnav-9 a").click(function(){
		index = $(".subnav-9 a").index(this);
		changeSpine($(this), ".subnav-9", index);
		return false;
	});
	if ($(".subnav-9 li").exists()) {
		currentNavigationElement = ".subnav-9";
		$(".subnav-9 li:first").addClass("current_page_item");
	}
	
	// contact
	$(".subnav-11 a").click(function(){
		index = $(".subnav-11 a").index(this);
		changeSpine($(this), ".subnav-11", index);
		return false;
	});
	if ($(".subnav-11 li").exists()) {
		currentNavigationElement = ".subnav-11";
		$(".subnav-11 li:first").addClass("current_page_item");
	}
	
	// collections
	$(".subnav-13 a").click(function(){
		index = $(".subnav-13 a").index(this);
		changeSpine($(this), ".subnav-13", index);
		return false;
	});
	if ($(".subnav-13 li").exists()) {
		currentNavigationElement = ".subnav-13";
		$(".subnav-13 li:first").addClass("current_page_item");
	}

	// wood tones
	$(".subnav-14 a").click(function(){
		index = $(".subnav-14 a").index(this);
		changeSpine($(this), ".subnav-14", index);
		return false;
	});
	if ($(".subnav-14 li").exists()) {
		currentNavigationElement = ".subnav-14";
		$(".subnav-14 li:first").addClass("current_page_item");
	}
	
	// room type
	$(".subnav-18 a").click(function(){
		index = $(".subnav-18 a").index(this);
		changeSpine($(this), ".subnav-18", index);
		return false;
	});
	if ($(".subnav-18 li").exists()) {
		currentNavigationElement = ".subnav-18";
		$(".subnav-18 li:first").addClass("current_page_item");
	}

	// finishes
	$(".subnav-16 a").click(function(){
		index = $(".subnav-16 a").index(this);
		changeSpine($(this), ".subnav-16", index);
		return false;
	});
	if ($(".subnav-16 li").exists()) {
		currentNavigationElement = ".subnav-16";
		$(".subnav-16 li:first").addClass("current_page_item");
	}
	

});

jQuery.fn.exists = function(){return jQuery(this).length>0;}



// Provides the proper address for the movie depending on browser
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


function changeSpine(obj, parent, index) {
	$(parent + " .current_page_item").each(function(index) {
		$(this).removeClass("current_page_item");
	});
	currentNavigationElement = parent;
	$(parent + " a:eq("+index+")").addClass("current_page_item");
	getFlashMovie("accordion").JStoASviaExternalInterface(index);
}

function changeSpineFromFlash(index) {
	$(currentNavigationElement + " .current_page_item").each(function(index) {
		$(this).removeClass("current_page_item");
	});
	$(currentNavigationElement + " a:eq("+index+")").addClass("current_page_item");
}

function quickPop(url) {
	showPopup(url, 'popup', 477, 965, 0, 1, 0);
}

function showPopup(url, name, height, width, scroll, resizable, statusbar, screenpos) {
	var Xwin = width;
	var Ywin = height;
	var Xsize = screen.availWidth;
	var Ysize = screen.availHeight;
	var Xpos = 0;
	var Ypos = 0;
	switch(screenpos) {
		case "topleft":
			Xpos = 0;
			Ypos = 0;
			break;
		case "topright":
			Xpos = Xsize - width;
			Ypos = 0;
			break;
		case "bottomleft":
			Xpos = 0;
			Ypos = Ysize - height;
			break;
		case "bottomright":
			Xpos = Xsize - width;
			Ypos = Ysize - height;
			break;
		default:
			Xpos = (Xsize - width) / 2;
			Ypos = (Ysize - height) / 2;
			break;
	}
	var popwin;
	var opts = "status=" + statusbar + ",resizable=" + resizable + ",scrollbars=" + scroll + ",height=" + Ywin + ",width=" + Xwin + ",left=" + Xpos + ",top=" + Ypos;

	popwin = window.open(url, name, opts);
	if (popwin != null) {
		popwin.focus();
	} else {
		if (confirm("We cannot open a popup window automatically, click on OK to open the webpage in a normal window, otherwise click CANCEL to stay on this page.\n\nNote: user experience may change as a result of viewing popup webpages in normal windows.")) {
			location.href = url;
		}
	}
}