function showTrailer() {
	$("div.overlay").overlay().load();
}

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}

$(document).ready(function(){
	var currentPlayer;
	
	$f(".player", "flow/flowplayer.commercial-3.1.3.swf",  {
	
		key: '#@d3b7b5575297453dadd',
			
		plugins: {
			controls: {
				url: 'flow/flowplayer.controls-tube-3.1.3.swf',
				autoHide: 'always',
				hideDelay: 1000000000,
				borderRadius: '0px',
				progressColor: '#858585',
				backgroundGradient: 'none',
				bufferGradient: 'none',
				bufferColor: '#4a4a4a',
				sliderColor: '#165988',
				tooltipTextColor: '#D00000',
				volumeSliderColor: '#D00000',
				progressGradient: 'none',
				//timeColor: '#e8e8e8',
				time:false,
				buttonColor: '#f7f7f7',
				buttonOverColor: '#eb7d24',
				sliderGradient: 'none',
				backgroundColor: '#333333',
				timeBgColor: '#262626',
				volumeSliderGradient: 'none',
				volumeSliderColor: '#4a4a4a',
				durationColor: '#595959',
				tooltipColor: '#C9C9C9',
				height: 20,
				opacity: 0,
				zIndex:1000,
				fullscreen: null
			}
		},
		
		play:{
			url: "flow/player_vid.png",
			width: 96,
			height: 96
		},
		
		onBeforeFullscreen: function()  {
			this.hide().pause();
			this.getControls().enable({fullscreen:null});
			currentPlayer="#"+this.id();
			$("div.overlay").overlay().load();
			return false;
		},
		
		onKeyPress: function(key) {
			if (key == 27) {
				$("div.overlay").overlay().close();
			}
		},
		
		onMouseOver: function() {
			this.getControls().animate({"opacity":1});
		},
		onMouseOut: function() {
			this.getControls().animate({"opacity":0});
		},

		clip: {
			scaling: 'fit',
			bufferLength:10,
			
			onMetaData: function(clip) {
				if (console)
					console.dir(clip);
			},
			
			onStart: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"start"
					},function(data) {
						if (console)
							console.dir(data);
					});
					thisMovie("finalfantasyxi").soundOff();
			},
		
			onPause: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"pause"
					},function(data) {
						if (console)
							console.dir(data);
					});
			},
			
			onResume: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"resume"
					},function(data) {
						if (console)
							console.dir(data);
					});
			},
			
			onLastSecond: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"lastsecond"
					},function(data) {
						if (console)
							console.dir(data);
					});
			},
			
			onFinish: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"finish"
					},function(data) {
						if (console)
							console.dir(data);
					});
			},
			
			onStop: function() {
				$.getJSON(
					"http://weblet.square-enix-europe.com/video.php?callback=?",
					{
						"URL":$f().getClip().completeUrl,
						"DOC":window.location.href,
						"ACTION":"stop"
					},function(data) {
						if (console)
							console.dir(data);
					});
			}
		},
		
		canvas: {
			backgroundGradient: 'none'
			//background: '#000000 url(http://uman.square-enix-europe.com/contents/FFXIIIAN/flow/video_overlay.png) no-repeat'
		},
		screen: {
			width:"100%",
			height:"100%",
			zIndex:0
		}

	}); //*/
	
	var oldCanvasBG;
	
	// setup overlay 
	$("div.overlay").overlay({ 
	 
		expose: '#000000',
		top: ($(window).height()-550)/2,
		closeOnClick: false,
 
		onLoad: function() {
 
			/*var embed = $(currentPlayer+" :first");
			var el = this.getContent(); 
			var height = el.height();
			var cssData={ 
				width:800, 
				height:450, 
				left: parseInt(el.css("left")) + 50, 
				top: parseInt(el.css("top")) + 50
			}

			embed.css(cssData);
			oldCanvasBG=$f().getConfig().canvas.background;
			$f().getConfig().canvas.background="none";
			$f().resume();//*/
			
			$f().load();
			var sY=$(window).scrollTop();
			
			$(window).scroll(function() {
				$(window).scrollTop(sY);
			});
		}, 
	 
		onClose: function() { 
			/*$(currentPlayer+" :first").css({top: null, left: null, width: null, height: null});
			$f().getConfig().canvas.background=oldCanvasBG; 
			$f().getControls().enable({fullscreen:true});//*/
			$f().unload();
			$(window).unbind("scroll");
		} 
	});
	if (location.search=="?video") {
		showTrailer();
	}
});


