//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	    updateChatWindow('Status: Could not create XmlHttpRequest Object.  Consider upgrading your browser.');
	}
}
/* -------------- pagination functions -------------- */

var curPaginate = 1;
var totalPages;

function disablePrevious() {
	var prevButton = document.getElementById('prevButton');
	prevButton.innerHTML = 'Prev&nbsp;';
}
function disableNext() {
	var nextButton = document.getElementById('nextButton');
	nextButton.innerHTML = 'Next';
}

function flushPagination(fnct) {
	//clear page links
	document.getElementById('pageLinks').innerHTML = '';
	//enable previous
	var prevButton = document.getElementById('prevButton');
	prevButton.innerHTML = '<a href="javascript:void(0);" onclick="changePagination(curPaginate-1, \''+fnct+'\');">Prev</a>&nbsp;';
	//enable next
	var nextButton = document.getElementById('nextButton');
	nextButton.innerHTML = '<a href="javascript:void(0);" onclick="changePagination(curPaginate+1,\''+fnct+'\');">Next</a>';
}

function createPageLink(pageNumber, curPage, fnct) {
	pageLinksHTML = document.getElementById('pageLinks').innerHTML;
	if(curPage) {
		document.getElementById('pageLinks').innerHTML = pageLinksHTML + '<span style="font-weight:bold;" class="selected">'+pageNumber+'</span>&nbsp;';
	} else {
		document.getElementById('pageLinks').innerHTML = pageLinksHTML + '<span><a href="javascript:void(0);" onclick="changePagination('+pageNumber+', \''+fnct+'\');">'+pageNumber+'</a></span>&nbsp;';
	}
}

function setPagination(fnct) {
	flushPagination(fnct);
	if(curPaginate == 1) {
		disablePrevious();
	}
	if(curPaginate == totalPages) {
		disableNext();
	}
	for(n=1;n<=totalPages;n++) {
		if(curPaginate != n) {
			createPageLink(n,0,fnct);
		} else {
			createPageLink(n,1,fnct);
		}
	}
}

function changePagination(page, fnct) {
	curPaginate = page;
	setPagination(fnct);
	window[fnct]();
}

var totalPagesReq = getXmlHttpRequestObject();
function getTotalPages() {
	if(totalPagesReq.readyState == 4 || totalPagesReq.readyState == 0) {
		var url = '/mediafs/mediagallery/getItems.php';
		var params = 'action=2';
		params += '&albumID='+currentAlbum;
		totalPagesReq.open("POST",url,false);
		totalPagesReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		totalPagesReq.setRequestHeader("Content-length", params.length);
		totalPagesReq.setRequestHeader("Connection", "close");
		totalPagesReq.send(params);
		var xmldoc = totalPagesReq.responseXML;
		var pages_nodes = xmldoc.getElementsByTagName("totalpages"); 
		totalPages = pages_nodes[0].firstChild.nodeValue;
	}
}


/* -------------- images.php functions -------------- */

var itemsReq = getXmlHttpRequestObject();

function getItems() {
	if(itemsReq.readyState == 4 || itemsReq.readyState == 0) {
		var url = '/mediafs/mediagallery/getItems.php';
		var params = 'currentPage='+curPaginate;
		params += '&albumID='+currentAlbum;
		itemsReq.open("POST",url,true);
		itemsReq.onreadystatechange = getItemsStateChanged;
		itemsReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		itemsReq.setRequestHeader("Content-length", params.length);
		itemsReq.setRequestHeader("Connection", "close");
		itemsReq.send(params);
	}
}

function getItemsStateChanged() {
	document.getElementById('photosContainer').style.height = '600px';
	if(itemsReq.readyState==4) {
		document.getElementById('photosContainer').innerHTML = '';
		var xmldoc = itemsReq.responseXML;
		var item_nodes = xmldoc.getElementsByTagName("item"); 
		var n_items = item_nodes.length;
		var i = 0;
		for (i = 0; i < n_items; i++) {
			var thumb_node = item_nodes[i].getElementsByTagName("thumb");
			var caption_node = item_nodes[i].getElementsByTagName("caption");
			if(caption_node[0].firstChild.nodeValue == 'NULL') {
				caption_node[0].firstChild.nodeValue = '';
			}
			var itemID = item_nodes[i].getAttribute("id");
			var addText = '';
			addText += '<div class="listContainer" style="font-size:12px;"><div class="itemBorder"><a href="/mediafs/mediagallery/'+currentAlbum+'/'+itemID+'/"><img src="/mediafs/photoGallery/'+thumb_node[0].firstChild.nodeValue+'" class="listPhoto" border="0" /></a><br />'+caption_node[0].firstChild.nodeValue+'</div></div>';
			document.getElementById('photosContainer').innerHTML += addText;
		}
		document.getElementById('photosContainer').style.height = 'auto';
	} else {
		document.getElementById('photosContainer').style.height = '600px';
		document.getElementById('photosContainer').innerHTML = '<img src="/images/ajax-loader.gif" />';
	}
}
/* -------------- viewImage.php functions -------------- */

viewItemReq = getXmlHttpRequestObject();

function viewItem(itemID) {
	if(viewItemReq.readyState == 4 || viewItemReq.readyState == 0) {
		var url = '/mediafs/mediagallery/viewItem.php';
		var params = 'itemID='+itemID;
		params += '&albumID='+currentAlbum;
		viewItemReq.open("POST",url,true);
		viewItemReq.onreadystatechange = viewItemStateChanged;
		viewItemReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		viewItemReq.setRequestHeader("Content-length", params.length);
		viewItemReq.setRequestHeader("Connection", "close");
		viewItemReq.send(params);
	}
}

function viewItemStateChanged() {
	document.getElementById('photosContainer').style.height = '600px';
	if(viewItemReq.readyState == 4) {
		var xmldoc = viewItemReq.responseXML;
		var type_node = xmldoc.getElementsByTagName("type"); 
		var location_node = xmldoc.getElementsByTagName("location"); 
		var next_node = xmldoc.getElementsByTagName("next"); 
		var prev_node = xmldoc.getElementsByTagName("prev"); 
		if(location_node[0].firstChild.nodeValue != '0') {
			var caption_node = xmldoc.getElementsByTagName("caption");
			if(caption_node[0].firstChild.nodeValue == 'NULL') {
				caption_node[0].firstChild.nodeValue = '';
			}
			document.getElementById('pageLinks').innerHTML = '';
			document.getElementById('prevButton').innerHTML = '<a href="/mediafs/mediagallery/'+currentAlbum+'/'+prev_node[0].firstChild.nodeValue+'/">Prev</a>';
			document.getElementById('nextButton').innerHTML = '&nbsp;<a href="/mediafs/mediagallery/'+currentAlbum+'/'+next_node[0].firstChild.nodeValue+'/">Next</a>';
			document.getElementById('subNav').innerHTML = '<a href="/mediafs/mediagallery/albums/'+currentAlbum+'/">Back to '+currentAlbumName+'</a>';
			document.getElementById('photosContainer').innerHTML = '';
			var addText = '';
			if(type_node[0].firstChild.nodeValue == "photo") {
				addText = '<a href="/mediafs/mediagallery/'+currentAlbum+'/'+next_node[0].firstChild.nodeValue+'/"><img src="/mediafs/photoGallery/'+location_node[0].firstChild.nodeValue+'" class="viewPhoto" border="0" /></a><br /><br />'+caption_node[0].firstChild.nodeValue+'<br /><br />';
			} else if(type_node[0].firstChild.nodeValue == "movie") {
				addText = '<object id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/ nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows® Media Player components..." type="video/x-ms-wvx" width="640" height="480" data="/mediafs/photoGallery/'+location_node[0].firstChild.nodeValue+'"><param name="fileName" value="/mediafs/photoGallery/'+location_node[0].firstChild.nodeValue+'"><param name="animationatStart" value="true"><param name="transparentatStart" value="true"><param name="autoStart" value="true"><param name="showControls" value="true"><embed type="video/x-ms-wvx" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="/mediafs/photoGallery/'+location_node[0].firstChild.nodeValue+'" name="MediaPlayer1" height="480" width="640" autostart="true" showcontrols="true"></object>';
			}
			document.getElementById('photosContainer').innerHTML = addText;
			document.getElementById('photosContainer').style.height = 'auto';
		} else {
			document.getElementById('photosContainer').style.height = 'auto';
		}
	} else {
		document.getElementById('photosContainer').style.height = '600px';
		document.getElementById('photosContainer').innerHTML = '<img src="/images/ajax-loader.gif" />';
	}
}

/* -------------- getAlbums.php functions -------------- */

var albumReq = getXmlHttpRequestObject();

function getAlbums() {
	if(albumReq.readyState == 4 || albumReq.readyState == 0) {
		var url = '/mediafs/mediagallery/getAlbums.php';
		var params = 'action=1';
		params += '&currentPage='+curPaginate;
		albumReq.open("POST",url,true);
		albumReq.onreadystatechange = getAlbumsStateChanged;
		albumReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		albumReq.setRequestHeader("Content-length", params.length);
		albumReq.setRequestHeader("Connection", "close");
		albumReq.send(params);
	}
}

function getAlbumsStateChanged() {
	document.getElementById('photosContainer').style.height = '600px';
	if(albumReq.readyState == 4) {
		document.getElementById('photosContainer').innerHTML = '';
		var xmldoc = albumReq.responseXML;
		var album_nodes = xmldoc.getElementsByTagName("album"); 
		var n_albums = album_nodes.length;
		var i = 0;
		for (i = 0; i < n_albums; i++) {
			var name_node = album_nodes[i].getElementsByTagName("name");
			var mainimage_node = album_nodes[i].getElementsByTagName("mainimage");
			var itemcount_node = album_nodes[i].getElementsByTagName("itemcount");
			var type_node = album_nodes[i].getElementsByTagName("type");
			var albumID = album_nodes[i].getAttribute("id");
			var addText = '';
			if(mainimage_node[0].firstChild.nodeValue == "0") {
				var displayMainImage = "Empty album";
			} else {
				var displayMainImage = "<img src=\"../"+mainimage_node[0].firstChild.nodeValue+"\" />";
			}
			addText += "<div class=\"albumContainer\"><a href=\"/mediafs/mediagallery/albums/"+albumID+"/\">"+displayMainImage+"</a><br /><a href=\"/mediafs/mediagallery/albums/"+albumID+"/\">"+name_node[0].firstChild.nodeValue+"</a><br />"+itemcount_node[0].firstChild.nodeValue+" "+type_node[0].firstChild.nodeValue+"</div>";
			document.getElementById('photosContainer').innerHTML += addText;
		}
		document.getElementById('photosContainer').style.height = 'auto';
	} else {
		document.getElementById('photosContainer').style.height = '600px';
		document.getElementById('photosContainer').innerHTML = '<img src="/images/ajax-loader.gif" />';
	}
}

function getAlbumName() {
	if(albumReq.readyState == 4 || albumReq.readyState == 0) {
		var url = '/mediafs/mediagallery/getAlbums.php';
		var params = 'action=2';
		params += '&albumID='+currentAlbum;
		albumReq.open("POST",url,false);
		albumReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		albumReq.setRequestHeader("Content-length", params.length);
		albumReq.setRequestHeader("Connection", "close");
		albumReq.send(params);
		var xmldoc = albumReq.responseXML;
		var pages_nodes = xmldoc.getElementsByTagName("name"); 
		currentAlbumName = pages_nodes[0].firstChild.nodeValue;
	}
}

function changeAlbum(albumID) {
	currentAlbum = albumID;
	curPaginate = 1;
	getTotalPages();
	if(albumID != '0') {
		getAlbumName();
		document.getElementById('albumTitle').innerHTML = currentAlbumName;
		setPagination('getItems');
		getItems();
		document.getElementById('subNav').innerHTML = '<a href="/mediafs/mediagallery/index.php#mediaTop">Back to all albums</a>';
	} else {
		document.getElementById('albumTitle').innerHTML = 'All albums';
		document.getElementById('subNav').innerHTML = '';
		setPagination('getAlbums');
		getAlbums();
	}
}