function slider(container,settings) {

	if (settings == undefined) { settings = {} }
	this.settings = this.getDefaults();
	this.settings = $.mergeObjects(this.settings, settings);
	this.container = $(container)[0];
	this.sliderClass = this.settings.sliderClass;
	this.rightClass = this.settings.rightClass;
	this.leftClass = this.settings.leftClass;
	
	this.letterbox = $('#' + this.container.id + ' ' + this.sliderClass);
	this.ul = $("ul", this.letterbox);
	this.sliderWidth = this.getWidth(this.ul);
	this.chunks = this.getChunks();
	this.position = this.settings.startPos;
	this.ul.css("width", this.sliderWidth+"px");
	this.ul.css('left', this.goToIndex(this.position,this.settings.justify));
	this.currentLeft = parseInt(this.ul.css("left"));
	this.setup();
	this.initControls();
	//this.currentLeft = parseInt(this.ul.css("left"));
};

slider.prototype.initControls = function(){
    var that = this;
    $('#' + this.container.id + ' ' + this.rightClass).click(function(){
        that.next();
    });
    $('#' + this.container.id + ' ' + this.leftClass).click(function(){
        that.prev();
    });
}

slider.prototype.getDefaults = function(){
    
    var defaults = {
        "justify"		: "left",
        "stopper"		: true,
        "select"		: false,
        "selectClass"	: "selected",
        "startPos"		: 0,
        "sliderClass"   : '.slider',
        "rightClass"    : '.choose-right',
        "leftClass"     : '.choose-left',
        "speed"         : 350
    };

    return defaults;
}

slider.prototype.setup = function() {
	if (!this.settings.stopper)
	{
		this.leftStop = function() { return this.position > 0; };
		this.rightStop = function() { return this.position + 1 < this.chunks.length; };
	}
	else
	{
		if (this.settings.justify == 'right' && this.settings.startPos == 0 && !this.settings.select)
		{
			this.position = this.chunks.length - 1;
			this.ul.css('left', this.goToIndex(this.position,this.settings.justify));
		}
	}
	if (!this.settings.select)
	{
		this.makeSelected = function() {return false;};
	}
	else
	{
		this.initPos();
		this.makeSelected(this.position);
	}
	this.checkEndStops();
};
slider.prototype.initPos = function() {
	var left;
	stopLeft = this.leftStop();
	stopRight = this.rightStop();
	if (stopLeft && false !== stopRight && true !== stopRight)
	{
		left = stopRight;
		this.ul.css('left', left);
		this.currentLeft = left;
	}
	else if (false !== stopLeft && true !== stopLeft && stopRight)
	{
		left = stopLeft;
		this.ul.css('left', left);
		this.currentLeft = left;
	}
};


slider.prototype.next = function() {
	var endstop = this.rightStop();
	if (true == endstop)	
	{
		this.position++;
		this.goTo(this.goToIndex(this.position, this.settings.justify));
		this.makeSelected(this.position);
	}
	else if (endstop !== false)
	{
		this.goTo(endstop);
	}
    this.checkEndStops();
	if (endstop !== true && (this.position < this.chunks.length - 1) && this.settings.select && this.settings.stopper)
	{
		this.position++;
		this.makeSelected(this.position);
	}
};
slider.prototype.prev = function() {
	var endstop = this.leftStop();
	if (true === endstop)	
	{
		this.position--;
		this.goTo(this.goToIndex(this.position, this.settings.justify));
		this.makeSelected(this.position);
	}
	else if (endstop !== false && (this.position > 0))
	{
		this.goTo(endstop);
	}
    this.checkEndStops();
	if (endstop !== true && (this.position > 0) && this.settings.select && this.settings.stopper)
	{
		this.position--;
		this.makeSelected(this.position);
	}
};
slider.prototype.checkEndStops = function(){
    
    if (this.position == 0){
        $(this.leftClass).addClass('disabled');
    } else {
        $(this.leftClass).removeClass('disabled');
    }
    
    if (this.position > this.chunks.length - 7){
        $(this.rightClass).addClass('disabled');
    } else {
        $(this.rightClass).removeClass('disabled');
    }
}

slider.prototype.rightStop = function() {
	if (this.currentLeft < this.goToIndex(this.position, this.settings.justify))
	{
		return false;
	}
	else if (this.currentLeft + this.sliderWidth - this.chunks[this.position].elementWidth - this.letterbox.width() >= 0)
	{
		return true;
	}
	else
	{
		return this.letterbox.width() - this.sliderWidth ;
	}
};
slider.prototype.leftStop = function() {
	if (this.goToIndex(this.position, this.settings.justify) < this.currentLeft)
	{
		return false;
	}
	else if (this.currentLeft + this.chunks[this.position].elementWidth <= 0 )
	{
		return true;
	}
	else
	{
		return 0;
	}
};
slider.prototype.getChunks = function() {
	var chunks = []
	var that = this;
	var total = 0;
	this.ul.children().each(function (index) {
		chunks[index] = {	"elementWidth"	: that.getEleWidth($(this)),
		 					"runningWidth"	: total
						};
		total += that.getEleWidth($(this));
	});
	return chunks;
};
slider.prototype.goToIndex = function(chunk, just) {
	if (just == undefined)
	{
		var just = 'left';
	}
	if (just == 'left')
	{
		left = -this.chunks[chunk].runningWidth;
	}
	else if (just == 'right')
	{
		var left = -this.chunks[chunk].runningWidth - this.chunks[chunk].elementWidth + this.letterbox.width();
	}
	else if (just == 'centre')
	{
		var left = -this.chunks[chunk].runningWidth + Math.floor((this.letterbox.width() - this.chunks[chunk].elementWidth) / 2);
	}
	return left;
};
slider.prototype.goTo = function(left) {
	this.currentLeft = left;
	this.ul.animate({
		left: left
	}, this.settings.speed);
};
slider.prototype.makeSelected = function(ele) {
	this.ul.children().removeClass(this.settings.selectClass);
	$(this.ul.children()[ele]).addClass(this.settings.selectClass);
};
slider.prototype.getEleWidth = function(ele) {
	var w = 0;
	w = ele.width() + parseInt(ele.css('marginLeft')) + parseInt(ele.css('marginRight'));
	return w;
};
slider.prototype.getWidth = function() {
	var w = 0;
	this.ul.children().each( function() {
		w += $(this).width() + parseInt($(this).css('marginLeft')) + parseInt($(this).css('marginRight'));
	});
	return w;
};
