(function($){
	$.fn.extend({
		modalPanel: function() {
			function modalHide() {
				$(document).unbind('keydown', handleEscape);
				var remove = function() { $(this).remove(); };
				overlay.fadeOut(remove);
				modalWindow.fadeOut(remove).empty();
				if (typeof document.body.style.maxHeight === 'undefined') { //if IE 6
					$('body','html').css({
						height: 'auto',
						overflow: 'auto',
						width: 'auto'
					});
				}
			}
			
			function handleEscape(e) {
				if (e.keyCode == 27) {
					modalHide();
				}
			}
			
			function embedFlash() {
				if (navigator.platform == "Nintendo Wii") {
					var flvFilePath = 'http://media.nintendo.com/zelda/universe/_flv/lozst_trailer_fl7.flv';
					var swfFilePath = '_swf/ZSTVideoPlayerFP7.swf';
				} else {
					var flvFilePath = 'http://media.nintendo.com/zelda/universe/_flv/lozst_trailer_fl8.flv';
					var swfFilePath = '_swf/ZSTVideoPlayer.swf';
				}
				
			    var settings_tr = {
			    	path: swfFilePath,
			    	width: '723px',
			    	height: '445px',
			    	version: '7.0.0',
			    	id: 'flash-video-target', // flash replace id
			    	expressInstall: false
			    };
			    
				if (window.location.host.toString().match(/(^|\.)zelda\.com$/)) {
					if ($('body').hasClass('spanish')){
						var omniAccount = 'ncomglobal,ncommxzeldaspirit';
						var omniCountry = 'mx';
						var omniLanguage = 'es';
					} else if ($('body').hasClass('french')) {
						var omniAccount = 'ncomglobal,ncomcazeldaspirit';
						var omniCountry = 'ca';
						var omniLanguage = 'fr';
					} else {
						var omniAccount = 'ncomglobal,ncomzeldaspirit';
						var omniCountry = 'us';
						var omniLanguage = 'en';
					}
				} else {
					if ($('body').hasClass('spanish')){
						var omniAccount = 'ncomdev';
						var omniCountry = 'mx';
						var omniLanguage = 'es';
					} else if ($('body').hasClass('french')) {
						var omniAccount = 'ncomdev';
						var omniCountry = 'ca';
						var omniLanguage = 'fr';
					} else {
						var omniAccount = 'ncomdev';
						var omniCountry = 'us';
						var omniLanguage = 'en';
					}
				}
				
			    var flashvars_tr = {
			    	swfPath: '_swf/',
					flvFile: flvFilePath,
					posterImage: '_img/temp/zst_video_poster_logo.jpg',
					autoPlay: true,
					sAccount: omniAccount,
					sCountry: omniCountry,
					sLanguage: omniLanguage
			    };
			    
				var params = {
					wmode: 'transparent',
					scale: 'noscale'
				};
				
				var attributes = {};
					
				swfobject.embedSWF(
					settings_tr.path,
					settings_tr.id,
					settings_tr.width,
					settings_tr.height,
					settings_tr.version,
					settings_tr.expressInstall,
					flashvars_tr,
					params,
					attributes
				);
			}
			
			var overlay = $('<div id="overlay"></div>');
			var modalWindow = $('<div id="modal-window"></div>');
			var flashTarget = $('<div id="flash-video-target"></div>');
			
			return this.each(function() {
				$(this).click(function(e) {					
					$('body').append(overlay.click(function() {
						modalHide();
					}));
					
					$('body').append(modalWindow, function(){
						embedFlash();
					});
					
					overlay.fadeIn(500);
					
					e.preventDefault();
					
					$(document).keydown(handleEscape);
					
					var bodyHeight = $(document.body).height();
					var viewportHeight = $(document.viewport).height();
					var viewportWidth = $(document.viewport).width();
					var modalHeight = modalWindow.height();
					var modalWidth = modalWindow.width();

					modalWindow.append(flashTarget).fadeIn(200);
					
					embedFlash();
					
					modalWindow.css({
						left: (modalWidth > viewportWidth) ? 10 : (viewportWidth - modalWidth) / 2 + 'px',
						top: (modalHeight > viewportHeight) ? 10 : (viewportHeight - modalHeight) / 2 + 'px'
					});
					
					if (typeof document.body.style.maxHeight === 'undefined' || navigator.platform == "Nintendo Wii") {
						overlay.css({
							height: (viewportHeight > bodyHeight ? viewportHeight : bodyHeight) + 'px'
						});
					}
					
					modalWindow.append($('<a id="btn-modal-close"><span class="offscreen">Close</span></a>').click(function() {
						modalHide();
					}));
				});
			});
		}
	});
})(jQuery);

$(function() {
	$('a.watch_the_trailer').modalPanel();
});
