// Extra Buttons Script (c)2011 John Davenport Scheuer
// as first seen in http://www.dynamicdrive.com/forums/
// add on for Ultimate Fade In Slideshow
// username: jscheuer1 - This Notice Must Remain for Legal Use
jQuery.extend(fadeSlideShow.prototype, {
	paginateinit:function($){ // replaces native paginate function
		var slideshow=this
		var setting=this.setting;
		if (setting.togglerid){ //if toggler div defined
			setting.$togglerdiv=$("#"+setting.togglerid+", ."+setting.togglerid); // add togglerid as a class to allow multiple cotrol divs
			setting.$prev=setting.$togglerdiv.find('.prev').data('action', 'prev')
			setting.$next=setting.$togglerdiv.find('.next').data('action', 'next')
			setting.$status=setting.$togglerdiv.find('.status')
			this.extrabuttons($); //invoke the extra buttons function
		}
		var show = this.showslide;
		this.showslide = function(keyword){ // extend native showslide function to provide an onbeforeslide callback function
			var lng = setting.imagearray.length, idx = keyword === 'next'? (setting.curimage + 1) % lng : keyword === 'prev'?
				(setting.curimage - 1 + setting.imagearray.length) % lng : keyword, didx;
			 if(setting.displaymode.type !== "auto" || (!setting.ismouseover && setting.currentstep <= setting.totalsteps)){
				if (setting.onbeforeslide){
					try{setting.onbeforeslide.call(this, idx);}catch(e){
						alert('Fade In Slideshow error: An error has occured somwhere in your code attached to the "onbeforeslide" event: ' + e);
					}
				}
				if (setting.extrabuttons.navfade && setting.$nav.size()){
					setting.$nav.removeClass('navfadestyleactive').each(function(i){
						if((didx = this.getAttribute('data-index')) && didx == idx){
							this.className += ' navfadestyleactive';
						} else if(!didx && i % lng === idx){
							this.className += ' navfadestyleactive';
						}
					});
				}
			}
			show.call(this, keyword);
		};
	},
	extrabuttons: function($){
		var eb = this.setting.extrabuttons = this.setting.extrabuttons || {},
		pend = eb.pend, pause = eb.pause, slideshow = this, setting = this.setting, dm = setting.displaymode,
		ims = setting.imagearray.length, lastdm, navaction = eb.navaction === 'mouseenter'? 'mouseenter' : 'click',
		$tog = setting.$togglerdiv, $nav, $buts, pauseplay, $stopbut, $gobut, navbut, i = 0,
		nav = function(num){
			if(pause && (dm.type === 'auto' || lastdm === 'auto')){ // if an extrabuttons.pause value is set later resume if show was in auto mode
				clearTimeout(setting.playtimer);
				clearTimeout(slideshow.resumetimer);
				lastdm = 'auto';
				slideshow.resumetimer = setTimeout(function(){setting.playtimer = setTimeout(resume, pause);}, setting.fadeduration + 100);
			} else if (!pause && dm.type === 'auto'){ // else if combo pause/play button toggle its appearance
				pauseplayswitch('paused');
				lastdm = 'manual';
			}
			slideshow.navigate(num); // navigate to the slide
		},
		stop = function(e){ // function for stop button
			e.preventDefault();
			slideshow.navigate(setting.imgindex);
			lastdm = 'manual';
			pauseplayswitch('paused'); // if combo pause/play button toggle its appearance
		},
		resume = function(e){
			if(e && e.preventDefault){e.preventDefault();}
			if(dm.type !== 'auto'){
				dm.type = 'auto';
				slideshow.showslide('next');
				pauseplayswitch('running');
			}
		},
		pauseplayfunc = function(e){
			e.preventDefault();
			clearTimeout(setting.playtimer);
			if($(this).hasClass('running')){
				stop(e);
			} else {
				resume();
			}
		},
		executeonpauseplayswitch = function(){
			if(pauseplay && setting.onpauseplayswitch){
				try{setting.onpauseplayswitch.call(slideshow, setting.$pauseplay);}catch(e){
					alert('Fade In Slideshow error: An error has occured somwhere in your code attached to the "onpauseplayswitch" event: ' + e);
				}
			}
		},
		pauseplayswitch = function(state){
			var cstate = state === 'paused'? 'running' : 'paused';
			if(eb.pauseplay && $tog.find('.pauseplay').hasClass(cstate)){
				$tog.find('.pauseplay').each(function(){$(this).replaceWith(pauseplay[state].clone().remove());});
				setting.$pauseplay = $tog.find('.pauseplay').unbind('click', pauseplayfunc).click(pauseplayfunc);
				executeonpauseplayswitch();
			}
		};
		if(pend){
			if(eb.pauseplay && !eb.navbutonly){
				pauseplay = {running: $(setting.running || '<input type="button" value="Pause" />').addClass('pauseplay running'),
					paused: $(setting.paused || '<input type="button" value="Play" />').addClass('pauseplay paused')};
			} else if (!eb.navbutonly){
				$stopbut = dm.type === 'auto' || dm.wraparound? $(setting.stopbut || '<input type="button" value="Stop" />').addClass('stop') : '';
				$gobut = dm.type === 'auto' || dm.wraparound? $(setting.gobut || '<input type="button" value="Go" />').addClass('resume') : '';
			}
			navbut = setting.navbut || '<input type="button" value="%c" />';
			$buts = $('<span class="extrabutspan"></span>').append(eb.navbutonly? '' : eb.pauseplay? (dm.type === 'auto'? pauseplay.running : pauseplay.paused) : $stopbut);
			for(i; i < ims; ++i){
				$buts.append(function(){
					return $(navbut.replace(/(%i)|(%c)/g, function(m){
						return m === '%c'? i + 1 : i;
					})).addClass('navbut');
				});
			}
			$buts.append(eb.pauseplay || eb.navbutonly? '' : $gobut);
		}
		if ($tog.find('.pauseplay.paused').size() && $tog.find('.pauseplay.running').size()){
			eb.pauseplay = true;
			pauseplay = {running: $tog.find('.pauseplay.running').eq(0).clone(),
			paused: $tog.find('.pauseplay.paused').eq(0).clone()};
			pauseplayswitch(dm.type === 'auto'? 'running' : 'paused');
		}
		$nav = setting.$nav = (/^(append)|(prepend)$/.test(pend)? $tog[pend]($buts) : pend? $tog.find(pend).append($buts).end() : $tog)
			.find('.stop').click(stop).end()
			.find('.resume').click(resume).end()
			.find('.pauseplay').click(pauseplayfunc).end()
			.find('.navbut')[navaction](function(e){
				e.preventDefault();
				nav(this.getAttribute('data-index') || ($nav.index(this) % ims));
		});
		setting.$stopbut = $tog.find('.stop');
		setting.$gobut = $tog.find('.resume');
		setting.$pauseplay = $tog.find('.pauseplay');
		if(typeof eb.navfade === 'number'){
			if(!fadeSlideShow.navfadestyle){
				fadeSlideShow.navfadestyle = '<style type="text/css">.navfadestyleactive {opacity: 1 !important;}</style>'
				/*@cc_on @*//*@if(@_jscript_version >= 5)
					if($.browser.version < 9){
						fadeSlideShow.navfadestyle = '<style type="text/css">.navfadestyleactive {filter:progid:DXImageTransform.Microsoft.Alpha(opacity=100) !important;}</style>'
					}
				@end @*/
				$('head').append(fadeSlideShow.navfadestyle);
			}
			$nav.css({opacity: eb.navfade}).mouseenter(function(){
				$nav.stop(true, true).animate({opacity: eb.navfade});
				$(this).stop(true, true).animate({opacity: 1})
			}).mouseleave(function(){
				$(this).stop(true, true).animate({opacity: eb.navfade})
			});
		}
		setting.$prev.add(setting.$next).click(function(e){ //assign click behavior to prev and next controls
			e.preventDefault();
			var $target = $(this);
			if(eb.nextprevresume !== false){
				nav($target.data('action'));
			} else {
				if(dm.type === 'auto' || lastdm === 'auto'){
					pauseplayswitch('paused');
				}
				slideshow.navigate($target.data('action'));
				lastdm = 'manual';
			}
		});
		executeonpauseplayswitch();
	}
});
