/**
 * This is actually a minimized version of EganuGalleryNavigator. No pages and smaller images
 * 
 * Expected format of items:
 * 0->image path (0708/123120ddfa) - no .jpg in the end
 * 1->title
 * 2->ratio
 * 3->user_id 4->image_pos  - both for creating the link
 */
 
function HomepageGalleryNavigator(galleryPath,containerId)
{
	this.items = null;
	this.galleryPath = galleryPath;
	this.imageContainer = document.getElementById(containerId);
	this.currentImage = 0;
	
	this.SetImage = function(imageNum)
	{
		if (imageNum < 0)
		{
			this.currentImage = this.items.length-1;
		}
		else if (imageNum >= this.items.length)
		{
			this.currentImage = 0;
		}
		else this.currentImage = imageNum;
		
		var width,height;
		var src = this.galleryPath + this.items[this.currentImage][0] + '.jpg';
		var currentImage = this.items[this.currentImage];
		// currentImage[2] is the ratio
		if (currentImage[2] > 1)
		{
			width = 200; height = 134;
		}
		else if (currentImage[2] < 1)
		{
			width = 134; height = 200;
		}
		else
		{
			width = height = 200;
		}
		var link = "/people/card.php?id="+currentImage[3] + "&mode=gallery&pic="+currentImage[4];
		// alert(currentImage[0] + ' ' + currentImage[1] + ' ' + currentImage[3]);
		
		this.imageContainer.innerHTML = 
			"<a href='"+link+"'><img class='imggray' width='"+width+
			"' height='"+height+"' src=\""+src+'" title="' +
			currentImage[1] +'" />';
	}
	this.NextImage = function() { this.SetImage(this.currentImage+1); }
	this.PrevImage = function() { this.SetImage(this.currentImage-1); }
}

/**
 * Object for navigating the eganu galleries
 * 
 * @param {string} galleryId
 * @param {string} galleryPath Where the images are stored - I.E http://www.test.com/gallery/
 * @param {string} varName Kind of a looped logic - this is the name of the object variable which
 * will be created. Used in the links of the page navigator
 */
function EganuGalleryNavigator(galleryId,galleryPath,varName)
{
	var imgList = document.getElementById('imageList');
	
	// properties
	this.varName        = varName; // for dynamically updated page nav links
	this.items          = null; // it's inited outside of here
	this.galleryId      = galleryId;
	this.galleryPath    = galleryPath;
	this.currentImage   = 0;
	this.currentPage    = -1; // so call to setimage(0) at start will init the page
	this.imgShortcuts   = imgList.getElementsByTagName('IMG');
	this.pageNavigator  = document.getElementById('pageNavigator');
	this.mainImageLink  = document.getElementById('mainImageLink');
	this.numPerPage     = this.imgShortcuts.length;	

	this.pageSpan = 3;	// how many pages left and right will there be in the navigator

	// method
	/** Called on init */	
	this.SetItems = function(items,selected)
	{
		this.items = items;
		this.totalPages = Math.ceil(this.items.length/this.numPerPage);
		if (typeof (selected) == 'undefined' || selected == null)	this.SetImage(0);
		else
		{
			var id;
			for (var i=0;i<this.items.length;i++)
			{
				id = this.items[i][0].split('/')[1];
				if (id == selected)
				{
					this.SetImage(i);
					break;
				}
			}
		}
	}
	this.SetPage = function(pageNum)
	{
		// bounds checking
		if (pageNum < 0 || pageNum > (this.totalPages-1)) return;
		this.currentPage = pageNum;
//		this.pageInfo.innerHTML = "Page " + (pageNum+1) + " out of " + this.totalPages;
		// update image shortcut cells cells
		var iStart,iEnd;
		iStart = pageNum * this.numPerPage;
		iEnd = iStart + this.numPerPage;
		var tempHTML = null; // for constructing cell info
		var j=0; // used for traversing the imgShortcuts (they're only 0 to 7)
		for (var i=iStart;i<iEnd;i++)
		{
			if (!this.items[i]) this.imgShortcuts[j].style.display = 'none';
			else
			{
				this.imgShortcuts[j].style.display = 'inline';
				this.imgShortcuts[j].src = this.galleryPath+this.items[i][0]+'_s.jpg';
			}
			j++;
		}

		// updating page navigator links
		if (this.totalPages == 1) // show nothing if there's only 1 page
		{
			this.pageNavigator.innerHTML = '';
			return;
		}
		var navigatorHTML = '', potentialPage = 0,iStart,iEnd;
		if (this.currentPage <= this.pageSpan)
		{
			iStart = 0;
			iEnd = this.pageSpan*2;
		}
		else if (this.currentPage > (this.totalPages-1)-this.pageSpan)
		{
			iStart = this.totalPages - this.pageSpan*2;
			iEnd = this.totalPages;
		}
		else
		{
			iStart=this.currentPage-this.pageSpan;
			iEnd=this.currentPage+this.pageSpan;
		}
		// out of bounds fix
		if (iEnd > (this.totalPages-1)) iEnd = (this.totalPages-1);
		if (iStart < 0) iStart = 0;
	
		if (iStart > 0)
			navigatorHTML += "<a class='member' href='javascript:"+this.varName+".SetPage(0)'>-1-</a> ... ";

		for (var i=iStart; i<=iEnd;i++)
		{
			if (i != this.currentPage)
			{
				navigatorHTML +=  " <a class='member' href='javascript:" + this.varName + '.SetPage('
							+ i + ")'>-" + (i+1) + "-</a> ";
			}
			else navigatorHTML += " -"+(i+1)+"- ";
		}

		if (iEnd < (this.totalPages-1))
		{
			navigatorHTML += " ... <a class='member' href='javascript:"+this.varName+
			".SetPage("+(this.totalPages-1)+")'>-"+this.totalPages+"-</a>";
		}

		this.pageNavigator.innerHTML =  navigatorHTML;		
				
	}
	this.NextPage = function() { this.SetPage(this.currentPage+1); }
	this.PrevPage = function() { this.SetPage(this.currentPage-1); }

	this.SetImage = function(imageNum)
	{
		if (imageNum < 0)
		{
			this.currentImage = this.items.length-1;
		}
		else if (imageNum >= this.items.length)
		{
			this.currentImage = 0;
		}
		else this.currentImage = imageNum;

		// update the global image_id variable - used when sending link to a friend
		image_id = this.galleryId + '_' + this.GetSelectedValue();
		var width,height;
		var src = this.galleryPath+this.items[this.currentImage][0];
		var ratio = this.items[this.currentImage][2];
		if (ratio > 1)
		{
			width = 320; height = 214;
		}
		else if (ratio < 1)
		{
			width = 214; height = 320;
		}
		else
		{
			width = height = 320;
		}
		this.mainImageLink.innerHTML = "<img name='mainImage' border='0' class='imggray'" +
										" src = '" + src +'.jpg' +
										"' width='"+width+"' height='"+height+"' alt='Eganu Party Image' />";

		// update the link
		var start = src.lastIndexOf('/')+1;
		this.mainImageLink.href = 'viewimage.php?id=' + this.galleryId + '_' + src.substr(start);
		// Check for possible update of the shortcut bar
		// I check for every image because the option to go from last image to first
		if (parseInt(this.currentImage/this.numPerPage) != this.currentPage)
		{
			this.SetPage(parseInt(this.currentImage/this.numPerPage));
		}
	}
	// shortcuts for moving back and forth between images.
	this.PrevImage = function() { this.SetImage(this.currentImage-1); }
	this.NextImage = function() { this.SetImage(this.currentImage+1); }

	this.GetSelectedValue = function ()
	{
		try
		{
			var value = this.items[this.currentImage][0];
			var start = value.lastIndexOf('/');
			return value.substr(start+1);
		}
		catch (e)
		{
			alert(e.message);
		}
	}
}