/*
	Generic Cycler Plugin
	Author: Tang Web Studio
*/

(function($){

//
//
//

	$.fn.gencycle = function(params){
	
		// give the genopts their default value
		genopts = $.extend({
			prevClass: '#slideNav .prev',
			nextClass: '#slideNav .next',
			speed: '0',
			slides:null,
			current: null,
			newSlide: null,
			simulFunction: null,
			callback: null
		}, params);
		
		
		// plugin actions. Most code should go here.
		this.each(function(){ 
			 
			$obj = $(this);
			
			$children = $obj.children(); 
			
			genopts.slides = $children.get();
			
			total = genopts.slides.length - 1;
			
			
			if(genopts.newItem == null){
				genopts.newItem = 0;
				change();
			}
			
			function change(){
				if(genopts.current != null){
				
					$(genopts.slides[genopts.current]).animate( { opacity:"hide" }, genopts.speed, function(){
						$(genopts.slides[genopts.newItem]).animate( { opacity:"show" }, genopts.speed, function(){
							if( typeof(genopts.callback) == 'function' ){
								genopts.callback();
							}
						} );
					} );
					
					genopts.current = genopts.newItem;
					
				}else{
				
					$(genopts.slides[genopts.newItem]).animate( { opacity:"show" }, genopts.speed, function(){
						if( typeof(genopts.callback) == 'function' ){
							genopts.callback();
						}
					} );
					genopts.current = genopts.newItem;
					
				}
				
				if( typeof(genopts.simulFunction) == 'function' ){
					genopts.simulFunction();
				}			
				
			}
			
			if(total < 1){
				$(genopts.prevClass + ', ' + genopts.nextClass).hide();
				return this;
			}
			
			$(genopts.nextClass).click(function(){
				if( genopts.current >= total ){
					genopts.newItem = 0;
					change();
				}else{
					genopts.newItem = genopts.current + 1;
					change();
				}
				
				return false;
			});
			
			$(genopts.prevClass).click(function(){
				if( genopts.current <= 0 ){
					genopts.newItem = total;
					change();
				}else{
					genopts.newItem = genopts.current - 1;
					change();
				}
				
				return false;
			});
			
		});
		
		return this; // keeps jquery chaining going
	
	}
	
})(jQuery);
