/*
 * 	sohtanaka - jQuery plugin
 *	written by Soh Tanaka	
 *	http://www.sohtanaka.com/web-design/automatic-image-slider-w-css-jquery/
 *
 */
if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			sohtanaka: function(options) {
			    var container = '#'+$(this).attr('id');
				var settings = $.extend({}, $.fn.sohtanaka.defaults, options);
				if(settings.buildPagination == true) {settings.showPagination = true;}
			    var play = null;			    
	            var imageSum = $(container + " "+settings.slide_container + "  img").size();
	            
			    if(settings.buildPagination == true) {
			        var pagination_container = $("<div class='"+settings.pagination.replace('.','')+"'>");
			        for(var i = 0; i < imageSum; i++ ) {
			            var page = i + 1;
			            pagination_container.append($("<a href='#' rel='"+page+"'>"+page+"</a>"));
			        }
			        $(container).append(pagination_container);
			    }
			    
	            //Set Default State of each portfolio piece
	            if(settings.showPagination == true) {
	                $(container + " " + settings.pagination + "").show();
	                $(container + " " + settings.pagination + " a:first").addClass("active");
	            } else {
	                $(container + " " + settings.pagination + "").hide();
	            }
            		
	            //Get size of images, how many there are, then determin the size of the image reel.
	            var imageWidth = $(container + " .window").width();
	            var imageReelWidth = imageWidth * imageSum;
            	
	            //Adjust the image reel to its new size
	            $(container + " "+settings.slide_container + " ").css({'width' : imageReelWidth});
            	
	            //Paging + Slider Function
	            rotate = function(){	
		            var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		            var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
             
		            $(container + " " + settings.pagination + " a").removeClass('active'); //Remove all active class
		            $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
            		
		            //Slider Animation
		            $(container + " "+settings.slide_container + " ").animate({ 
			            left: -image_reelPosition,
			            opacity: 0
		            }, settings.slidespeed);
		            
		            $(container + " "+settings.slide_container + " ").animate({
			            opacity: 100
		            }, 0 );
		            $('.search_box').html("as: "+settings.autoStart+"; ss: "+settings.slidespeed);
	            }; 
	                        	
	            //Rotation + Timing Event
	            rotateSwitch = function(){	
		            $active = $(container+' ' + settings.pagination + ' a.active').next();
		            if ( $active.length === 0) { //If paging reaches the end...
			            $active = $(container+' ' + settings.pagination + ' a:first'); //go back to first
		            }	
            	    if (settings.autoStart > 0) {
		                play = setInterval(function(){ //Set timer
		                    $active = $(container+' ' + settings.pagination + ' a.active').next();
		                    if ( $active.length === 0) { //If paging reaches the end...
			                    $active = $(container+' ' + settings.pagination + ' a:first'); //go back to first
			                }
			                rotate(); //Trigger the paging and slider function
		                }, settings.autoStart); //Timer speed in milliseconds 
		            }
	            };
	            rotateSwitch(); //Run function on launch
	            
            	
	            if(settings.pauseOnHover == true) { //On Hover
	                $(container + " "+settings.slide_container + "  a").hover(function() {
		                clearInterval(play); //Stop the rotation
	                }, function() {
		                rotateSwitch(); //Resume rotation
	                });	
	            }
            	
	            //On Click
	            $(container + " " + settings.pagination + " a").click(function() {	
		            $active = $(this); //Activate the clicked paging
		            //Reset Timer
		            clearInterval(play); //Stop the rotation
		            rotate(); //Trigger rotation immediately
		            if(settings.restart == true) {
		                rotateSwitch(); //Resume rotation
		            }
		            return false; //Prevent browser jump to link anchor
	            });	
			}
		});
		$.fn.sohtanaka.defaults = {
			slide_container: ".image_reel", //Class/id of slide container. You can use "#slides" for an id.
			pagination: ".paging", //Class name of parent ul for numbered links. Don't add a "." here.
			autoStart: 2000, //Set to positive number for true. This number will be the time between transitions.
			restart: false, //
			slidespeed: 300, //Speed of slide animation, 1000 = 1second.
			showPagination: false, //Add pagination links based on content? true/false
			pauseOnHover: true, //Add pagination links based on content? true/false
			buildPagination: false
		};
	});
}
