/* --------------------------------------------------------------------------------------------
	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
-------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------
	New user variables by
	Scott M. Sanders
	scott@a-1pringinginc.com
	
	Version 1.0b (July 1, 2008) *fixed
	
	- u_auto: auto-changes slide after five seconds
	- u_pause: pauses on click; restarts on additional click
	- u_menu: ends slideshow after one cycle, then performs function insFadeOut()
		- u_menuFadeOutID: secondary element to fade out if specified
		- u_menuURL: "menu" page url to visit when done
		- u_mainMenu: enables nav span "mainmenu" (if u_menu = 1)
	- u_fadeEnds: fades into slideshow and (if u_menu = 1) fades out, too
	* u_waitTime: time in seconds to wait until slideshow is displayed
	- u_titles: slide div titles outputted in HTML and titlebar ("photo x of xx" dropped for space)
	- u_pageNumberTitles: adds link titles to those page numbers (enhanced if u_titles = 1)
	- u_preload: disable if you don't wanna wait for all slides to load
	- u_loading: if you wanna use a "loading" div (you know, for dial-up users)
		- put "loading" div before slideshow container div in HTML on slideshow page
	- u_iframe: toggles iframe support
	
	- support for lowercase-insensitive permalinks
	
	on "menu" iframe:
	- u_slideName = use same as below
	- u_slideshowURL = url of slideshow page (for URL hash listening)
	- u_fadeEnds: fades into menu
	- u_loading: if you wanna use a "loading…" div
	- u_menuID = "menu" element to fade in if specified
-------------------------------------------------------------------------------------------- */

var u_importedCSSfile = 'slideshow.css'; // path from the root to the css file to import

var u_slideName = 'Photo' // this is the initial id of your slide, without the number, the url may look like ... http://yourSite.com#slideName1
var u_slideTitlePrefix = 'Photo'; // this appears on the title bar
var u_slideContainerID = 'photoContainer'; // 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.

	Your document structure will need to look like:

	<div id="u_slideContainerID">
		<div id="u_slideName1" class="u_slideName">
			<!-- some sort of slide, an image or text or whatever -->
		</div>
		<div id="u_slideName2" class="u_slideName">
			<!-- some sort of slide, an image or text or whatever -->
		</div>
		<div id="u_slideName3" class="u_slideName">
			<!-- some sort of slide, an image or text or whatever -->
		</div>
		<div id="u_slideName4" class="u_slideName">
			<!-- some sort of slide, an image or text or whatever -->
		</div>

		<!-- and on and on and on ..... (you can have other divs in here but not with the same
		class name or ID format)-->

	</div>

	if you are working within RM your code can do:

	<cfif exists('REQUEST.objectName')>
		<div id="u_slideContainerID">
			<cf_rm_content name="objectName">
			 	<div class="u_slideName" id="u_slideName#counter#">
					<!-- some sort of slide, an image or text or whatever -->
				</div>
			</cf_rm_content>
		</div>
	</cfif>

	in the initial css called from the template you need to find a way of displaying your
	slides so that it looks good on one page which is what it will look like without
	javascript, you will probably want to set the next and previous links as well as the page
	X of X text to display: none however leave your navigational elements as a list of
	permalinks to allow for navigation by non script enabled browsers, eg ...

	<cfif exists('REQUEST.objectName')>
		<ol id="u_navList">
			<cf_rm_content name="objectName">
				<li><a href="##u_slideName#counter#">#counter#</a></li>
			</cf_rm_content>
		</ol>
	</cfif>

	set the className 'selected' for the links in the above list to have some sort of special style

	in the imported CSS you will need to override the display: none's you did for the pages
	and next previous etc. (these arent added dynamicly btw because I dont know where you want
	to put them!) then you want to make all the slides stack up on each other. One way to do
	this is to have the #u_slideContainerID div set to position relative and the indevidual
	.u_slideName set to position: absolute; top:0;

	You will need to set the slides to display: none and slide 1 to display: block (you may
	also need to set #slideshowSlide1 to display: block) the Javascript will work out the rest.

	An example of an imported css file for an animated slideshow of all the same height might look like this:

	#u_slideContainerID {
		position: relative;
		height: 200px;
		overflow: hidden;
	}

	#u_slideContainerID .u_slideName {
		position: absolute;
		top: 0;
		display: none;
	}

	#u_slideContainerID #u_slideName1,
	#u_slideContainerID #slideshowSlide1  {
		display: block;
	}

	whereas naturally you could miss the positioning off if you are setting the u_fadeSlide (the time between fades) to 0;

	#u_slideContainerID {
			position: relative;
	}

	#u_slideContainerID .u_slideName {
			display: none;
	}

	#u_slideContainerID #u_slideName1,
	#u_slideContainerID #slideshowSlide1  {
			display: block;
	}


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

// 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 = 1; // index to gSlides[] the actual slide is 'gCurrentSlide+1' any passing of href must minus 1
var gSlides = [];
var gInitialDocTitle = "";
var gTitles = new Array(); // to collect div titles of slides
var URLchecker = null; // global variable so we can clear and reset interval
var fadeChecker = null; // global variable so we can clear and reset auto-change interval
var fadeOutChecker = null; // global variable so we can clear fade-out request if slide changed by user
var gChangeInProcess = false; // to stop unexpected behaviour if fading is interupted
var gPerma = false; // to stop auto-changing slideshow if permalink is used (restarted with inline nav)
var gPause = false; // to toggle pausing (by clicking on div or selected slide number in navlist)
var gWaitTime = 0; // to allow variable wait-time before slideshow displays
var gWaitDone = false; // timer variable for wait-time routine

if ((u_iframe) && (u_iframe == 1)) {
	var gTarget = top;
	} else {
	var gTarget = self;
}

function insStartChange(requested) {
	if((requested != gCurrentSlide) && (requested < gSlides.length) && (requested >= 0) && (!gChangeInProcess)) {
		gChangeInProcess = true;

		// using global varible from url checker
		// also that of auto-change checker
		clearInterval(URLchecker);
		if ((u_auto) && (u_auto == 1)) {
			clearInterval(fadeChecker);
		}
		if (((u_menu) && (u_menu == 1)) || ((u_fadeEnds) && (u_fadeEnds == 1))) {
			clearTimeout(fadeOutChecker);
		}
		if ((u_preload) && (u_preload == 0)) {
			insLoadCheck(requested);
		} else {
			insStartChangeOK(requested);
		}
	}
}

function insStartChangeOK(requested) {
	// 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;

	// reassign the navigation and URL
	// also prefix div titles (if u_titles = 1)
	insAssignNav();
	
	if (gPerma == true) {
		if (gTarget.location.hash) {
			gTarget.location.href = gTarget.location.href.replace(gTarget.location.hash, '#' + (u_slideName.toLowerCase()) + (gCurrentSlide +1));
		} else {
			gTarget.location.hash = '#' + u_slideName.toLowerCase() + (gCurrentSlide +1);
		}
	}
	if ((u_titles) && (u_titles == 1)) {
		gTarget.document.title = gTitles[gCurrentSlide] + ' (' + u_slideTitlePrefix + ' ' + (gCurrentSlide + 1) + ") - "+ gInitialDocTitle;
	} else {
		gTarget.document.title =  u_slideTitlePrefix + ': ' + (gCurrentSlide + 1) +" : "+ gInitialDocTitle;
	}

	// write what page we are on at the moment
	// also if u_titles = 1, write div titles
	var pagesText =  u_pages.split(',');
	for(var i = 0; i < pagesText.length; i++) {
		if($(pagesText[i])) {
			if ((u_titles) && (u_titles == 1)) {
				$(pagesText[i]).innerHTML = gTitles[gCurrentSlide];
			} else {
				$(pagesText[i]).innerHTML = u_slideName + ' ' + (gCurrentSlide + 1) + ' of ' + gSlides.length;
			}
		}
	}
	
	// officially end the fade
	gChangeInProcess = false;
	
	// if u_menu = 1, goto u_menuURL when slideshow ends
	// else if u_auto = 1, repeat slideshow
	if(gCurrentSlide >= (gSlides.length - 1)) {
		if (((u_menu) && (u_menu == 1)) && ((u_fadeEnds) && (u_fadeEnds == 1))) {
			fadeOutChecker = setTimeout("insFadeOut()",5000);
		} else if ((u_auto) && (u_auto == 1)) {
			setTimeout("insStartChange(0)",5000);
		}
	}

	// if u_auto = 1, restart the auto-change checker as well
	// restart the url checker
	if (((u_auto) && (u_auto == 1)) && (!gPerma)) {
		fadeChecker = setInterval("insStartChange(gCurrentSlide+1)",5000);
	}
	URLchecker = setInterval(insListenForURLchange, 500);
}

function insListenForURLchange() {
	// if menu page is to be used (i.e. u_menu = 1, and hash is #menu, case-insensitive)
	if(((u_menu) && (u_menu == 1)) && (((gTarget.location.hash) && ((gTarget.location.hash == "#Menu") || (gTarget.location.hash == "#menu"))))) {
		insGotoMenu();
	}
	// if the hash exists and is of the form #u_slideName
	if((gTarget.location.hash) && ((gTarget.location.hash.indexOf(u_slideName) != -1))) {
		var urlRequested = parseInt(gTarget.location.hash.replace('#' + u_slideName, '')) - 1;
	}
	gSlideNameLowcase = u_slideName.toLowerCase();
	if((gTarget.location.hash) && ((gTarget.location.hash.indexOf(gSlideNameLowcase) != -1))) {
		var urlRequested = parseInt(gTarget.location.hash.replace('#' + gSlideNameLowcase, '')) - 1;
	}
	if((urlRequested) && (urlRequested != gCurrentSlide)) {
		gPerma = true;
		gSlideNameLowcase = null;
		insStartChange(urlRequested);
	}
	gSlideNameLowcase = null;
}

function insLoadCheck(requested) {
	insStartChangeOK(requested);
}

function insAssignNav() {
	// calculate next and previous page index
	var nextPage = gCurrentSlide + 1;
	var previousPage = gCurrentSlide - 1;
	
	if(gCurrentSlide >= (gSlides.length - 1)) {
		// on the last slide
		nextPage = 0;
	} else if(gCurrentSlide <= 0) {
		// on the first slide
		previousPage = gSlides.length - 1;
	} else {
		// not
	}
	
	// assigns pause onclick to divs (if u_pause = 1)
	insPause(nextPage);
	
	var nextlinks =  u_nextLink.split(',');
	for(var i = 0; i < nextlinks.length; i++) {

		if($(nextlinks[i])) {
			var nextPageLink = $(nextlinks[i]);

			// set up listners for the forward and back links
			// NOTE: the code was throwing an error when I used links, hence currently using spans
			if ((u_titles) && (u_titles == 1)) {
				nextPageLink.title = 'Next: ' + gTitles[nextPage] + ' (' + u_slideName + ' ' + (nextPage + 1) + ')';
			}
			else {
				nextPageLink.title = 'Next (' + u_slideName + ' ' + (nextPage + 1) + ')';
			}
			nextPageLink.onclick = function() {
				insStartChange(nextPage);
				return false;
			};
			
			nextPageLink = null;
		}
	}

	var previousLinks =  u_previousLink.split(',');
	for(var i = 0; i < previousLinks.length; i++) {

		if($(previousLinks[i])) {
			var previousPageLink = $(previousLinks[i]);
			
			if ((u_titles) && (u_titles == 1)) {
				previousPageLink.title = 'Previous: ' + gTitles[previousPage] + ' (' + u_slideName + ' ' + (previousPage + 1) + ')';
			}
			else {
				previousPageLink.title = 'Previous (' + u_slideName + ' ' + (previousPage + 1) + ')';
			}
			previousPageLink.onclick = function() {
				insStartChange(previousPage);
				return false;
			};

			// Clear DOM references to avoid cycle (and resulting memoryleak)
			previousPageLink = null;
		}
	}


	var navLists =  u_navList.split(',');
	for(var i = 0; i < navLists.length; i++) {
		if($(navLists[i])){
			var navListElements = $(navLists[i]).getElementsByTagName('a');
			if(navListElements.length > 0) {
				// reset the className of all list items brefore setting selected - also add onclick
				var l = 0;
				for(l; l<navListElements.length;l++) {

					if(navListElements[l].className != '') {
						navListElements[l].className = navListElements[l].className.replace('selected', '');
					}

					(function() {
						var current=l;
						if ((u_titles == 1) && (u_pageNumberTitles == 1)) {
							navListElements[l].title = gTitles[current] + ' (' + u_slideName + ' ' + (current + 1) + ')';
						} else if (u_pageNumberTitles == 1) {
							navListElements[l].title = u_slideName + ' ' + (current + 1);
						}
						navListElements[l].onclick = function() {
							gPerma = true;
							insStartChange(current);
							return false;
						};
					})();
					navListElements[l].href = '';
				}

				// set current page to be selected
				navListElements[gCurrentSlide].className = 'selected ' + navListElements[gCurrentSlide].className;
				
				// sets current page to pause on click, restarts when clicked again
				if (((u_pause) && (u_pause == 1)) && ((u_auto) && (u_auto == 1))) {
					if(!gPause) {
						navListElements[gCurrentSlide].onclick = function() {
							gPause = true;
							navListElements[gCurrentSlide].onclick = function() {
								gPause = false;
								fadeChecker = setInterval("insStartChange(gCurrentSlide+1)",5000);
								return false;
							}
							clearInterval(fadeChecker);
							return false;
						}
					} else {
						navListElements[gCurrentSlide].onclick = function() {
							gPause = false;
							fadeChecker = setInterval("insStartChange(gCurrentSlide+1)",5000);
							return false;
						}
					}
				}
			}
		}
	}
	
	if (((u_mainMenu) && (u_mainMenu == 1)) && ((u_menu) && (u_menu == 1))) {
		var mainMenuID = document.getElementById("mainmenu");
		mainMenuID.title = 'Go to main menu';
		mainMenuID.onclick = function() {
			insFadeOut();
			return false;
		}
		mainMenuID = null;
	}
}

// pauses auto-changing slideshow if div clicked; restarts when div clicked again
function insPause(nextPage) {
	if (((u_pause) && (u_pause == 1)) && ((u_auto) && (u_auto == 1))) {
		if (gPause == true) {
			gSlides[gCurrentSlide].onclick = function() {
				gPause = false;
				insStartChange(nextPage);
				return false;
			}
		} else {
			gSlides[gCurrentSlide].onclick = function() {
				gPause = true;
				gSlides[gCurrentSlide].onclick = function() {
					gPause = false;
					insStartChange(nextPage);
					return false;
				}
				clearInterval(fadeChecker);
				return false;
			}
		}
	}
}

// fades out if u_fadeEnds = 1
function insFadeOut() {
	// if u_menu = 1
	if (((u_menu) && (u_menu == 1)) && (u_menuURL)) {
		insGotoMenu();
	}
	if ((u_fadeEnds) && (u_fadeEnds == 1)) {
		var fadeOutPhotoAnim = new YAHOO.util.Anim(u_slideContainerID, { opacity: { from: 1, to: 0 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
		fadeOutPhotoAnim.animate();
		fadeOutPhotoAnim = null;
	}
}

// goes to menu page, if u_menu = 1
// uses variables u_menuURL and (if specified) u_menuFadeOutID
function insGotoMenu() {
	if (u_menuFadeOutID) {
		var anim = new YAHOO.util.Anim(u_menuFadeOutID, { opacity: { from: 1, to: 0 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
		anim.animate();
		anim.onComplete.subscribe(function() { window.location.href=u_menuURL; });
		anim = null;
	} else {
		window.location.href=u_menuURL;
	}
}

// if "loading" div is used (u_loading = 1)
function insLoading() {
	var loadingID = 'loading';
	if ((u_fadeEnds) && (u_fadeEnds == 1)) {
		var anim = new YAHOO.util.Anim(loadingID, { opacity: { from: 1, to: 0 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
		anim.animate();
		anim.onComplete.subscribe(function() { $(loadingID).style.display = 'none'; loadingID = null; });
		anim = null;
	} else {
		$(loadingID).style.display = 'none';
		loadingID = null;
	}
}

// if wait-time is used (u_waitTime = x in seconds)
function insWaitTime() {
	gWaitDone = true;
}

function insSetupPage() {

	if($(u_slideContainerID)) {
		// initial document title
		gInitialDocTitle = gTarget.document.title;
		
		// set up array of slide divs and rename id
		var tempSlides = $(u_slideContainerID).getElementsByTagName('div');

		if(tempSlides.length > 0) {
			var tempSlideNo;
			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];
					
					// get div titles and add to array if u_titles = 1
					if ((u_titles) && (u_titles == 1)) {
						gTitles[i] = tempSlides[i].title;
					}

				}
			}

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

			// what page are we on? take hash and turn to index : #1 --> [0]
			var gSlideNameLowcase = u_slideName.toLowerCase();
			if(gTarget.location.hash) {
				gPerma = true;
				if((gTarget.location.hash.indexOf(u_slideName) != -1) && (gTarget.location.hash.indexOf(gSlideNameLowcase) == -1)) {
					gCurrentSlide = parseInt(gTarget.location.hash.replace('#' + u_slideName, ''), 10) - 1;
				} else if((gTarget.location.hash.indexOf(u_slideName) == -1) && (gTarget.location.hash.indexOf(gSlideNameLowcase) != -1)) {
					gCurrentSlide = parseInt(gTarget.location.hash.replace('#' + gSlideNameLowcase, ''), 10) - 1;
				} else if(gTarget.location.hash.indexOf('top') != -1) {
					gCurrentSlide = 0;
					gTarget.location.href = gTarget.location.href.replace(gTarget.location.hash, '#' + gSlideNameLowcase);
				} else {
					gCurrentSlide = 0;
					gTarget.location.href = gTarget.location.href.replace(gTarget.location.hash, '#' + gSlideNameLowcase);
				}
				if(gCurrentSlide >= gSlides.length) {
					// SLIDE TOO HIGH
					gCurrentSlide = 0;
					gTarget.location.href = gTarget.location.href.replace(gTarget.location.hash, '#' + gSlideNameLowcase + '1');
				}
			} else {
				gCurrentSlide = 0;
				if (gPerma == true) {
					gTarget.location.href = gTarget.location.href + '#' + gSlideNameLowcase + '1';
				}
			}
			gSlideNameLowcase = null;
			// replace current title with slide number and title
			// alsos if u_titles = 1, prefix div title
			if ((u_titles) && (u_titles == 1)) {
				gTarget.document.title = gTitles[gCurrentSlide] + ' (' + u_slideTitlePrefix + ' ' + (gCurrentSlide + 1) + ") - "+ gInitialDocTitle;
			} else {
				gTarget.document.title =  u_slideTitlePrefix + ': ' + (gCurrentSlide + 1) +" : "+ gInitialDocTitle;
			}

			// initialise and assign links
			insAssignNav();

			// write what page we are on at the moment
			// also if u_titles = 1, write div titles

			var pagesText = u_pages.split(',');
			for(var i = 0; i < pagesText.length; i++) {
				if($(pagesText[i])) {
					if ((u_titles) && (u_titles == 1)) {
						$(pagesText[i]).innerHTML = gTitles[gCurrentSlide];
					}
					else {
						$(pagesText[i]).innerHTML = u_slideName + ' ' + (gCurrentSlide + 1) + ' of ' + gSlides.length;
					}
				}
			}

			// display correct slide to start with
			if(gCurrentSlide != 0) {
				gSlides[0].style.display = 'none';
				YAHOO.util.Dom.setStyle(gSlides[0], 'opacity', 1);
				gSlides[gCurrentSlide].style.display = 'block';
			}
			if ((u_fadeEnds) && (u_fadeEnds == 1)) {
				YAHOO.util.Dom.setStyle(gSlides[gCurrentSlide], 'opacity', 0);
				var anim = new YAHOO.util.Anim(gSlides[gCurrentSlide], { opacity: { from: 0, to: 1 } }, u_fadeSlide, YAHOO.util.Easing.easeBoth);
				anim.animate();
				anim = null;
			} else {
				YAHOO.util.Dom.setStyle(gSlides[gCurrentSlide], 'opacity', 1);
			}
			
			// if loading div is used (u_loading = 1)
			if ((u_loading) && (u_loading == 1)) {
				if ((u_waitTime) && (u_waitTime > 0)) {
					if((!gWaitDone) && (!gTarget.location.hash)) {
						clearTimeout(waitDone);
						setTimeout(insLoading, u_waitTime);
					} else {
					insLoading();
					}
				}
			}
			
			// assign our listener to a global variable so we can purge it later
			// also if u_auto = 1, auto-change to next slide after 5 secs.
			URLchecker = setInterval(insListenForURLchange, 700);
			if (((u_auto) && (u_auto == 1)) && (!gPerma)) {
				if ((u_waitTime) && (u_waitTime > 0)) {
					if(!gWaitDone) {
						fadeChecker = setInterval("insStartChange(gCurrentSlide+1)",(u_waitTime+5000));
					}
				} else {
				fadeChecker = setInterval("insStartChange(gCurrentSlide+1)",5000);
				}
			}
		}
	}
}

// on load
if(((u_menu) && (u_menu == 1)) && (((gTarget.location.hash) && ((gTarget.location.hash == "#Menu") || (gTarget.location.hash == "#menu"))))) {
	window.location.href=u_menuURL;
}
// if wait-time used (u_waitTime = x in seconds)
u_waitTime = u_waitTime * 1000;
waitDone = setTimeout(insWaitTime, u_waitTime);
if ((u_preload) && (u_preload == 0)) {
	YAHOO.util.Event.onDOMReady(insSetupPage);
} else {
	$E.addListener(window,'load',insSetupPage);
}

function setCSS(css) {
	try {
		// append stylesheet to alter
		document.getElementsByTagName("head")[0].appendChild(css);
	} catch (e) {
		setTimeout(function(){setCSS(css)}, 100);
	}
}

// create CSS element to set up the page
var css = document.createElement("link");
css.setAttribute("href",u_importedCSSfile);
css.setAttribute("rel","stylesheet");
css.setAttribute("type","text/css");

// attempt to add the css and then keep trying till we do
setCSS(css);
css = null;