/*
	autoSlides
	Slideshow example of Chapter 6 of
	"Beginning JavaScript with DOM Scripting and AJAX" 
	by Christian Heilmann
	(c) Christian Heilmann and Apress
*/
autoSlides={

	// CSS classes
	slideClass:'slides',
	dynamicSlideClass:'dynslides',
	showClass:'show',
	slideCounterClass:'slidecounter',
	hideLinkClass:'hide',

	// labels 
	// play and stop links, you can use any HTML here
	playLabel:'<img src="control_play_blue.png" title="click to play slide show" alt="play" border="0" />',
	stopLabel:'<img src="control_stop_blue.png" title="click to stop slide show"alt="stop"   border="0"/>',
	forwardsLabel:'<img src="control_fastforward_blue.png" alt="next"  border="0" />',
	backwardsLabel:'<img src="control_rewind_blue.png" alt="previous"  border="0" />',
	// Counter text, # will be replaced by the current image count
	// and % by the number of all pictures
	counterLabel:'(# of %)', 

	// animation delay in milliseconds
	delay:10000,
	
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var uls=document.getElementsByTagName('ul');
		autoSlides.slideLists=new Array();
		for(var i=0;i<uls.length;i++){
			if(!DOMhelp.cssjs('check',uls[i],autoSlides.slideClass)){continue;}
			DOMhelp.cssjs('swap',uls[i],autoSlides.slideClass,autoSlides.dynamicSlideClass);
			uls[i].currentSlide=1;	
			uls[i].showIndex=i;
			uls[i].photoCount=uls[i].getElementsByTagName('li').length;
			autoSlides.initSlideShow(uls[i]);
			autoSlides.slideLists.push(uls[i]);
		}
	},
	initSlideShow:function(o){
		var p,temp;	
		p=document.createElement('span');
		p.className='buttons';
		DOMhelp.cssjs('add',p,autoSlides.slideCounterClass);
		o.parentNode.insertBefore(p,o.nextSibling);
		o.rew=DOMhelp.createLink('#',' ');
		o.rew.innerHTML=autoSlides.backwardsLabel;
		DOMhelp.addEvent(o.rew,'click',autoSlides.showSlideMan,false);
		DOMhelp.cssjs('add',o.rew,autoSlides.showClass);
		
		o.play=DOMhelp.createLink('#',' ');
		o.play.setAttribute('name','myplay');
		o.play.setAttribute('id','myplay');
		o.play.innerHTML=autoSlides.playLabel;
		DOMhelp.addEvent(o.play,'click',autoSlides.playSlide,false);
		//alert(o.play.name);
		
		o.count=document.createElement('span');
		temp=autoSlides.counterLabel.replace(/#/,o.currentSlide+1);
		temp=temp.replace(/%/,o.getElementsByTagName('li').length);
		o.count.appendChild(document.createTextNode(temp));
		
		o.fwd=DOMhelp.createLink('#',' ');
		o.fwd.innerHTML=autoSlides.forwardsLabel;
		DOMhelp.addEvent(o.fwd,'click',autoSlides.showSlideMan,false);
		
		temp=o.getElementsByTagName('li')[o.currentSlide];
		DOMhelp.cssjs('add',temp,autoSlides.showClass);
		
		//p.appendChild(o.count);
		p.appendChild(o.rew);
		p.appendChild(o.play);
		p.appendChild(o.fwd);
		
		o.play.onclick=DOMhelp.safariClickFix;
		o.fwd.onclick=DOMhelp.safariClickFix;
		o.rew.onclick=DOMhelp.safariClickFix;
		o.play.click();
	},
	playSlide:function(e){
		var t=DOMhelp.getTarget(e);
		while(t.nodeName.toLowerCase()!='a' 
			  && t.nodeName.toLowerCase()!='body'){
			t=t.parentNode;
		} 
		var parentList=DOMhelp.closestSibling(t.parentNode,-1);
		if(!parentList.loop){
			var loopCall="autoSlides.showSlide('"+parentList.showIndex+"')";
			parentList.loop = window.setInterval(loopCall, autoSlides.delay);
			t.innerHTML=autoSlides.stopLabel;
		} else {
			window.clearInterval(parentList.loop);
			parentList.loop=null;
			t.innerHTML=autoSlides.playLabel;
		}	
		DOMhelp.cancelClick(e);
	},
	showSlide:function(showIndex){
	    var currentShow=autoSlides.slideLists[showIndex];
	    var count=currentShow.currentSlide;
	    var photoCount= currentShow.getElementsByTagName('li').length;
	    var photo= currentShow.getElementsByTagName('li')[count];
	    DOMhelp.cssjs('remove',photo,autoSlides.showClass);
	    count++
	    if(count==photoCount){count=0};
	    photo= currentShow.getElementsByTagName('li')[count];
	    DOMhelp.cssjs('add',photo,autoSlides.showClass);
	    var counterText=currentShow.count.firstChild
	    counterText.nodeValue=counterText.nodeValue.replace(/\d/,count+1);
	    currentShow.currentSlide=count;
	},
	showSlideMan:function(e){
	
			
			
		var action;
		var t=DOMhelp.getTarget(e);
		while(	t.nodeName.toLowerCase()!='a' 
				&& t.nodeName.toLowerCase()!='body'){
			t=t.parentNode;
		} 
		if(DOMhelp.cssjs('check',t,autoSlides.hideLinkClass)){return;}
		var parentList=DOMhelp.closestSibling(t.parentNode,-1);
		var count=parentList.currentSlide;
		var photoCount=parentList.getElementsByTagName('li').length-1;
		var photo=parentList.getElementsByTagName('li')[count];
		
		
			window.clearInterval(parentList.loop);
			parentList.loop=null;
			//t.innerHTML=autoSlides.playLabel;
			//o.play.innerHTML=autoSlides.playLabel;
		document.getElementById('myplay').innerHTML=autoSlides.playLabel;
		DOMhelp.cancelClick(e);
		
		DOMhelp.cssjs('remove',photo,autoSlides.showClass);
		count = (t==parentList.fwd) ? count+1 : count-1;
		//action = (count>0) ? 'remove' : 'add' ;
	//	DOMhelp.cssjs(action,parentList.rew,autoSlides.hideLinkClass);
		//action = (count<photoCount) ? 'remove' : 'add';
	//	DOMhelp.cssjs(action,parentList.fwd,autoSlides.hideLinkClass);
		if(count>photoCount){
		count=0
		photo=parentList.getElementsByTagName('li')[0];
		} else {
		if(count<0){
		count=photoCount
		photo=parentList.getElementsByTagName('li')[photoCount];
		} else {
		
		photo=parentList.getElementsByTagName('li')[count];
		}	
		}	
		
		var counterText=parentList.count.firstChild
		counterText.nodeValue=counterText.nodeValue.replace(/\d/,count+1);
		parentList.currentSlide=count;
		DOMhelp.cssjs('add',photo,autoSlides.showClass);
		DOMhelp.cancelClick(e);
		
		
		
	}
	
	
}
DOMhelp.addEvent(window, 'load', autoSlides.init, false);
 
