window.log = function(){
  log.history = log.history || [];  
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});


(function($){
	$.fn.slidey = function(options) {
	
		var settings = {};	
		
		var defaults = {
			rotationSpeed: 	3000,		// The speed of the image rotation
			animSpeed:		500,		// The speed of the transitions
			child: 			'img',		// The type of element which we will be rotating
			controls: 		false,		// Does the slider have controls?
			centerControls:	false,		// Vertically center the controls?
			nextText:		'',			// Text to use for next button
			prevText:		'',			// Text to use for previous button
			hoverPause: 	false,		// Does the slide pause on hover?
			showPosition:	false,		// Do we want positional indicators?
			centerMarkers:	true		// Horizontally center the markers?
		}
		
		// Overwrite the defaults with the provided options (if any)
		settings = $.extend({}, defaults, options);
		
		// jQuery vars
		var	$slider = this,									// The slider parent
			$banners = $slider.children(settings.child), 	// The slides
			$position,										// Marker wrapper
			$markers,										// Markers
			$next,											// Next button
			$previous										// Previous button
		
		// non-jQuery vars
		var paused = false,					// Is the slider currently paused?
			fwd = 0,						// Value indicating next
			back = -1,						// Value indicating previous
			current_slide = 0,				// The currently active slide
			marker_pos = 0,					// The currently active marker
			slide_count = $banners.length, 	// The total number of slides
			first = 0,						// The First slide in the array
			last = slide_count-1,			// The last slide in the array
			next = slide_count,				// value for next
			prev = -1						// value for previous
		
		
		//extra stuff for project text
		var $projects = $('#projects').children('div');
		console.log($projects);
		
		$($banners[0]).show().css('opacity',1);
		$($projects[0]).show().css('opacity',1);
		
		// Hook me with some positional indicators!	
		if(settings.showPosition){
		
			// Create the parent element and append it to the slider
			$position = $('<ul class="position"></ul>');	
			$position.appendTo($slider);
			
			$.each($banners,function(key,value){
				key++
				$('<li><a href="#">'+key+'</a></li>').appendTo($position);
			});

			// Gather all the markers
			$markers = $position.children('li');
		
			// Iterate over the markers and bind a click event to each one
			$.each($markers, function(key,value){
				$(value).click(function(e){
					e.preventDefault();
					slideyGo(key);
				});
			})

			// Horizontally center the markers
			if(settings.centerMarkers){
				offset = ($slider.width() - $position.innerWidth() )/ 2;
				$position.css('left', offset);
			}

			$($markers[first]).addClass('active');
		}

		// Hook up the controls!
		if(settings.controls){
			
			// Create the control elements and append them to the slider
			$next = $('<a href="#" id="slidey-next" class="slidey-controls">'+settings.nextText+'</a>');
			$previous = $('<a href="#" id="slidey-prev" class="slidey-controls">'+settings.prevText+'</a>');

			$next.appendTo($slider);
			$previous.appendTo($slider);
			
			// Bind click events to the controllers
			$next.click(function(e){
				e.preventDefault();
				slideyGo(next);
			});
			
			$previous.click(function(e){
				e.preventDefault();
				slideyGo(prev);
			});
			
			// Vertically center the controllers
			if(settings.centerControls){
				offset = ($slider.height() - $('#slidey-next').innerHeight()) / 2;
				$next.css('top', offset);
				$previous.css('top', offset);
			}
	
		}
		
		//If pause on hover is enabled, we need to hook that shit up!
		if(settings.hoverPause){
			
			$slider.hover(function(){
				
				if(!paused){
					clearInterval(slideyInterval);
					paused=true;
				}
				
			},function(){
				
				if(paused){
					slideyInterval = setInterval(function(){ slideyGo(next) }, settings.rotationSpeed);
					paused=false;
				}
				
			});
			
		}
		
		//PARTY TIME
		var slideyInterval = setInterval(function(){ slideyGo(next) }, settings.rotationSpeed);
		
		
		var slideyGo = function(move){
			
			if(move != current_slide){
			
				$($banners[current_slide]).animate({opacity: 0.0}, settings.animSpeed, function(){
					$(this).hide();
				});
				
				$($projects[current_slide]).animate({opacity: 0.0}, settings.animSpeed, function(){
					$(this).hide();
				});
	
				if (move == next){
					
					// Forza, Slidey!
					
					if(current_slide == last){
						$($banners[first]).show().animate({opacity: 1.0}, settings.animSpeed);
						$($projects[first]).show().animate({opacity: 1.0}, settings.animSpeed);
						current_slide=first;
					}
					else{
						$($banners[current_slide+1]).show().animate({opacity: 1.0}, settings.animSpeed);
						$($projects[current_slide+1]).show().animate({opacity: 1.0}, settings.animSpeed);
						current_slide++;
					}
					
				} else if (move == prev){
					
					// Back slidey! Go Back!
					
					if(current_slide == first){
						$($banners[last]).show().animate({opacity: 1.0}, settings.animSpeed);
						$($projects[last]).show().animate({opacity: 1.0}, settings.animSpeed);
						current_slide=last;
					}
					else{
						$($banners[current_slide-1]).show().animate({opacity: 1.0}, settings.animSpeed);
						$($projects[current_slide-1]).show().animate({opacity: 1.0}, settings.animSpeed);
						current_slide--;
					}
					
				} else {
					
					// Jump around Slidey! Jump up, jump up and get down!
					$($banners[move]).show().animate({opacity: 1.0}, settings.animSpeed);
					$($projects[move]).show().animate({opacity: 1.0}, settings.animSpeed);
					current_slide=move;
					
				}
				
				// update the markers
				if(settings.showPosition){
					$markers.removeClass('active');
					$($markers[current_slide]).addClass('active');
				}
				
			}
			
		}
		
		return this;
	
	}
})(jQuery);

