var isOpen = 0;
var openDiv = '';
var openImg = '';
var imgDirection = 'horizontal';
var imgType = 'image';
var winWidth = '';
var winHeight = '';

var projectPath = 'http://leilan.yale.edu/';
var images = new Array();
var currentImage = 0;
	
function loadPreview() {
	if( isOpen == 1 ) {
		hidePreview();
	}
	
	openDiv = document.getElementById(imgDirection+'PreviewDiv');
	openDiv.innerHTML = this.req.responseText;
		
	var pos = findPos(document.getElementById(openImg + imgType));
	var x = pos[0];
	var y = pos[1];

	var xPos = 0;
	var yPos = 0;
	
	if( imgType == 'text' ) {
		if( imgDirection == 'horizontal' ) {
			xPos = x - 350;
			yPos = y - 215;	
		} else {
			xPos = x - 260;
			yPos = y - 300;		
		}
	} else {
		if( imgDirection == '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 showPreview( id, direction, type ) {
	openImg = id;
	imgDirection = direction;
	imgType = type;
	
	var url = projectPath+ 'gallery.php?action=load_preview&image_id=' +id;
	new net.ContentLoader(url, loadPreview);
}

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

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

function loadPopup() {
	var response = this.req.responseText;
	var parts = response.split('|');

	var popup = escape(parts[0]);
	var download = escape(parts[1]);
	var title = escape(parts[2]);
	var description = escape(parts[3]);
			
	var url = projectPath+ 'photo_viewer.php?popup=' +makeItSafe(popup)+ '&download=' +makeItSafe(download)+ '&title=' +title+ '&description=' +description+ '&direction=' +imgDirection;
	var win = window.open(url, openImg,'HEIGHT=' +winHeight+ ',WIDTH=' +winWidth+ ',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=no');
}

function openPopup(id, direction, width, height) {
	if( isOpen == 1 ) {
		hidePreview();
	}

	openImg = id;
	imgDirection = direction;
	
	if( direction == 'vertical' ) {
		winWidth = parseInt(width);
		winHeight = parseInt(height) + 100;
		
		if( winWidth < 375 ) {
			winWidth += (375-winWidth) + 265;
		} else {
			winWidth += 265;
		}
	} else {
		winWidth = parseInt(width) + 100;
		winHeight = parseInt(height) + 265;
	}

	var url = projectPath+ 'gallery.php?action=load_popup&image_id=' +id;
	new net.ContentLoader(url, loadPopup);
}
	
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 retrieveMetaData() {
	var image = images[currentImage];
	var url = projectPath + 'gallery.php?action=load_preview&image_id=' +image.getId();
	new net.ContentLoader(url, storeMetaData);	
}

function storeMetaData() {
	images[currentImage].setPreviewHtml(this.req.responseText);
	
	currentImage++;
	if( currentImage < images.length ) {
		retrieveMetaData();
	}
}

function showPreloadedPreview(pos, type) {
	if( isOpen == 1 ) {
		hidePreview();
	}
	
	pos -= 1;
	
	var image = images[pos];
	var direction = image.getDirection();
			
	openDiv = document.getElementById(direction+'PreviewDiv');
	openDiv.innerHTML = image.getPreviewHtml();
		
	var pos = findPos(document.getElementById(image.getId() + type));
	var x = pos[0];
	var y = pos[1];

	var xPos = 0;
	var yPos = 0;
	
	if( type == 'text' ) {
		if( direction == 'horizontal' ) {
			xPos = x - 350;
			yPos = y - 215;	
		} else {
			xPos = x - 260;
			yPos = y - 300;		
		}
	} else {
		if( direction == '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;

}


