(function($) {
$.fn.Autoslider_vertical = function(o) {
    o = $.extend({
        btnPrev: null,
        btnNext: null,
		myMask: null,
		itemH: null
    }, o || {});

    
	this.each(function() {

        var div = $(this);
		var imgs = $("img", div);
		div.totMoves = div.nextMove = 0;
		var li = $("li", div);
		var ii = 0;
		div.totW = 0;
		var visibleWidth = $(o.myMask).width();
		var enableScroll = false;
		var inMoving = false;
	
		if (!o.itemH) {

			for (ii = 0; ii < imgs.size(); ii++) {
				div.totH += imgs[0].height;
			}
			
			if (div.totH > visibleWidth) {
				enableScroll = true;
				// totMoves = Math.round(div.totW / visibleWidth);
				div.totMoves = imgs.size();
			}
		
		} else {
			enableScroll = false;
			div.totMoves = li.size();
		}

		var count = 1;
		
		function animateIt() {
			
			var leftDiv = (o.itemH * div.totMoves) - o.itemH;

			if (count == div.totMoves) { 
				direction = parseInt(div.css("margin-top")) + leftDiv;
				count = 1;
			}
			else {
			  direction = parseInt(div.css("margin-top")) - o.itemH;
			  count++;
			}

			div.animate({foo: 1}, 4000).animate({ 'marginTop' : direction }, animateIt);

		}

		animateIt();


		
		if (enableScroll) {
			if (o.btnPrev)
				$(o.btnPrev).click(function() {
					if (!inMoving) {
						inMoving = true;
						div.nextMove--;
						if (div.nextMove < 0) {
							div.nextMove = 0;
							inMoving = false;

						} else {
							if (!o.itemH) {
								div.nextPos = parseInt(div.css("margin-top")) + imgs[div.nextMove + 1].height;
							
							} else {
								div.nextPos = parseInt(div.css("margin-top")) + o.itemH;
							}
							
							go(div.nextPos);
						}

						// div.nextPos = visibleWidth * div.nextMove;
					}					
				});

			
			if (o.btnNext)
				$(o.btnNext).click(function() {
					if (!inMoving) {
						inMoving = true;
						div.nextMove++;
						if (div.nextMove >= div.totMoves) { 
							div.nextMove = div.totMoves - 1;
							inMoving = false;

						} else {
							if (!o.itemH) {
								div.nextPos = parseInt(div.css("margin-top")) - imgs[div.nextMove].height;

							} else {
								div.nextPos = parseInt(div.css("margin-top")) - o.itemH;
							}

							go(div.nextPos);
						}

						// div.nextPos = -(visibleWidth * div.nextMove);
					}

				});
		
		} else {
			$(o.btnPrev).css("visibility", "hidden"); 
			$(o.btnNext).css("visibility", "hidden"); 
		};


        function go(to) {
			div.animate({ 'marginTopt' : to }, function(){ inMoving = false; } );
        };

    });
};


})(jQuery);
