/**
 * White Pixels Image Control
 * Slide show and image navigation controller.
 * Only one instance per page should be used as the main container for the slideshow
 * is intended to be the bulk of the page.
 * Use the ID's defined in the css section to set up the page
 * Loosely based on miniSlides by Chris Heilmann
 * @author bdgeorge
 */
imgctrl = {
	//CSS Hooks
	navID : 'navlist',
	targetID : 'photo',
	currentImg : 'current',
	navBack:'leftarrow',
	navFwd:'rightarrow',
	hidingClass:'hide',
	capTarget:'captiontarget',
	//Set up some parameters to control the show
	altText : ' large view', //Text to append to the large image for Alt Text
	imgThumb : '_t', 	//Suffix for thumbnail image
	showInterval :'3000', //duration of slide show
	//Some private objects to preserve
	objMainPhoto:0,//stores the main photo object
	objNav:0,//stores the navigation list
	objCaptionTarget:0,//stores the image caption target
	objCaptionOriginal:0,//stores the original caption text
	//Methods
	init : function(){
		//check for DOM
		if(!document.getElementById||!document.createTextNode){
			return;
		}
		//check to see if we have a list and a target zone to work with
		//initialise the show if we do
		if(document.getElementById(imgctrl.navID)&&document.getElementById(imgctrl.targetID)){
			imgctrl.initShow();
		}
	},
	initShow : function (){
		//remove the link from the first image in the navlist as this is already loaded
		var oNav = document.getElementById(imgctrl.navID);
		oNav = oNav.getElementsByTagName('a');
		imgctrl.objNav=oNav;
		var oTarget = document.getElementById(imgctrl.targetID);
		var photo = oTarget.getElementsByTagName('img');
		imgctrl.objMainPhoto =photo;
		var mainPhoto = imgctrl.fileName(photo[0].src,4);
		imgctrl.objCaptionTarget = document.getElementById(imgctrl.capTarget);
		if ($chk($(imgctrl.capTarget))) {
			imgctrl.objCaptionOriginal = $(imgctrl.capTarget).get('text');			
		}	
		//search the list for the link that points to the main image
		//when we find it change the anchor class to currentImg 
		//at the same time attach an event to each one to trigger a new
		//image load, and attach a request value to let us shortcut to it later
		for (var i=0; i < oNav.length; i++){
			if(imgctrl.fileName(oNav[i].href,4) == mainPhoto){
				//Add the image number to the main photo so we can keep track of where we are
				imgctrl.objMainPhoto.count=i;
			}
			oNav[i].req = i;
			$(oNav[i]).addEvent('click',imgctrl.showPic);
			oNav[i].imgPre = new Image();
			oNav[i].imgPre.src = oNav[i].href;
		}				
		//Now add the event to the forward and back links as well
		//attach request values of "Fwd" and "Back" to these
		//imgctrl.objNavFwd.onclick=WH.safariClickFix;	
		//add mouseover events to the main photo to start and stop the show
		$(imgctrl.objMainPhoto[0]).addEvent('mouseover',imgctrl.stopShow);
		$(imgctrl.objMainPhoto[0]).addEvent('mouseout',imgctrl.runShow);
		//add a click event to the main photo to act as another forward button
		$(imgctrl.objMainPhoto[0]).addEvent('click',imgctrl.nextSlide);
		//Finally set up the links
		imgctrl.setLinks(imgctrl.objMainPhoto.count,0);			
		//Then start the show
		imgctrl.runShow();
	},
	//Loads the selected picture into the main area
	//Changes the class on the navigation list and stores it in the main property for later access
	//Check the count associated with each one, if the count is 0 we are at the first item
	showPic : function (e)
	{
		var t=this;
		var current = imgctrl.objMainPhoto.count;
		if (t.req == "Fwd") {
			//alert(imgctrl.objMainPhoto.count + 1 +" href is: "+imgctrl.objNav[imgctrl.objMainPhoto.count+1].href);
			imgctrl.objMainPhoto[0].src = imgctrl.objNav[++imgctrl.objMainPhoto.count].href;
		}
		else if (t.req == "Back") {
			//alert(imgctrl.objMainPhoto.count - 1 +" href is: "+imgctrl.objNav[imgctrl.objMainPhoto.count-1].href);
			imgctrl.objMainPhoto[0].src = imgctrl.objNav[--imgctrl.objMainPhoto.count].href;
		}
		else {
			imgctrl.objMainPhoto[0].src = imgctrl.objNav[t.req].href;
			imgctrl.objMainPhoto.count = t.req;
		}
		imgctrl.setLinks(imgctrl.objMainPhoto.count,current);	
		e.stop();
	},
	/**
	 * Set the fwd, back links and change the class on the navigation links based on the target number
	 * @param {Object} target
	 * @param {Object} current
	 */
	setLinks:function(target,current){
	
		//Remove the current image class from the last link and add it to the new current one 
		//WH.cssjs('remove', imgctrl.objNav[current], imgctrl.currentImg);
		$(imgctrl.objNav[current]).removeClass(imgctrl.currentImg);
		//WH.cssjs('add', imgctrl.objNav[target], imgctrl.currentImg);
		$(imgctrl.objNav[target]).addClass(imgctrl.currentImg);
		//Find the caption if there is one and overwrite the caption target object (if there is one)
		//otherwise restore the original text
		if(imgctrl.objNav[target].getAttribute('caption') && $chk(imgctrl.objCaptionTarget)){
			$(imgctrl.capTarget).set('text',imgctrl.objNav[target].getAttribute('caption'));
			} else if (imgctrl.objCaptionTarget) {
			$(imgctrl.capTarget).set('text',imgctrl.objCaptionOriginal);			
			}
		imgctrl.runShow();
	},
	/**
	 * trims the file name from the path and file type
	 * returns just the filename
	 * @param {Object} szFile
	 * @param {Object} iTrim
	 */
	fileName:function ( szFile, iTrim ){   
		return szFile.substring(szFile.lastIndexOf("http://whitepixels.com.au/") + 1, szFile.length - iTrim);
	}, 
	/**
	 * Runs the animated slide show
	 */
	runShow:function(){
		//attach a timer to the main photo object
		if(imgctrl.objMainPhoto.timer != null){
			window.clearInterval(imgctrl.objMainPhoto.timer);
			imgctrl.objMainPhoto.timer=null;		
		}
		imgctrl.objMainPhoto.timer = window.setInterval(imgctrl.nextSlide, imgctrl.showInterval)		
	},
	/**
	 * Reads the current slide and increments to the next or loops
	 */
	nextSlide:function(){
		var current = imgctrl.objMainPhoto.count;
		if(imgctrl.objMainPhoto.count<imgctrl.objNav.length-1){
			++imgctrl.objMainPhoto.count;		
		}	else {
			//if there is a next link on the page follow it
			if ($('nextPage')!=null){
				window.location = $('nextPage').getElement('a').href;				
			}
			else if ($('prevPage')!=null){
				window.location = $('prevPage').getElement('a').href;				
			} else {
			//else return to the start of the show
			imgctrl.objMainPhoto.count=0;				
			}
		}	
		imgctrl.objMainPhoto[0].src = imgctrl.objNav[imgctrl.objMainPhoto.count].href;
		imgctrl.setLinks(imgctrl.objMainPhoto.count,current);
	},
	/**
	 * Pauses the slideshow
	 */
	stopShow:function(){
		if (imgctrl.objMainPhoto.timer) {
			window.clearInterval(imgctrl.objMainPhoto.timer);
			imgctrl.objMainPhoto.timer=null;
		}		
	}
}
//WH.addEvent(window,'load',imgctrl.init,false);
window.addEvent('domready',imgctrl.init);