/*
 * Slide 1.0 vertical
 * Renan Vaz, renanvaz.com.br
 */
(function($){
	$.fn.extend({
		init_slide: function(opt){
			var defaults = {infinity: false, orientation: 'horizontal', index: 0};
			var o = $.extend(defaults, opt);
			
			return $(this).each(function(){
				var $self = $(this);
				var $kids = $self.children();
				
				var w = 10;
				
				$kids.each(function(){
					w += $(this).outerHeight(true);
				});
				
				$kids.wrapAll('<div class="wrap-slide" style="height:'+(o.wrap_height || w)+'px; position:relative; display:block;"></div>');
				o.width  = o.width || $self.width();
				
				$kids.css({display:'block', float:'none', clear:'both'});
				
				o.height = o.height || $self.height();
				
				$self.width(o.width);
				$self.height(o.height);
				
				$self.css({'overflow': 'hidden', 'position': 'relative'});
				
				$self.data('options', o);
				$self.data('slide_inited', true);
				
				$self.goto_slide(o.index, false);
				
			});
		},
		next_slide: function(){
			return $(this).each(function(){
				var $self = $(this);
				$self.goto_slide(Number($self.data('index')) + 1);
			});
		},
		prev_slide: function(){
			return $(this).each(function(){
				var $self = $(this);
				$self.goto_slide(Number($self.data('index')) - 1);
			});
		},
		goto_slide: function(n, animate, fn){
			var fn = fn;
			var animate = typeof animate === "undefined" ? true : animate;
			var n  = n || 0;
			return $(this).each(function(){
				//$.fx.off = !animate;
				var $self = $(this);
				var $kids = $self.find('.wrap-slide').children();
				var o = $self.data('options') || {};
				var i = $kids.length - 1 < n ? $kids.length - 1 : n > 0 ? n : 0;
				var l = $kids.eq(i).offset().top - $self.find('.wrap-slide').offset().top;
				
				$self.data('info', {have_prev: !(i == 0), have_next: !($kids.length - 1 == i), index: i, total: $kids.length});
				$self.data('index', i);
				
				var _fn = fn || o.fn || function(){}; _fn.apply($self, arguments);
				if(animate)
				$self.find('.wrap-slide').stop().animate({top:-l}, o.time || 800, o.easing || 'swing');
				else
				$self.find('.wrap-slide').stop().css({top:-l});
				//$self.find('.wrap-slide').stop().animate({left:-l}, o.time || 800, o.easing || 'swing', function(){ var _fn = fn || o.fn || function(){}; _fn.apply($(this).parent(), arguments);});
				
				//$.fx.off = false;
				
				//log
				try{
					/*console.log('kids: '+$kids.length);
					console.log('goto: '+i);
					console.log('left kids: '+$kids.eq(i).offset().left);
					console.log('left self: '+$self.find('.wrap-slide').offset().left);
					console.log({left:-l, animate: animate}, o.time || 800, o.easing || 'swing', fn || o.fn || function(){});*/
				}catch(e){}
				
			});
		},
		reset_slide: function(){
			var $self = $(this);
			$self.recalc_slide().goto_slide(0, false);
		},
		recalc_slide: function(){
			return $(this).each(function(){
				var $self = $(this);
				var o = $self.data('options') || {};
				var $kids = $self.find('.wrap-slide').children();
				
				var w = 10;
				
				$kids.each(function(){
					w += $(this).outerHeight(true);
				});
				
				$self.find('.wrap-slide').height((o.wrap_height || w));
	
				$kids.css({display:'block', float:'none', clear:'both'});
				
				$self.goto_slide($self.data('index'));
			});
		},
		info_slide: function(){
			return $(this).data('info');
		}
	});
	
	function registerPlugin (){
		$.each( arguments, function(i,n) {
			if (!$.fn[n]) return;

			var old = $.fn[n];
			
			$.fn[n] = function() {
				if($(this).data('slide_inited')){
					var r = old.apply($(this).find('.wrap-slide'), arguments);
					$(this).recalc_slide();
				}else
				var r = old.apply(this, arguments);
				
				return r;
			}
		});
	}
	registerPlugin('append', 'prepend', 'html');
})(jQuery);
