/** TABLE SLIDER PLUGIN **/

jQuery.fn.accessNews = function(settings) {
	/** Obtenemos los anchos **/
	var elementSlider = $(this);
	var widthSlider = $(elementSlider).width();
    //alert (elementSlider.text() );
	var widthButtonNext = $(elementSlider).find("a.next").width();
	var widthButtonPrev = $(elementSlider).find("a.prev").width();
	var widthItenSlider = widthSlider - ( widthButtonNext + widthButtonPrev);
    //alert (widthItenSlider);
	/** Asignamos los valores **/
	$(elementSlider).find('.news_items').css('width', widthItenSlider + 'px');
    $(elementSlider).find('.fl').css('width', widthItenSlider + 'px');
    //alert($(elementSlider).find('.fl').width());
	/** cambiamos el left, segun el nuevo ancho **/
    $(elementSlider).find('.container').css('left',0);
    $(elementSlider).find('.prev').hide();
	
    settings = jQuery.extend({
        newsHeadline: "Top Stories",
        newsSpeed: "normal",
        easing: false,
		slide: 2
    }, settings);
    return this.each(function(i) {
    	/*var $nBox = jQuery(this);
		$nBox.wrapAll("<div></div>");
		var $nDummy=$nBox.parent();
		$nDummy.css({visibility: "hidden"});*/
        //settings.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".news_slider").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".news_slider").css("margin-right"));
		//alert(settings.itemWidth);
        //aNewsSlider.init(settings,this,function(){$nDummy.css({visibility: "visible"})});
        aNewsSlider.init(settings,this,i);
        jQuery(".view_all > a", this).click(function() {
            aNewsSlider.vAll(settings,this);
            return false;
        });
    });
};


var aNewsSlider = {
	itemWidth: 0,
	//init: function(s,p,callback) {
	init: function(s,p,i) {
		//alert(jQuery(p).text() );
		//itemWidth = parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").width()) + parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").css("margin-right"));
		itemWidth = jQuery(p).find('.item',p).width();
		jQuery(p).find(".messaging",p).css("display","none");
		itemLength = jQuery(p).find(".item",p).length;
		newsContainerWidth = itemLength * itemWidth;
		jQuery(p).find(".container",p).css("width",newsContainerWidth + "px");
		jQuery(p).find(".next",p).css("display","block");
		animating = false;
		jQuery(p).find(".next",p).click(function() {
			if (animating == false) {
				animating = true;
				//itemWidth = parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").css("width")) + parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").css("margin-right"));
				itemWidth = jQuery(p).find('.item',p).width();
				animateLeft = parseInt(jQuery(p).find(".container",p).css("left")) - (itemWidth * s.slide);
				if (animateLeft + parseInt(jQuery(p).find(".container",p).css("width")) > 0) {
					jQuery(p).find(".prev",p).css("display","block");
					if(s.easing){
						jQuery(p).find(".container",p).animate({left: animateLeft}, s.newsSpeed, s.easing, function(){
								if (parseInt(jQuery(p).find(".container",p).css("left")) + parseInt(jQuery(p).find(".container",p).css("width")) <= itemWidth * s.slide) {
									 jQuery(p).find(".next",p).css("display","none");
								}
								animating = false;
						});
						
					} else {
						jQuery(p).find(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
								//alert("this.left: "+jQuery(p).css("left")+" || container p"+jQuery(".container",p).css("left"));
							jQuery(p).css("left",animateLeft);
							if (parseInt(jQuery(p).find(".container",p).css("left")) + parseInt(jQuery(p).find(".container",p).css("width")) <= itemWidth * s.slide) {
								jQuery(p).find(".next",p).css("display","none");
							}
							animating = false;
						});
					}
				} else {
					animating = false;
				}
			}
			return false;
		});
		jQuery(p).find(".prev",p).click(function() {
			if (animating == false) {
				animating = true;
				//itemWidth = parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").css("width")) + parseInt(jQuery(p).find(".item:eq(" + i + ")",".news_slider").css("margin-right"));
				itemWidth = jQuery(p).find('.item',p).width();
				animateLeft = parseInt(jQuery(p).find(".container",p).css("left")) + (itemWidth * s.slide);
				if ((animateLeft + parseInt(jQuery(p).find(".container",p).css("width"))) <= parseInt(jQuery(p).find(".container",p).css("width"))) {
					jQuery(p).find(".next",p).css("display","block");
					if (s.easing){
						jQuery(p).find(".container",p).animate({left: animateLeft}, s.newsSpeed, s.easing, function(){
								if (parseInt(jQuery(p).find(".container",p).css("left")) == 0) {
								jQuery(p).find(".prev",p).css("display","none");
							}
							animating = false;
											});
					} else {
						jQuery(p).find(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
							jQuery(p).css("left",animateLeft);
							if (parseInt(jQuery(p).find(".container",p).css("left")) == 0) {
								jQuery(p).find(".prev",p).css("display","none");
							}
							animating = false;
						});
					}
				} else {
					animating = false;
				}
			}
			return false;
		});
		/*if (callback){
			callback.call();
		}*/
	},
	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(p).find(".next",p).css("display","none");
			jQuery(p).find(".prev",p).css("display","none");
			jQuery(o).text("View Less");
			jQuery(p).find(".container",p).css("left","0px").css("width",this.itemWidth * s.slide + "px");
		} else {
			jQuery(o).text("View All");
			this.init(s,p);
		}
	}
};