var currentVideoID = 0;

var flowplayerObject;
var overlay;

function replay() {
	hideEmailForm();
	hideContent();
	$f('videoplayer').seek(0);
	$f('videoplayer').play();
}

function emailForm(videoid) {
	if(videoid >= 0) var videoURL = location.protocol + "//" + location.host + location.pathname + "#" + videoid;
	else videoURL = location.href;
	
	//var url = "<a href='" + videoURL + "'>" + videoURL + "</a>";
	
	$('#url').val(videoURL);

	$("#info").hide();
	$("#shareForm").show();
	$("#emailPane").show();	
}

function hideEmailForm() {
	$("#emailPane").hide();
	$("#info").hide();
	$("#shareForm").show();
}

function hideContent() {
	$f().getPlugin("content").css({opacity: '0',zIndex:'1'});
	$f().getPlugin("content").hide();
	$f().getPlugin("contentReplayButton").css({opacity: '0',zIndex:'2'});
	$f().getPlugin("contentReplayButton").hide();
	$f().getPlugin("contentSeeMoreButton").css({opacity: '0',zIndex:'2'});
	$f().getPlugin("contentSeeMoreButton").hide();
	$f().getPlugin("contentEmailButton").css({opacity: '0',zIndex:'2'});
	$f().getPlugin("contentEmailButton").hide();
	
}

function showContent() {
	$f().getPlugin("content").css({opacity: '1',zIndex:'1'});
	$f().getPlugin("content").show();  
	$f().getPlugin("contentReplayButton").css({opacity: '1',zIndex:'2'});
	$f().getPlugin("contentReplayButton").show(); 
	$f().getPlugin("contentSeeMoreButton").css({opacity: '1',zIndex:'2'});
	$f().getPlugin("contentSeeMoreButton").show(); 
	$f().getPlugin("contentEmailButton").css({opacity: '1',zIndex:'2'});
	$f().getPlugin("contentEmailButton").show(); 
	$f().getPlugin("play").hide(); 
}

function embedPlayer(videoURL, thumbURL) {

	$f("videoplayer", { src:"/static/js/flowplayer.commercial-3.1.0.swf", wmode: 'opaque'}, { key: '#$d3ce079795650eafddd',
			play: { 
				replayLabel: null 
			},
			playlist: [

				// this first PNG clip works as a splash image
				{
					url: thumbURL, 
					autoBuffering: true,
					scaling: 'fit',
					onStart: function() {
						//alert("Starting thumb");
						hideContent();
						$f().getPlugin("play").show();	
					} 
				},
				
				// second clip is a video. when autoPlay is set to false the splash screen will be shown
				{
					url: videoURL, 
					autoPlay: false, 
					scaling: 'fit',
					// video will be buffered when splash screen is visible
					autoBuffering: false,
					onFinish: function() { 
						showContent();                     
					}
				}
			], 
			canvas: { backgroundColor: '#FFFFFF' } , 
			plugins: { controls: { 
					url: 'flowplayer.controls-3.1.0.swf', 
					autoHide: 'always', 
				        play:true, 
				        volume:true, 
				        mute:true, 
				        time:true, 
				        stop:false, 
				        playlist:false, 
				        fullscreen:true, 
					scrubber: true 
				},
				content: { 

					// the only required property 
					url: '/js/flowplayer.content-3.1.0.swf', 

					// some display properties 
					height:'87%',
					padding:30, 
					backgroundColor: '#000000', 
					opacity: 0,
					border: '1px solid #000000', 
					backgroundGradient:'none',
					html: '<p></p>'           
				},
				contentReplayButton: { 

					// the only required property 
					url: '/js/flowplayer.content-3.1.0.swf', 
					zIndex: 1,
					// some display properties 
					height:90,
					padding:0,
					margin:0, 
					width: 90,
					top:'45%',
					left:'25%',  
					opacity: 0, 
					backgroundColor: '#000000',
					border: '1px solid #000000',
					backgroundGradient:'none',
					html: '<p><a href="javascript:replay();"><img src="/images/replay.png"></a></p>'            
				},
				contentSeeMoreButton: { 

					// the only required property 
					url: '/js/flowplayer.content-3.1.0.swf', 
					zIndex: 1,
					// some display properties 
					height:90,
					padding:0,
					margin:0, 
					width: 90,
					top:'45%',
					left:'50%',  
					opacity: 0, 
					backgroundColor: '#000000',
					border: '1px solid #000000',
					backgroundGradient:'none',
					html: '<p><a href="/Solutions/"><img src="/images/seeMore.png"></a></p>'            
				},
				contentEmailButton: { 

					// the only required property 
					url: '/js/flowplayer.content-3.1.0.swf', 
					zIndex: 1,
					// some display properties 
					height:90,
					padding:0,
					margin:0, 
					width: 90,
					top:'45%',
					left:'75%',  
					opacity: 0, 
					backgroundColor: '#000000',
					border: '1px solid #000000',
					backgroundGradient:'none',
					html: '<p><a href="javascript:emailForm(-1);"><img src="/images/email.png"></a></p>'            
				} 
			},
			onLoad: function(clip) { 
				//alert("Hiding stuff");
			    	hideContent(); 
			} 
	}).load();

}

function playit(videoid) {
	if(typeof videoid == 'undefined') videoid = 0;
	currentVideoID = videoid;

	flowplayerObject = flowplayer("videoplayer", "/js/flowplayer.commercial-3.1.5.swf", { 	key: '#$d3ce079795650eafddd', wmode: 'opaque',
												clip: { url: videos[videoid],
													autoPlay: true,  
													autoBuffering: true, 
													scaling: 'fit',

													onStart: function() {
														hideContent();
															
													},
													onFinish: function() { 
														showContent();                   
													} 
												},
												play: { 
													replayLabel: null 
												}, 
												canvas: { backgroundColor: '#FFFFFF' } , 
												plugins: { controls: { 
        													url: 'flowplayer.controls-3.1.5.swf', 
        													autoHide: 'always', 
													        play:true, 
													        volume:true, 
													        mute:true, 
													        time:true, 
													        stop:false, 
													        playlist:false, 
													        fullscreen:true, 
        													scrubber: true 
													},
													content: { 
             
														// the only required property 
														url: '/js/flowplayer.content-3.1.0.swf', 
														zIndex: 1,
														// some display properties 
														height:'87%',
														padding:30, 
														backgroundColor: '#000000', 
														opacity: 0,
														border: '1px solid #000000', 
														backgroundGradient:'none',
														html: '<p></p>'           
													},
													contentReplayButton: { 
             
														// the only required property 
														url: '/js/flowplayer.content-3.1.0.swf', 
														zIndex: 9999,
														// some display properties 
														height:90,
														padding:0,
														margin:0, 
														width: 90,
														top:'45%',
														left:'25%',  
														opacity: 0, 
														backgroundColor: '#000000',
														border: '1px solid #000000',
														backgroundGradient:'none',
														html: '<p><a href="javascript:replay();"><img src="/images/replay.png"></a></p>'            
													},
													contentSeeMoreButton: { 
             
														// the only required property 
														url: '/js/flowplayer.content-3.1.0.swf', 
														zIndex: 9999,
														// some display properties 
														height:90,
														padding:0,
														margin:0, 
														width: 90,
														top:'45%',
														left:'50%',  
														opacity: 0, 
														backgroundColor: '#000000',
														border: '1px solid #000000',
														backgroundGradient:'none',
														html: '<p><a href="/Solutions/"><img src="/images/seeMore.png"></a></p>'            
													},
													contentEmailButton: { 
             
														// the only required property 
														url: '/js/flowplayer.content-3.1.0.swf', 
														zIndex: 9999,
														// some display properties 
														height:90,
														padding:0,
														margin:0, 
														width: 90,
														top:'45%',
														left:'75%',  
														opacity: 0, 
														backgroundColor: '#000000',
														border: '1px solid #000000',
														backgroundGradient:'none',
														html: '<p><a href="javascript:emailForm(' + videoid + ');"><img src="/images/email.png"></a></p>'            
													}
												} 
		});

	overlay = $("#InfoVideo").overlay({ 
		api: true, 
		expose: 'black',

		onBeforeLoad: function() { 
			$("#emailPane").css('display','none');			 
	        },

		// when overlay is opened, load our player 
	        onLoad: function() { 			
			$('#mediaMobzHome').css('display','none');
			$('#flashFiller').css('display','block');
			$("#emailPane").css('display','none');
	            	$f().load();
			 
	        }, 
	         
	        // when overlay is closed, unload our player 
	        onClose: function() { 		
			$('#flashFiller').css('display','none');
			$('#mediaMobzHome').css('display','block');
			
			$f().unload();
	        }
	}).load();


	

}

//<a href="/Solutions/"><img src="/images/seemorebutton.png"></a><a href="javascript:emailForm(' + videoid + ');"><img src="/images/emailbutton.png"></a>',
//,
														// content plugin specific properties 
														//style: {'.center':{marginLeft:'auto', marginRight:'auto'}, '.button':{marginLeft:20, display:'inline'}, p:{fontSize:20, color:'#ffffff',display:'inline'}, a:{color:'#ffffff'} } 

$(document).ready(function() {
	$("#shareForm").submit(function(e) {
		var json;		
		var form = $(this);
		form.hide();
		
		
		$.getJSON(form.attr("action") + "&" + form.serialize() + "&format=json&jsoncallback=?", function(json) {
			var info = $("#info");
			
			if (json.message) {
				info.html(json.message);
				info.show();
				$("#info").fadeOut(3000, function() {
					info.hide();
					form.show();	
				});			
			} else {
				info.html("Email was successfully sent");
				info.show();
				$("#info").fadeOut(3000, function() {
					hideEmailForm();	
				});
			} 		
		});	
	
		e.preventDefault();
		return false;		
	});
});

