/*===========================================================================
	Actions (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Collapsing/expanding sections
	* Form validation
	* Popup windows
===========================================================================*/


/* Email Disclaimer
===========================================================================*/

YAHOO.util.Event.onContentReady("primary-content", function() {
	var emailLinks = YAHOO.util.Dom.getElementsByClassName("email", "a");
	
	YAHOO.util.Event.addListener(emailLinks, "click", function(e) {
		var emailAddress = this.innerHTML;
		var theURL = "/_includes/email-disclaimer.php?email=" + emailAddress;

		var newWindow = window.open(theURL,'_blank','width=500,height=231,scrollbars=0,resizable=1,status=1');
		
		if(newWindow) {
			newWindow.focus();
			YAHOO.util.Event.stopEvent(e);
		}
	});
});


/* Video Launcher
===========================================================================*/

(function($) {
	/**
	 * Small 'plugin' to run on the links that
	 * need to launch a video. It sets up the
	 * necessary events to launch the video
	 * when the link is clicked.
	 *
	 * @name prepareVideoLauncher
	 * @type jQuery
	 */
	jQuery.fn.prepareVideoLauncher = function() {
		this.click(launchVideo);
	};
	
	/**
	 * This launches the videos via a click event
	 * on an A tag. It uses the href of the a tag
	 * to load the proper video. 
	 * This also adds the extra markup necessary
	 * to launch the video. 
	 * The video is always placed just below 
	 * the logo and the window's scrollTop is
	 * set to zero and reset when closed.
	 * 
	 * @private
	 */
	var launchVideo = function(event) {
		var vidName = this.search.substr(1);

		if ( !$('#overlay').size() )
			// create html for overlay
			$('<div id="overlay">')
				.css({ 
					opacity: .6,
					width: $('#page').width(),
					height: $('#page').outerHeight() - 54,
					top: 26,
					left: 53
				})
				// append to page
				.appendTo('#page')
				// hook up click event to close video
				.click(closeVideo);
		
		if ( !$('#videoPopUp').size() ) {
			// create html for video pop up
			$('<div id="videoPopUp">')
				// append to page div
				.appendTo('#page')
				// add shadow markup
				.append('<div class="shadow tl">') // top left corner
				.append('<div class="shadow tr">') // top right corner
				.append('<div class="shadow bl">') // bottom left corner
				.append('<div class="shadow br">') // bottom right corner
				.append('<div class="shadow ht">') // horizontal top
				.append('<div class="shadow hb">') // horizontal bottom
				.append('<div class="shadow vl">') // vertical left
				.append('<div class="shadow vr">') // vertical right
				// add video place holder
				.append('<div id="videoPlaceHolder"><p><a href="http://http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash/">Get the Adobe&reg; Flash Player</a></p></div>')
				// add close button
				.append('<a href="#" class="close" title="close">close</a>')
				.find('a.close')
					// hook up click event to close video
					.click(closeVideo);
		}
		
		$('#overlay, #videoPopUp').show();
		
		// safari likes to jump due to the overlay ... adjust
		if ($.browser.safari) $('#page').css({marginTop: -55});
		
		// load the swf
		UFO.create({
			movie: '/_media/videos/video.swf',
			width: '320',
			height: '250',
			majorversion: '7',
			build: 0,
			flashvars: vidName,
			xi: 'true'
		}, 'videoPlaceHolder');
		
		// save current scroll location
		window.$oldScrollTop = $(window).scrollTop();
		// scroll to top of the window
		$(window).scrollTop(0);
		
		this.blur(); // remove focus from link
		return false; // cancel event
	};
	
	/**
	 * This closes the videos and is fired either
	 * by clicking on the overlay or on the close
	 * 'button'. It resets the window's scrollTop
	 * to the previous value before launching the 
	 * video.
	 * 
	 * @private
	 */
	var closeVideo = function(event) {
		if (event.target.blur) event.target.blur();
		
		// remove flash element
		$('#videoPlaceHolder').css('visibility', 'hidden').find('*').remove();
		// hide videoPopUp
		$('#overlay, #videoPopUp').hide();
		
		// scroll back to the old location
		$(window).scrollTop(window.$oldScrollTop);
		window.$oldScrollTop = null;
		
		return false; // cancel event
	};
})(jQuery);