jQuery.fn.extend({
	slider: function(params){
		var item_height = 0;
		var item_width = 0;
		var total = 0;
		var view = 0; 		
		//alert($.cookie('slider_position'));
		if(params['viewcount'] > jQuery('li', this).size()) params['viewcount'] = jQuery('li', this).size();
		if(total > view){
			$(params['left_top']).css({'cursor':'pointer'});
			$(params['right_bottom']).css({'cursor':'pointer'});
			$(params['left_top']).show();
			$(params['right_bottom']).show();
		}		
		jQuery('li', this).css({'width':params['item_width'], 'padding': params['padding']});
		(params['direction'] == 'hor')?(jQuery('li', this).css({'float':'left'})):(0);
		jQuery('li', this).each(function(){(jQuery(this).height() > item_height)?(item_height = jQuery(this).height()):(0);});
		item_height = item_height + parseInt(jQuery('li', this).css('padding')) * 2;
		item_width = parseInt(params['item_width']) + parseInt(jQuery('li', this).css('padding')) * 2;
		
		view = (params['direction'] == 'hor')?(params['viewcount'] * item_width):(params['viewcount'] * item_height);
		total = (params['direction'] == 'hor')?(jQuery('li', this).size() * item_width):(jQuery('li', this).size() * item_height);
				
		this.css({'position':'absolute', 'display':'block', 'padding':'0px', 'margin':'0px', 'list-style-type':'none'});
		(params['direction'] == 'hor')?( this.css({'width':total, 'height':item_height}) ):( this.css({'height':total, 'width':item_width}) );
		
		this.wrap("<div></div>").parent().css({'position':'relative', 'padding':'0px', 'margin':'0px', 'overflow':'hidden'});
		(params['direction'] == 'hor')?( this.parent().css({'width':view, 'height':item_height}) ):( this.parent().css({'height':view, 'width':item_width}) ); 
		this.wrap("<div></div>").parent().css({'position':'relative', 'left':'0px', 'top':'0px'});

		//var pos = parseInt(jQuery.cookie('slider_position_' + this.attr('id')));
		pos = 0;
		(params['direction'] == 'hor')?(this.parent().css({'left':pos})):(this.parent().css({'top':pos}));
		
		var lock;
		jQuery(params['left_top']).bind('click', {'id':this.attr('id'), 'slide_direction': params['direction'], 'slider':this.parent(), 'slide_area':(total - view), 'slide_step': slide_step = ((params['direction'] == 'hor')?(item_width):(item_height))}, function(a){
			var slide_direction = a.data.slide_direction;
			var slider = a.data.slider;
			var slide_area = a.data.slide_area;
			var slide_step = a.data.slide_step;
			if (!lock){
				var new_pos = (slide_direction == 'hor')?(parseInt(slider.css('left')) - slide_step):(parseInt(slider.css('top')) - slide_step);
				if(new_pos >= -slide_area){				
					lock = true;
					(slide_direction == 'hor')?( slider.animate({left:new_pos}, function(){ lock = false }) ):( slider.animate({top:new_pos}, function(){ lock = false;  })) ;
				}
			}	
			jQuery.cookie('slider_position_' + a.data.id, new_pos);
			return false;
		});
		jQuery(params['right_bottom']).bind('click', {'id':this.attr('id'), 'slide_direction': params['direction'], 'slider':this.parent(), 'slide_step': slide_step = ((params['direction'] == 'hor')?(item_width):(item_height))}, function(a){
			var slide_direction = a.data.slide_direction;
			var slider = a.data.slider;
			var slide_step = a.data.slide_step;
			if (!lock){				
				var new_pos = (slide_direction == 'hor')?(parseInt(slider.css('left')) + slide_step):(parseInt(slider.css('top')) + slide_step);
				if(new_pos <= 0){
					lock= true;
					(slide_direction == 'hor')?( slider.animate({left:new_pos}, function(){lock = false}) ):( slider.animate({top:new_pos}, function(){lock = false;}) );
				}
			}
			//jQuery.cookie('slider_position_' + a.data.id, new_pos);
			return false;			
		});

	}
});