/*
*************************************************************************************************
	Customer:		PLA

	Author:			George Broom - MSO.net
	Copyright:		Copyrighted 2011 (c) MSO.net

	File:			/javascript/widget_Videos.js.cfm
	Description:	
	
	Mod History:	********** - *******  - File Created. GB
					20/12/2011 - MSO10530 - Allow the ability to place videos in content. GB

*************************************************************************************************
*/


// Ajax Connection code
function makeRequestVideos(url) {
	//new Ajax.Request(url,{onSuccess:processResponseVideos})
	var req = new Request({
		method: 'get',
		url: url,
		onSuccess: function(responseText, responseXML){
			processResponseVideos(responseXML);
		}
	}).send();
}

function makeRequestOtherVideos(url,vidid) {
	//new Ajax.Request(url,{onSuccess:processResponseVideos})
	var req = new Request({
		method: 'get',
		url: url,
		onSuccess: function(responseText, responseXML){
			//update html, re write html tags using id or names
			document.getElementById("WIDGET_OtherVideos").innerHTML = responseText;
			/*var xmldoc = responseXML;
			
			var testing = xmldoc.getElementsByTagName('other_videos').length - 1;
			for (var i = 0; i <= testing; i++) {
				var str_othervid_pic = xmldoc.getElementsByTagName('vidImage').item(i).firstChild.data;;
				var str_othervid_title = xmldoc.getElementsByTagName('vidTitle').item(i).firstChild.data;;
				var str_othervid_description = xmldoc.getElementsByTagName('vidDescription').item(i).firstChild.data;;
							
				$('vidPic').innerHTML = str_othervid_pic;
				$('vidTitle').innerHTML = str_othervid_title;
				$('vidDescription').innerHTML = str_othervid_description;
			}*/
			
		}
	}).send();
}


var curFlashVideo;
function processResponseVideos(transport) {
	var xmldoc = transport;

	// Get the form fields
	var num_videos = xmldoc.getElementsByTagName('video').length - 1;		
	var str_tmp = '';

	for (var i = 0; i <= num_videos; i++) {
		var intID = xmldoc.getElementsByTagName('vidId').item(i).firstChild.data;
		var str_Filename = xmldoc.getElementsByTagName('streams').item(i).firstChild.data;
		var fullscreenVal = xmldoc.getElementsByTagName('extraOptions').item(i).firstChild.data;
		var xml_file = xmldoc.getElementsByTagName('xml_file').item(i).firstChild.data;
		var str_vid_title = xmldoc.getElementsByTagName('vidTitle').item(i).firstChild.data;
		var str_vid_desc = xmldoc.getElementsByTagName('vidDescription').item(i).firstChild.data;
		if (str_Filename == 'undefined') str_Filename = '/images/spacer.gif';

		/*str_tmp = str_tmp +'<object id="videoPlayer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="270" height="190">';
		str_tmp = str_tmp +'<param name="movie" value="/templates/widgets/plaPlayer3.swf" />';
		str_tmp = str_tmp +'<param name="FlashVars" value="videos=/images/videos/file'+intID+'.xml" />';
		str_tmp = str_tmp +'<param name="allowFullScreen" value="true" />';
		str_tmp = str_tmp +'<param NAME="wmode" VALUE="opaque" />';
		str_tmp = str_tmp + '<param name="allowScriptAccess" value="always">';
		str_tmp = str_tmp +'<embed id="videoPlayer" src="/templates/widgets/plaPlayer3.swf" FlashVars="videos=/images/videos/file'+intID+'.xml" allowFullScreen="true" width="270" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		str_tmp = str_tmp +'</object>';
		//str_tmp = str_tmp +'</noscript>';*/
		
		if (str_Filename.toLowerCase().indexOf('.swf') > -1) {
			var newFlashVideo = new SWFObject(str_Filename, "videoPlayer", "315", "178", "8.0.15", "#ffffff", true);
			newFlashVideo.write("widgetMainVideo");
			curFlashVideo = newFlashVideo;
		} else {
			var newFlashVideo = new SWFObject("/flash/plaPlayerWidescreen.swf", "videoPlayer", "315", "178", "8.0.15", "#ffffff", true);
			
			newFlashVideo.addVariable("videos", "/videos/"+xml_file);
			newFlashVideo.addVariable("allowFullScreen","true");
			newFlashVideo.addParam("allowScriptAccess", "always");
			newFlashVideo.addParam("allowFullScreen", "true");
			newFlashVideo.write("widgetMainVideo");
			curFlashVideo = newFlashVideo;
		}
	}
	
	
	if($('main-vid-title')){
		$('main-vid-title').innerHTML = str_vid_title;
		$('main-vid-description').innerHTML = str_vid_desc;
		
		makeRequestOtherVideos("ajax_video.cfm?flag=2&vid_id="+intID,intID);
	}
	

	/*if (fullscreenVal == 1){	
		flashMovie = document.getElementById("videoPlayer");

		if (flashMovie) {
			setTimeout(function(){
				alert("go");
				flashMovie.externalGoFullScreen();
			},3000);
           // flashMovie.externalGoFullScreen();
        }
	} else {
		$('widgetMainVideo').innerHTML = str_tmp;
		//playMovieNow('videoPlayer');
	}&*/

} 


/*function showFullscreen() {
	debugger
	if (flashMovie) {
        flashMovie.externalGoFullScreen();
	}
}

function getFlashMovie(movieName) {   var isIE = navigator.appName.indexOf("Microsoft") != -1;   return (isIE) ? window[movieName] : document[movieName];  }*/


//************************    Large Video Request Code    ******************

// Ajax Connection code
function makeRequestVideosLarge(url) {
	new Ajax.Request(url,{onSuccess:processResponseVideosLarge});
}

function processResponseVideosLarge(transport) {
	var xmldoc = transport.responseXML;

	// Get the form fields
	var num_videos = xmldoc.getElementsByTagName('video').length - 1;		
	var str_tmp = '';
	
	for (var i = 0; i <= num_videos; i++) {
		var intID = xmldoc.getElementsByTagName('vidId').item(i).firstChild.data;
		var str_Filename = xmldoc.getElementsByTagName('streams').item(i).firstChild.data;
		if (str_Filename == 'undefined') str_Filename = '/images/spacer.gif';
		
		//str_tmp = str_tmp +'<script type="text/javascript">AC_FL_RunContent( \'codebase\',\'http:\/\/download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\',\'width\',\'270\',\'height\',\'190\',\'allowFullScreen\',\'true\',\'pluginspage\',\'http:\/\/www.macromedia.com/go/getflashplayer\',\'movie\',\'\/templates\/widgets\/plaPlayer3\',\'FlashVars\',\'videos=/images/videos/'+str_Filename+'\' ); ';
		//str_tmp = str_tmp +'\n</script><noscript>';
		str_tmp = str_tmp +'<object id="videoPlayer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="270" height="152">';
		str_tmp = str_tmp +'<param name="movie" value="/templates/widgets/plaPlayerWidescreen.swf" />';
		str_tmp = str_tmp +'<param name="FlashVars" value="videos=/images/videos/file'+intID+'.xml" />';
		str_tmp = str_tmp +'<param name="allowFullScreen" value="true" />';
		str_tmp = str_tmp +'<param NAME="wmode" VALUE="opaque" />';
		str_tmp = str_tmp + '<param name="allowScriptAccess" value="always">';
		str_tmp = str_tmp +'<embed src="/templates/widgets/plaPlayerWidescreen.swf" FlashVars="videos=/images/videos/file'+intID+'.xml" allowFullScreen="true" width="270" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		str_tmp = str_tmp +'</object>';
		//str_tmp = str_tmp +'</noscript>';
	}
	
	$('widgetMainVideo').innerHTML = str_tmp;
	//setTimeout(playMovieNow('videoPlayer'),500);
	playMovieNow('videoPlayer');

} //function


function playMovieNow(str_objectID) {
	try {
		thisMovie(str_objectID).playVideo();
	} catch (e) {
		setTimeout(function(){playMovieNow('videoPlayer')},100);	
	}
}

function thisMovie(movieName){
	if (window.document[movieName]) return window.document[movieName];
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[movieName]) return document.embeds[movieName]; 
		else return document.getElementById(movieName);
	}
}

//************************** end of large video request *************************

//centenary code

// Ajax Connection code
function makeRequestVideosCent(url) {
	new Ajax.Request(url,{onSuccess:processResponseVideosCent});
}

function processResponseVideosCent(transport) {
	var xmldoc = transport.responseXML;

	// Get the form fields
	var num_videos = xmldoc.getElementsByTagName('video').length - 1;		
	var str_tmp = '';
	
	for (var i = 0; i <= num_videos; i++) {
		var intID = xmldoc.getElementsByTagName('vidId').item(i).firstChild.data;
		var str_Filename = xmldoc.getElementsByTagName('streams').item(i).firstChild.data;
		var xml_file = xmldoc.getElementsByTagName('xml_file').item(i).firstChild.data;
		if (str_Filename == 'undefined') str_Filename = '/images/spacer.gif';
		
		//str_tmp = str_tmp +'<script type="text/javascript">AC_FL_RunContent( \'codebase\',\'http:\/\/download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\',\'width\',\'270\',\'height\',\'152\',\'allowFullScreen\',\'true\',\'pluginspage\',\'http:\/\/www.macromedia.com/go/getflashplayer\',\'movie\',\'\/templates\/widgets\/plaPlayerWidescreen\',\'FlashVars\',\'videos=/images/videos/'+str_Filename+'\' ); ';
		//str_tmp = str_tmp +'\n</script><noscript>';
		str_tmp = str_tmp +'<object id="videoPlayer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="270" height="152">';
		str_tmp = str_tmp +'<param name="movie" value="/templates/widgets/plaPlayerWidscreen.swf" />';
		//str_tmp = str_tmp +'<param name="FlashVars" value="videos=/images/videos/file'+intID+'.xml" />';
		str_tmp = str_tmp +'<param name="FlashVars" value="videos=/images/videos/'+xml_file+'" />';
		str_tmp = str_tmp +'<param name="allowFullScreen" value="true" />';
		str_tmp = str_tmp +'<param NAME="wmode" VALUE="opaque" />';
		str_tmp = str_tmp + '<param name="allowScriptAccess" value="always">';
		str_tmp = str_tmp +'<embed src="/templates/widgets/plaPlayerWidescreen.swf" FlashVars="videos=/images/videos/'+xml_file+'" allowFullScreen="true" width="270" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		str_tmp = str_tmp +'</object>';
		//str_tmp = str_tmp +'</noscript>';
	}

	$('widgetMainVideo' + intID).innerHTML = str_tmp;
	//setTimeout(playMovieNow('videoPlayer'),500);
	playMovieNow('videoPlayer');

} //function

