/*===============================================================

	This is SLIDESHOW NOVICE
	
	This is a proof of concept and steals from the design of SlideShow Pro,
	a flash slideshow player developed by Todd Dominey of Dominey Design.

==================================================================*/

var SSManager = new SlideShowManager('http://lakesidebiblecamp.org/wp-content/themes/lbc');
SSManager.startShows();
function SlideShowManager(ssDir){
	this.slideShows = new Array();
	this.slideShowDir = ssDir;
	this.resources = new Array();
	
	this.findShows = SlideShowFinder;
	this.createShow = createShow;
	this.initManager = initManager;
	this.startShows = startShows;
	
	this.initManager();
	this.findShows();

	function initManager(){
		var resource_imgs = new Array(), v;
		resource_imgs[0] = 'play_msg.png';//play graphic
		resource_imgs[1] = 'pause_msg.png';//pause graphic
		resource_imgs[2] = 'tray.gif';
		resource_imgs[3] = 'next.png';
		resource_imgs[4] = 'previous.png';
		resource_imgs[5] = 'play.png';
		resource_imgs[6] = 'nextTray_btn.jpg';
		resource_imgs[7] = 'previousTray_btn.jpg';
		resource_imgs[8] = 'loading_msg.png';
		resource_imgs[9] = 'pause.png'
		for(v in resource_imgs){
			this.resources[v] = new Image();
			this.resources[v].src = this.slideShowDir + '/' + resource_imgs[v];
		}
	}

	function createShow(vars, obj){
		var i = this.slideShows.length;
		vars = vars.split(';');
		var t = 'label';
		var settings = new Array();
		var s;
		for(var j=1;j<vars.length;j++){
			s = vars[j].split(':');
			settings[s[0]] = s[1];
			t = (t=='label') ? 'value' : 'label';
		}
		this.slideShows[i] = new SlideShow(vars[0], obj, i, settings['width'], settings['height'], settings['background'], settings['border'], settings['navbar'], settings['autosize'], settings['autoplay'], settings['menu_elm'], settings['controls']);
	}

	function SlideShowFinder(){
		var allDivs = document.getElementsByTagName('div');
		var theAlt, ss_ind, ss_vars;
		for(i=0;i<allDivs.length;i++){
			if(theAlt = allDivs[i].className){
				ss_ind = theAlt.indexOf('[slideshow]');
				if(ss_ind == 0){
					ss_vars = theAlt.slice(11,theAlt.length);
					this.createShow(ss_vars, allDivs[i]);
				}
			}
		}
	}
	
	function startShows(){
		for(i=0;i<this.slideShows.length;i++){
			this.slideShows[i].init();
		}
	}
}
/*-------------------------------------------------------------
	The SlideShow object is responsible for loading the resources and controlling
	playback as well as displaying the interface.
---------------------------------------------------------------*/

function SlideShow(ss_path, ss_obj, ss_objId, ss_width, ss_height, ss_bg, ss_border, ss_nav, autosize, autoplay, menu_elm_ID, control_elm_ID) {

	//Properties
	this.controller = document.getElementById(control_elm_ID);
	this.path = ss_path; //the path to the slideshow xml file
	this.playerWidth = (ss_width) ? ss_width : '400px'; //the viewer width
	this.playerHeight = (ss_height) ? ss_height : '300px'; //the viewer height
	this.navbarOnload = (ss_nav != 'off') ? 'on' : 'off' ;  //boolean to show controls
	this.showTimer; //the SetTimeout ID that controls playback
	this.messageTimer;
	this.messageOpacity = 75;
	this.slideTimer;
	this.playerBG = (ss_bg) ? ss_bg : "#000"; //the background of the player
	this.playerBorder = (ss_border) ? ss_border : '1px solid #000';
	this.playerObj = ss_obj;//the element that will contain the slideshow
	this.viewerObj;//the element that will contain the images
	this.messageObj;//the message element
	this.navbar;//the element that will contain the tray
	this.tray;
	this.captionPane;
	this.http;
	this.playerID = ss_objId;//the array index of SlideShowManager.slideShows
	this.ssXML;//the xml we get for the images
	this.slides = new Array();
	this.slideIndex;//current slide
	this.slideTotal;
	this.playStatus = 'play';
	this.autosize = autosize;
	this.autoplay = autoplay;
	this.resizeTimeout = null;
	this.menuID = menu_elm_ID;

	//Methods
	this.init = init;
	this.initPlayer = initPlayer;
	this.initViewer = initViewer;
	this.initNav = initNav;
	this.initTray = initTray;
	this.initCaption = initCaption;
	this.setPlayerSettings = setPlayerSettings;
	this.setViewerSettings = setViewerSettings;
	this.createXML = createRequestObject;
	this.getXML = sndReq;
	this.notify = notify;
	this.fadeMessage = fadeMessage;
	this.createSlides = createSlides;
	this.changeSlide = changeSlide;
	this.nextSlide = nextSlide;
	this.previousSlide = previousSlide;
	this.pauseShow = pauseShow;
	this.playShow = playShow;
	this.toggleShow = toggleShow;
	this.goToSlide = goToSlide;
	this.clearShowTimer = clearShowTimer;
	this.setBehaviours = setBehaviours;
	this.reSize = reSize;
	this.initController = initController;
	this.setPlayPause = setPlayPause;

	function init(){ //lets start the show
		this.initPlayer();
		//this.initViewer();
		this.setBehaviours();
		//this.initNav()
		//this.initTray();
		//this.initCaption();
		this.menu = new ThumbMenu(this.menuID,this.playerID);
		this.initController();
		this.getXML(this.path);
	}
	
	function initController(){
		//we're going create the menu
		if(this.controller){
				imgs = {
					previous:'previous.png',
					next:'next.png',
					play:'play.png',
					pause:'pause.png'
				};
				this.buttons = new Object();
				for(x in imgs){
					this.buttons[x] = document.createElement('img');
					this.buttons[x].src = SSManager.slideShowDir + '/' + imgs[x];
				}
				this.controller.appendChild(this.buttons.previous);
				if(this.autoplay){
					this.controller.appendChild(this.buttons.pause);
				}else{
					this.controller.appendChild(this.buttons.play)
				}
				this.controller.appendChild(this.buttons.next);
				this.buttons.next.onclick = new Function("SSManager.slideShows["+this.playerID+"].nextSlide();");
				this.buttons.previous.onclick = new Function("SSManager.slideShows["+this.playerID+"].previousSlide();");
				this.buttons.pause.onclick = new Function("SSManager.slideShows["+this.playerID+"].pauseShow();");
				this.buttons.play.onclick = new Function("SSManager.slideShows["+this.playerID+"].playShow();");
		}
	}
	
	function setPlayPause(){
		var new_child;
		if(this.playStatus == 'play'){
			if(this.buttons.play) this.buttons.play.parentNode.replaceChild(this.buttons.pause, this.buttons.play);
		}else{
			if(this.buttons.pause) this.buttons.pause.parentNode.replaceChild(this.buttons.play, this.buttons.pause);
		}
	}

	function initCaption(){
		this.captionPane = new CaptionPane(this.playerID);
	}

	function initPlayer(){
		//remove the object's children
		this.playerObj.innerHTML = '';
		this.setPlayerSettings();
	}

	function initNav(){
		this.navbar = new NavBar(this.playerID);
	}

	function initTray(){
		this.tray = new IndexTray(this.playerID);
		this.tray.initIndexButtons();
	}

	function setPlayerSettings(){
		var settings = new Array();
		settings['position'] = 'relative';
		settings['height'] = this.playerHeight;
		settings['width'] = this.playerWidth;
		settings['background'] = this.playerBG;
		settings['overflow'] = 'hidden';
		settings['border'] = this.playerBorder;
		for(s in settings){
			this.playerObj.style[s] = settings[s];
		}
		if(this.autoplay == 'off'){
			this.playStatus = 'pause';
		}
	}

	function setViewerSettings(){
		var settings = new Array();
		if(this.controlObj){
			settings['height'] = (this.playerObj.offsetHeight - this.controlObj.offsetHeight) + 'px';
		}else{
			settings['height'] = this.playerObj.offsetHeight + 'px';
		}
		settings['width'] = this.playerObj.offsetWidth-2 + 'px';
		settings['position'] = 'relative';
		settings['margin'] = '0 auto';
		settings['left'] = 0;
		settings['top'] = 0;
		for(s in settings){
			this.viewerObj.style[s] = settings[s];
		}
	}

	function initViewer(){//create and attach div where images will be placed
		this.viewerObj = document.createElement('div');//we need to set the correct sizes depending on the existance of the controls/tray
		this.playerObj.appendChild(this.viewerObj);
		this.setViewerSettings();
	}

	function setBehaviours(){
		//this.viewerObj.onmouseover = new Function("SSManager.slideShows["+this.playerID+"].pauseShow()");
		//this.viewerObj.onmouseout = new Function("SSManager.slideShows["+this.playerID+"].playShow()");
	}

	function notify(msg_type, autofade){
		var graphic;

		if(this.messageTimer) clearTimeout(this.messageTimer);
		if(this.messageObj) this.messageObj.parentNode.removeChild(this.messageObj);

		if(msg_type == 'play'){
			graphic = SSManager.resources[0];		
		}else if(msg_type == 'pause'){
			graphic = SSManager.resources[1];
		}else if(msg_type == 'loading'){
			graphic = SSManager.resources[8];
			autofade = 'off';
		}
		var settings = new Array();
		settings['width'] = graphic.width + 'px';
		settings['height'] = graphic.height + 'px';
		settings['position'] = 'absolute';
		if(msg_type != 'loading'){
			settings['top'] = Math.floor((this.playerObj.offsetHeight - graphic.height)/2) + 'px';
			settings['left'] = Math.floor((this.playerObj.offsetWidth - graphic.width)/2) + 'px';
		}else{
			settings['bottom'] = '5px';
			settings['right'] = '5px';
		}
		settings['backgroundImage'] = 'url("'+graphic.src+'")';
		this.messageObj = document.createElement('div');
		for(s in settings){
			this.messageObj.style[s] = settings[s];
		}
		setOpacity(this.messageObj, this.messageOpacity);
		this.playerObj.appendChild(this.messageObj);
		if(this.messageObj.runtimeStyle){
			this.messageObj.style.backgroundImage = 'none';
			this.messageObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+graphic.src+"',sizingMethod='scale')";
		}
		this.messageTimer = (autofade == 'off') ? setTimeout('SSManager.slideShows['+this.playerID+'].fadeMessage(5)',1250) : null;

	}

	function fadeMessage(speed){
		clearTimeout(this.messageTimer);
		this.messageOpacity = (this.messageOpacity - speed < 0) ? 0 : this.messageOpacity - speed;
		setOpacity(this.messageObj, this.messageOpacity);
		if(this.messageOpacity == 0){
			this.messageObj.parentNode.removeChild(this.messageObj);
			this.messageObj = null;
			this.messageOpacity = 100;
			this.messageTimer = null;
		}else{
			this.messageTimer = setTimeout('SSManager.slideShows['+this.playerID+'].fadeMessage(15)',1);
		}
	}

	function changeSlide(){
		var s;
		clearTimeout(this.slideTimer);
		this.slideTimer = null;
		this.slides[this.slideIndex].loadSlide();
		//if the image is complete
		if(this.slides[this.slideIndex].photo.complete){
			//remove loading message
			if(this.messageObj) this.fadeMessage(15);
			for(s in this.slides){
				if(s != this.slideIndex){
					this.slides[s].fadeSlide('out',25);
				}
			}
			this.slides[this.slideIndex].displaySlide();
			this.menu.changeSelected(this.slideIndex);
			//this.captionPane.chgCaption();
			//this.tray.activateBtn(this.slideIndex);
			//this.navbar.setNextPrev();
		}else{
			this.slideTimer = setTimeout('SSManager.slideShows['+this.playerID+'].changeSlide();',1);
			this.notify('loading');//reload the change slide
			//display the loading message;
		}
	}

	function createSlides(){
		if(this.http.readyState == 4){
			this.ssXML = this.http.responseXML;
			this.http = null;//clear the HTTP Request object
			var slidenodes = this.ssXML.getElementsByTagName('slide')
			this.slideIndex = 0;
			var slide, photosrc, slidecaption, j;
			for(i=0;i<slidenodes.length;i++){
				if(slidenodes[i].nodeType==1){
					j = this.slides.length;
					photosrc = slidenodes[j].getElementsByTagName('image')[0].getAttribute('filename');
					captionNode = slidenodes[i].getElementsByTagName('caption');
					if (captionNode.length > 0){
						if(captionNode[0].firstChild) slidecaption = captionNode[0].firstChild.nodeValue;
					}
					this.slides[j] = new Slide(j, this.playerID, photosrc, slidecaption);
					photosrc = null;
					slidecaption = null;
				}
			}
			//if(this.navbarOnload == 'on') this.navbar.showNav();
			//this.tray.loadButtons();
			this.menu.setBehaviours();
			this.changeSlide();
		}
	}

	function nextSlide(){
		this.clearShowTimer();
		this.slideIndex = (this.slideIndex + 1 == this.slides.length) ? 0 : this.slideIndex + 1; 
		this.changeSlide();
	}

	function previousSlide(){
		this.clearShowTimer();
		this.slideIndex = (this.slideIndex == 0) ? this.slides.length - 1 : this.slideIndex - 1;
		this.changeSlide();
	}

	function pauseShow(){
		this.clearShowTimer();
		if(this.playStatus != 'pause'){
			this.playStatus = 'pause';
			this.notify('pause');
			//this.navbar.setPlayPause();
			this.setPlayPause();
		}
	}

	function playShow(){
		this.clearShowTimer();
		if(this.playStatus != 'play'){
			this.playStatus = 'play';
			this.showTimer = setTimeout("SSManager.slideShows["+this.playerID+"].nextSlide();",3000);
			this.notify('play');
			//this.navbar.setPlayPause();
			this.setPlayPause();
		}
	}
	
	function toggleShow(){
		if(this.playStatus == 'play'){
			this.pauseShow();
		}else{
			this.playShow();
		}
	}

	function goToSlide(i){
		if(this.slideIndex != i){
			this.slideIndex=i;
			this.pauseShow();
			this.changeSlide();
		}
	}

	function clearShowTimer(){
		if(this.showTimer) clearTimeout(this.showTimer);
		this.showTimer = null;
	}

	this.keyboardNav = function (key){
			var currentIndex = this.menu.selectedIndex;
			var nextIndex = null;
			var upperBound = this.menu.menu.length - 1;
			if(key == 37) nextIndex = currentIndex - 1;					
			if(key == 38) nextIndex = currentIndex - 5;
			if(key == 39) nextIndex = currentIndex + 1;
			if(key == 40) nextIndex = currentIndex + 5;
			if(nextIndex > upperBound || nextIndex < 0){
				return;
			}
			this.goToSlide(nextIndex);

	}

	function reSize(w,h){
		if(this.autosize == 'on'){
			clearTimeout(this.resizeTimeout);
			current_w = this.playerObj.offsetWidth;
			current_h = this.playerObj.offsetHeight;
			distance_w = Math.abs(current_w - w);
			distance_h = Math.abs(current_h - h);
			direction_w = (current_w > w) ? -1 : 1;
			direction_h = (current_h > h) ? -1 : 1;
			change_w = Math.ceil(distance_w / 5);
			change_h = Math.ceil(distance_h / 5);
			next_w = current_w + (direction_w * (change_w+2));
			next_h = current_h + (direction_h * (change_h+2));
			finished_w = false;
			finished_h = false;
			if((next_w < w && direction_w == -1) || (next_w > w && direction_w == 1) || current_w == w){
				this.playerObj.style.width = w + 'px';
				finished_w = true;
			}else{
				this.playerObj.style.width = next_w + 'px';
			}
			if((next_h < h && direction_h == -1) || (next_h > h && direction_h == 1) || (current_h == h)){
				this.playerObj.style.height = h + 'px';
				finished_h = true;
			}else{
				this.playerObj.style.height = next_h + 'px';
			}
			if(!finished_w || !finished_h){
				this.resizeTimeout = setTimeout("SSManager.slideShows["+this.playerID+"].reSize("+w+","+h+");",1);			
			}

		}
		
	}

	// a little ajax for our slideshow

	function createRequestObject() {
		var ro;
		var browser = navigator.appName;
		if(this.http){
			this.http.abort();
			this.http = false;
		}
		/*if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			ro = new XMLHttpRequest();
		}
		this.http = ro;
		var xmlhttp=false;*/
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		try
		{
		  this.http = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
		  try
		  {
		      this.http = new ActiveXObject("Microsoft.XMLHTTP");
		  }
		  catch (E)
		  {
		      this.http = false;
		  }
		}
		@end @*/

		if (!this.http && typeof XMLHttpRequest != 'undefined')
		{
		  this.http = new XMLHttpRequest();
		}
		this.http.onreadystatechange = new Function("if(SSManager){ SSManager.slideShows[" + this.playerID + "].createSlides(); }");
	}

	function sndReq(request_url) {
		this.createXML();
		this.http.open('get', request_url);
		this.http.send(null);
	}
}

/*---------------------------------------------------------------
	This is a slide.  It controls it's own fade effect and load status
-----------------------------------------------------------------*/

function Slide(slideID, playerID, slide_src, slideCaption){
	this.playerID = playerID;
	this.slideID = slideID;
	this.slideCaption = slideCaption;
	this.slideSrc = slide_src;//the path to the image
	this.slideTimer;//the SetTimeout id that controls the opacity
	this.slideOpacity = 0;
	this.slideObj;//the slide element div
	this.slideStatus = 'off';
	this.photo;
	
	this.loadSlide = loadSlide;
	this.displaySlide = displaySlide;
	this.removeSlide = removeSlide;
	this.fadeSlide = fadeSlide;
	this.setSlide = setSlide;
	this.clearSlideTimer = clearSlideTimer;
	
	//this.loadSlide();

	function loadSlide(){
		if(!this.photo){
			this.photo = new Image();
			this.photo.src = this.slideSrc;
		}	
	}

	function setSlide(){
		var ratio;
		var settings = new Array();
		var player = SSManager.slideShows[this.playerID].playerObj;
		var navbar = SSManager.slideShows[this.playerID].navbar;
		var av_w = player.offsetWidth;
		var av_h = player.offsetHeight;
		this.slideObj.style.visibility = 'hidden';
		SSManager.slideShows[this.playerID].playerObj.appendChild(this.slideObj);
		if(SSManager.slideShows[this.playerID].autosize != 'on'){
			var t_offset = Math.round((av_h-this.photo.height)/2);
			var l_offset = Math.round((av_w-this.photo.width)/2);
			settings['position'] = 'absolute';
		}else{
			var t_offset = "0";
			var l_offset = "0";
			settings['position'] = 'relative';
		}
		settings['visibility'] = 'visible';
		settings['top'] = t_offset + 'px';
		settings['left'] = l_offset + 'px';
		settings['display'] = 'block';
		for(s in settings){
			this.slideObj.style[s] = settings[s];
		}
	}

	function displaySlide(){
		this.loadSlide();
		if(this.photo.complete){
			SSManager.slideShows[this.playerID].reSize(this.photo.width, this.photo.height);
			if(!this.slideObj){
				this.slideObj = document.createElement('img');
				this.slideObj.src = this.slideSrc;
			}
			this.setSlide();
			setOpacity(this.slideObj, this.slideOpacity);
			this.slideObj.style['visibility'] = 'visible';
			this.clearSlideTimer();
			this.fadeSlide('in', 1);
		}else{
			//show the loading window
			setTimeout('SSManager.slideShows[' + this.playerID + '].slides['+ this.slideID +'].displaySlide()',1);
		}
	}

	function removeSlide(){
		var slideparent;
		if(slideparent = this.slideObj.parentNode) slideparent.removeChild(this.slideObj);
		this.slideObj = null;
		this.photo = null;
	}

	function fadeSlide(inorout, speed){
		var player = SSManager.slideShows[this.playerID];
		this.clearSlideTimer();
		if(this.slideObj){
			if(inorout == 'in') this.slideOpacity = (this.slideOpacity + speed > 100) ? 100 : this.slideOpacity + speed;
			if(inorout == 'out') this.slideOpacity = (this.slideOpacity - speed < 0 ) ? 0 : this.slideOpacity - speed;
			setOpacity(this.slideObj, this.slideOpacity);
			if((this.slideOpacity < 100 && inorout == 'in') || (this.slideOpacity > 0 && inorout == 'out')){
				speed = Math.round(speed * 1.5);
				this.slideTimer = setTimeout('SSManager.slideShows[' + this.playerID + '].slides[' + this.slideID + '].fadeSlide("' + inorout + '",' + speed + ')', 1);
				this.slideStatus = inorout;
			}else if(inorout == 'out'){
				this.removeSlide();
				this.slideStatus = 'off';
			}else if(inorout == 'in'){
				this.slideStatus = 'on';
				if(player.playStatus == 'play') player.showTimer = setTimeout('SSManager.slideShows[' + this.playerID + '].nextSlide()',3000);
			}
		}
	}

	function clearSlideTimer(){
		if(this.slideTimer)clearTimeout(this.slideTimer);
		this.slideTimer = null;
	}

}

function ThumbMenu(ulid, playerId){
	this.playerId = playerId;
	this.menu_elm = document.getElementById(ulid);
	this.menu = new Array();
	this.selectedIndex = 0;
	
	this.init = function(){
		if(this.menu_elm){
			this.menu = this.menu_elm.getElementsByTagName('a');
			this.disableClicks();
		}
	}
	
	this.disableClicks = function(){
		for(i=0;i<this.menu.length;i++){
			this.menu[i].onclick = new Function("alert('hello'); return false;");
		}
	}
	
	this.setBehaviours = function(){
		if(this.menu_elm){
			for(i=0;i<this.menu.length;i++){
				if(this.menu[i]) this.menu[i].onclick = new Function("SSManager.slideShows["+this.playerId+"].goToSlide("+i+");this.blur();return false;");
			}
				keyPasser = new Function("e","e = (!e) ? window.event : e;if(e.keyCode >= 37 && e.keyCode <= 40){SSManager.slideShows["+this.playerId+"].keyboardNav(e.keyCode);if(e.preventDefault) e.preventDefault(); return false;}");
				if(document.attachEvent){
					document.attachEvent('onkeydown',keyPasser);
				}else if(document.addEventListener){
					document.addEventListener('keydown',keyPasser,false);		
				}
				var hint = document.getElementById('hint');
				if(hint){
					hint.style.display = "block";
				}
		}
		//attach events for keypress
	}
	
	this.changeSelected = function(newIndex){
		if(this.menu_elm){
			this.menu[this.selectedIndex].parentNode.className = "";
			this.selectedIndex = newIndex;
			this.menu[this.selectedIndex].parentNode.className = "here";
		}
	}
	
	this.init();
	
}

/*----------------------------------------------------------------
	You can guess what this does, plus other functions that everyone will use
------------------------------------------------------------------*/

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function isChildOf(c, p){
	//c.style.border = '1px solid #F00';
	var e = c.parentNode, isChild;
	//alert(e);
	isChild = false;
	while(e.nodeName != 'BODY' && e.nodeName != 'body'){
		e = e.parentNode;
		//alert(e.nodeName);
		if(e == p){
			isChild = true;
		}
	}
	return isChild;
}