

function handlerTrabajosListado(main) {
	//Save DOM locations
	this.location = {};
	this.location.main		= main;
	this.location.elements	= "div.trabajosContainer div.slider ul.elements li";
	this.location.viewport	= "div.trabajosContainer div.viewport";
	this.location.slider	= "div.trabajosContainer div.slider";
	this.location.arrows	= "nav div.arrows div.arrow";
	this.location.dots		= "nav ul.dots li";
	
	//Construct
	this.$main = jQuery(this.location.main);
	this.viewportWidth = "0px";
	this.elementsPerPage = 3;
	this.currentElement = 1;
	this.totalElements = 0;
	
	this.animationSpeed = 'fast';
	this.enableAnimation = true;
	
	this.direction = false;
	
	this.attach();
}
handlerTrabajosListado.prototype.get = function(selector) {
	return this.$main.find(selector);
}
handlerTrabajosListado.prototype.handleKey = function(event) {
	if (!window.contactoAbierto) {
		var that = this;
		if (event.keyCode == 37) { //left
			that.nudgePage(-1);
		} else if (event.keyCode == 39) { //right
			that.nudgePage(1);
		}
	}
}
handlerTrabajosListado.prototype.attach = function() {
	var that = this;
	var $dots = this.get(this.location.dots);
	var $arrows = this.get(this.location.arrows);
	var $viewport = this.get(this.location.viewport);
	this.totalElements = $dots.length;
	//this.viewportWidth = $viewport.width();
	this.viewportWidth = jQuery( this.get(this.location.elements).get(0) ).outerWidth() * this.elementsPerPage;
	
	$dots.each(function(index){
		jQuery(this).click(function(){
			that.switchPage(index+1);
		});
	});
	$arrows.each(function(index){
		jQuery(this).find(".arrowdot").click(function(event){
			event.stopPropagation();
			that.nudgePage( 2*index-1 ); /* f(0)=-1, f(1)=1 */
		});
	});
	this.$main.swipe({
		swipeLeft: function(){
			that.nudgePage(1);
		},
		swipeRight: function(){
			that.nudgePage(-1);
		},
		triggerOnTouchEnd: false
	});
	jQuery(document).keydown(function(event){
		return that.handleKey(event);
	});
}
handlerTrabajosListado.prototype.nudgePage = function(direction) {
	if (!this.enableAnimation) {
		return false;
	}
	
	this.direction = "next";
	if (direction==-1) { this.direction = "prev"; }

	return this.switchPage(this.currentElement+direction);
}
handlerTrabajosListado.prototype.switchPage = function(pageNumber) {
	
	if (pageNumber<1) {
		pageNumber=1;
	}
	if (pageNumber>this.totalElements) {
		pageNumber=this.totalElements;
	}
	
	if (this.currentElement==pageNumber) {
		return false;
	}
	if (!this.enableAnimation) {
		return false;
	}
	this.enableAnimation = false;
	
	var that = this;
	var $slider = this.get(this.location.slider);
	var $arrows = this.get(this.location.arrows);
	var $dots = this.get(this.location.dots);
	
	var newSliderOffset = (pageNumber-1)*this.viewportWidth;
	
	if (this.direction) {
		$arrows.filter("."+this.direction).addClass("hover");
	}
	
	$slider.animate(
		{
			left: '-'+newSliderOffset+'px'
		},
		that.animationSpeed,
		function() {
			if (that.direction) {
				$arrows.filter("."+that.direction).removeClass("hover");
				that.direction=false;
			}
			if (pageNumber>1) {
				$arrows.filter(".prev").show();
			} else {
				$arrows.filter(".prev").hide();
			}
			if (pageNumber<that.totalElements) {
				$arrows.filter(".next").show();
			} else {
				$arrows.filter(".next").hide();
			}
			that.enableAnimation = true;
		}
	);
	
	
	$dots.filter(".selected").removeClass("selected");
	jQuery($dots.get(pageNumber-1)).addClass("selected");
	
	
	
	this.currentElement = pageNumber;
	return true;
}





jQuery(document).ready(function(){
	var handler = new handlerTrabajosListado("section#contenidoPrincipal");
	handler.animationSpeed = 'slow';
});
