/*

Title:		jShowOff: a jQuery Content Rotator Plugin
Author:		Erik Kallevig + Dragos Mandache
Version:	0.1.2
Website:	http://ekallevig.com/jshowoff
License: 	Dual licensed under the MIT and GPL licenses.

jShowOff Options

animatePause :		whether to use 'Pause' animation text when pausing [boolean, defaults to true]
autoPlay :			whether to start playing immediately [boolean, defaults to true]
changeSpeed :		speed of transition [integer, milliseconds, defaults to 600]
controls :			whether to create & display controls (Play/Pause, Previous, Next) [boolean, defaults to true]
controlText :		custom text for controls [object, 'play', 'pause', 'previous' and 'next' properties]
cssClass :			custom class to add to .jshowoff wrapper [string]
effect :			transition effect [string: 'fade', 'slideLeft' or 'none', defaults to 'fade']
hoverPause :		whether to pause on hover [boolean, defaults to true]
links :				whether to create & display numeric links to each slide [boolean, defaults to true]
speed :				time each slide is shown [integer, milliseconds, defaults to 3000]

*/

var Rotator = {

	// is the rotator in mid-transition
	isBusy: function ($cont) {
		return $cont.children().length > 1 ? true : false;
	},
	
	// load a specific slide
	goToAndPause: function (index, gallery, config, counter, $cont, uniqueClass, id, useCachedIndex, direction) {
		$cont.children().stop(true, true);
		counter = this.instances[id] ? this.instances[id]._counter : counter;
		if (useCachedIndex == true && this.instances[id]) {
			index = this.instances[id]._index;
		}	
		if (direction) {
			index = eval(index + direction);
			if (index >= gallery.length && direction == '+1') {
				index = 0;
			}
			if (index < 0 && direction == '-1') {
				index = gallery.length - 1;
			}
			
		}
		if ((counter != index) || ((counter == index) && this.isBusy($cont))) {
			if (this.isBusy($cont)) $cont.children().eq(0).remove();
			counter = this.transitionTo(gallery, index, config, counter, $cont, uniqueClass, id);
			//pause();
		};
		return counter;
	},	
				
	// utility for loading slides
	transitionTo: function (gallery, index, config, counter, $cont, uniqueClass, id) {
		var oldCounter = counter;
		if ((counter >= gallery.length) || (index >= gallery.length)) { counter = 0; }
		else if((counter < 0) || (index < 0)) { counter = gallery.length-1; }
		else { counter = index; }
		if (config.effect=='slideLeft'){
			var newSlideDir, oldSlideDir;
			function slideDir(dir) {
				newSlideDir = dir=='right' ? 'left' : 'right';
				oldSlideDir = dir=='left' ? 'left' : 'right';					
			}
			counter >= oldCounter ? slideDir('left') : slideDir('right') ;
	
			$(gallery[counter]).clone().appendTo($cont).slideIt({direction:newSlideDir,changeSpeed:config.changeSpeed});
			if($cont.children().length>1){
				$cont.children().eq(0).css('position','absolute').slideIt({direction:oldSlideDir,showHide:'hide',changeSpeed:config.changeSpeed},function(){$(this).remove();});
			};
		} else if (config.effect=='fade') {
			$(gallery[counter]).clone().appendTo($cont).hide().fadeIn(config.changeSpeed,function(){if($.browser.msie)this.style.removeAttribute('filter');});
			if($cont.children().length>1){
				$cont.children().eq(0).css({
					'position': 'absolute',
					'width': $cont.width()+'px'
				}).fadeOut(config.changeSpeed,function(){$(this).remove();});
			};
		} else if (config.effect=='none') {
			$(gallery[counter]).clone().appendTo($cont);
			if($cont.children().length>1){
				$cont.children().eq(0).css('position','absolute').remove();
			};
		}
		
		// update active class on slide link
		if (config.links){
			$('.'+uniqueClass+'-active').removeClass(uniqueClass+'-active jshowoff-active');
			$('.'+uniqueClass+'-slidelinks '+config.elementType).eq(counter).addClass(uniqueClass+'-active jshowoff-active');
		}
		
		// update tabs
		if (config.tabs) {
			$(config.tabParentSelector).removeClass('selected');
			$(config.tabList[index]).addClass('selected');
		}
		
		if (!this.instances[id]) {
			this.instances[id] = {
				_gallery: gallery,
				_config: config, 
				_counter: counter,
				_cont: $cont, 
				_uniqueClass: uniqueClass
			};
		} else {
			this.instances[id]._counter = counter;
			this.instances[id]._index = index;
		}
		return counter;
	},
	_transitionTo: function(id, step) {
		if (this.instances[id]) {
			var o = this.instances[id];
			var _c = this.goToAndPause(
				step,
				o._gallery,
				o._config,
				o._counter,
				o._cont,
				o._uniqueClass,
				id,
				false,
				null
			);
		} else {
			alert('[Rotator::_transitionTo] Invalid Instance ID: #' + id);
		}
	},
	addInstance: function(id, o) {
		this.instances[id] = o;
	},
	getInstance: function(id) {
		return Rotat.instances[id];
	},
	instances: {}
};

(function($) {


	$.fn.jshowoff = function(settings) {

		// default global vars
		var config = {
			animatePause : true,
			autoPlay : true,
			changeSpeed : 600,
			controls : true,
			controlText : {
				play :		'Play',
				pause :		'Pause',
				next :		'Next',
				previous :	'Previous'
			},
			effect : 'fade',
			hoverPause : true,
			links : true,
			elementType: 'a',
			speed : 3000,
			tabs: false,
			tabParentSelector: '',
			tabList: []
		};
		
		// merge default global variables with custom variables, modifying 'config'
		if (settings) $.extend(true, config, settings);

		// make sure speed is at least 20ms longer than changeSpeed
		if (config.speed < (config.changeSpeed+20)) {
			alert('jShowOff: Make speed at least 20ms longer than changeSpeed; the fades aren\'t always right on time.');
			return this;
		};
		
		// create slideshow for each matching element invoked by .jshowoff()
		this.each(function(i) {
			
			// declare instance variables
			var $cont = $(this);
			
			var gallery = $(this).children().remove();
			var timer = '';
			var counter = 0;
			var preloadedImg = [];
			var howManyInstances = $('.jshowoff').length+1;
			var uniqueClass = 'jshowoff-'+howManyInstances;
			var cssClass = config.cssClass != undefined ? config.cssClass : '';
			var id = $cont.attr('id');
			
			// set up wrapper
			if($.browser.msie && parseInt($.browser.version, 10) == 7) {
				$cont.wrap('<div class="jshowoff '+uniqueClass+'" />');
				var $wrap = $('.'+uniqueClass);
				$wrap.addClass(cssClass);
			} else {
				$cont.css('position','relative').wrap('<div class="jshowoff '+uniqueClass+'" />');
				var $wrap = $('.'+uniqueClass);
				$wrap.css('position','relative').addClass(cssClass);
			}

			// add first slide to wrapper
			$(gallery[0]).clone().appendTo($cont);
			
			// preload slide images into memory
			preloadImg();
			
			// add controls
			if(config.controls){
				addControls();
				if(config.autoPlay==false){
					$('.'+uniqueClass+'-play').addClass(uniqueClass+'-paused jshowoff-paused').text(config.controlText.play);
				};
			};
			
			// add slide links
			if(config.links){
				addSlideLinks();
				$('.'+uniqueClass+'-slidelinks '+config.elementType).eq(0).addClass(uniqueClass+'-active jshowoff-active');
			};
			
			// pause slide rotation on hover
			if(config.hoverPause){ $cont.hover(
				function(){ if(isPlaying()) pause('hover'); },
				function(){ if(isPlaying()) play('hover'); }
			);};
			
			// determine autoPlay
			if(config.autoPlay && gallery.length>1) {
				timer = setInterval( function(){ play(); }, config.speed );
			};
			
			// display error message if no slides present
			if(gallery.length<1){
				$('.'+uniqueClass).append('<p>For jShowOff to work, the container element must have child elements.</p>');
			};

			// is the rotator currently in 'play' mode
			function isPlaying(){
				return $('.'+uniqueClass+'-play').hasClass('jshowoff-paused') ? false : true;
			};
			
			// start slide rotation on specified interval
			function play(src) {
				if(!Rotator.isBusy($cont)){
					counter++;
					counter = Rotator.transitionTo(gallery,counter, config, counter, $cont, uniqueClass, id);
					if(src=='hover' || !isPlaying()) {
						timer = setInterval(function(){ play(); },config.speed);
					}
					if(!isPlaying()){
						$('.'+uniqueClass+'-play').text(config.controlText.pause).removeClass('jshowoff-paused '+uniqueClass+'-paused');
					}
				};
			};
			
			// stop slide rotation
			function pause(src) {
				clearInterval(timer);
				if(!src || src=='playBtn') $('.'+uniqueClass+'-play').text(config.controlText.play).addClass('jshowoff-paused '+uniqueClass+'-paused');
				if(config.animatePause && src=='playBtn'){
					$('<p class="'+uniqueClass+'-pausetext jshowoff-pausetext">'+config.controlText.pause+'</p>').css({ fontSize:'62%', textAlign:'center', position:'absolute', top:'40%', lineHeight:'100%', width:'100%' }).appendTo($wrap).addClass(uniqueClass+'pauseText').animate({ fontSize:'600%', top:'30%', opacity:0 }, {duration:500,complete:function(){$(this).remove();}});
				}
			};
			
			// load the next slide
			function next() {
				Rotator.goToAndPause(counter+1, gallery, config, counter, $cont, uniqueClass, id, true, '+1');
			};
		
			// load the previous slide
			function previous() {
				Rotator.goToAndPause(counter-1, gallery, config, counter, $cont, uniqueClass, id, true, '-1');
			};
			
			// load images into memory
			function preloadImg() {
				$(gallery).each(function(i){
					$(this).find('img').each(function(i){
						preloadedImg[i] = $('<img>').attr('src',$(this).attr('src'));					
					});
				});
			};
			
			// generate and add play/pause, prev, next controls
			function addControls() {
				$wrap.append('<p class="jshowoff-controls '+uniqueClass+'-controls"><a class="jshowoff-play '+uniqueClass+'-play" href="#null">'+config.controlText.pause+'</a> <a class="jshowoff-prev '+uniqueClass+'-prev" href="#null">'+config.controlText.previous+'</a> <a class="jshowoff-next '+uniqueClass+'-next" href="#null">'+config.controlText.next+'</a></p>');
				$('.'+uniqueClass+'-controls a').each(function(){
						if($(this).hasClass('jshowoff-play')) $(this).click(function(){ isPlaying() ? pause('playBtn') : play(); return false; } );
						if($(this).hasClass('jshowoff-prev')) $(this).click(function(){ previous(); return false; });
						if($(this).hasClass('jshowoff-next')) $(this).click(function(){ next(); return false; });
	
				});
			};	

			// generate and add slide links
			function addSlideLinks() {
				$wrap.append('<div class="jshowoff-slidelinks '+uniqueClass+'-slidelinks"></div>');
				$.each(gallery, function(i, val) {
					var linktext = $(this).attr('title') != '' ? $(this).attr('title') : i+1;
					if (config.cssClass == 'bulletList') {
						var el = $('<div class="bullet '+ uniqueClass +'"></div>');
					} else {
						var el = $('<a class="jshowoff-slidelink-'+i+' '+uniqueClass+'-slidelink-'+i+'" href="#null">'+linktext+'</a>');
					}
					var storage = {
						index:i,
						g:gallery,
						cfg:config,
						c:counter,
						ctrl:$cont,
						css:uniqueClass,
						rotatorInstance:id
					};
					el.bind('click', storage, function(e){
						//Rotator._transitionTo(e.data.rotatorInstance, i);
						Rotator.goToAndPause(e.data.index, e.data.g, e.data.cfg, e.data.c, e.data.ctrl, e.data.css, e.data.rotatorInstance, false, null);
						return false; 
					}).appendTo('.'+uniqueClass+'-slidelinks');	
				});
			};		
	
			var rotatorInstance = {
				_gallery: gallery,
				_config: config, 
				_counter: counter,
				_index: 0,
				_cont: $cont, 
				_uniqueClass: uniqueClass
			};
			Rotator.addInstance(id, rotatorInstance);
	
		// end .each
		});
	
		return this;

	// end .jshowoff
	};

// end closure
})(jQuery);




(function($) {

	$.fn.slideIt = function(settings,callback) {
	
		// default global vars
		var config = {
			direction : 'left',
			showHide : 'show',
			changeSpeed : 600
		};
		
		// merge default global variables with custom variables, modifying 'config'
		if (settings) $.extend(config, settings);
		
		this.each(function(i) {	
			$(this).css({left:'auto',right:'auto',top:'auto',bottom:'auto'});
			var measurement = (config.direction == 'left') || (config.direction == 'right') ? $(this).outerWidth() : $(this).outerHeight();
			var startStyle = {};
			startStyle['position'] = $(this).css('position') == 'static' ? 'relative' : $(this).css('position');
			startStyle[config.direction] = (config.showHide == 'show') ? '-'+measurement+'px' : 0;
			var endStyle = {};
			endStyle[config.direction] = config.showHide == 'show' ? 0 : '-'+measurement+'px';
			$(this).css(startStyle).animate(endStyle,config.changeSpeed,callback);
		// end .each
		});
	
		return this;
		
	// end .slideIt
	};

// end closure
})(jQuery);
