function GetXmlHttpObject() { 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

//var xmlHttp;
var MAX_COLS = 2;
var liveId = 'Photos';
var coverFile = 'cover.jpg';
var coverFileJPG = 'cover.JPG';
var coverFileCJPG = 'Cover.JPG';
var coverFileC = 'Cover.jpg';
var coverDenote = '_COVER';
var coverDenoteJPG = '_COVER_JPG';
var coverDenoteCJPG = '_COVER_CJPG';
var coverDenoteC = '_COVER_C';
var defaultCover = '../images/default_album_cover.jpg';

function processResponse(xmlHttp, dir, response) {
	var cols = 0;
	var code = '';
	var file;
	var albumDirId;
	var cover;
	var albumName;
	var files = response.split("\n");
	var dirId = getDirId(dir);
	var divHtml = '<div id="'+dirId+'"';
	if (dir == 'Photos') divHtml += ' style="display: block;"';
	else divHtml += ' style="display: none;"';
	divHtml += '></div>';
	document.getElementById('photo-space').innerHTML = document.getElementById('photo-space').innerHTML + divHtml;
	code += '<table class="photo-page">';
	if (response == '') {
		return;
	}
	for (var i in files) {
		file = files[i];
		if (file.match(coverDenote+"$") == coverDenote) { // cover.jpg
			file = file.substring(0, file.length-coverDenote.length);
			cover = file+'/'+coverFile;
		} else if (file.match(coverDenoteJPG+"$") == coverDenoteJPG) { // cover.JPG
			file = file.substring(0, file.length-coverDenoteJPG.length);
			cover = file+'/'+coverFileJPG;
		} else if (file.match(coverDenoteCJPG+"$") == coverDenoteCJPG) { // Cover.JPG
			file = file.substring(0, file.length-coverDenoteCJPG.length);
			cover = file+'/'+coverFileCJPG;
		} else if (file.match(coverDenoteC+"$") == coverDenoteC) { // Cover.jpg
			file = file.substring(0, file.length-coverDenoteC.length);
			cover = file+'/'+coverFileC;
		} else {
			cover = defaultCover;
		}
		if (cols == 0) code += '<tr>';
		cols++;
		code += '<td>';
		if (file.indexOf('.') > -1) { // file
			code += '<div class="image-shadow"><img src="'+file+'"/></div>';
		} else { // directory
			albumDirId = getDirId(file);
			albumName = file.substring(file.lastIndexOf('/')+1, file.length);
			code += '<a href="#" onClick="setPhotoPage(\''+file+'\'); return false;" onmouseover="clickPrompt(\''+albumDirId+'\', true);" onmouseout="clickPrompt(\''+albumDirId+'\', false);"><div class="image-shadow"><div class="image-white"><div class="image-shadow"><div class="image-white"><div class="image-shadow"><img src="'+cover+'"/></div></div></div></div></div>'+albumName+'</a><div id="'+albumDirId+'_prompt" class="view-album-prompt">&nbsp;</div>';
			showDirPhotos(file);
		}
		code += '</td>';
		if (cols >= MAX_COLS) {
			code += '</tr>';
			cols = 0;
		}
	}
	code += '</table>';
	document.getElementById(dirId).innerHTML = code;
	//document.getElementById('test').innerHTML = document.getElementById('test').innerHTML+code+"\n\n";
}

function clickPrompt (dirId, show) {
	if (navigator.appName != 'Microsoft Internet Explorer') {
		if (show) {
			document.getElementById(dirId+'_prompt').innerHTML = 'Click to view album';
		}
		else {
			document.getElementById(dirId+'_prompt').innerHTML = '&nbsp;';
		}
	}
}

function setPhotoPage (dir) {
	if (liveId != '') {
		document.getElementById(liveId).style.display = 'inline';
		document.getElementById(liveId).style.display = 'none';
	}
	setBreadcrumb(dir);
	liveId = getDirId(dir);
	document.getElementById(liveId).style.display = 'inline';
	document.getElementById(liveId).style.display = 'block';
	self.location = '#photo-top';
}

function setBreadcrumb (dir) {
	var crumbLoc = '';
	var crumb = '';
	var crumbs = dir.split('/');
	for (var i in crumbs) {
		if (i > 0) {
			crumbLoc += '/';
			crumb += ' ... ';
		}
		crumbLoc += crumbs[i];
		crumb += '<a href="#" onClick="setPhotoPage(\''+crumbLoc+'\'); return false;">'+crumbs[i]+'</a>';
	}
	document.getElementById('breadcrumb').innerHTML = crumb;
}

function getDirId (dir) {
	return dir.replace(/\//g, '_');
}

function showDirPhotos (dir) {
	var xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="/photos/showDirPhotos.php?dir="+dir+"&sid="+Math.random();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			processResponse(xmlHttp, dir, xmlHttp.responseText);
		}
	};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
