// Draw a photo display overlay using ajax
function drawPhoto(pkContent) {
	_closedraw();
	obj = document.createElement('img');
	obj.src = '/_images/loading.gif';
	if(obj.getAttribute('className') != null) obj.setAttribute('className','center'); 
	else obj.setAttribute('class','center');
	drawSimple(obj);
		
	var d = new Date();
	url = '/_xml/photogallery/?comaction=show&pkContent='+pkContent+'&x='+d.getTime();
	
	ajax = new AjaxRequest();
	ajax.open('GET', url, true);
	ajax.onreadystatechange = function(){
		if (ajax.readyState==4 && ajax.status==200) {
			_closedraw();
			obj = document.createElement('div');
			
			if(obj.getAttribute('className') != null) obj.setAttribute('className','drawphoto center'); 
			else obj.setAttribute('class','drawphoto center');
			
			obj.innerHTML = ajax.responseText;
			
			oImg = obj.getElementsByTagName('img')[0];
			oDiv = obj.parentNode;
			if(typeof oImg == 'object') oImg.onload = function(){
				var arr = getPageSize();
				odivwidth = navigator.userAgent.indexOf('MSIE')==-1 ? oDiv.offsetWidth : oDiv.clientWidth;
				imagewidth = oImg.width < odivwidth ? odivwidth : oImg.width;
				oDiv.style.left = (arr[0]-imagewidth)/2+'px';
				oPs = oDiv.getElementsByTagName('p');
				
				// Make the summary as wide as the image
				for (i in oPs)
					if (typeof oPs[i] == 'object' && oPs[i].getAttribute('class') == 'summary')
						oPs[i].style.maxWidth = oImg.width+'px';

			}
				
			
			drawSimple(obj);
		}
		
		else if (ajax.readyState==4) window.location = url;
	};
	
/*
	oImgPreload = new Image();
	oImgPreload.onload = function() {
		ajax.send(null);
	}
	oImgPreload.src = '/_xml/photogallery/?comaction=redirect&pkContent='+pkContent;
*/
	
	ajax.send(null);
	
	return false;
}


function drawImage(path,text) {
	obj = document.createElement('p');
	obj.setAttribute('class','center');
	obj.appendChild(document.createTextNode("Loading image..."));
	drawSimple(obj);
	oImgPreload = new Image();
	//oImg = new Image();
	oImg = document.createElement("img");
	oImgPreload.onload = function() {
		oP = document.createElement("p");
		oP.appendChild(document.createTextNode(text));
		oImg.src = path;
		_closedraw();
		oDiv = document.createElement("div");
		oDiv.appendChild(oP);
		oDiv.appendChild(oImg);
		drawSimple(oDiv);
	}
	oImgPreload.src = path;
}

function drawSimple(html) {
	document.onkeydown = _closedraw;
	/*oSelects = document.getElementsByTagName('select');
	for (i=0;i<oSelects.length;i++) {
		oSelects[i].style.display = 'none';	
	}*/
	var arr = getPageSize();
	oBody = document.getElementsByTagName("body")[0];
	oDiv = document.createElement("div");
	oDiv.setAttribute("id","drawling");
	oDiv.appendChild(html);
	oA = document.createElement("a");
	oA.innerHTML = 'x';
	oA.setAttribute("href","javascript:_closedraw()");
	if(oA.getAttribute('className') != null) oA.setAttribute('className','close'); 
	else oA.setAttribute('class','close');
	oDiv.appendChild(document.createElement("br"));
	oDiv.appendChild(oA);
	oOverlay = document.createElement("div");
	oOverlay.style.height = arr[1]+'px';
	oOverlay.setAttribute("id","overlay");
	oOverlay.setAttribute('onclick', '_closedraw()');
	oBody.appendChild(oOverlay);
	oBody.appendChild(oDiv);
	oDiv.style.top = '50px';
	odivwidth = (navigator.userAgent.indexOf('MSIE')==-1) ? oDiv.offsetWidth : oDiv.clientWidth;
	if (html.nodeName == "IMG") {
		oImg = document.getElementById('drawling').childNodes[0];
		imagewidth = (oImg.width < odivwidth) ? odivwidth : oImg.width;
		oDiv.style.left = ((arr[0]/2)-(imagewidth/2))+'px';
	} else oDiv.style.left = ((arr[0]/2)-(odivwidth/2))+'px';
	window.scrollTo(0,0);
}
function drawError(message) {
	/*oSelects = document.getElementsByTagName('select');
	for (i=0;i<oSelects.length;i++) {
		oSelects[i].style.display = 'none';	
	}*/
	var arr = getPageSize();
	aMessage = message.split("\n");
	oBody = document.getElementsByTagName("body")[0];
	oDiv = document.createElement("div");
	oDiv.setAttribute("id","drawling");
	oUl = document.createElement("ul");
	for (i=0;i<aMessage.length;i++) {
		if (aMessage[i] != "") {
			oLi = document.createElement("li");
			oLi.appendChild(document.createTextNode(aMessage[i]))
			oUl.appendChild(oLi);
		}
	}
	oDiv.appendChild(oUl);
	oA = document.createElement("a");
	oA.appendChild(document.createTextNode("Close"));
	oA.setAttribute("href","javascript:_closedraw()");
	oDiv.appendChild(document.createElement("br"));
	oDiv.appendChild(oA);
	oOverlay = document.createElement("div");
	oOverlay.style.height = arr[1]+'px';
	oOverlay.setAttribute("id","overlay");;
	oBody.appendChild(oOverlay);
	oBody.appendChild(oDiv);
	oDiv.style.top = '100px';
	oDiv.style.left = ((arr[0]/2)-200)+'px';
	window.scrollTo(0,0);
}

function _closedraw() {
	if (document.getElementById('drawling')==null) return;
	tip = document.getElementById('drawling');
	if (typeof tip == "object") tip.parentNode.removeChild(tip);
	over = document.getElementById('overlay');
	if (typeof over == "object") over.parentNode.removeChild(over);
	oSelects = document.getElementsByTagName('select');
	/*for (i=0;i<oSelects.length;i++) {
		oSelects[i].style.display = '';	
	}*/
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
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;
}
