//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}


function createMedia()
{
  document.write('<div id="Transparent"  style="display:none"></div>\r\n');
  document.write('<div id="LoadingContainer" style="display:none">\r\n');
  document.write('<div id="MediaContainer" align="center">\r\n');
  document.write('<div id="MediaPadding">\r\n');
  document.write('</div>\r\n');
  //document.write('<div id="MediaController" style="display:none">\r\n');
  //document.write('</div>\r\n');
  document.write('</div>\r\n');
  document.write('</div>\r\n');
}

function replaceSpecialChar(oggetto)
{
    var remo = /--lt--/g;
    carter = "<";
	
    var text = oggetto;
    oggetto=text.replace(remo,carter);
	
	
	remo = /--gt--/g;
    carter = ">";
	oggetto=oggetto.replace(remo,carter);
	
	remo = /--apos--/g;
    carter = "'";
	oggetto=oggetto.replace(remo,carter);
	
	remo = /--quot--/g;
    carter = '"';
	oggetto=oggetto.replace(remo,carter);
	
	remo = /--amp--/g;
    carter = '&';
	oggetto=oggetto.replace(remo,carter);
	
	return oggetto;
}


function mediaShow(m,t,i)
{

document.getElementById('MediaPadding').innerHTML='';
medialist=m.split('#@#');
titlelist=t.split('#@#');

if (medialist.length>1 && (i+1)<medialist.length)
  nextmedia='&nbsp;&nbsp;<img src="'+jsAbsolutePath+'images/medianext.gif" border="0" onclick="mediaShow(\''+m+'\',\''+t+'\','+(i+1)+')">';
else
  nextmedia='';

if (medialist.length>1 && i>0)
  prevmedia='<img src="'+jsAbsolutePath+'images/mediaprev.gif" border="0" onclick="mediaShow(\''+m+'\',\''+t+'\','+(i-1)+')">&nbsp;&nbsp;';
else
  prevmedia='';
mediafile=replaceSpecialChar(medialist[i]);
mediatitle=replaceSpecialChar(titlelist[i]);
//c='<div style="margin-bottom:5px;"><table cellpadding="0" cellspacing="0" border="0" style="font-size:16px;padding-bottom:5px;background-color:#fff"><tr><td style="padding-left:10px">'+prevmedia+ (i+1) + "/" + medialist.length +nextmedia+'</td><td align="right" style="padding-right:10px"><img src="'+jsAbsolutePath+'images/closelabel.gif" border="0" onclick="mediaClose();"></td></tr></table></div>';
s='<table cellpadding="0" cellspacing="10" border="0" style="background-color:#fff;font-family:Arial, sans-serif;font-size:12px;"><tr><td colspan="2">';
if (mediatitle!='')
  s+='<div style="font-size:12px;margin-bottom:3px;">'+mediatitle+'</div>';
if (mediafile.lastIndexOf('.mp4')==-1 && mediafile.lastIndexOf('.flv')==-1 && mediafile.lastIndexOf('<embed')==-1) //mpeg avi mpv mp3 wav 
  s+='<embed  src="'+mediafile+'" SCALE="TOFIT" autostart="true" autoplay="true" showcontrols="true" showstatusbar="false" controller="true" showdisplay="false"></embed>';
else if (mediafile.lastIndexOf('.flv')==-1 && mediafile.lastIndexOf('<embed')==-1) //mp4
  s+='<applet name="mediaframe" codebase="'+jsAbsolutePath+'" code="mediaframe.mpeg4.MPEG4.class" archive="mediaframe-mpeg4.jar,mediaframe-aac.jar" width="320" height="240" mayscript="true"><param name="id" value="ID: subtitled demo"><param name="default_media" value="'+mediafile+'"><param name="pre_buffer" value="28%"><param name="playback"	value="auto_start"><param name="loop" value="false"><param name="controls"	value="float"><param name="pre_screen" value="posters/pre.gif"><param name="pre_screen_loop" value="false"><param name="post_screen" value="posters/post.gif"><param name="post_screen_loop" value="false"><param name="smooth_video" value="true"><param name="video_license"	value="false"><param name="allow_save" value="false"><HR>&egrave; richiesto Java per vedere il filmato; Java is required to watch movies<HR></applet>';
else if (mediafile.lastIndexOf('<embed')==-1)  // flv
  s+='<embed src="'+jsAbsolutePath+'flvplayer.swf" width="320" height="260" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  flashvars="&file='+mediafile+'&autostart=true&image=&allowfullscreen=true&width=320&height=260&linkfromdisplay=false&linktarget=_blank&link=">';
else
  s+=mediafile; //youtube e google video
s+='</td></tr>';
s+='<tr><td style="font-size:12px;">'+prevmedia+ (i+1) + "/" + medialist.length +nextmedia+'</td><td align="right"><img src="'+jsAbsolutePath+'images/closelabel.gif" border="0" onclick="mediaClose();"></td></tr>';
//document.getElementById('MediaController').innerHTML=c;
//if (i==0)
  //new Effect.SlideDown('MediaController',1);
//document.getElementById('MediaController').style.display='block';
//alert(s);
document.getElementById('MediaPadding').innerHTML=s;

}

function mediaClose()
{
  document.getElementById('MediaPadding').innerHTML='';
  //document.getElementById('MediaController').style.display="none";
  document.getElementById('LoadingContainer').style.display="none";
  document.getElementById('Transparent').style.display="none";
  
}

function mediaOpen(medialist,titlelist)
{
var arrayPageSize = getPageSize();
Element.setWidth('Transparent', arrayPageSize[0]);
Element.setHeight('Transparent', arrayPageSize[1]);
new Effect.Appear('Transparent', { duration: 0.2, from: 0.0, to: 0.8 });

var arrayPageScroll = getPageScroll();
var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
var lightboxLeft = arrayPageScroll[0];
Element.setTop('LoadingContainer', lightboxTop);
Element.setLeft('LoadingContainer', lightboxLeft);

document.getElementById('LoadingContainer').style.display="block";
mediaShow(medialist,titlelist,0);
}