var videoNum			= 3;
var firstVideoIndex		= -1;
var currentVideoIndex 	= -1;
var ump_container		= null;
var ump_lang			= null;
var ump_section			= null;

function ump_displayPoster(container, mediaNum, lang, section) {

	// Remove contents of target media container
	$('#' + container).children('.fallback').empty();
	$('#' + container).children('.fallback').append('<a href="#"></a>');
	
	if (section == "productListing") {
	
		$('#' + container).children('.fallback').children('a').append('<img src="images/' + section + '-' + mediaNum + '-poster.jpg" border="0" />');
		
	} else {		
		
		$('#' + container).children('.fallback').children('a').append('<img src="images/' + section + '-' + mediaNum + '-' + lang + '-poster.jpg" border="0" />');
	
	}
	
	$('#' + container).children('.fallback').children('a').bind('click', function() {

		firstVideoIndex = mediaNum;

		ump_changeVideo(container, mediaNum, lang, section);	
	
	});
	
}

function ump_changeVideo(container, mediaNum, lang, section) {

	// alert(container + ", " + mediaNum + ", " + lang + ", " + section);
	
	var media 				= {};
	media.autoplay			= "true";
	media.fileName			= section + "-" + mediaNum + (section == "home" ? ("-" + lang) : "");
	media.type 				= "video";
	media.duration			= 0;
	media.width				= 940;
	media.height			= (section == "home" ? 529 : 340);
	media.streaming			= "false";
	media.mediaPath			= escape('videos/');
	media.flashServerPath	= escape('');
	media.flashMediaPath	= escape('videos/');
	media.preview			= escape('images/' + section + '-' + mediaNum + '-' + lang + '-poster.jpg');
	media.lang				= "en";
	media.lockvid_ratio		= "false";
	media.branding			= "false";
	media.containerName		= container;
	
	currentVideoIndex 		= parseInt(mediaNum);
	ump_container			= container;
	ump_lang				= lang;
	ump_section				= section;
	
	ump_init(media);

}

function ump_changeVideoProxy(duration){

	// Only queue the playback if the section is home
	if (ump_section == "home") {
	
		// Check to see if there is a next video in the queue

		// The next video doesn't exist; go back to the first video.
		if (currentVideoIndex+1 > videoNum) {
		
			// There are no other videos in the queue. Go back to counting from the beginning, but only if video1 is not the first video played
			if (firstVideoIndex != 1) {
			
				ump_changeVideo(ump_container, 1, ump_lang, ump_section);
			
			} else {
			
				// Playback of the series videos is finished. Do nothing.
				// alert("B: " + firstVideoIndex + "/" + (currentVideoIndex+1));
			
			}
		
		} else {
		
			// There is a next video. See to make sure it's not the same video as the first one.
			if (currentVideoIndex+1 != firstVideoIndex) {
			
				ump_changeVideo(ump_container, currentVideoIndex+1, ump_lang, ump_section);
				
			} else {
			
				// The next video was the first video. Do nothing.
				// alert("C: " + firstVideoIndex + "/" + (currentVideoIndex+1));
			
			}
		
		
		}
		
	}

}

function ump_detectBrowserMediaCapabilities(media) {

	switch (media.type) {
	
		case "audio" :

			var audioElement = document.createElement('audio'); 

			if (!!audioElement.canPlayType) {

				// Currently canPlayType(type) returns: "", "maybe" or "probably" 
		
				var canPlayMP3 = !!audioElement.canPlayType && "" != audioElement.canPlayType('audio/mpeg');
				var canPlayOgg = !!audioElement.canPlayType && "" != audioElement.canPlayType('audio/ogg; codecs="vorbis"');
			    
			    // Check to see if there is support for either MP3 or OGG
			    // if (canPlayMP3 != false || canPlayOgg != false) {
			    if (canPlayMP3 != false) {
			   
					// Browser supports HTML5 audio tag & can play either MP3 or OGG
					ump_deployHTML5Player(media);
					
				} else {
				
					// Fallback to Flash player
					ump_deployFlashPlayer(media);
					
				}
		
			} else {
		
				// Browser does not support HTML5 audio tag. Deploy Flash player instead.
				ump_deployFlashPlayer(media);
			
			}
		
			break;
			
			
		default : // video
		
			var videoElement = document.createElement('video'); 

			if (!!videoElement.canPlayType) {

				// Currently canPlayType(type) returns: "", "maybe" or "probably" 
		
				var canPlayMP4 		= !!videoElement.canPlayType && "" != videoElement.canPlayType('video/mp4');
				var canPlayTheora 	= !!videoElement.canPlayType && "" != videoElement.canPlayType('video/ogg; codecs="theora"');
				// var canPlayWebM 	= !!videoElement.canPlayType && "" != videoElement.canPlayType('video/webm; codecs="vp8"');
			    
			    // Check to see if there is support for either MP3 or OGG
			    // if (canPlayMP4 != false || canPlayTheora != false) {
			    if (canPlayMP4 != false) {
			   
					// Browser supports HTML5 audio tag & can play either MP3 or OGG
					ump_deployHTML5Player(media);
					
				} else {
				
					// Fallback to Flash player
					ump_deployFlashPlayer(media);
					
				}
		
			} else {
		
				// Browser does not support HTML5 audio tag. Deploy Flash player instead.
				ump_deployFlashPlayer(media);
			
			}
		
			break;
			
	
	}

}

function ump_init(media) {

	ump_detectBrowserMediaCapabilities(media);
	
}

function ump_deployFlashPlayer(media) {

	// If the path to the full DME is specified, forward the user to the full DME URL
	
	if (media.fullDMEPath != null) {
	
		window.location.href = media.fullDMEPath;

	
	} else {
	
		// Otherwise, deploy the Flash version of the video
	
		var flashvars						= {};
		flashvars.media_type 				= media.type;
		flashvars.file_duration 			= (media.duration != null ? media.duration : 0);
		flashvars.file_url 					= media.fileName;
		flashvars.file_width 				= media.width;
		flashvars.file_height 				= media.height;
		flashvars.file_beginPlay			= media.autoplay;
		flashvars.server_streaming			= media.streaming;
		flashvars.server_serverPath 		= media.flashServerPath;
		flashvars.server_mediaPath 			= media.flashMediaPath;
		flashvars.preview_url 				= media.preview;
		flashvars.widgetLang 				= media.lang;
		flashvars.container_name 			= media.containerName;
		flashvars.branding					= media.branding;
		flashvars.lockvid_ratio				= (media.lockvid_ratio != null ? media.lockvid_ratio: "true");
	
		var params 							= {}
		params.scale 						= "noscale";
		params.salign 						= "tl";
		params.bgcolor 						= "#edf7ff";
	
		swfobject.embedSWF("mediaplayer.swf", flashvars.container_name, media.width, "100%", "8.0.0", "expressInstall.swf", flashvars, params);

	}
		
}

function ump_deployHTML5Player(media) {

	// Remove contents of target media container
	$('#' + media.containerName).empty();
	
	// Check what kind of media is being played
	switch (media.type) {
	
		case "audio" :

			// Create HTML elements
			$('#' + media.containerName).append('<div class="poster"><img src="' + media.preview + '" /></div>');
			$('#' + media.containerName).append('<audio controls' + (media.autoplay == "true" ? " autoplay" : "") + '></audio>');
		
			$('#' + media.containerName).children('audio').append('<source src="' + media.mediaPath + media.fileName + '.ogg" type="audio/ogg" />');
			$('#' + media.containerName).children('audio').append('<source src="' + media.mediaPath + media.fileName + '.mp3" type="audio/mpeg" />');
		
			break;

		default : // video
		
			// Create HTML elements
			// $('#' + media.containerName).append('<video controls' + (media.autoplay == "true" ? ' autoplay' : "") + ' poster="' + media.preview + '" width="' + media.width + '" height="' + media.height + '"></video>');
			
			$('#' + media.containerName).append('<video controls' + (media.autoplay == "true" ? ' autoplay' : "") + ' width="' + media.width + '" height="' + media.height + '"></video>');
		
			$('#' + media.containerName).children('video').append('<source src="' + media.mediaPath + media.fileName + '.mp4" type="video/mp4" />');
	
	}

	// Bind an event to determine if the video has stopped playing
	
	$('#' + media.containerName + ' > video').bind('ended', function() {
	
		// Only queue the playback if the section is home
		if (ump_section == "home") {
		
			// Check to see if there is a next video in the queue

			// The next video doesn't exist; go back to the first video.
			if (currentVideoIndex+1 > videoNum) {
			
				// There are no other videos in the queue. Go back to counting from the beginning, but only if video1 is not the first video played
				if (firstVideoIndex != 1) {
				
					ump_changeVideo(ump_container, 1, ump_lang, ump_section);
				
				} else {
				
					// Playback of the series videos is finished. Do nothing.
					// alert("B: " + firstVideoIndex + "/" + (currentVideoIndex+1));
				
				}
			
			} else {
			
				// There is a next video. See to make sure it's not the same video as the first one.
				if (currentVideoIndex+1 != firstVideoIndex) {
				
					ump_changeVideo(ump_container, currentVideoIndex+1, ump_lang, ump_section);
					
				} else {
				
					// The next video was the first video. Do nothing.
					// alert("C: " + firstVideoIndex + "/" + (currentVideoIndex+1));
				
				}
			
			
			}
			
		}
		
		
		
		
		
		//alert(currentVideoIndex);
	
	});
	
		

	// Create fallback flash content
	/*
	fallbackContainerName = String((new Date()).getTime()).replace(/\D/gi,'');
	
	$('#' + media.containerName).children('audio').append('<div id="' + fallbackContainerName + '"></div>');

	media.containerName = fallbackContainerName;
	
	ump_deployFlashPlayer(media);
	*/
	
}

function resize_container(element, newsize) {
	d = document.getElementById(element);
	d.style.height = newsize + "px";
}

