var projectPath = 'http://cmi2.yale.edu/dev/tell_leilan/';
var isOpen = 0;
var openDiv = '';
var images = new Array();


function showPreview(imgStr, type, arrayInfo) {
	var arr = eval(arrayInfo);
	var imgObj = arr[imgStr];

	if( isOpen == 0 ) {
		displayPreview(imgObj, type);
	}
}

function displayPreview(imgObj, type) {
	
	if( isOpen == 1 ) {
		hidePreview();
	}
	
	// get image object associated with rollover and assign to global var, which is used 
	// during closing of preview window
	openDiv = document.getElementById(imgObj.getDirection()+'PreviewDiv');
	
	// load content
	loadContent(imgObj);	
	
	
	var pos = findPos(document.getElementById(imgObj.getId() + type));
	var x = pos[0];
	var y = pos[1];

	var xPos = 0;
	var yPos = 0;
	
	if( type == 'text' ) {
		if( imgObj.getDirection() == 'horizontal' ) {
			xPos = x - 350;
			yPos = y - 215;	
		} else {
			xPos = x - 260;
			yPos = y - 300;		
		}
	} else {
		if( imgObj.getDirection() == 'horizontal' ) {
			xPos = x - 290;
			yPos = y - 213;
		} else {
			xPos = x - 210;
			yPos = y - 300;
		}
	}

	openDiv.style.left = xPos + 'px';
	openDiv.style.top = yPos + 'px';
	
	openDiv.style.display = "block";

	isOpen = 1;
}


function hidePreview() {
	openDiv.style.display = "none";
	isOpen = 0;
	openDiv = '';
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function loadContent( imgObj ) {

	var scale = "height='160'";
	if(imgObj.getDirection() == 'vertical') {
		scale = "height='240'";
	}

	var html = "<table id=\"previewContents\" cellspacing=\"0\" cellpadding=\"0\">\n";
	html += "	<tr>\n";
	html += "		<td>\n";
	html += "			<img src=\"" +imgObj.getPreviewFilePath()+ "\" " +scale+ " />\n";
	html += "		</td>\n";
	html += "		<td>\n";
	html += "			<p id=\"previewTitle\">" +imgObj.getTitle()+ "</p>\n";
	html += "			<p id=\"previewDesc\">" +imgObj.getDescription()+ "</p>\n";
	html += "		</td>\n";
	html += "	</tr>\n";
	html += "</table>\n";
		
	openDiv.innerHTML = html;

}


function makeItSafe(path) {
    var safePath=escape(path);
    safePath=safePath.replace(/\+/g, "%2B");
    safePath=safePath.replace(/\//g, "%2F"); 
    return safePath;
}

function openPopup(url, figure, width, height) {
	var figureObj = eval(figure);

	if( height == undefined ) {
		height = '600';
	} 
	if( width == undefined ) {
		width = '600';
	}
	
	var popup = escape(figureObj.getPopupFilePath());
	var download = escape(figureObj.getDownloadFilePath());
	var title = escape(figureObj.getTitle());
	var description = escape(figureObj.getDescription());
	var direction = figureObj.getDirection();
	
	url += '?popup=' +makeItSafe(popup)+ '&download=' +makeItSafe(download)+ '&title=' +title+ '&description=' +description+ '&direction=' +direction;
	
	var win = window.open(url, figure,'HEIGHT=' +height+ ',WIDTH=' +width+ ',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=no');
}
