
//attention : nécessite le plugin "dimensions" pour pouvoir sélectionner l'item actif

jQuery.fn.slideNews = function(settings) {
    settings = jQuery.extend({
        headline: "Top Stories",
        newsWidth: 116,
        newsSpeed: "normal",
		slidingStep: 1,
		nbNewsToDisplay: 3,
		activatedItem: 1
    }, settings);
    return this.each(function(i){
        jQuery(".messaging",this).css("display","none");
        //jQuery("a:eq(0)",this).attr("href","#skip_to_news_" + i);
        //jQuery("a:eq(4)",this).attr("name","skip_to_news_" + i);
        itemLength = jQuery(".item",this).length;
        newsContainerWidth = itemLength * settings.newsWidth;
        jQuery(".container",this).css("width",newsContainerWidth + "px");
		jQuery(".container",this).parent().css("width",settings.newsWidth * settings.nbNewsToDisplay + "px");
        jQuery(".news_items",this).prepend("<p class='view_all'>" + settings.headline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All</a></p>");
        jQuery("a:eq(3)",this).click(function() {
            thisSlider = jQuery(this).parent().parent().parent();
            jQuery(".next",thisSlider).css("display","none");
            jQuery(".prev",thisSlider).css("display","none");
            jQuery(".container",thisSlider).css("left","0px");
            //jQuery(".container",thisSlider).css("width",settings.newsWidth * settings.slidingStep + "px");
            jQuery(".view_all",thisSlider).css("display","none");
			jQuery(".container",thisSlider).parent().css("width","");
        });
        if ((parseInt(jQuery(".container",this).css("width"))) > parseInt(jQuery(".container",this).parent().css("width"))) {
        	jQuery(".next",this).css("display","block");
		}
		
		//positionnement sur l'item actif
		thisParent = jQuery(".news_items",this).parent();
		if (settings.activatedItem > settings.nbNewsToDisplay && thisParent.offset) {
			jQuery(".prev",thisParent).css("display","block");
			for (i=1; i<Math.floor(settings.activatedItem/settings.slidingStep); i++) {
				animateLeft = parseInt(jQuery(".container",thisParent).css("left")) - (settings.newsWidth * settings.slidingStep);
				jQuery(".container",thisParent).css('left', animateLeft);
			}
		}
		
        animating = false;
        jQuery(".next",this).click(function() {
            thisParent = jQuery(this).parent();
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",thisParent).css("left")) - (settings.newsWidth * settings.slidingStep);
                if (animateLeft + parseInt(jQuery(".container",thisParent).css("width")) > 0) {
                    jQuery(".prev",thisParent).css("display","block");
                    jQuery(".container",thisParent).animate({left: animateLeft}, settings.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",thisParent).css("left")) + parseInt(jQuery(".container",thisParent).css("width")) <= parseInt(jQuery(".container",thisParent).parent().css("width"))) {
                            jQuery(".next",thisParent).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
                return false;
            }
        });
        jQuery(".prev",this).click(function() {
            thisParent = jQuery(this).parent();
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",thisParent).css("left")) + (settings.newsWidth * settings.slidingStep);
                if ((animateLeft + parseInt(jQuery(".container",thisParent).css("width"))) <= parseInt(jQuery(".container",thisParent).css("width"))) {
                    jQuery(".next",thisParent).css("display","block");
                    jQuery(".container",thisParent).animate({left: animateLeft}, settings.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",thisParent).css("left")) == 0) {
                            jQuery(".prev",thisParent).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
                return false;
            }
        });
    });
};
