function createRequest(){
    http_request=false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
    }
    return http_request;
}

function carica_lista(gestione,idComponente,listaGiorni){
   http_request_carica_lista=createRequest();
   if (!http_request_carica_lista) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
   }
   http_request_carica_lista.onreadystatechange = function scrivi_lista() {
		 try{
			   if (http_request_carica_lista.readyState == 4) {
				     if (http_request_carica_lista.status == 200) {
						      document.getElementById('divLista').innerHTML=http_request_carica_lista.responseXML.documentElement.getElementsByTagName('lista')[0].firstChild.data;				
      						if(http_request_carica_lista.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data!=''){
							        if(http_request_carica_lista.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data.match("<img")){
								          setTimeout('document.getElementById(\'divVideo\').innerHTML=http_request_carica_lista.responseXML.documentElement.getElementsByTagName(\'video\')[0].firstChild.data',1000);
        							}else{
								          drawVideoNoStretch(http_request_carica_lista.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data, 'wmpcontrol', '320', '304' );
							        }
						      }
						      document.getElementById('divGiorno').innerHTML=document.getElementById('listaGiorni').options[document.getElementById('listaGiorni').selectedIndex].text;
				     }else{
					       document.getElementById('divLista').innerHTML=" Il servizio non &egrave; al momento disponibile...Si prega di riprovare";
				     }			
			   }
		 }catch(e){document.getElementById('divLista').innerHTML=" Il servizio non &egrave; al momento disponibile...Si prega di riprovare";}
	  }
   http_request_carica_lista.open('POST', '/'+nomecontesto+'/s2magazine/moduli/MODULO_VIDEO/carica_lista.jsp', true);  
  	var parametri='idComponente='+escape(idComponente)+'&listaGiorni='+escape(listaGiorni.value);
  	if(gestione){
    		parametri+="&gestione=true";
   }
   http_request_carica_lista.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	  http_request_carica_lista.setRequestHeader('Content-Length', parametri.length);
   http_request_carica_lista.setRequestHeader('Connection', 'close'); 
   http_request_carica_lista.send(parametri);   
	  document.getElementById('divLista').innerHTML='<div>...Caricamento in corso...</div>';
}

function carica_video(id,idComponente){
   http_request_carica_video=createRequest();
   if (!http_request_carica_video) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
   }
   http_request_carica_video.onreadystatechange = function scrivi_video() {
      try{
         if (http_request_carica_video.readyState == 4) {
            if (http_request_carica_video.status == 200) {	
               for(i=0;i<document.getElementById("divLista").getElementsByTagName("div").length;i++){
                  if(http_request_carica_video.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data!=''){
                     if(http_request_carica_video.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data.match("<img")){
                        setTimeout('document.getElementById(\'divVideo\').innerHTML=http_request_carica_video.responseXML.documentElement.getElementsByTagName(\'video\')[0].firstChild.data',1000);
                     }else{
                        drawVideoNoStretch(http_request_carica_video.responseXML.documentElement.getElementsByTagName('video')[0].firstChild.data, 'wmpcontrol', '320', '304' );
                     }
                  }
               }
            } 			
         }
      }catch(e){
      }
	  }

    http_request_carica_video.open('POST', '/'+nomecontesto+'/s2magazine/moduli/MODULO_VIDEO/carica_video.jsp', true);  

	var parametri='idComponente='+escape(idComponente)+'&id='+escape(id);

    http_request_carica_video.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request_carica_video.setRequestHeader('Content-Length', parametri.length);
    http_request_carica_video.setRequestHeader('Connection', 'close'); 
    http_request_carica_video.send(parametri);   
}































































// wmp detect

var _wmpinfo = null;

function getWmpInfo()
{
	if ( _wmpinfo==null )
		_wmpinfo = buildWmpInfo();
	return _wmpinfo;
}

function buildWmpInfo()
{
	var wi = new Object();
	wi.installed = false;
	wi.version = 0;
	wi.platform = "";
	
	// set platform ( win or linux )
	var agt=navigator.userAgent.toLowerCase();
	if ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) ) wi.platform = "win";
	if (agt.indexOf("mac")!=-1) wi.platform = "mac";
	if (agt.indexOf("linux")!=-1) wi.platform = "linux";
	
	// try activexobject 7.1
	var axobj = null;
	axobj = getActiveXObject("WMPlayer.OCX.7");
	if ( axobj==null )
	{
		// try activexobject 6.4
		axobj = getActiveXObject("MediaPlayer.MediaPlayer.1")
	}

	if ( axobj!=null )
	{
		wi.installed = true;
		parseFloat(axobj.versionInfo);
		wi.version=parseFloat(axobj.versionInfo);
	}
	else if (navigator.plugins && navigator.plugins.length)
	{
		for (var i=0; i<navigator.plugins.length; i++)
		{
			if (navigator.plugins[i].name.toLowerCase().indexOf('windows media') != -1)
			{
				wi.installed=true;
				break;
			}
		}
	}
	else if (navigator.mimeTypes)
	{
      	wi.installed = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
	}

	return wi;
}

function getActiveXObject(id)
{
	// "try" is reserved word in NS4, use eval to hide
	var obj = null;
	if (window.ActiveXObject)
		eval ( "try { obj = new ActiveXObject(id); } catch ( e ) {}" );
	else if (window.GeckoActiveXObject)
		eval ( "try { obj = new GeckoActiveXObject(id); } catch ( e ) {}" );
	return obj;
}

function checkWmp()
{
	return ( getWmpInfo().installed && getWmpInfo().platform!="linux" );
}

function checkVersionWmp()
{
	return getWmpInfo().version;
}

// player functions

function drawVideo(src, objid, width, height, hidecontrols)
{
	if ( checkWmp() )
	{
		var h = "";
		
		if ( getWmpInfo().version>=7 )
		{
			h =
			'<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="url" value="' + src + '"> ' +
			'<param name="AutoStart" VALUE="True"> ' +
			'<param name="uiMode" value="' + ( hidecontrols ? "none" : "full" ) + '"> ' +
			'<param name="stretchToFit" value="True"> ' +
			'</object>';
		}
		else
		{
			h =
			'<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="Filename" value="' + src + '"> ' +
			'<param name="AutoStart" value="1"> ' +
			'<param name="ShowControls" value="' + ( hidecontrols ? "0" : "1" ) + '"> ' +
			'<param name="ShowDisplay" value="0"> ' +
			'<param name="ShowStatusBar" value="0"> ' +
			'<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
			'	name="' + objid + '" ' +
			'	width="' + width + '" ' +
			'	height="' + height + '" ' +
			'	filename="' + src + '" ' +
			'	autostart="1" ' +
                        '       fullscreen="1" ' +
			'	showcontrols="' + ( hidecontrols ? "0" : "1" ) + '" ' +
                        '       displaysize=4 ' +
                        '       autosize=-1 ' + 
			'	showdisplay="0" ' +
			'	showstatusbar="0"></embed> ' +
			'</object>';
		}

		document.write ( h );
	}
	else
	{
		drawWmpDownload(src);
	}
}
function drawVideoNoStretch(src, objid, width, height, hidecontrols){
   if ( src.indexOf(".flv")==-1 && src.indexOf(".FLV")==-1  ){
      if ( checkWmp() )	{
         var h = "";
         if ( getWmpInfo().version>=7 ){
            h =
            '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
            'id="' + objid + '" ' +
            'width="' + width + '" ' +
            'height="' + height + '"> ' +
            '<param name="url" value="' + src + '"> ' +
            '<param name="AutoStart" VALUE="True"> ' +
            '<param name="uiMode" value="' + ( hidecontrols ? "none" : "full" ) + '"> ' +
            '<param name="stretchToFit" value="True"> ' +
            '</object>';
         }else{
            h =
            '<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' +
            'id="' + objid + '" ' +
            'width="' + width + '" ' +
            'height="' + height + '"> ' +
            '<param name="Filename" value="' + src + '"> ' +
            '<param name="AutoStart" value="1"> ' +
            '<param name="ShowControls" value="' + ( hidecontrols ? "0" : "1" ) + '"> ' +
            '<param name="ShowDisplay" value="0"> ' +
            '<param name="ShowStatusBar" value="0"> ' +
            '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
            '	name="' + objid + '" ' +
            '	width="' + width + '" ' +
            '	height="' + height + '" ' +
            '	filename="' + src + '" ' +
            '	autostart="1" ' +
                                 '       fullscreen="-1" ' +
            '	showcontrols="' + ( hidecontrols ? "0" : "1" ) + '" ' +
                                 '       displaysize=0 ' +
                                 '       autosize=-1 ' + 
            '	showdisplay="0" ' +
            '	showstatusbar="0"></embed> ' +
            '</object>';
         }
         if (document.all){
            document.getElementById('divVideo').innerHTML=h+"<br/><a href=\"\" onclick=\"fullscreenVideo('wmpcontrol');return false\" class=\"fullscreen\">full screen</a>";
         }else{
            document.getElementById('divVideo').innerHTML=h+"<br/><font face='verdana' size='1'>Doppio click sul video per la modalità 'full screen'</font>";
         }
      }else{
         drawWmpDownload(src);
      }
   }else{
      document.getElementById('divVideo').innerHTML=" "+
      " <object width=\""+width+"\" height=\""+height+"\" id=\"flvPlayer\">"+
      "  <param name=\"allowFullScreen\" value=\"true\">"+
      "  <param name=\"movie\" value=\"player.swf?movie="+src+"&autoplay=true&width="+width+"&height="+height+"\">"+
      "  <embed src=\"/"+nomecontesto+"/s2magazine/moduli/MODULO_VIDEO/player.swf?movie="+src+"&autoplay=true&width="+width+"&height="+height+"\" width=\""+width+"\" height=\""+height+"\" allowFullScreen=\"true\" type=\"application/x-shockwave-flash\">"+
      "</object>";
   }
}

function drawAudio(src, objid, hidecontrols)
{
width=450;
height=45;
	if ( checkWmp() )
	{
		var h = "";
		
		if ( getWmpInfo().version>=7 )
		{
			h =
			'<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="url" value="' + src + '"> ' +
			'<param name="AutoStart" VALUE="True"> ' +
			'<param name="uiMode" value="' + ( hidecontrols ? "none" : "full" ) + '"> ' +
			'<param name="stretchToFit" value="True"> ' +
			'</object>';
		}
		else
		{
			h =
			'<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="Filename" value="' + src + '"> ' +
			'<param name="AutoStart" value="1"> ' +
			'<param name="ShowControls" value="' + ( hidecontrols ? "0" : "1" ) + '"> ' +
			'<param name="ShowDisplay" value="0"> ' +
			'<param name="ShowStatusBar" value="0"> ' +
			'<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
			'	name="' + objid + '" ' +
			'	width="' + width + '" ' +
			'	height="' + height + '" ' +
			'	filename="' + src + '" ' +
			'	autostart="1" ' +
                        '       fullscreen="1" ' +
			'	showcontrols="' + ( hidecontrols ? "0" : "1" ) + '" ' +
                        '       displaysize=4 ' +
                        '       autosize=-1 ' + 
			'	showdisplay="0" ' +
			'	showstatusbar="0"></embed> ' +
			'</object>';
		}

		document.write ( h );
	}
	else
	{
		drawWmpDownload(src);
	}
}
function drawAudioDim(src, objid, width, height, hidecontrols )
{
	if ( checkWmp() )
	{
		var h = "";
		
		if ( getWmpInfo().version>=7 )
		{
			h =
			'<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="url" value="' + src + '"> ' +
			'<param name="AutoStart" VALUE="True"> ' +
			'<param name="uiMode" value="' + ( hidecontrols ? "none" : "full" ) + '"> ' +
			'<param name="stretchToFit" value="True"> ' +
			'</object>';
		}
		else
		{
			h =
			'<object classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ' +
			'id="' + objid + '" ' +
			'width="' + width + '" ' +
			'height="' + height + '"> ' +
			'<param name="Filename" value="' + src + '"> ' +
			'<param name="AutoStart" value="1"> ' +
			'<param name="ShowControls" value="' + ( hidecontrols ? "0" : "1" ) + '"> ' +
			'<param name="ShowDisplay" value="0"> ' +
			'<param name="ShowStatusBar" value="0"> ' +
			'<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
			'	name="' + objid + '" ' +
			'	width="' + width + '" ' +
			'	height="' + height + '" ' +
			'	filename="' + src + '" ' +
			'	autostart="1" ' +
                        '       fullscreen="1" ' +
			'	showcontrols="' + ( hidecontrols ? "0" : "1" ) + '" ' +
                        '       displaysize=4 ' +
                        '       autosize=-1 ' + 
			'	showdisplay="0" ' +
			'	showstatusbar="0"></embed> ' +
			'</object>';
		}

		document.write ( h );
	}
	else
	{
		drawWmpDownload(src);
	}
}
function playVideo()
{
            wmpcontrol.controls.Play();
}
function pauseVideo()
{
 if (wmpcontrol.controls.isAvailable('Pause'))
	wmpcontrol.controls.Pause();
 else   
        wmpcontrol.controls.Stop();
}
var mute = 'false';
function muteVideo()
{
	if(mute=='false')
         {
            wmpcontrol.controls.Mute = "true";
            mute = 'true';
         }
         else   
        {
            wmpcontrol.controls.Mute = "false";
            mute = 'false';
         }
         
}
function fullscreenVideo( objid )
{
	var objref = getVideoObjRef(objid);
 if (objref.playState==3){
  	objref.fullscreen = true;
 }else{
   alert("Attenzione!!! La modalità full screen è disponibile solo se il video è in esecuzione.");
   return false;
 }

}

function getVideoObjRef( objid )
{
	var objref = document[objid];
	if ( document.getElementById )
		objref = document.getElementById(objid);
	return objref;
}

function drawWmpDownload(url)
{
	document.write("<span class='lighttxtbigtext2'><b>Attenzione</b>.<br>Windows Media Player non &egrave; installato.<br>");
	
	if ( getWmpInfo().platform == "linux" )
	{
		document.write("<br /><a href='" + url + "'><font color='yellow'>Clicca qui</font></a><br />" +
					"per visualizzare il video<br />" +
					"con un player differente.");
	}
	else
	{
		document.write("Clicca qui sotto per installare<br>gratuitamente Windows Media Player.<br><br>");
		document.write("<a class=\"lighttxttitle2\" href='http://www.microsoft.com/windows/windowsmedia/' target='_new'>Scarica Windows Media Player</a></span>");
	}
	
	
}

// test functions

function testWmp()
{

		alert ( "Windows Media Player rilevato: " + checkVersionWmp());

}
