(function($){
  $.fn.wm_slide = function(options){
		var defaults = {
			controls_container: '.pagination',
			controls_label: '.scroll_label',
			controls_up: '.scrollup',
			controls_down: '.scrolldown',
			animate_time: 200
		};
		var opts = $.extend(defaults, options);
		return $(this).each(function(){
			var $div = $(this);
			var $height = $(this).height();
			var $content_height = $(this).attr('scrollHeight');
			if($height < $content_height){
				$div.css('overflow','hidden');
				$div.nextAll(opts.controls_container).show();
				$div.scrollTop('0');
				$div.scroll_target = 0;
				$num_pages = Math.ceil($content_height/$height);
				$(this).parent().find(opts.controls_label).html('1/' + $num_pages);
				// click up
				$div.nextAll(opts.controls_container).find(opts.controls_up).click(function(){
					$current_scroll = $div.scroll_target;
					$target_scroll = $current_scroll-$height;
					$amount_scroll = $target_scroll/$height;
					if($target_scroll != 0 || parseInt($amount_scroll) != $amount_scroll){
						$amount_scroll = Math.ceil($amount_scroll);
						$target_scroll = $amount_scroll * $height;
					}
					if($target_scroll >= 0){
						$div.scroll_target = $target_scroll;
						$num_pages = Math.ceil($content_height/$height);
						$(this).parent().find(opts.controls_label).html(parseInt($amount_scroll+1) + '/' + $num_pages);
					}
					$div.stop().animate({scrollTop: $target_scroll}, opts.animate_time);
					return false;
				});
				// click down
				$div.nextAll(opts.controls_container).find(opts.controls_down).click(function(){
					$current_scroll = $div.scroll_target;
					$target_scroll = $current_scroll+$height;
					if($target_scroll < $content_height){
						$div.scroll_target = $target_scroll;
						$amount_scroll = Math.ceil($target_scroll/$height);
						$num_pages = Math.ceil($content_height/$height);
						$(this).parent().find(opts.controls_label).html(parseInt($amount_scroll+1) + '/' + $num_pages);
					}
					$div.stop().animate({scrollTop: $target_scroll}, opts.animate_time);					
					return false;					
				});
			}
		});
  };
})(jQuery);

