//
// 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">\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 mediaShow(m,t,i)
{

medialist=m.split('#@#');
titlelist=t.split('#@#');

if (medialist.length>1 && (i+1)<medialist.length)
  nextmedia='<img src="images/medianext.gif" border="0" onclick="mediaShow(\''+m+'\',\''+t+'\','+(i+1)+')">';
else
  nextmedia='';

if (medialist.length>1 && i>0)
  prevmedia='<img src="images/mediaprev.gif" border="0" onclick="mediaShow(\''+m+'\',\''+t+'\','+(i-1)+')">&nbsp;&nbsp;&nbsp;&nbsp;';
else
  prevmedia='';

c='<div style="padding-left:10px;padding-right:10px;margin-bottom:5px"><table width="320" cellpadding="0" cellspacing="0" border="0"><tr><td>'+prevmedia+nextmedia+'</td><td align="right"><img src="images/closelabel.gif" border="0" onclick="mediaClose();"></td></tr></table></div>';
s='';
if (titlelist[i]!='')
  s+='<div style="font-size:11px;margin-bottom:16px;margin-top:5px;">'+titlelist[i]+'</div>';
if (medialist[i].lastIndexOf('.mp4')==-1 && medialist[i].lastIndexOf('.flv')==-1)
  s+='<embed  width="320" height="240" src="'+medialist[i]+'"   SCALE="TOFIT" autostart="true" autoplay="true" showcontrols="true" showstatusbar="false" controller="true" showdisplay="false"></embed>';
else if (medialist[i].lastIndexOf('.flv')==-1)
  s+='<applet name="mediaframe" 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="'+medialist[i]+'"><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
  //s+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="342" height="291" id="FLVPlayer"><param name="movie" value="FLVPlayer.swf" /><param name="salign" value="lt" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="FlashVars" value="&MM_ComponentVersion=1&skinName=Skin&streamName='+medialist[i]+'&autoPlay=true&autoRewind=false" /><embed src="FLVPlayer.swf" flashvars="&MM_ComponentVersion=1&skinName=Skin&streamName='+medialist[i]+'&autoPlay=true&autoRewind=false" quality="high" scale="scale" width="342" height="291" name="FLVPlayer" salign="LT" type="application/x-shockwave-flash" /></object>';
  s+='<embed src="flvplayer.swf" width="320" height="260" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  flashvars="&file='+medialist[i]+'&autostart=true&image=&allowfullscreen=true&width=320&height=260&linkfromdisplay=false&linktarget=_blank&link=">';
document.getElementById('MediaController').innerHTML=c;
//if (i==0)
  //new Effect.SlideDown('MediaController',1);
document.getElementById('MediaController').style.display='block';
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);
}