jQuery.fn.xmlScrollIt = function(settings) {
    settings = jQuery.extend({
        scrollHeadline: "Top Stories",
        scrollSpeed: "normal"
    }, settings);
    return this.each(function(i) {
        anXMLSlider.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".xmlScroller").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".xmlScroller").css("margin-right"));
        anXMLSlider.init(settings,this);
        jQuery(".view_all > a", this).click(function() {
            anXMLSlider.vAll(settings,this);
            return false;
        });
    });
};
var anXMLSlider = {
    itemWidth: 0,
    init: function(s,p) {
        jQuery(".messaging",p).css("display","none");
        itemLength = jQuery(".item",p).length;
        if (jQuery(".view_all",p).width() == null) {
            // jQuery(".xml_items",p).prepend("<p class='view_all'>" + s.scrollHeadline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All</a></p>"); ------ with totals
			jQuery(".xml_items",p).prepend("<p class='view_all'>" + s.scrollHeadline + " &nbsp;-&nbsp; <a href='#'>View All</a></p>");
        }
        newsxmlItemsContainerWidth = itemLength * anXMLSlider.itemWidth;
        jQuery(".xmlItemsContainer",p).css("width",newsxmlItemsContainerWidth + "px");
        jQuery(".next",p).css("display","block");
        animating = false;
        jQuery(".next",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".xmlItemsContainer",p).css("left")) - (anXMLSlider.itemWidth * 2);
                if (animateLeft + parseInt(jQuery(".xmlItemsContainer",p).css("width")) > 0) {
                    jQuery(".prev",p).css("display","block");
                    jQuery(".xmlItemsContainer",p).animate({left: animateLeft}, s.scrollSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".xmlItemsContainer",p).css("left")) + parseInt(jQuery(".xmlItemsContainer",p).css("width")) <= anXMLSlider.itemWidth * 2) {
                            jQuery(".next",p).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
        jQuery(".prev",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".xmlItemsContainer",p).css("left")) + (anXMLSlider.itemWidth * 2);
                if ((animateLeft + parseInt(jQuery(".xmlItemsContainer",p).css("width"))) <= parseInt(jQuery(".xmlItemsContainer",p).css("width"))) {
                    jQuery(".next",p).css("display","block");
                    jQuery(".xmlItemsContainer",p).animate({left: animateLeft}, s.scrollSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".xmlItemsContainer",p).css("left")) == 0) {
                            jQuery(".prev",p).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
    },
    vAll: function(s,p) {
        var o = p;
        while (p) {
            p = p.parentNode;
            if (jQuery(p).attr("class") != undefined && jQuery(p).attr("class").indexOf("xmlScroller") != -1) {
                break;
            }
        }
        if (jQuery(o).text().indexOf("View All") != -1) {
            jQuery(".next",p).css("display","none");
            jQuery(".prev",p).css("display","none");
            jQuery(o).text("View Less");
            jQuery(".xmlItemsContainer",p).css("left","0px").css("width",anXMLSlider.itemWidth * 5 + "px");
        } else {
            jQuery(o).text("View All");
            anXMLSlider.init(s,p);
        }
    }
};
