/**** VERSIONE LIBRERIA PLAYER PER GLI OFFSITE LINKS ***/


/*
Crea WMPinstance con i seguenti parametri
mode = video|audio
src= file da riprodurre
w = larghezza del player
h = altezza del player

*/

var VIDEO = 1;
var AUDIO = 0;
var IMG = 2;
var peProgressbar = null;
var valueProgress = null;
var media_progressbar = null;

function writePlayer(mode,src,link, audioLogo, strTitle) {
// inclusione degli script necessari
	/*var css = document.createElement('link');
	css.setAttribute('href', 'http://www.multimedia.ilsole24ore.com/css/video_images_show.css');
	css.setAttribute('type', 'text/css');
		
	var head = document.getElementsByTagName('head').item(0);
	head.appendChild(css);*/
	//document.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://www.ilsole24ore.com/css2007/video_images_show.css\" /> ");
 
	var width = '324';
	var height = '266';
  var autosize = "false";
  var cmenu = "false";
	var exist;
	var htmlString = '';

	if (window.ActiveXObject && navigator.userAgent.indexOf('Windows') != -1){
		clsid = "6BF52A52-394A-11D3-B153-00C04F79FAA6";
		api = 7;
	} else {
		clsid = "22D6F312-B0F6-11D0-94AB-0080C74C7E95";
		api = 6;
  } 

	if (mode == AUDIO) {
		var width = '1';
		var height = '1';
	}
	// Se stiamo navigando la pagina con firefox, facciamo un check per controllare che il plugin di media player sia installato
	if (navigator.userAgent.indexOf('Firefox') != -1)
		exist = checkPlugins();
	else exist = 1;

	if(exist){
	  //var playerobject = '<OBJECT id="WMPlayer" WIDTH="'+width+'" HEIGHT="'+height+'" '; \
		var playerobject = '';
		// eplayer object non serve piu', perche' nel caso di firefox + plugin usa comunque l'object e non l'embed. Se si usa l'embed si rischiano malfunzionamenti sui bottoni.
		if (window.ActiveXObject && navigator.userAgent.indexOf('MSIE') != -1)
			playerobject +='<OBJECT id="WMPlayer" WIDTH="'+ width +'" HEIGHT="'+ height +'" CLASSID="CLSID:' + clsid + '" TYPE="application/x-mplayer2"> '; 
		else 
			playerobject += '<object id="WMPlayer" type="application/x-ms-wmp" data="'+ src +'" WIDTH="'+ width +'" HEIGHT="'+ height + '"> ';

		playerobject += '<PARAM NAME="autoStart" VALUE="False">';
		playerobject += '<PARAM NAME="enableContextMenu" VALUE="' + cmenu + '">';
		playerobject += '<PARAM NAME="TransparentAtStart" VALUE="true">';
		playerobject += '<PARAM NAME="AutoSize" VALUE="' + autosize + '">';
		playerobject += '<PARAM NAME="stretchToFit" VALUE="' + autosize + '">';
		playerobject += '<PARAM NAME="uiMode" VALUE="none">';	
		//playerobject += '<param name="windowlessVideo" value="true">';	
		playerobject += '<PARAM NAME="ShowControls" VALUE="false">';	
		playerobject += '<PARAM NAME="ShowStatusBar" VALUE="false">';
		playerobject += '<PARAM NAME="invokeURLs" VALUE="false">';

		if (api == 7) playerobject += '<PARAM NAME="URL" VALUE="' + src + '">';
		else playerobject += '<PARAM NAME="FileName" VALUE="' + src + '">';
		playerobject += '</object>';
	  
		if (mode == VIDEO){

			htmlString +="<div id=\"PlayerBox\">";
			htmlString +="<form style=\"padding:0px; margin: 0px;\">";
			htmlString += '	<div id=\"Preview\" style="display: block">';
			htmlString += '<img src="' + audioLogo + '" width=\"324\" height=\"266\">';
			htmlString += '</div>';
			htmlString += '	<div id=\"Video\" style="visibility: hidden">';
			htmlString += playerobject;
			htmlString +="	</div>";
			htmlString +="	<input type=\"hidden\" id=\"volume\" value=\"50\" />";
			htmlString +="	<input type=\"hidden\" id=\"slide_progress_bar\" value=\"0\" />";
			htmlString +="	<div id=\"VideoCommandBox\" >";
			htmlString +="		<div id=\"VideoCommand\" style=\"height: 50px;\">";
			htmlString +="			<ul style=\"float:left; padding: 0px;\">";
			htmlString +="				<li><img id=\"cmdPlay\" src=\"http://www.ilsole24ore.com/img2007/shim.gif\" title=\"Play\" width=\"30\" height=\"25\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li><img id=\"cmdPause\" src=\"http://www.ilsole24ore.com/img2007/shim.gif\" title=\"Pause\" width=\"35\" height=\"25\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li id=\"playercontrols\">	<div style=\"text-align: left;\" id=\"progress_bar\"><img id=\"progressbar\" src=\"http://www.ilsole24ore.com/img2007/ico_timePivot.png\" alt=\"\"></div></li>";
			htmlString +="				<li id=\"position_values\">0:00/0:00</li>";
			htmlString +="			</ul>";
			htmlString +="			<ul class=\"right_commands\">";
			htmlString +="				<li><img id=\"ico_volume\" onclick=\"\" src=\"http://www.ilsole24ore.com/img2007/ico_audio.png\" title=\"Volume\" width=\"12\" height=\"11\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li id=\"slider_target\"></li>";
			//htmlString +="				<li class=\"plusb\"><img id=\"cmdPlusd\"src=\"http://www.ilsole24ore.com/img2007/shim.gif\" title=\"Plus\" /></li>";
			htmlString +="			</ul>";
			htmlString +="		</div>";
			htmlString +="	</div>";
			htmlString +="</form>";
			htmlString +="</div>";
			htmlString +="</div></div>";

			new Insertion.Top("playerzone", htmlString);

			if($('volume')) form_widget_amount_slider('slider_target', $('volume'),42,0,100,"aFunction()");

			if ($('playercontrols')) {
				media_progressbar = new Control.Slider('progressbar','progress_bar');
				media_progressbar.setValue(0);
				Event.observe('progress_bar', 'click', function() {
				
					setPosition(media_progressbar.value);
				}.bind(this));

				/* event play */
				if ($('cmdPlay'))
					Event.observe('cmdPlay', 'click', function() {
						play();
					}.bind(this));

				/* event pause */
				if ($('cmdPause'))
					Event.observe('cmdPause', 'click', function() {
						pause();
					}.bind(this));

				if (peProgressbar) peProgressbar.stop();
				peProgressbar = new PeriodicalExecuter(updateProgressbar,1);

				// gestione degli eventi per aggiornare i numeretti di progressione
				valuesProgress = new PeriodicalExecuter(updateProgressValues,1);
			} // player controls
		} else if (mode == AUDIO){

			htmlString +="<div id=\"PlayerBox\" style=\"margin: 0 auto;\">";
			htmlString +="<form>"; 
			htmlString +="	 <div id=\"Video\">";
			/*if ( audioLogo == "" || audioLogo == "/") htmlString += r24logo("default");
			else 	htmlString += r24logo(audioLogo);*/
			htmlString += "<div><img src=" + audioLogo + " width=\"324\" height=\"266\"></div>";
			htmlString += playerobject;
			htmlString +="	</div>";
			htmlString +="	<input type=\"hidden\" id=\"volume\" value=\"50\" />";
			htmlString +="	<input type=\"hidden\" id=\"slide_progress_bar\" value=\"0\" />";
			htmlString +="	<div id=\"VideoCommandBox\">";
			htmlString +="		<div id=\"VideoCommand\">";
			htmlString +="			<ul style=\"float:left;\ padding:0px;\">"; 
			htmlString +="				<li><img id=\"cmdPlay\" src=\"http://www.ilsole24ore.com/img2007/shim.gif\" title=\"Play\" width=\"30\" height=\"25\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li><img id=\"cmdPause\" src=\"http://www.ilsole24ore.com/img2007/shim.gif\" title=\"Pause\" width=\"35\" height=\"25\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li id=\"playercontrols\">	<div style=\"text-align: left;\" id=\"progress_bar\"><img id=\"progressbar\" src=\"http://www.ilsole24ore.com/img2007/ico_timePivot.png\" alt=\"\"></div></li>";
			htmlString +="				<li id=\"position_values\">0:00/0:00</li>";
			htmlString +="			</ul>";
			htmlString +="			<ul class=\"right_commands\">";
			htmlString +="				<li><img id=\"ico_volume\" src=\"http://www.ilsole24ore.com/img2007/ico_audio.png\" title=\"Volume\" width=\"12\" height=\"11\" border=\"0\" vspace=\"0\" hspace=\"0\" /></li>";
			htmlString +="				<li id=\"slider_target\"></li>";
			// La lightbox per gli audio non ha senso
			//htmlString +="				<li class=\"plusb\"><img id=\"cmdPlusd\" onclick=\"\" src=\"/img2007/shim.gif\" title=\"Plus\" /></li>";
			htmlString +="			</ul>";
			htmlString +="		</div>";
			htmlString +="	</div>";
			htmlString +="</form>";
			htmlString +="</div>";
			htmlString +="</div></div>";
			
			new Insertion.Top("playerzone", htmlString);

			if($('volume')) form_widget_amount_slider('slider_target', $('volume'),42,0,100,"aFunction()");

			if ($('playercontrols')) {
				media_progressbar = new Control.Slider('progressbar','progress_bar');
				media_progressbar.setValue(0);

				Event.observe('progress_bar', 'click', function() {
					setPosition(media_progressbar.value);
				}.bind(this));

				/* event play */
				if ($('cmdPlay'))
					Event.observe('cmdPlay', 'click', function() {
						play();
					}.bind(this));
				
				/* event pause */
				if ($('cmdPause'))
					Event.observe('cmdPause', 'click', function() {
						pause();
					}.bind(this));
				
				if (peProgressbar) peProgressbar.stop();
				peProgressbar = new PeriodicalExecuter(updateProgressbar,1);
				
				// gestione degli eventi per aggiornare i numeretti di progressione
				valuesProgress = new PeriodicalExecuter(updateProgressValues,1);
			} //playercontrols
		
		} //else if (mode == IMG) {
		// non toccare
		//	document.write("<a href=\""+ link +"\" border=\"0\" alt=\"" + strTitle + "\" title=\"" + strTitle + "\"> <img src=\"" + src + "\" /> </a>"  +
		//							"<div style=\"border-top: 2px solid #d6d6d6; background-color: #f5f5f5; color: #666666; font-family: Arial; font-size: 11px; height: 50px; line-height: 16px; margin-top: 1px; position: relative;\">" +
		//							"<span style=\"display: block; float: left; height: 35px; margin-left: 10px; margin-top: 10px; overflow: hidden; width: 280px;\"><a style=\"background:transparent url(../img2007/ico_linkBlue.gif) no-repeat scroll left 1px; color:#3388CC; font-family:arial; font-size:12px; font-weight:bold; margin-left:10px; padding-left:16px; \" href=\"" + link + "\" target=\"_blank\" title=\"\">VAI ALLA GALLERY</a></span>" );
		//	}
	} else {
		htmlString = "<div><img src=" + audioLogo + " width=\"324\" height=\"266\"></div><div style='padding-left: 15px; padding-top: 10px; padding-bottom: 10px; border-top: 2px solid #d6d6d6; background-color: #f5f5f5; font-size: 11px;line-height: 15px; font-family: arial;'>Per  visualizzare il contenuto correttamente ti consigliamo di scaricare il plugin  di Windows Media Player cliccando <a style='color:#3388CC;' href='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx'>qui</a></div>";
		new Insertion.Top("playerzone", htmlString);
	}

}

function writeImage (mode,src,link, audioLogo, strTitle){
	document.getElementById('playerzone').innerHTML = "<a href=\""+ link +"\" border=\"0\" alt=\"" + strTitle + "\" title=\"" + strTitle + "\"> <img src=\"" + src + "\" /> </a>"  +
								"<div style=\"border-top: 2px solid #d6d6d6; background-color: #f5f5f5; color: #666666; font-family: Arial; font-size: 11px; height: 50px; line-height: 16px; margin-top: 1px; position: relative;\">" +
								"<span style=\"display: block; float: left; height: 35px; margin-left: 10px; margin-top: 10px; overflow: hidden; width: 280px;\"><a style=\"background:transparent url(http://www.ilsole24ore.com/img2007/ico_linkBlue.gif) no-repeat scroll left 1px; color:#3388CC; font-family:arial; font-size:12px; font-weight:bold; margin-left:10px; padding-left:16px; \" href=\"" + link + "\" target=\"_blank\" title=\"\">VAI ALLA GALLERY</a></span>";
}

/* controlli di playback */
function play() {
	$('WMPlayer').controls.play();  
	if($('Preview')){
		$('Video').setStyle(
			{visibility:'visible'}
		);
		if (navigator.userAgent.indexOf('Opera') != -1)
			$('WMPlayer').setStyle(
				{visibility:'visible'}
			);
		// remove e non hide altrimenti stoppa il playback con firefox.
		$('Preview').remove();
	}
		
}

function pause(){
	if($('WMPlayer').controls.isAvailable('Pause')){
	// non tutti i media type permettono la pausa; se non e' disponibile uso lo stop.
		$('WMPlayer').controls.pause();
	} else {
		$('WMPlayer').controls.stop();      
	}
}

function pauseExec(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

function updateProgressValues(){
	var position, total;
	if ($('WMPlayer').controls != null) {
		if ($('WMPlayer').controls.currentPositionString != '')
			position = $('WMPlayer').controls.currentPositionString;
		else position = '00.00';
		if ($('WMPlayer').currentMedia != null)
			total = $('WMPlayer').currentMedia.durationString;
		else total = '00.00';
		$('position_values').update(position + " / " + total);
	}
}

function updateProgressbar() {
	if ($('WMPlayer').playState == 3) {
		
		var newpos = $('WMPlayer').controls.currentPosition / $('WMPlayer').currentMedia.duration;
		//window.alert("newpos: " + newpos);
		if (!media_progressbar.dragging && !media_progressbar.active) media_progressbar.setValue(newpos);
		if (media_progressbar.disabled) media_progressbar.setEnabled();
	} else {
		if (!media_progressbar.disabled) media_progressbar.setDisabled();
	}
	//window.alert("current " + $('WMPlayer').controls.currentPosition + " duration " + $('WMPlayer').currentMedia.duration + " newpos " + newpos + " media_progressbar.value " + media_progressbar.value);
}

function checkPlugins(){
		// Questa funzione controlla che sia installato il plugin di windows media player. 
		// Se il plugin non e' installato, la funzione ritorna una stringa con un messaggio che invita l'utente a scaricare il plugin
		// Il messaggio viene visualizzato al posto del video e dei controlli.
		
		var plugins = navigator.plugins
		var exist = 0;
		
		for (var i = 0; i < plugins.length; i++) {
			if (plugins[i].name.indexOf('Windows Media Player Firefox Plugin') != -1)
				exist = 1;
		}
		
		
		return exist;
	}
