(function($){
	
$.widget("hereu.pCarousel", {
	options: {
		data: false,
		scrollStep: 30,		//pixels por step
		scrollTime: 100,		// mSecs entre steps
		scrollOnOver: false
	},
	element: null,
	carousel: null,
	scrollTimer: null,
	scrollLeft: null,
	scrollable: null,
	
	_create: function(){
		this._beforeCreate();
	
		var me = this;
		this.element
			.addClass("portada-carousel-main")
			.append(
				this._createLeftBtn()
			)
			.append(
				this.scrollable = $("<div>")
					.addClass("portada-carousel-container")
					.append(
						this.carousel = $("<ul>")
							.addClass("portada-carousel clearfix LinkBar")
					)
			)
			.append(
				this._createRightBtn()
			)
		;
		
		this._afterCreate();
		
		this._placeElms();
		this._resizeCarousel();
		this.resize();
	},

	_createLeftBtn: function(){
		var me = this;
		var elm = $("<div>")
					.addClass("pc-btn pc-leftBtn")
		;

		if(this.options.scrollOnOver === true){
			elm
				.mouseover(function(){
					me._startScroll(-1);
				})
				.mouseout(function(){
					me._stopScroll();
				})
			;		
		}else{
			elm.click(function(){
				me._move(-1);
			});
		}
		;
		
		return this._onCreateLeftBtn(elm);
	},

	_createRightBtn: function(){
		var me = this;
		var elm = $("<div>")
					.addClass("pc-btn pc-rightBtn")
		;
		
		if(this.options.scrollOnOver === true){
			elm
				.mouseover(function(){
					me._startScroll(1);
				})
				.mouseout(function(){
					me._stopScroll();
				})

			;		
		}else{
			elm.click(function(){
				me._move(1);
			});
		}
		
		

		return this._onCreateRightBtn(elm);
	},
	
	_startScroll: function(dir){
		var maxScrollLeft = this.scrollable.attr("scrollWidth");
		this.scrollLeft = this.scrollable.scrollLeft();
		var me = this;

		var step = (dir * me.options.scrollStep);
		this.scrollTimer = setInterval(function(){
			me.scrollLeft += step
			if(me.scrollLeft == 0 || me.scrollLeft >= maxScrollLeft){
				me._stopScroll();
				return false;
			}
			
			me.scrollable.scrollLeft( me.scrollLeft );
		}, this.options.scrollTime);
		
	},

	_stopScroll: function(){
		clearInterval(this.scrollTimer);
	},
	
	_move: function(dir){
		var me = this;
		var maxScrollLeft = this.scrollable.attr("scrollWidth");
		this.scrollLeft = this.scrollable.scrollLeft();
		var W = this.scrollable.width();

		if(dir > 0){	// move to Right
			var newSL = this.scrollLeft + W;
			if(newSL > maxScrollLeft)
				newSL = maxScrollLeft;
		}else{			// move to Left
			var newSL = this.scrollLeft - W;
			if(newSL < 0)
				newSL = 0;
		}

		this.scrollable.animate({scrollLeft: newSL}, 300);
	},
	
	_resizeCarousel: function(){
		var W = 0;
		this.carousel.children().each(function(){
			var elm = $(this);
			W += elm.outerWidth() + (parseInt( elm.css("marginLeft") ) + parseInt( elm.css("marginRight") ));
		});
		this.carousel.width(W);
	},


// HOOK´S
	resize: function(){},
	
	_onCreateRightBtn: function(elm){return elm;},
	_onCreateLeftBtn: function(elm){return elm;},

	_beforeCreate: function(){},
	_afterCreate: function(){},
	
	_createElement: function(dataObj){},
	
	_placeElms: function(){}
});

}(jQuery));

function getMaxWSiblings(elm){
	var $ = jQuery;
	if(elm.length == 0) return 0;

	var maxW = elm.parent().innerWidth();
	
	elm.siblings().each(function(){
		var W = ($(this).outerWidth() + parseInt( $(this).css("marginLeft") ) + parseInt( $(this).css("marginRight") ) );
		maxW = maxW - W;
	});
	var selfW = parseInt( $(elm).css("marginLeft") ) + parseInt( $(elm).css("marginRight") ) + (elm.outerWidth() - elm.width());
	maxW = maxW - selfW;
	return maxW;
}

function setMaxWSiblings(elm){
	var $ = jQuery;
	elm.width( getMaxWSiblings(elm) );
}
