function Slider(obj){
	var currentPage;
	var maxPages;
	var object;
	var delta=500;
	var isVert=false;	
	var self=this;
	var locked=false;
	
	this.setVertical=function(){
		isVert=true;
	}
	this.setHorizontal=function(){
		isVert=false;
	}
	
	this.init=function(obj){
		object=jQuery(obj);
		currentPage=0;
//		object.find('.scroller').css('position', 'absolute');
		var items=object.find('.scroller .page');
		maxPages=items.length;
		var i=0;
		items.each(function(){
			jQuery(this).addClass('page_'+i);
			if(i==0){
				jQuery(this).addClass('on');
			}
			i++;
		});
		i=0;
		object.find(".controls .page").each(function(){
			jQuery(this).addClass('page_'+i);
			if(i==0){
				jQuery(this).addClass('on');
			}
			jQuery(this).attr('i',i);
			i++;
		});
		i=0;
		object.find(".scroller .page").each(function(){
			jQuery(this).addClass('page_'+i);
			if(i==0){
				jQuery(this).addClass('on');
			}
			else{
				jQuery(this).hide();
			}
			jQuery(this).attr('i',i);
			jQuery(this).css('position','absolute');
			jQuery(this).css('top','0px');
			jQuery(this).css('left','0px');
			i++;
		});
		self.startTriggers();
	}
	
	this.startTriggers=function(){
		object.find('.controls .prev').click(function(e){
			if(!locked){
				self.setPage(currentPage-1);
			}
		});
		object.find('.controls .next').click(function(e){
			if(!locked){
				self.setPage(currentPage+1);
			}
		});
		object.find('.controls .page').click(function(e){
			if(!locked){
				var newPage= jQuery(this).attr('page');
				self.setPage(newPage);
			}
		});
	}
	
	this.clean=function(value){
		if(value>=maxPages)
		{
			return 0;
		}
		if(value<0){
			return maxPages-1;
		}
		return value;
	}
	
	this.setPage=function(page){
		locked=true;
		var setAnimation="left";
		if(page>currentPage){
			setAnimation="right";
		}
		page=self.clean(page);
		var scrollerWidth;
		if(isVert)
		{
			scrollerWidth=object.find('.scroller').height();
		}
		else{
			scrollerWidth=object.find('.scroller').width();
		}
		
		var current=object.find('.scroller .on');
		var next=object.find('.scroller .page_'+page);

		var first="-";
		var second="+";
		if(setAnimation=="right"){
			first="+";
			second="-";
		}
		object.find('.controls .on').removeClass('on');
		object.find('.scroller .page_'+page).addClass('on');
		object.find('.controls .page_'+page).addClass('on');
/*
		next.animate({left:first+"="+scrollerWidth+"px"},0);
		next.show();
		next.animate({left:second+"="+scrollerWidth+"px"}, delta);
		current.animate({left:second+"="+scrollerWidth+"px"}, delta);
		current.animate({left:first+"="+scrollerWidth+"px"}, 0, function(){
			object.find('.scroller .page_'+current.attr('i')).removeClass('on');
			object.find('.scroller .page_'+current.attr('i')).hide();
		});
*/		
		if(isVert)
		{
			next.animate({top:first+"="+scrollerWidth+"px"},0);
			next.show();
			next.animate({top:second+"="+scrollerWidth+"px"}, delta);
			current.animate({top:second+"="+scrollerWidth+"px"}, delta);
			current.animate({top:first+"="+scrollerWidth+"px"}, 0, function(){
				object.find('.scroller .page_'+current.attr('i')).removeClass('on');
				object.find('.scroller .page_'+current.attr('i')).hide();
				locked=false;
			});
		}
		else{		
			next.animate({left:first+"="+scrollerWidth+"px"},0);
			next.show();
			next.animate({left:second+"="+scrollerWidth+"px"}, delta);
			current.animate({left:second+"="+scrollerWidth+"px"}, delta);
			current.animate({left:first+"="+scrollerWidth+"px"}, 0, function(){
				object.find('.scroller .page_'+current.attr('i')).removeClass('on');
				object.find('.scroller .page_'+current.attr('i')).hide();
				locked=false;
			});
		}
		currentPage=page;
	}
	self.init(obj);
}

