/*
VJEX Photo Album Copyright (C) 2006 Terrence Tay
Version 1.0
Last Modified: 1/20/2006

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Terrence Tay
    varatare@gmail.com
    http://www.varatare.co.nr
*/

var mainpicid = 'vjexphotoarea';
var thumbnailid = 'vjexthumbnailarea';
var commentid = 'vjexcommentarea';
var titleid = 'vjextitlearea';
var thumbnailrows=1;
var thumbnailcols=5;
var displayfirstpic = true;

var xmlhttp;
var imgnum=0;
var albums = null;

function Album() 
{
	this.imgsrc = '';
	this.thbsrc = '';
	this.comment = '';
	this.title = '';

	this.setImgSrc= function setImgSrc(data){ this.imgsrc = data; }
	this.setThbSrc= function setThbSrc(data){ this.thbsrc = data; }
	this.setComment= function setComment(data){ this.comment = data; }
	this.setTitle= function setTitle(data){ this.title = data; }

	this.getImgSrc= function getImgSrc(){ return this.imgsrc; }
	this.getThbSrc= function getThbSrc(){ return this.thbsrc; }
	this.getComment= function getComment(){ return this.comment; }
	this.getTitle= function getTitle(){ return this.title; }
} 

function loadXMLDoc(xmlfile)
{
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
  		xmlhttp.onreadystatechange=state_Change;
  		xmlhttp.open("GET",xmlfile,true);
  		xmlhttp.send(null);
  	}
	else
		if (window.ActiveXObject)
		  	{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			if (xmlhttp)
    				{
				xmlhttp.onreadystatechange=state_Change;
				xmlhttp.open("GET",xmlfile,true);
				xmlhttp.send();
				}
			}
}

function state_Change()
{
	if (xmlhttp.readyState==4)
	{
  		if (xmlhttp.status==200)
  		{
			var nodes = xmlhttp.responseXML.getElementsByTagName('image');
			albums = new Array();
			for(i=0; i < nodes.length; i++)
			{
				var album = new Album();
				commentindex = -1;
				thumbnailindex = -1;
				titleindex = -1;
				childnode = nodes[i].childNodes;
				for(j=0; j < childnode.length; j++)
				{
					var tag = childnode[j].nodeName;
					if(tag.toLowerCase() == 'src')
					{
						album.setImgSrc(childnode[j].firstChild.nodeValue);
					}
					if(tag.toLowerCase() == 'thumbnail')
					{
						thumbnailindex = j;
					}
					if(tag.toLowerCase() == 'comment')
					{
						commentindex = j;
					}
					if(tag.toLowerCase() == 'title')
					{
						titleindex = j;
					}
				}
				if(thumbnailindex >= 0)
				{
					album.setThbSrc(childnode[thumbnailindex].firstChild.nodeValue);
				}
				if(titleindex >= 0)
				{
					album.setTitle(childnode[titleindex].firstChild.nodeValue);
				}
				if(commentindex >= 0)
				{
					album.setComment(childnode[commentindex].firstChild.nodeValue);
				}
				albums.push(album);
			}

			if(document.getElementById(thumbnailid) != null)
			{
				displayThumbnail();
			}
			if(displayfirstpic)
			{
				displayMain('0');
			}
  		}
  		else
  		{
  			alert("Problem retrieving XML data:" + xmlhttp.statusText);
  		}
  	}
}

function displayThumbnail()
{
	var thumbnailcode = '<table cellpadding="0" cellspacing="4" border="0">';
	var totaldisplay = thumbnailrows * thumbnailcols;
	var erow = 0;
	var ecol = 0;
	var i = imgnum;
	while(erow < thumbnailrows)
	{
		thumbnailcode += '<tr>';
		ecol = 0;
		while(ecol < thumbnailcols)
		{
			if(i < albums.length)
			{
				thumbnailcode += '<td valign="bottom" class="vjexthumbnail">';
				if(albums[i].getThbSrc() != '')
				{
					thumbnailcode += '<a href="javascript:displayMain(\'' + i + '\');"><img src="' + albums[i].getThbSrc()+ '" " border="1" class="vjexthbimage" alt="' + albums[i].getTitle() +'"></a>';
				}
				else if(albums[i].getTitle() != '')
				{
					thumbnailcode += '<a href="javascript:displayMain(\'' + i + '\');" title="' + albums[i].getTitle() + '">' + albums[i].getTitle() + '</a>';
				}
				thumbnailcode += '</td>';
				i++;
			}
			else
			{
				thumbnailcode += '<td class="vjexthumbnail">';
				thumbnailcode += '</td>';
			}
			ecol++;
		}
		thumbnailcode += '</tr>';
		erow++;
	}
	thumbnailcode += '</table>';
	if(totaldisplay != albums.length)
	{
		thumbnailcode += '<p class="pagenum">Sayfa ';
		var pages = 1;
		for(j=0; j < albums.length; j += totaldisplay)
		{
			thumbnailcode += '<a href="javascript:changePage(' + j + ');">' + pages + '</a> ';
			pages++;
		}
	}
	document.getElementById(thumbnailid).innerHTML = thumbnailcode;
}

function changePage(pagenum)
{
	imgnum = pagenum;
	if(document.getElementById(thumbnailid) != null)
	{
		displayThumbnail();
	}
}

function displayMain(index)
{
	if(document.getElementById(mainpicid) != null)
	{
		document.getElementById(mainpicid).innerHTML = '<img src="' + albums[index].getImgSrc() + '" border="1" class="vjexmainimage" alt="' + albums[index].getTitle() +'">';
	}
	else
	{
		alert("Photo Display Area is required.");
	}
	if(document.getElementById(commentid) != null)
	{
		document.getElementById(commentid).innerHTML = '<p class="vjexcomment">' + albums[index].getComment() + '</p>';
	}
	if(document.getElementById(titleid) != null)
	{
		document.getElementById(titleid).innerHTML = '<p class="vjextitle">' + albums[index].getTitle() + '</p>';
	}
}