/****************************************************************
 * Gallery functions
 ****************************************************************
 */

var iPic;
var aPictures;
var currentPic;

function initGallery() {

	aPictures = getElementsByClassName('gallery-item');
	iPic = 0;

	// do nothing if there is only one image
	if (aPictures.length < 2)
		return;

	// take off styles on gallery-item if javascript enabled
	for(i=0; i <= aPictures.length; i++) {
		classDel(aPictures[i], 'noJS');
	}

	classAdd(document.getElementById('gNav'), 'enabled');

	// Previous link
	var previous = document.createElement('li');
	previous.id = 'previous';
	//previous.setAttribute('onclick', 'previous()');
	//previous.setAttribute('href', '');
	previous.innerHTML = '<a onclick="previous(); return false;" href="#">Previous</a>';

	// Next link
	var next = document.createElement('li');
	next.id = 'next';
	//next.setAttribute('onclick', 'next(); return false;');
	//next.setAttribute('href', '');
	next.innerHTML = '<a onclick="next(); return false;" href="#">Next</a>';

	// UL to hold these links
	var ulNav = document.createElement('ul');
	ulNav.id = 'prevnext';
	ulNav.className = 'prevnext';
	document.getElementById('gNav').appendChild(ulNav);

	document.getElementById('prevnext').appendChild(previous);
	document.getElementById('prevnext').appendChild(next);

	// Render image pagination
	galleryPagination();
	updateGallery();
}

function next() {
	iPic++;
	if(iPic == aPictures.length) {
		iPic = 0;
	}
	updateGallery();
}

function previous() {
	iPic--;
	if(iPic < 0) {
		iPic = aPictures.length - 1;
	}
	updateGallery();

}

// hide all pics apart from the one to show
function updateGallery() {

	for (i=0; i < aPictures.length; i++) {

		// for pagination
		var pic = document.getElementById('pic' + (i+1));

		if(i == iPic) {
			// Current picture
			classDel(aPictures[i], 'hide');
			classAdd(pic, 'current');
		} else {
			classAdd(aPictures[i], 'hide');
			classDel(pic, 'current');
		}

	}

}

function galleryPagination() {

	// create pagination <ul> list
	var title = document.createElement('p');
	title.innerHTML = 'Image:';
	document.getElementById('gNav').appendChild(title);

	var pager = document.createElement('ul');
	pager.id = 'pager';
	document.getElementById('gNav').appendChild(pager);

	var li; // <li>

	for(i=0; i < aPictures.length; i++) {
		li = document.createElement('li');
		document.getElementById('pager').appendChild(li);
		li.id = 'pic' + (i+1);
		li.innerHTML = '<a href="#" onclick="showPic(' + i + '); return false;">' + (i+1) + '</a>';
	}
}

function showPic(iShow) {
	iPic = iShow;
	//var currentPic = document.getElementById('pic' + (iShow+1));
	//classAdd(currentPic, 'current');
	updateGallery();
}



/****************************************************************
 * Class-handling helper functions
 ****************************************************************
 */
var aCachedREs = new Array();
function classCacheRE(sClass) {
	if(!aCachedREs[ sClass ])
		aCachedREs[ sClass ] = new RegExp('^\\s*(.*)\\b('+ sClass +')\\b(.*)\\s*$');
	return( aCachedREs[ sClass ] );
}

function classMatch(oObject, sClass) {
	if(oObject && oObject.className)
		return(classCacheRE(sClass).exec(oObject.className));
	return(false);
}

function classAdd(oObject, sClass) {
	if(oObject && !classMatch(oObject, sClass))
		oObject.className += ' '+ sClass;
}

function classDel(oObject, sClass) {
	var aMatches = classMatch(oObject, sClass);
	if(aMatches)
		oObject.className = aMatches[1] + aMatches[ aMatches.length - 1 ];
}

/****************************************************************
 * Array Prototypes
 ****************************************************************
 */
// Add the ability to push something onto the end of an array
if(!Array.prototype.push) {
	Array.prototype.push = function( mNewEntry ) {
		this[ this.length++ ] = mNewEntry;
	}
}

// Add the ability to pop something off the end of an array
if(!Array.prototype.pop) {
	Array.prototype.pop = function() {
		var mReturnValue = false;
		if(this.length) {
			mReturnValue = this[ this.length - 1 ];
			this.length--;
		}
		return(mReturnValue);
	}
}

if(!Array.prototype.search) {
	Array.prototype.search = function(sTerm) {
		var iIndex = this.length;
		while(iIndex--) {
			if(this[ iIndex ] == sTerm) return(true);
		}
		return(false);
	}
}

/****************************************************************
 * getElementsByClassName
 ****************************************************************
 */
// Moz or IE.6
if(!navigator.userAgent.match(/MSIE/) ||
	navigator.userAgent.match(/MSIE 6\./)) {

	function getElementsByClassName(sClassName, oObject, bNoRecursion) {
		var aoReturnValue = new Array();

		// Default the object to the document
		if(!oObject) {
			oObject = document.body;
		}

		// Recursion: get all the elements and run through them
		if(!bNoRecursion) {
			var aoElements = oObject.getElementsByTagName('*');
			for(var iElement = 0; iElement < aoElements.length; iElement++) {
				if(classMatch(aoElements[ iElement ], sClassName)) {
					aoReturnValue.push( aoElements[ iElement ] );
				}
			}

		// No recursion: Just run run through all the children
		} else {
			for(var oChild = oObject.firstChild; oChild; oChild = oChild.nextSibling) {
				if(classMatch(oChild, sClassName)) {
					aoReturnValue.push(oChild);
				}
			}
		}

		return(aoReturnValue);
	}

// IE 5.x
} else {
	function getElementsByClassName(sClassName, oObject, bNoRecursion) {
		var aoReturnValue = new Array();

		// Object supplied
		if(oObject) {
			for(var oChild = oObject.firstChild; oChild; oChild = oChild.nextSibling) {
				if(classMatch(oChild, sClassName)) {
					aoReturnValue.push(oChild);
				}

				// Recurse if possible / allowed
				if(oChild.hasChildNodes() && !bNoRecursion) {
					aoReturnValue = aoReturnValue.concat( getElementsByClassName(sClassName, oChild) );
				}
			}

		// No object and *no* recursion: Just run run through all the children
		} else if(bNoRecursion) {
			for(var oChild = document.body.firstChild; oChild; oChild = oChild.nextSibling) {
				if(classMatch(oChild, sClassName)) {
					aoReturnValue.push(oChild);
				}
			}

		// No object and recursion: get all the elements and run through them
		} else {
			for(var iElement = 0; iElement < document.all.length; iElement++) {
				if(classMatch(document.all[ iElement ], sClassName)) {
					aoReturnValue.push(document.all[ iElement ]);
				}
			}
		}

		return(aoReturnValue);
	}
}

var fOnLoad = window.onload;

window.onload = function() {
	if (fOnLoad)
		fOnLoad();

	initGallery();
}
