// AQUI DEFINIM EL NUMERO DE ITEM Q MOU EL SLIDER A CADA CLIC
var itemPerRow = 4;

jQuery.fn.accessNews = function(settings) {
    settings = jQuery.extend({
        newsHeadline: "Galeria",
        newsSpeed: "normal"
    }, settings);
    return this.each(function(i) {
        aNewsSlider.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".SSG").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".SSG").css("margin-right"));
        aNewsSlider.init(settings,this);
        jQuery(".view_all > a", this).click(function() {
        	//alert(this);
            aNewsSlider.vAll(settings,this);
            return false;
        });
    });
};
var aNewsSlider = {
    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(".news_items",p).prepend("<p class='view_all'>" + s.newsHeadline + " [ " + itemLength + " en total ] &nbsp;-&nbsp; <a href='#'>Veure'n més</a></p>");
			jQuery(".news_items",p).append("<p class='view_all'><a href='#'>Veure'n més</a> &nbsp;-&nbsp; [ " + itemLength + " en total ] </p>");
			//jQuery(".news_items",p).append("<p class='view_all'><a href='#'>Veure'n més</a> &nbsp;-&nbsp;" + s.newsHeadline + " [ " + itemLength + " en total ] </p>");
        }		
        newsContainerWidth = itemLength * aNewsSlider.itemWidth;
        jQuery(".container",p).css("width",newsContainerWidth + "px");
        jQuery(".next",p).css("display","block");
		// AFEGIT CONTROL EN EL INIT
		if (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * itemPerRow) {
           jQuery(".next",p).css("display","none");
        }
        animating = false;
        jQuery(".next",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * itemPerRow);
                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 (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * itemPerRow) {
                            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(".container",p).css("left")) + (aNewsSlider.itemWidth * itemPerRow);
                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("SSG") != -1) {
					break;
				}		
			}

        if (jQuery(o).text().indexOf("Veure'n més") != -1) {
            jQuery(".next",p).css("display","none");
            jQuery(".prev",p).css("display","none");
            jQuery(o).text("Minimitza");
            jQuery(".container",p).css("left","0px").css("width",aNewsSlider.itemWidth * itemPerRow + "px");
//Millora: Per evitar que els DIVS quedin mal colˇlocats degut a la diferent altura dels .desc_fl assignem acada un d'aquests la altura del més alt.     
        	var min = 0;
            jQuery('.desc_fl',p).each(function(){
            	if(jQuery(this).height() > min) min = jQuery(this).height();	
            });
            jQuery('.desc_fl',p).height(min);
        } else {
            jQuery(o).text("Veure'n més");
            aNewsSlider.init(s,p);
        }
    }
};

