/*
 * jQuery Plugin: peepShow
 * http://www.roydukkey.com/
 *
 * Copyright (c) 2009 Rory Dueck
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Date: 2009-10-12 (Mon, 12 Oct 2009)
 * Version: 0.0.3a
 *
 * NOTE: Please report any improvements to roydukkey-at-gmail-dot-com.
 *       There are still many improvements that can me made to this
 *       script. Thanks to all in the open community.
 */
(function($){
// Do CAST all VARS

$.fn.peepShow = function(s) {

	o = $.extend(true,{
		width: 0,
		height: 0,
		order: "numeric",
		orderBy: "",
		animation: {
			type: "fade",
			delay: 5000,
			speed: 700,
			overlay: false
		},
		controls:	{
			numeric: 0,
			next: false,
			play: false,
			stop: false,
			prev: false
		}
	},s);

	// Set Width and Height
	$(this).css({
		width: o.width,
		height: o.height,
		position: "relative",
		overflow: "hidden"
	});

	return this.each(function() {
		var ps, s, uB, aS=0, i, g;
		ps = $.fn.peepShow;
		uB = ( s = ( g = $(this) ).find(".slide") ).length;
		
		// Add Class
		this.className += (this.className ? " " : "") + "peepShow pShw"+ps.luid;
		
		// Create Index of Slides
		for( i=0; i<uB; i++ ) ps.index[ps.luid][0].push(i);
		
		// Set Order of Slides in Index
		switch(o.order) {
			// Radomize
			case "random":
				ps.index[ps.luid][0].sort( function(){return 0.5 - Math.random()} ); break;
			// Specific Order
			case "specific":
				if(o.orderBy) {
					ps.index[ps.luid][0]=[];
					for( i in o.orderBy = o.orderBy.replace(" ","").split(",") ) ps.index[ps.luid][0][i] = o.orderBy[i]-1;
					uB = ps.index[ps.luid][0].length;
				}
				break;
		}
		
		// Add CSS to Slides
		s.css({
			position: "absolute",
			top: 0,
			left: 0
		})
		// Show First Slide
		.eq( ps.index[ps.luid][0][aS++] ).show();
		
		if( uB > 1 ) {
			// Set all other Index values
			ps.index[ps.luid][1].push(
				o.animation.delay,																																			// 0
				o.animation.speed,																																			// 1
				o.animation.type,																																				// 2
				o.animation.overlay,																																		// 3
				o.controls.numeric>uB ? uB : o.controls.numeric,																				// 4
				o.controls.next,																																				// 5
				o.controls.play,																																				// 6
				o.controls.stop,																																				// 7
				o.controls.prev,																																				// 8
				uB,																																											// 9
				setTimeout( "$.fn.peepShow.step("+ps.luid+","+aS+","+(aS+1)+")", o.animation.delay)			// 10
			);
			
			// Create Button Actions
			if(
				ps.index[ps.luid][1][4] > 0 ||
				ps.index[ps.luid][1][5] ||
				ps.index[ps.luid][1][6] ||
				ps.index[ps.luid][1][7] ||
				ps.index[ps.luid][1][8]
			) {
				g.append('<div class="controls"><ul></ul></div>');
				ps.updateBtn(ps.luid,aS);
			}
		}
		
		// Increment for next peepShow
		ps.luid++;
	});
	
}

$.extend($.fn.peepShow, {
	luid: 0,
	index: [[[],[],[]],[[],[],[]]], // [ luid ][ arrayIndex | timers ][ position ]
	set: function(i,a,d){
		var ps=$.fn.peepShow, a, d;
		ps.index[i][1][10] = setTimeout( "$.fn.peepShow.step("+i+","+a+","+(a==ps.index[i][1][9]?1:a+1)+")", d );
	},
	step: function(i,a,t){
		var ps=$.fn.peepShow;
		
		// If overlay=false then bellow will be positive number, else is set to zero
		if( ps.index[i][1][10] ) {
			
			var s=$(".pShw"+i+" .slide");
			clearTimeout( ps.index[i][1][10] );
			// If overlay=false set timer position to 0 for above evaluation
			if( !ps.index[i][1][3] ) ps.index[i][1][10]=0;
			
			switch( ps.index[i][1][2].toLowerCase() ){
				case "fade":
					s.eq( ps.index[i][0][a-1] ).fadeOut( ps.index[i][1][1] );
					s.eq( ps.index[i][0][t-1] ).fadeIn( ps.index[i][1][1], function(){
						// If overlay=true we can't do this, cause we'd be overwriting /// Line: A
						if( !ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]);
					});
					break;
					
				case "slideup":
					s.eq( ps.index[i][0][a-1] ).slideUp( ps.index[i][1][1] );
					s.eq( ps.index[i][0][t-1] ).css({
						top: "",
						bottom: 0
					}).slideDown( ps.index[i][1][1], function(){
						$(this).css({
							bottom: "",
							top: 0
						});
						if( !ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]);
					})
					break;
					
				case "slidedown":
					s.eq( ps.index[i][0][a-1] ).css({
						top: "",
						bottom: 0
					}).slideUp( ps.index[i][1][1], function(){
						$(this).css({
							bottom: "",
							top: 0
						});
						if( !ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]);
					})
					s.eq( ps.index[i][0][t-1] ).css({
						top: "-"+s.eq( ps.index[i][0][t-1] ).innerHeight()+"px",
						display: "block"
					}).animate({
						top: 0
					}, ps.index[i][1][1]);
					break;
					
				case "slideright":
					s.eq( ps.index[i][0][a-1] ).animate({
						left: s.eq( ps.index[i][0][a-1] ).innerWidth()+"px"
					}, ps.index[i][1][1], function(){
						this.style.display = "none";
						if( !ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]);
					});
					s.eq( ps.index[i][0][t-1] ).css({
						left: "-"+s.eq( ps.index[i][0][t-1] ).innerWidth()+"px",
						display: "block"
					}).animate({
						left: 0
					}, ps.index[i][1][1]);
					break;
					
				case "slideleft":
					s.eq( ps.index[i][0][a-1] ).animate({
						left: "-"+s.eq( ps.index[i][0][a-1] ).innerWidth()+"px"
					}, ps.index[i][1][1], function(){
						this.style.display = "none";
						if( !ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]);
					});
					s.eq( ps.index[i][0][t-1] ).css({
						left: s.eq( ps.index[i][0][t-1] ).innerWidth()+"px",
						display: "block"
					}).animate({
						left: 0
					}, ps.index[i][1][1]);
					break;
			} // end switch
			
			// If overlay=false then bellow will be positive number, else is set to zero
			if( ps.index[i][1][10] ) ps.set(i,t,ps.index[i][1][0]+ps.index[i][1][1]);  /// Line: A
			
			ps.updateBtn(i,t)
		}
	},
	stop: function(i){
		clearTimeout($.fn.peepShow.index[i][1][10])
	},
	updateBtn: function(i,a){
		var ps=$.fn.peepShow.index[i][1], o="", x, u;
		
		// Prev
		if(ps[8]) o += '<li class="btnPrev"><a href="javascript:$.fn.peepShow.step('+i+','+a+','+(a-1==0?ps[9]:a-1)+')"></a></li>';
		// Play
		if(ps[6]) o += '<li class="btnPlay"><a href="javascript:$.fn.peepShow.step('+i+','+a+','+(a==ps[9]?1:a+1)+')"></a></li>';
		// Stop
		if(ps[7]) o += '<li class="btnStop"><a href="javascript:$.fn.peepShow.stop('+i+')"></a></li>';
		// Numeric
		if( ps[4] > 0 ) {
			
			if( a-Math.round(ps[4]/2) < 0 ) {
				x = 0;
				u = ps[4];
			} else {
				if( a+Math.round(ps[4]/2) > ps[9] ) {
					x = ps[9]-ps[4];
					u = ps[9];
				} else {
					x = a-Math.round(ps[4]/2);
					u = a+Math.round(ps[4]/2)+(Math.round(ps[4]/2)!=ps[4]/2?-1:-0);
				}
			}
			for( ++x; x<=u; x++ ) {
				o += '<li class="btnNum'+x;
				if(x==a) {
					o += ' current"><a>'+x+'</a></li>';
				} else {
					o += '"><a href="javascript:$.fn.peepShow.step('+i+','+a+','+x+')">'+x+'</a></li>';
				}
			}
			
		}
		// Next
		if(ps[5]) o += '<li class="btnNext"><a href="javascript:$.fn.peepShow.step('+i+','+a+','+(a==ps[9]?1:a+1)+')"></a></li>';
		
		// DO remover events before removing elements????
		$(".pShw"+i+" > .controls > ul").html(o).find("li").hover(function(){
			this.className+=(this.className?" ":"")+"hover";
		}, function(){
			this.className=this.className.replace("hover","").replace(/^\s+|\s+$/g,"");
		});
	}
});	

})(jQuery);