/* 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 xmlVideoMetadata;

function _dbg(key, args, msg) {
  try {
    if ( console && console.log ) {
      console.log(key + ': ("' + args.join('","') + '")' + ( msg ? " [" + msg + ']' : ' []' ) );
    }
  } catch (exception) {
    //install firebug
  }
}

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';
	//
}



/* popup video */


function openPopupVideo(xmlVideo){
		
	//document.getElementById("player").innerHTML = makeObjectPlayer(xmlVideo);
	
  	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';

	makeObjectPlayer(xmlVideo)
	return false;
}

function initVideogallery(){
      _dbg("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','playerFlex');

	/*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);
	
	$(".videosingle > a").each(function(i) {
		var urlXmlVideo = '/video/metainfo/'+$(this).attr('id')+'.sxml?view=false';
      		$(this).click(function() {
      			openPopupVideo(urlXmlVideo)
		});
      	});
}

function makeObjectPlayer(urlXmlVideo) {	
	$.ajax({
	  type: "GET",
	  url: urlXmlVideo,
	  dataType: "text",
	  success: function(data) {
	    xmlVideoMetadata = data;
	    CreatePlayer(xmlVideoMetadata);
	  }
});

	

	
	
}


$(function() {
	_dbg("onload()", [], "");	
	initVideogallery();
})



