/*
 *		Marriott Vacation Club
 *		Site-wide scripts
 *
 *		Lee Allen [lee.allen@acquitygroup.com]
 *
 *		Copyright © 2008 Acquity Group LLC
 */

/* Load all required functions on DOM load */
dojo.addOnLoad(function() {
	ClearDefault.initilize();
	GlobalNavigation.initilize();
	HomepageNavigation.initilize();
	LocationNavigation.initilize();
	Reveal.initilize();
});

// Clears the default value in all text inputs when user selects them. Returns to default value if user enters nothing.
var ClearDefault = {
	initilize: 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;
			}
		});
	}
};

// Drives drop-down menu system on M-T
var GlobalNavigation = {
	initilize: function() {
		// Hide all menus
		GlobalNavigation.clearAll();
		// Bind to navigation mouseovers
		dojo.query('.global-navigation-M-T > li').onmouseover(function(event) {
			// Re-hide all menus
			GlobalNavigation.clearAll();
			// Show this menu
			dojo.query(this).removeClass('hide');
			dojo.query(this).addClass('show');
		});
		// Bind to navigation mouseouts
		dojo.query('.global-navigation-M-T > li > ul').onmouseout(function(event) {
			GlobalNavigation.clearAll();
		});
	},
	clearAll: function() {
		dojo.query('.global-navigation-M-T > li').removeClass('show');
		dojo.query('.global-navigation-M-T > li').addClass('hide');
	}
};

// Drives behaviors for special homepage navigation on M-V
var HomepageNavigation = {
	initilize: function() {
		
		// Make sure #view element exists on page
		if(dojo.query('#view').length > 0) {
			// Bind to the view link and the Resort Collection navigation link
			dojo.query('#view a, #global-navigation-resort-collection')
			.onmouseover(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '0');
				var top = (dojo.isIE ? '-39' : '-41');
				// Slides up tooltip
				dojo.fx.slideTo({ node: 'tooltip-view', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-view-start').style({ zIndex: '15000' });
					dojo.query('#cross-view-end').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-view-start', 'cross-view-end'], duration: 375 }).play();
				}
			})
			.onmouseout(function() {
				// Left and top values are different in IE & Firefox 3
				var left = left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '0');
				var top = (dojo.isIE ? '2' : '0');
				// Slides down tooltip
				dojo.fx.slideTo({ node: 'tooltip-view', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-view-end').style({ zIndex: '15000' });
					dojo.query('#cross-view-start').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-view-end', 'cross-view-start'], duration: 375 }).play();
				}
			});
		}
		
		// Make sure #stay element exists on page
		if(dojo.query('#stay').length > 0) {
			// Bind to the stay link and the Vacation Specials navigation link
			dojo.query('#stay a, #global-navigation-vacation-specials')
			.onmouseover(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '194');
				var top = (dojo.isIE ? '-39' : '-41');
				// Slides up tooltip
				dojo.fx.slideTo({ node: 'tooltip-stay', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-stay-start').style({ zIndex: '15000' });
					dojo.query('#cross-stay-end').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-stay-start', 'cross-stay-end'], duration: 375 }).play();
				}
			})
			.onmouseout(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '194');
				var top = (dojo.isIE ? '2' : '0');
				// Slides down tooltip
				dojo.fx.slideTo({ node: 'tooltip-stay', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-stay-end').style({ zIndex: '15000' });
					dojo.query('#cross-stay-start').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-stay-end', 'cross-stay-start'], duration: 375 }).play();
				}
			});
		}
		
		// Make sure #own element exists on page
		if(dojo.query('#own').length > 0) {
			// Bind to the own link and the Ownership Specials navigation link
			dojo.query('#own a, #global-navigation-ownership-specials')
			.onmouseover(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '397');
				var top = (dojo.isIE ? '-39' : '-41');
				// Slides up tooltip
				dojo.fx.slideTo({ node: 'tooltip-own', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-own-start').style({ zIndex: '15000' });
					dojo.query('#cross-own-end').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-own-start', 'cross-own-end'], duration: 375 }).play();
				}
			})
			.onmouseout(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '397');
				var top = (dojo.isIE ? '2' : '0');
				// Slides down tooltip
				dojo.fx.slideTo({ node: 'tooltip-own', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-own-end').style({ zIndex: '15000' });
					dojo.query('#cross-own-start').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-own-end', 'cross-own-start'], duration: 375 }).play();
				}
			});
		}
		
		// Make sure #win element exists on page
		if(dojo.query('#win').length > 0) {
			// Bind to the win link
			dojo.query('#win a')
			.onmouseover(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '600');
				var top = (dojo.isIE ? '-39' : '-41');
				// Slides up tooltip
				dojo.fx.slideTo({ node: 'tooltip-win', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-win-start').style({ zIndex: '15000' });
					dojo.query('#cross-win-end').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-win-start', 'cross-win-end'], duration: 375 }).play();
				}
			})
			.onmouseout(function() {
				// Left and top values are different in IE & Firefox 3
				var left = (dojo.isIE || (dojo.isFF && dojo.isFF < 3) ? '0' : '600');
				var top = (dojo.isIE ? '2' : '0');
				// Slides down tooltip
				dojo.fx.slideTo({ node: 'tooltip-win', left: left, top: top, unit: 'px', duration: 375 }).play();
				// Cross-fade between two images. If IE6 or FF2, just swap on rollover
				if((dojo.isIE && dojo.isIE < 7) || (dojo.isFF && dojo.isFF < 3)) {
					dojo.query('#cross-win-end').style({ zIndex: '15000' });
					dojo.query('#cross-win-start').style({ zIndex: '20000' });
				} else {
					dojox.fx.crossFade({ nodes: ['cross-win-end', 'cross-win-start'], duration: 375 }).play();
				}
			});
		}
		
	}
};

// Drives <div> swapping for resort listings
// Looks for a query string in the URL that matches a category <div> on page
// If found, shows that category on page load
// If not, defaults to view-all <div>
// Sets up event handling to switch categories using #location-navigation
var LocationNavigation = {
	initilize: function() {
		// Hide all sections
		LocationNavigation.clearAll();
		// Define ID of <div> to be shown by default
		var categoryName = 'view-all';
		var categorySelector = '#' + categoryName;
		// Extract query string if it exists and replace default ID to be shown
		if(window.location.href.indexOf('?') != -1) {
			var urlAnchor = window.location.href.slice((window.location.href.indexOf('?')),window.location.href.length);
			if(urlAnchor.length > 1) {
				// Only change the category variables if the query string matches the name of a category <div>
				if(dojo.byId(window.location.href.slice((window.location.href.indexOf('?') + 1),window.location.href.length))) {
					categoryName = window.location.href.slice((window.location.href.indexOf('?') + 1),window.location.href.length);
					categorySelector = '#' + categoryName;
				}
			}
		}
		// Show the correct category
		dojo.query(categorySelector).style({ display: 'block' });
		// Toggle selected navigation item
		dojo.query('#location-navigation li#location-navigation-' + categoryName + ' a').addClass('selected');
		// Bind to location-navigation clicks
		dojo.query('#location-navigation li a').onclick(function(event) {
			// Re-hide all sections
			LocationNavigation.clearAll();
			// Toggle selected navigation item
			dojo.query(this).addClass('selected');
			// Enable div matching the anchor from the clicked navigation item
			dojo.query(this.href.slice((this.href.indexOf('#')),this.href.length)).style({ display: 'block' });
			event.preventDefault();
		});
	},
	clearAll: function() {
		dojo.query('#location-navigation li a').removeClass('selected');
		dojo.query('div.location-category').style({ display: 'none' });
	}
};

// Drives revealed elements
// All content with class "reveal-text" is hidden by Javascript.
// It is toggled using a link with a "reveal-toggle" class.
// The ID of the <div> to be revealed is specified in the href attribute of the toggle link.
var Reveal = {
	initilize: function() {
		// Hide all hide/show elements
		dojo.query('.reveal-text').style({ display: 'none' });
		// Bind to toggle links
		dojo.query('.reveal-toggle').onclick(function(event) {
			// Grab ID of element to toggle from link href
			var item = dojo.query(this.href.slice((this.href.indexOf('#')),this.href.length));
			// If hidden show, if shown hide
			var display = ((item.style('display') == 'none') ? 'block' : 'none');
			item.style({ display: display });
			// Don't follow the link
			event.preventDefault();
		});
	}
};



//open window for video tour
function videoTour(url, windowName, windowHeight, windowWidth){

  var winwidth      = windowWidth;
  var winheight     = windowHeight;
  var desktopHeight = screen.availHeight;
  var desktopWidth  = screen.availWidth; 
  var top   = (desktopHeight - winheight) / 2;
  var left  = (desktopWidth - winwidth) / 2;
  var urlPrefix = "";
  var winurl    = url;
  var winname   = windowName;
  var winopt    = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0,width="+winwidth+",height="+winheight+",left="+left+",top="+top;
  var newwin    = window.open(winurl,winname,winopt);

}