/**
 * Slider Function
 *
 * @author Wen Cano
 * @url http://www.wencano.com
 * @copyright Dec 2010
 */

jQuery.fn.wenSlider = function(settings) {
    settings = jQuery.extend({
        newsWidth: 74,
		showItems: 1,
		itemWidth: 74,
		autoStart: 3000,
        newsSpeed: "normal",
		nextbutton: ".next",
		prevbutton: ".prev",
		container: ".container",
		effect: "slide"
    }, settings);
    return this.each(function(i){     
        thisParent = jQuery(this);
        itemLength = jQuery(settings.container, thisParent).children().size();
		newsContainerWidth = 0;
		jQuery(settings.container, thisParent).children().each(function(){
			newsContainerWidth += jQuery(this).outerWidth();
		});
		jQuery(settings.container, thisParent).css({width: newsContainerWidth, position: "absolute", left: 0});
        if (parseInt(jQuery(settings.container,thisParent).css("left")) == 0) {
            jQuery(settings.prevbutton,thisParent).css("opacity",0);
        }
		
        if ((parseInt(jQuery(settings.container,thisParent).width())) <= settings.newsWidth ) {
          jQuery(settings.nextbutton,thisParent).css("opacity",0);
        } else{
            jQuery(settings.nextbutton,this).css("opacity",1);
        }
        
        animating = false;
		
		if (settings.autoStart && itemLength > 1) {
			if(settings.effect == 'slide') {
				sliderIntervalID = setInterval(function(){
					animating = true;
					wenAnimate('next',true);
				}, settings.autoStart);
			} else if (settings.effect == 'fade') {
				jQuery(".slide", thisParent).css({
					position: 'absolute',
					opacity:	0
				});
				jQuery(settings.container + ' div.slide:first', thisParent).addClass('active').animate({"opacity": 1}, 2000);
				sliderIntervalID = setInterval( function() {
					slideSwitch(settings, this);
				}, settings.autoStart );
			}
			
		} else {
			animating = false;
		}
		
		animating2 = true;
		
        jQuery(settings.nextbutton,thisParent).click(function() {
			
            thisParent = jQuery(this).parent();
			
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(settings.container,thisParent).css("left")) - (settings.newsWidth * settings.showItems);
				
                if (animateLeft + parseInt(jQuery(settings.container,thisParent).css("width")) > 0) {
                    jQuery(settings.prevbutton,thisParent).animate({opacity: 1});
                    jQuery(settings.container, thisParent).animate({left: animateLeft}, settings.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if(parseInt(jQuery(settings.container,thisParent).css("left")) + parseInt(jQuery(settings.container,thisParent).css("width")) <= (settings.newsWidth * settings.showItems)) {
                          jQuery(settings.nextbutton,thisParent).animate({opacity: 0});
                          }
                        if(parseInt(jQuery(settings.container,thisParent).css("left")) == 0) {
                          jQuery(settings.prevbutton,thisParent).animate({opacity: 0});
                          }
                        animating = false;
						animating2 = false;
                    });
                } else {
                    animating = false;
					animating2 = false;
                }
                return false;
            }
        });
		
        jQuery(settings.prevbutton,thisParent).click(function() {
            thisParent = jQuery(this).parent();
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(settings.container,thisParent).css("left")) + (settings.newsWidth * settings.showItems);
                if ((animateLeft + parseInt(jQuery(settings.container,thisParent).css("width"))) <= parseInt(jQuery(settings.container, thisParent).css("width"))) {
                    jQuery(settings.nextbutton,thisParent).animate({opacity: 1});
                    jQuery(settings.container, thisParent).animate({left: animateLeft}, settings.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(settings.container,thisParent).css("left")) == 0) {
                            jQuery(settings.prevbutton,thisParent).animate({opacity: 0});
                        }
                        if (parseInt(jQuery(settings.container,thisParent).css("left")) + parseInt(jQuery(settings.container,thisParent).css("width")) <= (settings.newsWidth * settings.showItems)) {
                            jQuery(settings.nextbutton,thisParent).animate({opacity: 0});
                        }
                        animating = false;
						animating2 = false;
                    });
                } else {
                    animating = false;
					animating2 = false;
                }
                return false;
            }
        });
		
		function wenAnimate(dir, clicked) {
			switch(dir) {
				case 'next':
					if(animating2) {
						animateLeft = parseInt(jQuery(settings.container,thisParent).css("left")) - (settings.newsWidth * settings.showItems);
						if (animateLeft + parseInt(jQuery(settings.container,thisParent).css("width")) > 0) {
							jQuery(settings.prevbutton,thisParent).fadeIn();
							jQuery(settings.container, thisParent).animate({left: animateLeft}, settings.newsSpeed, function() {
								jQuery(this).css("left",animateLeft);
								if (parseInt(jQuery(settings.container,thisParent).css("left")) == 0) {
									jQuery(settings.prevbutton,thisParent).fadeOut();
								}
								if (parseInt(jQuery(settings.container,thisParent).css("left")) + parseInt(jQuery(settings.container,thisParent).css("width")) <= settings.newsWidth) {
									jQuery(settings.nextbutton,thisParent).fadeOut();
								}
							});
						} else {
							jQuery(settings.container, thisParent).animate({left: 0}, settings.newsSpeed, function() {
								jQuery(this).css("left",0);
								jQuery(settings.container,thisParent).css("left", 0);
								if(parseInt(jQuery(settings.container,thisParent).css("left")) + parseInt(jQuery(settings.container,thisParent).css("width")) <= settings.newsWidth ) {
								  jQuery(settings.nextbutton,thisParent).fadeOut();
								  }
								if(parseInt(jQuery(settings.container,thisParent).css("left")) == 0) {
								  jQuery(settings.prevbutton,thisParent).fadeOut();
								  jQuery(settings.nextbutton,thisParent).fadeIn();
								  }
							});
						}
					} 
					animating = false;
					break;
				default:
					break;
			}
		
		}
		
    });
}


function slideSwitch(settings, $this) {
	
	var $active = jQuery(settings.container + ' div.active');

	if ( $active.length == 0 ) $active = jQuery(settings.container + ' div.slide:last');

	// use this to pull the divs in the order they appear in the markup
	var $next =  $active.next().length ? $active.next()
		: jQuery(settings.container + ' div.slide:first');

	// uncomment below to pull the divs randomly
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );


	$active.addClass('last-active', jQuery($this));

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active').css("opacity", 0);
	});
}
