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);
        jQuery(".view_all > a", this).click(function() {
            aNewsSlider.vAll(settings,this);
            return false;
        });
    });
};
var aNewsSlider = {
    itemWidth: 0,
    init: function(s,p) {
		var selectedSlide = portOn;
        jQuery(".messaging",p).css("display","none");
        itemLength = jQuery(".item",p).length;
        if (jQuery(".view_all",p).width() == null) {
			//original: jQuery(".news_items",p).prepend("<p class='view_all'>" + s.newsHeadline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All Images</a></p>");
			//DISPLAY VIEW ALL, TOTAL, etc
			//Sam:use the line below instead.
            //better: jQuery(".news_items",p).prepend("<div class='slider_menu'><a href='gallery.html'>View Albums</a> &nbsp;-&nbsp; <span class='view_all'>" + s.newsHeadline + "<a href='#'>View All Images</a> [ " + itemLength + " total ] </span></div>");
        }
        newsContainerWidth = itemLength * aNewsSlider.itemWidth;
        jQuery(".container",p).css("width",newsContainerWidth + "px");
		//sam: added if so 'next' link doesnt show if there is only one image in the album
		if (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * 1) {
		    jQuery(".next",p).css("display","none");
		} else {
	        jQuery(".next",p).css("display","block");
		}
        animating = false;
		
		
		//sam: added this so it is possible to jump to specific slide
		if (selectedSlide != "") {
			if (animating == false) {
                animating = true;
				var nextButton = document.getElementById("next");
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * selectedSlide);
				
				//alert(selectedSlide);
                if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0) {
                    jQuery(".prev",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
						
						if (selectedSlide >= (itemLength-1)) {
                            //jQuery(".next",p).css("display","none"); //original code
							//nextButton.src = "images/arrow_home.gif";
                        } else {
							//nextButton.src = "images/arrow_next.gif";
						}
						
						//if this is the first image, dont show previous button
						if (selectedSlide == 0) {
							jQuery(".prev",p).css("display","none");
						}
						
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
		}
		
        jQuery(".next",p).click(function() {
            if (animating == false) {
                animating = true;
				var nextButton = document.getElementById("next");
				
				if (selectedSlide < (itemLength-1)) {
					//move to next image
					animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * 1);
					selectedSlide++;
				} else {
					//go back to the begining
					animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * (itemLength-1));
					//nextButton.src = "images/arrow_next.gif";
					selectedSlide = 0;
				}
				//alert(selectedSlide);
                if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0) {
                    jQuery(".prev",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
						
						if (selectedSlide == (itemLength-1)) {
							//nextButton.src = "images/arrow_home.gif";
						}
						
						//if this is the first image, dont show previous button
						if (selectedSlide == 0) {
							jQuery(".prev",p).css("display","none");
						}
						
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
        jQuery(".prev",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 1);
				selectedSlide--;	//decreased one image
                if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= parseInt(jQuery(".container",p).css("width"))) {
                    jQuery(".next",p).css("display","block");
                    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("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("news_slider") != -1) {
                break;
            }
        }
        if (jQuery(o).text().indexOf("View All") != -1) {
            jQuery(".next",p).css("display","none");
            jQuery(".prev",p).css("display","none");
			jQuery(".fl",p).css("float","none");	//sam: change to 'vertical view'
			jQuery(".fl",p).css("display","block");	//sam: change to 'vertical view'
            jQuery(o).text("Back to Slideshow");
            jQuery(".container",p).css("left","0px").css("width",aNewsSlider.itemWidth * 1 + "px");
        } else {
			jQuery(".fl",p).css("float","left");	//sam: return to 'inline'
			jQuery(".fl",p).css("display","inline");//sam: return to 'inline'
            jQuery(o).text("View All Images");
            aNewsSlider.init(s,p);
        }
    }
};
