/* popup photos */
var imgloading= '/res/img/loading.gif';
var divPopup;
var divBackground;
var prebox;
var nextbox;
var chiudipop;
var bigimg;
var fotoTitle;
var videoplayer;
var initiate = false;

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 hideImagebox(){
	if (document.getElementById("player")){		
			document.getElementById("player").innerHTML = '';				
	}
	divBackground.style.display = 'none';
	divPopup.style.display = 'none';	
	// IE le combo non vengono nascoste
	var selectElem = document.getElementsByTagName('select');
	for(var i=0 ; i< selectElem.length;i++)
		selectElem[i].style.display = 'block';
	//
}

function openPopupImg(objLink){
	if (!initiate)
		initFotogallery();

	if (fotoTitle.firstChild)
		fotoTitle.removeChild(fotoTitle.firstChild);
		
	bigimg.src = imgloading;
  bigimg.alt = 'loading ...';

	var imgPreload = new Image();

	imgPreload.onload = function(){
												bigimg.src = objLink.href;
  											return false;
											}

	imgPreload.src = objLink.href;
	fotoTitle.appendChild(document.createTextNode(objLink.title));	
				
	var id = parseInt(objLink.id.slice(5));	
		
	if (document.getElementById('photo' +(id - 1))){
		prebox.onclick = function () {openPopupImg(document.getElementById('photo' +(id - 1)));};
		prebox.style.visibility = 'visible';
	} else {
		prebox.style.visibility = 'hidden';
	}
		
	if (document.getElementById('photo' +(id + 1))){		
		nextbox.onclick = function () {openPopupImg(document.getElementById('photo' +(id + 1)));};
		nextbox.style.visibility = 'visible';
	} else {
		nextbox.style.visibility = 'hidden';
	}
		
  if (divPopup.style.display == 'block')
  	return;

  var pageSize = getPageSize();

 	divPopup.style.visibility = 'hidden';
 	divPopup.style.display = 'block';
  var divPopupWidth = divPopup.offsetWidth;
	var divPopupHeight = divPopup.offsetHeight;
	divPopup.style.visibility = 'visible';
 	divPopup.style.display = 'none';

	var boxTop = Math.round((pageSize.windowHeight - divPopupHeight)/2);
	var boxLeft = Math.round((pageSize.pageWidth -  divPopupWidth)/2);

	boxTop += pageSize.scrollTop;
	boxLeft += pageSize.scrollLeft;

	divPopup.style.top = (boxTop < 0) ? '0px' : boxTop + 'px';
	divPopup.style.left = (boxLeft < 0) ? '0px' : boxLeft + 'px';

	divBackground.style.width = pageSize.xScroll +'px';
	divBackground.style.height = pageSize.yScroll +'px';
	
	// IE le combo non vengono nascoste
	var selectElem = document.getElementsByTagName('select');
	for(var i=0 ; i< selectElem.length;i++)
		selectElem[i].style.display = 'none';
	//	
		
	divBackground.style.display = 'block';
	divPopup.style.display = 'block';

	return false;
}

function initFotogallery(){
	if (!document.getElementsByTagName || !document.getElementById )
		 return;

	var objBody = document.getElementsByTagName("body").item(0);

	// div Background
	divBackground = document.createElement("div");
	divBackground.setAttribute('id','fotogbackground');
	divBackground.onclick = function () {hideImagebox(); return false;}
	objBody.appendChild(divBackground);

	// div foto
	divPopup = document.createElement("div");
	divPopup.setAttribute('id','fotopop');
	
	fotoTitle = document.createElement("h4");
	fotoTitle.setAttribute('id','fototitle');

	var wrapTocenter = document.createElement("div");	
	wrapTocenter.className = 'wraptocenter';	
	var navbar = document.createElement("div");
	navbar.setAttribute('id','navbar');
	
	prebox = document.createElement("div");
	prebox.setAttribute('id','prebox');
	preboxspan = document.createElement("span")
	prebox.appendChild(preboxspan);
	preboxspan.appendChild( document.createTextNode('< INDIETRO') );	
	prebox.style.visibility = 'hidden';
	
	chiudipop = document.createElement("div");
	chiudipop.setAttribute('id','chiudipop');
	chiudipopspan = document.createElement("span")
	chiudipop.appendChild(chiudipopspan);
	chiudipopspan.appendChild( document.createTextNode('X CHIUDI') );
	chiudipop.onclick = function () {hideImagebox(); return false;}
	
	nextbox = document.createElement("div");
	nextbox.setAttribute('id','nextbox');
	nextboxspan = document.createElement("span")
	nextbox.appendChild(nextboxspan);
	nextboxspan.appendChild( document.createTextNode('AVANTI >') );	
	nextbox.style.visibility = 'hidden';

	navbar.appendChild(prebox);
	navbar.appendChild(chiudipop);
	navbar.appendChild(nextbox);

	bigimg = document.createElement("img");
	bigimg.setAttribute('id','fotogbigimg');
	bigimg.setAttribute('src','');

	wrapTocenter.appendChild(bigimg);	
	divPopup.appendChild(fotoTitle);
	divPopup.appendChild(wrapTocenter);
	divPopup.appendChild(navbar);
		
	objBody.appendChild(divPopup);
	initiate = true;
}
/* end popup photos */

/* popup video */


function openPopupVideo(video_file){
	if (!initiate)
		initVideogallery();
		
	document.getElementById("player").innerHTML = makeObjectPlayer(video_file);
	
  if (divPopup.style.display == 'block')
  	return;

  var pageSize = getPageSize();

 	divPopup.style.visibility = 'hidden';
 	divPopup.style.display = 'block';
  	var divPopupWidth = divPopup.offsetWidth;
	var divPopupHeight = divPopup.offsetHeight;
	divPopup.style.visibility = 'visible';
 	divPopup.style.display = 'none';

	var boxTop = Math.round((pageSize.windowHeight - divPopupHeight)/2);
	var boxLeft = Math.round((pageSize.pageWidth -  divPopupWidth)/2);

	boxTop += pageSize.scrollTop;
	boxLeft += pageSize.scrollLeft;

	divPopup.style.top = (boxTop < 0) ? '0px' : boxTop + 'px';
	divPopup.style.left = (boxLeft < 0) ? '0px' : boxLeft + 'px';

	divBackground.style.width = pageSize.xScroll +'px';
	divBackground.style.height = pageSize.yScroll +'px';
	
	// IE le combo non vengono nascoste
	if (navigator.userAgent.toLowerCase().indexOf("msie")>-1){
		var selectElem = document.getElementsByTagName('select');
		for(var i=0 ; i< selectElem.length;i++)
			selectElem[i].style.display = 'none';
	}
	//	
	
	divBackground.style.display = 'block';
	divPopup.style.display = 'block';

	
	return false;
}

function initVideogallery(){
	if (!document.getElementsByTagName || !document.getElementById )
		 return;

	var objBody = document.getElementsByTagName("body").item(0);

	// div Background
	divBackground = document.createElement("div");
	divBackground.setAttribute('id','fotogbackground');
	divBackground.onclick = function () {hideImagebox(); return false;}
	objBody.appendChild(divBackground);

	// div video
	divPopup = document.createElement("div");
	divPopup.setAttribute('id','videopop');

	
	chiudipop = document.createElement("div");
	chiudipop.setAttribute('id','chiudipop');
	chiudipopspan = document.createElement("span")
	chiudipop.appendChild(chiudipopspan);
	chiudipopspan.appendChild( document.createTextNode('X CHIUDI') );
	chiudipop.onclick = function () {hideImagebox(); return false;}
	
	
	
	
	videoplayer = document.createElement("div");	
	videoplayer.setAttribute('id','player');	
	
	
	
	
	
	divPopup.appendChild(videoplayer);	
	divPopup.appendChild(chiudipop);
	objBody.appendChild(divPopup);
	initiate = true;
}

function createActiveXObject(id) {
  var error;
  var control = null;

  try {
    if (window.ActiveXObject) {
      control = new ActiveXObject(id);
    } else if (window.GeckoActiveXObject) {
      control = new GeckoActiveXObject(id);
    }
  } catch (error) {
    //nothing
  }
  return control;
}

function makeObjectPlayer(video_file) {	

	var is_iexplorer = navigator.userAgent.toLowerCase().indexOf("msie")>-1;
	


	var video_w = 320;
	var video_h = 305;

	var urlvideo = 'http://video.farm.mediaset.it/txt/fabbricadelsorriso/'+video_file;
	
	
	var objectPlayer='';

	if (is_iexplorer) {				
			objectPlayer += '<object id="MediaPlayer" name="MediaPlayer" width="' + video_w + '" height="' + video_h + '" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">';
			objectPlayer += '<param name="URL" value="' + urlvideo + '" >';
			objectPlayer += '<param name="ShowStatusBar" value="1" >';
			objectPlayer += '<param name="AutoStart" value="1" >';
			objectPlayer += '<param name="ShowControls" value="1" >';
			objectPlayer += '<param name="StretchToFit" value="1" >';
			objectPlayer += '<param name="ShowTracker" value="0" >';
			objectPlayer += '<param name="EnableTracker" value="0" >';
			objectPlayer += '<param name="ShowPositionControls" value="0" >';
			objectPlayer += '<param name="uiMode" value="full" >';
			objectPlayer += '<param name="enableContextMenu" value="1" >';
			objectPlayer += '<param name="windowlessVideo" value="0" >';
			objectPlayer += '</object>';			
	} else {
		objectPlayer +='<object id="MediaPlayer" name="MediaPlayer" width="' + video_w + '" height="' + video_h + '" type="video/x-ms-wmv" data="' + urlvideo + '">';
		objectPlayer +='<param name="src" value="' +  urlvideo + '" >';
		objectPlayer +='<param name="filename" value="' +  urlvideo + '" >';
		objectPlayer +='<param name="autostart" value="1" >';
		objectPlayer +='<param name="controller" value="1" >';
		objectPlayer +='<param name="ShowControls" value="1" >';
		objectPlayer +='<param name="StretchToFit" value="1" >';
		objectPlayer +='<param name="maintainaspectratio" value="1" >';
		objectPlayer +='<param name="EnablePositionControls" value="1" >';
		objectPlayer +='<param name="enablecontextmenu" value="1" >'; //Firefox on Win
		objectPlayer +='<param name="showstatusbar" value="1" >';
		objectPlayer +='<param name="uiMode" value="full" >';
		objectPlayer +='<param name="DisplaySize" value="0" >'; //For WMP 11 on 3rd party browser prevent horizontal stretch
		objectPlayer +='</object>';				
	}
	
	return objectPlayer;
}

/* fine popup video */

/* util date */

function daysElapsed(dateFrom, dateTo){
	var param = dateFrom.split("/");
	var datetimeFrom = Date.UTC(param[0], parseInt( param[1]) - 1 , param[2]);
	param = dateTo.split("/");
	var datetimeTo = Date.UTC(param[0], parseInt( param[1]) - 1 , param[2]);
	
	var days = (datetimeTo - datetimeFrom) / (1000*3600*24);
	document.write('Aggiunto: '+ ( days > 0 ?  days == 1 ? '1 giorno fa' : days +' giorni fa' : ' oggi') );	
}
