var appName = "FrontPicture";
var appType = "P";
  
var fpTimerWait=null;
var fpTimerBetweenPics=null;
var b=null;
var h=null;
function FrontPicture_onload() {
	h = document.getElementsByTagName('html')[0];
    b = document.getElementsByTagName('body')[0];
	getPicture();
	oldSize = fpGetSize();
}

var pic = null;
function getPicture() {
	var params = new Array();
	params["position"] = position+1;
	oXmlRequest = new XmlRequest();
	oXmlRequest.functionName	= "getPicture";
	oXmlRequest.params 			= params;	
	oXmlRequest.onsuccess 		= getPicture_success;
	oXmlRequest.appName = "FrontPicture";
	oXmlRequest.start();
	function getPicture_success(oXmlResponse) {
		pic = oXmlResponse.data[0];
		position = parseInt(pic["position"]);
		calculateImage(); //pic["albumid"], pic["pictureid"], pic["width"], pic["height"], pic["caption"]);
		fpTimerBetweenPics = setTimeout(getPicture, pictureTimeout * 1000);
		
		
	}	
}

function setPic(albumid, pictureid, width, height, caption) {
	if (fpTimerBetweenPics != null) 
		clearTimeout(fpTimerBetweenPics);
	
	pic = new Array();
	pic["albumid"] = albumid;
	pic["pictureid"] = pictureid;
	pic["caption"] = caption;
	pic["width"] = width;
	pic["height"] = height;
}


var oldSize = "";

function fpGetSize() {
	return h.offsetWidth + ' ' + ' ' + h.clientWidth  + ' ' + b.offsetWidth  + ' ' + b.clientWidth;
}

onresize = function() {
	//var tempData = h.offsetHeight + ' ' + h.scrollHeight + ' ' + h.clientHeight + ' ' + b.offsetHeight + ' ' + b.scrollHeight + ' ' + b.clientHeight;
	var tempData = fpGetSize();
    if (oldSize != tempData) {
    	oldSize = tempData;
     	hideImage();
     	if (fpTimerWait != null)
     		clearTimeout(fpTimerWait);
     	fpTimerWait = setTimeout(getPicture, 1000);
    }
}

function calculateImage() {
	if (fpTimerWait != null)
     		clearTimeout(fpTimerWait);
     	
	td = document.getElementById("MainPhotoTd");
	var tdWidth = td.offsetWidth - 8; // border
	var tdHeight = td.offsetHeight - 8;
	var picWidth = parseInt(pic["width"]);
	var picHeight = parseInt(pic["height"]);
	if (picWidth > tdWidth) {
		var resizeFactor = tdWidth / picWidth;
		picWidth = tdWidth;
		picHeight = Math.round(picHeight * resizeFactor);
		swapImage(pic["albumid"], pic["pictureid"], pic["caption"], picWidth, picHeight, 0, true);
	}
	else {
		var leftPosition = Math.round((tdWidth - picWidth) / 2); 
		swapImage(pic["albumid"], pic["pictureid"], pic["caption"], picWidth, picHeight, leftPosition, true);
	}
}


function hideImage() {
	if (activePicture == 1) {
		document.getElementById("FrontPicture1").style.display = "none";
	}
	else {
		document.getElementById("FrontPicture2").style.display = "none";
	}
}

var previousHeight = 0;
function swapImage(albumid, pictureid, caption, width, height, leftPosition, resizeNeeded) {
	//if (fpTimerWait != null)
 //    		clearTimeout(fpTimerWait);
  /*if (leftPosition > 0) {}
  else
  	leftPosition = 0;
  */
	if (activePicture == 1) {
		src = document.getElementById("FrontPicture1");
		dest = document.getElementById("FrontPicture2");
		activePicture = 2;
	}
	else {
		src = document.getElementById("FrontPicture2");
		dest = document.getElementById("FrontPicture1");
		activePicture = 1;
	}
	if (height > previousHeight) {
		// resize onmiddellijk
		document.getElementById("MainPhotoDiv").style.height = height + "px";
	}
	document.getElementById("FrontPictureLink").href = forumroot + "/album.php?albumid=" + albumid + "&pictureid=" + pictureid;
	dest.src = layoutroot + "/img/spacer.gif";
	dest.src = forumroot + "/picture.php?albumid=" + albumid + "&pictureid=" + pictureid;
	dest.alt = caption;
	dest.title = caption;
	dest.style.display = "none";
	if (resizeNeeded) {
		dest.width = width;
		dest.height = height;
	}
	var top = 140 + document.getElementById('Banner_Large').offsetHeight;
	dest.style.top = top + "px";
	
	
	dest.style.left = (leftPosition+14) + "px"; // 14 = basePos (css)
	new Effect.Fade(src);	
	destId = dest.id;
	if (height > previousHeight) { 
		new Effect.Appear(destId, { afterFinish: getThumbnails });
		
	}
	else {
		new Effect.Appear(destId, { afterFinish: afterFin });
	}
	previousHeight = height;
}

var destId = "";



function afterFin() {
	document.getElementById("MainPhotoDiv").style.height = previousHeight + "px";
	
	getThumbnails();
}

