var videoPopup;
var videoBackground;
var chiudiVideoPop;
var video;

function pageSize(pageWidth,pageHeight,windowWidth,windowHeight,scrollLeft,scrollTop,xScroll,yScroll){	
	this.pageWidth=pageWidth;
	this.pageHeight=pageHeight;
	this.windowWidth=windowWidth;
	this.windowHeight=windowHeight;
	this.scrollLeft=scrollLeft;
	this.scrollTop=scrollTop;
	this.xScroll=xScroll;
	this.yScroll=yScroll;
}

function getPageSize(){	
	var pageWidth,pageHeight,windowWidth,windowHeight;
	var xScroll, yScroll,scrollLeft,scrollTop;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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;
	if (self.innerHeight) {	// all except Explorer
		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;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	
	if (self.pageYOffset) {
		scrollLeft = self.pageXOffset;
		scrollTop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		scrollLeft = document.documentElement.scrollLeft;
		scrollTop = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		scrollLeft = document.body.scrollLeft;
		scrollTop = document.body.scrollTop;
	}
	
	// su tutto schermo	
	if (xScroll < screen.width )
		xScroll = screen.width;
			
	if (yScroll < screen.height )
		yScroll = screen.height;		

	return new pageSize(pageWidth,pageHeight,windowWidth,windowHeight,scrollLeft,scrollTop,xScroll,yScroll);	
}

function hideVideobox(){
	try {
				document.MediaPlayer.Stop();				
			} catch (ex1) {
					try {
						document.MediaPlayer.controls.stop();
					} catch (ex2) {	}
			}	
	videoBackground.style.display = 'none';
	videoPopup.style.display = 'none';
	video.innerHTML = '';
}

function makePlayerObject(url_video,container){
	var player = '';
	player +='<object id="MediaPlayer" classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" name="MediaPlayer" width="324" height="300">';
	player +='<param name="URL" value="'+ url_video +'"/>';
	player +='<param name="AnimationatStart" value="true"/>';
	player +='<param name="TransparentatStart" value="true"/>';
	player +='<param name="AutoStart" value="true"/>';
	player +='<param name="ShowControls" value="1"/>';
	player +='<param name="ShowStatusBar" VALUE="0"/>';
	player +='<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" src="'+ url_video +'" name="MediaPlayer" id="MediaPlayer" showcontrols="1" width="324" height="300"/>';
	player +='</object>';	
	
	container.innerHTML = player;
}

function showVideoPopup(objLink){
		
	if (!document.getElementsByTagName || !document.getElementById )
		 return;
			
	var objBody = document.getElementsByTagName("body").item(0);
	
	videoPopup = document.createElement("div");
	videoPopup.setAttribute('id','videopop');
		
	videoBackground = document.createElement("div");
	videoBackground.setAttribute('id','videobackground');
	videoBackground.onclick = function () {hideVideobox(); return false;}	
		
	video = document.createElement("div");
	video.setAttribute('id','objectvideo');
	
	var menunav = document.createElement("div");
	menunav.setAttribute('id','menunav');
	
	menunav.innerHTML ='<p><span class="right" id="chiudivideopop">X CHIUDI</span></p>';
			
	videoPopup.appendChild(video);
	videoPopup.appendChild(menunav);
	
	objBody.insertBefore(videoPopup, objBody.firstChild);
	objBody.insertBefore(videoBackground, objBody.firstChild);
						
	chiudiVideoPop = document.getElementById('chiudivideopop');
								 							
	chiudiVideoPop.onclick = function () {hideVideobox(); return false;}			
			               
  var pageSize = getPageSize();
 	videoPopup.style.visibility = 'hidden';
 	videoPopup.style.display = 'block';
  var videoPopupWidth = videoPopup.offsetWidth; 
	var videoPopupHeight = videoPopup.offsetHeight;
	videoPopup.style.visibility = 'visible';
 	videoPopup.style.display = 'none';
	  		
	var boxTop = Math.round((pageSize.windowHeight - videoPopupHeight)/2);
	var boxLeft = Math.round((pageSize.windowWidth -  videoPopupWidth)/2);
					
	boxTop += pageSize.scrollTop;
	boxLeft += pageSize.scrollLeft;

	videoPopup.style.top = (boxTop < 0) ? '0px' : boxTop + 'px';
	videoPopup.style.left = (boxLeft < 0) ? '0px' : boxLeft + 'px';
	
	videoBackground.style.width = pageSize.xScroll +'px';
	videoBackground.style.height = pageSize.yScroll +'px';		
		
	makePlayerObject(objLink.href,video);
	
	videoBackground.style.display = 'block';	
	videoPopup.style.display = 'block';	
}
