
function create(num)
{
	var me = this;
	this.me = me;
	this.num = num;
	this.index = 1;
	this.max = 0;
	this.obj = $(".slider").eq(num);
	this.prev = function(){me.animate("prev")};
	this.next = function(){me.animate("next")};
	this.init()
}

var _create = create.prototype;

_create.init = function()
{
	var me = this.me;
	this.max = $("li",this.obj).length;
	this.index = Math.floor( Math.random() * (this.max) ) + 1;

	$("ul", this.obj).css('width',(this.max+1) * 200);
	$(this.obj).css("position", "relative");
	$("ul",this.obj).css("position", "relative");
	$("li", this.obj).css("position", "absolute");
	$("li", this.obj).css("left", "192px");
	$("li:nth-child("+this.index+")",this.obj).css("left", "0");
	$(this.obj).after('<span class="prevBtn"><a href=\"javascript:void(0);\">prev</a></span> <span class="nextBtn"><a href=\"javascript:void(0);\">next</a></span>');	
	
	var prevBtn = $(".prevBtn", this.obj.parent());
	var nextBtn = $(".nextBtn", this.obj.parent());
	
	if (me.max == 1) {
		nextBtn.hide();
		prevBtn.hide();
	}
	
	prevBtn.click(function()
	{
		me.prev()
		nextBtn.show();
	});
	
	nextBtn.click(function()
	{
		me.next()
		prevBtn.show();
	});	
}

_create.animate = function(dir)
{
	if(dir == "next"){
		var befor_index = this.index;
		this.index = (this.index >= this.max) ? 1 : this.index + 1;
		
		$("li:nth-child("+ this.index +")",this.obj).css("left", "192px");
		$("li:nth-child("+ this.index +")",this.obj).animate({ "left": "0px" }, 500, "swing");
		$("li:nth-child("+ befor_index +")",this.obj).animate({ "left": "-192px" }, 500, "swing");
	}
	else if(dir == "prev") {
		var befor_index = this.index;
		this.index = (this.index <= 1) ? this.max : this.index - 1;
		
		$("li:nth-child("+ this.index +")",this.obj).css("left", "-192px");
		$("li:nth-child("+ this.index +")",this.obj).animate({ "left": "0px" }, 500, "swing");
		$("li:nth-child("+ befor_index +")",this.obj).animate({ "left": "192px" }, 500, "swing");
	};
}

$(function() {
	
	for(var i = 0; i < $(".slider").length; i++)
	{
		new create(i);
	}
	
});
