/* --------------------------------------------------------------------------------------------
	Natalie Downe
	natalie@natbat.co.uk

	Version 1.0 (18/11/2006)
	(based on Permalink Slideshow Version 0.20 (27/02/2006))

	TODO :
		- make this object oriented to avoid namespace collisions!
		- if you change the url then use the forward/back buttons the url doesnt change, this
		bug is also in the origial version (FF) and could be to do with the URL casheing
		- make more componantised, keep namespace and make customisable
-------------------------------------------------------------------------------------------- */

var u_fadeSlide = 0.5; // the time in which to fade the slide - if you set it to 0 the fade will be instantanious
var u_importedCSSfile = '../files/styles.css'; // path from the root to the css file to import

var u_slideName = 'park' // this is the initial id of your slide, without the number, the url may look like ... http://yourSite.com#slideName1
var u_slideTitlePrefix = ''; // this appears on the title bar
var u_slideContainerID = 'parkContainer'; // the containing div for all your slides

/* optional */
var u_nextLink = 'next'; // id of nextlink link - comma separated id list if there is more than one
var u_previousLink = 'previous'; // id of previouslink link - comma separated id list if there is more than one
var u_navList = 'pagenumbers'; // id of list for paging elements - comma separated id list if there is more than one
var u_pages = 'pages'; // id of page one of one paragraph - comma separated id list if there is more than one

/* --------------------------------------------------------------------------------------------
	Customise variables above, you probably shouldnt need to change anything below
	- if you are using this on a site that already has one similar then rename the functions
		to avoud namespace issues
   --------------------------------------------------------------------------------------------

   PLEASE NOTE: if you want your slides to fade set u_fadeSlide to a fraction of time in seconds for the transition, 0.3 works nicely. however if you *do* want fading slides you need to be sure of the height of your slide and that it will remain constant.


-------------------------------------------------------------------------------------------- */

// uses yahoo dom, event and animation
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $ = $D.get; // get element by ID

var gCurrentSlide = 0; // index to gSlides[] the actual slide is 'gCurrentSlide+1' any passing of href must minus 1
var gSlides = [];
var gInitialDocTitle = "";
var URLchecker = null; // global variable so we can clear and reset interval
var gChangeInProcess = false; // to stop unexpected behaviour if fading is interupted

// change from one slide to another
function fadeImage(requested) {
	requested = requested - 1;
	
	if((requested != gCurrentSlide) && (requested < gSlides.length) && (requested >= 0) && (!gChangeInProcess)) {
		gChangeInProcess = true;
		
		// using global varible from url checker
		clearInterval(URLchecker);

		// set all non requested slides to not display except for current slide
		var k = 0;
		for(k; k<gSlides.length; k++) {
			if(k != gCurrentSlide) {
				gSlides[k].style.display = 'none';
				gSlides[k].style.visibility = 'hidden';
				gSlides[k].style.zIndex = '3';
			} else {
				gSlides[k].style.display = 'block';
				gSlides[k].style.visibility = 'visible';
				gSlides[k].style.zIndex = '5';
			}
		}
		k = 0;

		// display requested slide
		var requestedSlide  = $('slideshowSlide' + (requested + 1));
		if(u_fadeSlide <= 0) {
			gSlides[gCurrentSlide].style.display = 'none';
			gSlides[gCurrentSlide].style.visibility = 'hidden';
			gSlides[gCurrentSlide].style.zIndex = '3';
			requestedSlide.style.display = 'block';
			requestedSlide.style.visibility = 'visible';
			// fadeFrom must have a higher zindex than fadeTo (need to reset only for safari)
			requestedSlide.style.zIndex = '4';
			insEndFade(requestedSlide);

		} else {
			// TODO this might need debugging
			requestedSlide.style.display = 'block';
			requestedSlide.style.visibility = 'visible';
			// fadeFrom must have a higher zindex than fadeTo (need to reset only for safari)
			requestedSlide.style.zIndex = '4';

			// fade the top slide onto the bottom
			var anim = new YAHOO.util.Anim(gSlides[gCurrentSlide], { opacity: { from: 1, to: 0 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
			anim.onComplete.subscribe(function() { insEndFade(requestedSlide) });
			anim.animate();
			anim = null;
		}
	}
}

function insEndFade(slideTo) {

	// have numbers but we need nodes
	var slideFrom = gSlides[gCurrentSlide];

	// the fade has finished so reset visiblity, display and opacity
	// zindex need to reset only for safari (might fix it)
	slideFrom.style.zIndex = '0';
	slideTo.style.zIndex = '5';
	slideFrom.style.visibility = 'hidden';
	slideFrom.style.display = 'none';
	YAHOO.util.Dom.setStyle(slideFrom, 'opacity', 1);

	// reset current slide
	gCurrentSlide = parseInt(slideTo.id.replace('slideshowSlide', ''), 10) - 1;
	// officially end the fade
	gChangeInProcess = false;
}

function insSetupPage() {
	if($(u_slideContainerID)) {
		// initial document title
		gInitialDocTitle = document.title;

		// set up array of slide divs and rename id
		var tempSlides = $(u_slideContainerID).getElementsByTagName('div');

		if(tempSlides.length > 0) {
			var tempSlideNo;
			var largestHeight;
			var i = 0;
			for(i;i<tempSlides.length; i++) {
				if(tempSlides[i].className.indexOf(u_slideName) != -1) {

					// what slide is this
					tempSlideNo = parseInt(tempSlides[i].id.replace(u_slideName, ''), 10);

					// re-set slide id to avoid jumping but to maintain graceful degredation
					// URL will still maintain old ID of #u_slideNameXX
					tempSlides[i].id = 'slideshowSlide' + tempSlideNo;

					// add to array
					gSlides[gSlides.length] = tempSlides[i];

				}
			}

			// reseting temporary variables
			tempSlides = '';
			tempSlideNo = '';

			// display correct slide to start with
			if(gCurrentSlide == 0) {
				//gSlides[0].style.display = 'none';
				gSlides[gCurrentSlide].style.display = 'block';
			}
		}
	}
}


// on load
$E.addListener(window,'load',insSetupPage);


