/*

	----------------------------------------------------------------------------------------------------
	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 : "No Title",
        total    : "total",
        view_all : "view all",
        view_less: "view less",
        speed    : "normal",
        slideBy  : 2,
        position : "left"
    }, settings);
    return this.each(function() {
        jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews.run = function( $this, settings ) {
    var ul = jQuery( "ul:eq(0)", $this );
    var li = ul.children();
    if ( li.length > settings.slideBy ) {
        var $next = jQuery( ".next > a", $this );
        var $back = jQuery( ".back > a", $this );
        var liWidth = jQuery( li[0] ).width();
        var animating = false;
        ul.css( "width", ( li.length * liWidth ) );
        ul.css(settings.position,"0px");
        $next.click(function() {
            if ( !animating ) {
                animating = true;
                offsetLeft = parseInt( ul.css(settings.position)) - ( liWidth * settings.slideBy );

                if ( offsetLeft + ul.width() > 0 ) {
                    $back.css( "display", "block" );

                    if(settings.position=='right'){
                        ul.animate({right:offsetLeft},settings.speed,function() {
                            if ( parseInt( ul.css(settings.position)) + ul.width() <= liWidth * settings.slideBy ) {
                                $next.css( "display", "none" );
                            }
                            animating = false;
                        });
                    }
                    else{
                          ul.animate({left:offsetLeft},settings.speed,function() {
                            if ( parseInt( ul.css(settings.position)) + ul.width() <= liWidth * settings.slideBy ) {
                                $next.css( "display", "none" );
                            }
                            animating = false;
                        });
                    }


                } else {
                    animating = false;
                }
            }
            return false;
        });
        $back.click(function() {
            if ( !animating ) {
                animating = true;
                offsetRight = parseInt( ul.css(settings.position) ) + ( liWidth * settings.slideBy );
                if ( offsetRight + ul.width() <= ul.width() ) {
                    $next.css( "display", "block" );
                     if(settings.position=='right'){
                            ul.animate({
                            right: offsetRight
                        }, settings.speed, function() {
                            if ( parseInt( ul.css(settings.position) ) == 0 ) {
                                $back.css( "display", "none" );
                            }
                            animating = false;
                        });
                     }
                     else{
                             ul.animate({
                            left: offsetRight
                        }, settings.speed, function() {
                            if ( parseInt( ul.css(settings.position) ) == 0 ) {
                                $back.css( "display", "none" );
                            }
                            animating = false;
                        });
                     }

                } else {
                    animating = false;
                }
            }
            return false;
        });

        settings.total=settings.total+" "+li.length
        var html='';

        if(settings.headline)
           html+=settings.headline;
        if(settings.view_all)
           html+=" "+settings.total+"(<a href=\"#\">"+settings.view_all+"</a>)";


        $next.css( "display", "block" ).parent().after("<p class=\"view_all\">"+html+"</p>");

        jQuery( ".view_all > a", $this ).click(function() {
            if ( jQuery( this ).html() == settings.view_all) {
                ul.css( "width", "auto" ).css(settings.position, "0" );
                $next.css( "display", "none" );
                $back.css( "display", "none" );
                jQuery(this).html(settings.view_less);
            } else {
                jQuery(this).html(settings.view_all);
                ul.css( "width", ( li.length * liWidth ) );
                $next.css( "display", "block" );
            }
            return false;
        });
    }
};
