var status=0;
jQuery.fn.accessNews = function(settings) {
    settings = jQuery.extend({
        newsHeadline: "Top Stories",
        newsSpeed: "normal"
    }, settings);
    return this.each(function(i) {
     // alert(jQuery(".item:eq(" + i + ")",".news_slider").css("width"));
	  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);
	   //alert("hiiiiiiii");
        jQuery(".view_all > a", this).click(function() {
            aNewsSlider.vAll(settings,this);
            return false;
        });
    });
};
var aNewsSlider = {
   
	
	itemWidth: 0,
    init: function(s,p) {
       
	   jQuery(".messaging",p).css("display","block");
        itemLength = jQuery(".item",p).length;
      //alert(itemLength);
	 //alert(jQuery(".view_all",p).width());
	   if (jQuery(".view_all",p).width() == null) {
            jQuery(".news_items",p).prepend("<span class='view_all'>" + s.newsHeadline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All</a></span>");
        }
        newsContainerWidth = itemLength * aNewsSlider.itemWidth;       
	   //alert(newsContainerWidth);
	   jQuery(".container",p).css("width",newsContainerWidth + "px");
        jQuery(".next",p).css("display","block");
        animating = false;
        jQuery(".next",p).click(function() {
										 
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * 4);
               
			//alert(animateLeft + parseInt(jQuery(".container",p).css("width")));	
		if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0 && animateLeft + parseInt(jQuery(".container",p).css("width"))>6  && status==0) {
               // alert(animateLeft + parseInt(jQuery(".container",p).css("width")));
			   jQuery(".prev",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")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * 4) {
                            jQuery(".next",p).css("display","block");
                        }
                        animating = false;
                    });
                } else {
					 status=0;
					 animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 4);
             //  alert(animateLeft+parseInt(jQuery(".container",p).css("width")));
				if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= 1150) {
                    jQuery(".next",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                         status=1;
						jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",p).css("left")) == 0) {
                            jQuery(".prev",p).css("display","block");
                        }
                        animating = false;
                    });
                }          
                    animating = false;
                }
            }
            return false;
        });
		
		jQuery(".next1",p).click(function() {
				
					//alert(document.getElementById('changeclass').className);
           // alert("hiiii");
			if (animating == false) {
				
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * 4);
               
			   if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0 && animateLeft + parseInt(jQuery(".container",p).css("width"))>6 && status==0) {
                    jQuery(".prev",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")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * 4)                        {
                            jQuery(".next1",p).css("display","block");
							
                        }
						
					
                       	
						animating = false;
						
                    });
                } else {
					  status=0;
					        animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 4);
                if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= 1150) {
                    jQuery(".next",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                     status=1;
					 jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",p).css("left")) == 0) {
                            jQuery(".prev",p).css("display","block");
                        }
                        animating = false;
                    });
                }          
				   animating = false;
                }
				
				
            }
			
            return false;
        
			
		
		
		});
      
        jQuery(".prev",p).click(function() {
			
            if (animating == false) {
				
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 4);
                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","block");
                        }
                        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(".next1",p).css("display","block");
			jQuery(".next",p).css("display","block");
            jQuery(".prev",p).css("display","block");
            jQuery(o).text("View Less");
            jQuery(".container",p).css("left","0px").css("width",aNewsSlider.itemWidth * 4 + "px");
        } else {
            jQuery(o).text("View All");
            aNewsSlider.init(s,p);
        }
    }
};
