jQuery.fn.accessNews = function(settings) {
    settings = jQuery.extend({
        newsHeadline: "Top Stories",
        newsSpeed: "normal"
    }, settings);
    return this.each(function(i) {
        aNewsSlider.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".news_slider").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".news_slider").css("margin-right"));
        aNewsSlider.init(settings,this);
    });
};
var aNewsSlider = {
    itemWidth: 0,
    init: function(s,p) {
        jQuery(".messaging",p).css("display","none");
        itemLength = jQuery(".item",p).length;
        
        newsContainerWidth = itemLength * aNewsSlider.itemWidth;
        jQuery(".container",p).css("width",newsContainerWidth + "px");
        jQuery(".next",p).css("visibility","visible");
        animating = false;
        jQuery(".next",p).click(function() {
            go_droite();
            return false;
        });
        jQuery(".prev",p).click(function() {
            go_gauche();
            return false;
        });

		function go_droite(){
			if (animating == false) {
				animating = true;
				animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * 1);
				if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0) {
					jQuery(".prev",p).css("visibility","visible");
					jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
						jQuery(this).css("left",animateLeft);
						if (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * 1) {
							jQuery(".next",p).css("visibility","hidden");
							lance('gauche');
						}else{
							lance('droite');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
		}

		function go_gauche(){
			if (animating == false) {
				animating = true;
				animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 1);
				if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= parseInt(jQuery(".container",p).css("width"))) {
					jQuery(".next",p).css("visibility","visible");
					jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
						jQuery(this).css("left",animateLeft);
						if (parseInt(jQuery(".container",p).css("left")) == 0) {
							jQuery(".prev",p).css("visibility","hidden");
							lance('droite');
						}else{
							lance('gauche');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
		}

		function lance(sens){
			jQuery("#news_right").stopTime();
			jQuery("#news_right").oneTime(3000,function(i) {
				if(sens == "droite"){
					go_droite();
				}else{
					go_gauche();			
				}
			});
		}
		lance('droite');
    }
};