/*

    ----------------------------------------------------------------------------------------------------
    Accessible News Slider
    ----------------------------------------------------------------------------------------------------

    Author:
    Brian Reindel

    Author URL:
    http://blog.reindel.com

    License:
    Unrestricted. This script is free for both personal and commercial use.

*/

jQuery.fn.accessNews = function( settings ) {
    settings = jQuery.extend({
        headline : "Top Stories",
        speed : "normal",
		disableAutoLoop : false,
		disableAutoStop : false,
		autoLoopInterval:5000,
        slideBy : 2
    }, settings);
    return this.each(function() {
        jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews.run = function( $this, settings ) {
    jQuery( ".javascript_css", $this ).css( "display", "none" );
    var ul = jQuery( "ul:eq(0)", $this );
    var li = jQuery("li",ul);

    var isVertical=false;
    if(settings.direction && settings.direction=='vertical'){
        isVertical=true;
    }

    if ( li.length > settings.slideBy ) {
        var $next = jQuery( ".next > a", $this );
        var $back = jQuery( ".back > a", $this );
		var heightOrWidth="width";
		var topOrLeft="left";
		var getDimension=function(element){
			return jQuery(element).width();
		};
        if(isVertical){
			heightOrWidth="height";
			topOrLeft="top";
			getDimension=function(element){
				return jQuery(element).height();
			};
        }
		var liWidth = getDimension(jQuery( li[0] ));
		ul.css( heightOrWidth, ( li.length * liWidth ) );

        var animating = false;
		
        $next.bind("slideNow",function() {
            if ( !animating ) {
                animating = true;
				offsetLeft = parseInt( ul.css( topOrLeft ) ) - ( liWidth * settings.slideBy );
				var ampleSlider = parseInt( ul.parent().css(heightOrWidth) );
				//alert("offsetLeft: "+offsetLeft+", ul: "+getDimension(ul)+", ample: "+ampleSlider);
				if(offsetLeft + getDimension(ul) < ampleSlider){
					offsetLeft=ampleSlider-getDimension(ul);
					jQuery.data($this,"autoLoopDirection",1);
				}
				if ( offsetLeft!=0 && (offsetLeft + getDimension(ul) >= ampleSlider )) {

					$back.parent().removeClass("fadeArrowBack" );
					$back.css( "display", "block" );

					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						var ampleSlider = parseInt( ul.parent().css(heightOrWidth) );
						if ( parseInt( ul.css( topOrLeft ) ) + getDimension(ul) <= ampleSlider ) {
							$next.parent().addClass("fadeArrowNext" );
						}
						animating = false;
					});				
				}else{					
					animating = false;
				}
            }

            return false;
        }).click(function(){
			$(this).trigger("slideNow");
			if(jQuery.data($this,"autoLoop") && !settings.disableAutoStop){
				clearInterval(jQuery.data($this,"autoLoopInterval"));
				jQuery.data($this,"autoLoop",false);
			}
			return false;
		});
		
        $back.bind("slideNow",function() {
            if ( !animating ) {
				animating = true;
				offsetRight = parseInt( ul.css( topOrLeft ) ) + ( liWidth * settings.slideBy );
				if(offsetRight>0){
					offsetRight=0;
					jQuery.data($this,"autoLoopDirection",0);
				}
				if ( offsetRight <= 0 ) {

					$next.parent().removeClass("fadeArrowNext");
					$next.css( "display", "block" );

					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( ul.css(topOrLeft) ) == 0 ) {
							$back.parent().addClass("fadeArrowBack" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
            return false;
        }).click(function(){
			$(this).trigger("slideNow");
			if(jQuery.data($this,"autoLoop") && !settings.disableAutoStop){
				clearInterval(jQuery.data($this,"autoLoopInterval"));
				jQuery.data($this,"autoLoop",false);
			}
			return false;
		});
		
		if(!settings.disableAutoLoop){
			//generate the interval
			jQuery.data($this,"autoLoop",true);
			jQuery.data($this,"autoLoopDirection",0);
			var auxInterval=setInterval(function(){
					var direction=jQuery.data($this,"autoLoopDirection");
					if(direction==0){
						$next.trigger("slideNow");
					}else{
						$back.trigger("slideNow");
					}
				},settings.autoLoopInterval);
			jQuery.data($this,"autoLoopInterval",auxInterval);
		}
    }
};

