/* 	Author: Abdul Rehman Khawar (ark)
	coded while learning Javasript
	made to work with mozilla firefox
	designbysoil@gmail.com
	
*/



//alert("display");

/*
	startViewer
	------------------------------------------------------------------------
	takes the anchors inside the object 'image_gallery' and makes an array
	then passes the href of the image clicked to the function displayImage()

*/

function startViewer(){
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById) return false;
	if(!document.getElementById("image_gallery")) return false;
	
	// get image gallery obj
	var viewer = document.getElementById("image_gallery");
	
	// get an array of all the anchor tags inside of the image gallery obj	
	var links = viewer.getElementsByTagName("a");
	
	
	for(var i=0; i<links.length; i++){
		links[i].onclick  = function(){
			return displayImage(this);	
		}
	}
		
}


/*
	displayImage
	------------------------------------------------------------------------
	takes the href from startViewer function and changes the placeholder src

*/


function displayImage(p_image){
	
	//alert(p_image);
	if(!document.getElementById) return true;
	document.getElementById('placeholder').src = p_image.href;
	return false;	
}

/*
	loadControls
	------------------------------------------------------------------------
	takes the anchors inside the object 'image_gallery' and makes an array of
	all the images
	
	then based on the button clicked the placeholder.src is changed to the next
	index inside the image_gallery

*/

function loadControls(){
	var all_images = document.getElementById("image_gallery");
	var alinks = all_images.getElementsByTagName("a");
	
	//alert(alinks[0]);
	
	var button_next = document.getElementById('next');
	var button_prev = document.getElementById('prev');
	
	// when button next is clicked
	
	button_next.onclick = function(){
		var current_image = document.getElementById('placeholder');
		//alert(current_image.src);
		
		var next_image = 0;
			var maximum = alinks.length - 1;
		
		// incase the maximum number of images is reached
		// reset the placeholder with the first image
			
		if(alinks[maximum] == current_image.src){
						//alert(alinks[0]);
						document.getElementById('placeholder').src = alinks[0];
						return false;
		}
		
		// changes the image to the next image in image gallery array
		
		for(var i=0; i<alinks.length; i++){
			if(current_image.src == alinks[i]){
								
					next_image = alinks[i+1];				
					//alert(next_image);
					document.getElementById('placeholder').src = next_image;
					return false;
			}
		}
		
	}
	
	// when button prev is clicked
	
	button_prev.onclick = function(){
		var current_image = document.getElementById('placeholder');
		//alert(current_image.src);
		var next_image = 0;
		var minimum = 0;
		var maximum = alinks.length - 1;
		
		// incase the minimum number of images is reached
		// reset the placeholder with the last image
		
		if(alinks[minimum] == current_image.src){
						//alert(alinks[0]);
						document.getElementById('placeholder').src = alinks[maximum];
						return false;
		}

			
		// changes the image to the prev image in image gallery array
		
		for(var i=0; i<alinks.length; i++){		
			//alert(alinks.length);
			if(current_image.src == alinks[i]){
					next_image = alinks[i-1];
					//alert(next_image);
					document.getElementById('placeholder').src = next_image;
					return false;
			}				
		}
		return false;
	}
			
	
	
		
}


// when page loads automatic calls to the functions is made

window.onload= function(){
	startViewer();
	loadControls();
}
