(function($) {

	$.fn.pontoscroll = function(settings) {

		var defaults = {
			pos:'h',
			visivel: 3,
			larguraFoto: 120,
			alturaFoto: 97,
			auto:false
		}

		var settings = $.extend(defaults, settings);

		return this.each(function() {

			var me = this;

			var $element = $(this);

			var atual = 0;
			var idAuto = 0;
			var total = $element.find('li').length;

			$.scrollTo.defaults.axis = 'x';
			$(this).scrollTo(0);

			this.init = function() {
				atual = settings.visivel;
				if (settings.auto) me.passarAuto();
			}
			
			this.passarAuto = function() {
				idAuto = setInterval(function() {  
					if (settings.pos == 'h') {
						me.moveRight();
					} else {
						me.moveBot();
					}
				}, 3000);
			};

			this.bindTop = function() {
				$element.parent().find('.arrow-top').bind('click',function() { me.moveTop() });
			};

			this.bindBot = function() {
				$element.parent().find('.arrow-bot').bind('click',function() { me.moveBot() });
			};

			this.bindLeft = function() {
				$element.parent().find('.scrollfotos-arrowleft').bind('click',function() { me.moveLeft() });
			};

			this.bindRight = function() {
				$element.parent().find('.scrollfotos-arrowright').bind('click',function() { me.moveRight() });
			};

			switch(settings.pos) {
				case 'v':
					me.bindTop();
					me.bindBot();

					me.moveTop = function() {
						if($element.is(':not(:animated)')){
							if (atual > settings.visivel) {
								atual--;
								$element.scrollTo({top:'-='+settings.alturaFoto, left:0}, 600);
							} else {
								$element.scrollTo(settings.alturaFoto*(total-settings.visivel), 1000);
								atual = total;
							}
						}
					};

					me.moveBot = function() {
						if($element.is(':not(:animated)')){
							if ((total > settings.visivel) && (atual < total)) {
								atual++;
								$element.scrollTo({top:'+='+settings.alturaFoto, left:0}, 600);
							} else {
								atual = settings.visivel;
								$element.scrollTo(0, 1000);
							}
						}
					};
				break;

				case 'h':
					me.bindLeft();
					me.bindRight();

					me.moveLeft = function() {
						if($element.is(':not(:animated)')){
							if (atual > settings.visivel) {
								atual--;
								$element.scrollTo({left:'-='+settings.larguraFoto, top:0}, 600);
							} else {
								$element.scrollTo(settings.larguraFoto*(total-settings.visivel), 1000);
								atual = total;
							}
						}
					};

					me.moveRight = function() {
						if($element.is(':not(:animated)')){
							if ((total > settings.visivel) && (atual < total)) {
								atual++;
								$element.scrollTo({left:'+='+settings.larguraFoto, top:0}, 600);
							} else {
								atual = settings.visivel;
								$element.scrollTo(0, 1000);
							}
						}
					};
				break;
			}
			this.init();
		});
	}
})(jQuery);
