
Event.observe(window, 'load', initPreview, false);

function changePreview(imageURL,submissionID,width,height,order){
	var objPreview = document.getElementById("preview");
	var arrayPScroll  = getPageScroll();
	var arrayPageSize = getPageSize();

 	fullWidth   = width + 100;
  	fullHeight  = height + 134;

	leftIndent  = (1000 - (fullWidth + 2)) / 2;
  	topIndent   = arrayPScroll[1] + ((arrayPageSize[3] - (fullHeight + 2)) / 2);

	objPreview.style.width  = fullWidth  + "px";
  	objPreview.style.height = fullHeight + "px";
  	objPreview.style.left   = leftIndent + "px";
  	objPreview.style.top    = topIndent  + "px";

	AJAXRequest('preview','blank','','','','');	
	AJAXRequest('preview','votingPreview',imageURL,submissionID,fullWidth,order);
}

function showPreview(imageURL,submissionID,width,height,order){
  var objPreview = document.getElementById("preview");
  var objPreload = document.getElementById("preload");
  var objOverlay = document.getElementById("overlay");
  var arrayPScroll  = getPageScroll();
	var arrayPageSize = getPageSize();
	
  var pageHeight = arrayPageSize[1] + arrayPScroll[1];
  objOverlay.style.height = pageHeight + "px";
  
  fullWidth   = width + 100;
  fullHeight  = height + 124;
    
  leftIndent  = (1000 - (fullWidth + 2)) / 2;
  topIndent   = arrayPScroll[1] + ((arrayPageSize[3] - (fullHeight + 2)) / 2); 
    
  objPreview.style.width  = fullWidth  + "px";
  objPreview.style.height = fullHeight + "px";
  objPreview.style.left   = leftIndent + "px";
  objPreview.style.top    = topIndent  + "px";

  AJAXRequest('preview','votingPreview',imageURL,submissionID,fullWidth,order);
  new Effect.Appear('preview', { duration: 1, from: 0.0, to: 1 });
  new Effect.Appear('overlay', { duration: 0.6, from: 0.0, to: 0.6 });
}

function hidePreview(){
  new Effect.Fade('overlay', { duration: 1, from: 0.6, to: 0.0 });
  new Effect.Fade('preview', { duration: 0.6, from: 1, to: 0.0 });
  AJAXRequest('preview','blank','','','','');
}

function initPreview(){
  var arrayPageSize = getPageSize();
  var arrayPScroll  = getPageScroll();
  
  var objOverlay = document.getElementById("overlay");
  var pageHeight = arrayPageSize[1] + arrayPScroll[1];
  objOverlay.style.height = pageHeight + "px";
}

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function getPageSize(){
	var xScroll, yScroll;
	
	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;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	
	return arrayPageScroll;
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}
}