/**
 * Ferenyl's simple gallery 1.0
 * 
 * simple setup. just folow the steps in the readme file
 * 
 * 
 * http://www.madebyjohan.se 
 * 
 * This script is created by Johan "Ferenyl".
 * 
 * 
 */

var FSG = {
	//a function that starts when the page is fully loaded.
	init: function(){
		//gets the div node for the element that contains the images.
		var div = document.getElementById("fsgimages");
		//gets all links from that node
		var links = div.getElementsByTagName("a");
		//sets an id on the links if next and prev buttons is used
		FSG.setId(links);
		//a for  loop that sets an onclick event on the links
		for (var i=0; i< links.length; i++) {
				//the event function
				links[i].onclick = function(){
					//takes the source from the link
					var source = this.href;
					//send the source to a function that puts the
					//source in the placeholder
					FSG.setSource(source);
					//takes the title in the links
					var title = this.title;
					//sends the title to a functions that writes the
					//title in the correct node
					FSG.setTitle(title);
					//checks if next and prev buttons exists. if those exist
					//a function for those burttons starts.
					if (document.getElementById("fsgnext") != null && document.getElementById("fsgprev") != null) {
						FSG.nextPrev(this.id, links);
					}
					//return false so that the links don't sends the user to
					//the page for the image
					return false;
		};
		}
	},
	//function to set source in the placeholder
	setSource: function(source){
		//gets the node for the placeholder
		var holder = document.getElementById("fsgplaceholder");
		//set the source
		//holder.setAttribute("src", source); doesent work in ie.
		holder.src = source;
		return false;
	},
	
	//writes the title to the div
	setTitle: function(title){
		//get the node for the title
		var titleNod = document.getElementById("fsgtitle");
		//if title node is not null (does exist)
		//write the title
		if (titleNod != null){
			titleNod.innerHTML= "\n<p>\n" + title + "\n</p>\n";
			
		}
	},
	//sets id's for the links
	setId: function(links){
		for (var i=0; i< links.length; i++) {
			links[i].setAttribute("id", "I" + i);
		};
	},
	
	// a function for the next and prev buttons
	nextPrev: function(id, links){
		
		//gets the next button
		var next = document.getElementById("fsgnext");
		//gets the prev button
		var prev = document.getElementById("fsgprev");
		//slice the id so that only the number remains
		//that number represents the corresponding index for the link
		//in the array 
		var number = id.slice(1, 5);
		//parse to int just to be sure
		number = parseInt(number);
		//get the number of links
		var total = links.length - 1;
		
		//onclick event for the next button
		next.onclick = function(){
			//if the id is lower than total links
			if (number < total) {
				//get the next image
				var thisImage = links[number + 1];
				//send that to the function that puts the source
				//in placeholder
				FSG.setSource(thisImage);
				//sends the title to the function
				FSG.setTitle(thisImage.title)
				//adds 1 to the number.
				number += 1;
			}
			//if not lower than total
			else {
				//sets the first link in the array
				var thisImage = links[0];
				//sends source to the function
				FSG.setSource(thisImage);
				//sends the title to the function
				FSG.setTitle(links[0].title)
				//sets number to 0
				number = 0;
			}
		}
		//event for prev button
		prev.onclick = function(){
			//if the number is bigger than 0
			if (number > 0) {
				//get the prev image
				var thisImage = links[number - 1];
				//send to the right functions
				FSG.setSource(thisImage);
				FSG.setTitle(thisImage.title)
				//removes 1 from the number
				number -= 1;
			}
			//if not bigger than 0
			else {
				//get the last link in the array
				var thisImage = links[total];
				//send that to the right functions
				FSG.setSource(thisImage);
				FSG.setTitle(thisImage.title);
				//sets number to the number for the last link
				number = total;
			}
		}
	}
}
//starts init when finished loading.
window.onload = FSG.init;

