var galeria;
var dadosGaleriaFotos = [];

(function($) {

/*************************************************************
												PRELOAD
*************************************************************/

	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = 0; i < args_len; i++) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			dadosGaleriaFotos.push(cacheImage);
		}
	}

/*************************************************************
												GALERIA
*************************************************************/
	
	$.Galeria = function (el) {

		var me = this;
		var $element = this.$element = $(el);
		var $containerFoto = $element.find('#galeria_foto');
		var indice = 0;
	
		this.init = function() {
			me.bindclick();
		};
	
		this.show = function() {
			$containerFoto.html(dadosGaleriaFotos[indice]);
		};
	
		this.click = function (e) {
			var $obj = $element.find("#galeria_thumbs li");
	
			indice = $obj.index($(e.target).closest('li'));
			if ($obj.hasClass('current')) $obj.removeClass('current');
			$obj.eq(indice).addClass('current');
			me.show();
		};
	
		this.bindclick = function(e){
			$element.find('#galeria_thumbs li a').bind("click",function(e) { me.click(e) });
		};
	
		this.unbindclick = function(){
			$element.find('#galeria_thumbs li a').unbind("click");
		};
	
		this.init();
	
	};
	
/*************************************************************
												PONTO SCROLL
*************************************************************/
	
	$.fn.pontoscroll = function(settings) {

		var defaults = {
			pos:'h',
			visivel: 1,
			alturaFoto: 73,
			larguraFoto: 400
		}

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

		return this.each(function() {

			var me = this;

			var $element = $(this);

			var atual = 0;
			var total = $element.find('.group_scroll').children().length;
			
			this.init = function() {
				atual = settings.visivel;
				$element.scrollLeft(0);
				$element.scrollTop(0);
			}

			this.bindTop = function() {
				$element.parents('.pontoscroll').find('.arrow_top').bind('click',function() { me.moveTop() });
			};

			this.bindBot = function() {
				$element.parents('.pontoscroll').find('.arrow_bot').bind('click',function() { me.moveBot() });
			};

			this.bindLeft = function() {
				$element.parents('.pontoscroll').find('.arrow_left').bind('click',function() { me.moveLeft() });
			};
	
			this.bindRight = function() {
				$element.parents('.pontoscroll').find('.arrow_right').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.animate({scrollTop:'-='+settings.alturaFoto},{queue:false});
							} else {
								$element.animate({scrollTop:settings.alturaFoto*(total-settings.visivel)},{queue:false});
								atual = total;
							}
						}
					};

					me.moveBot = function() {
						if($element.is(':not(:animated)')){
							if ((total > settings.visivel) && (atual < total)) {
								atual++;
								$element.animate({scrollTop:'+='+settings.alturaFoto},{queue:false});
							} else {
								atual = settings.visivel;
								$element.animate({scrollTop:0},{queue:false});
							}
						}
					};
				break;

				case 'h':
					me.bindLeft();
					me.bindRight();
	
					me.moveLeft = function() {
						if($element.is(':not(:animated)')){
							if (atual > settings.visivel) {
								atual--;
								$element.animate({scrollLeft:'-='+settings.larguraFoto},{queue:false});
							} else {
								$element.animate({scrollLeft:settings.larguraFoto*(total-settings.visivel)},{queue:false});
								atual = total;
							}
						}
					};

					me.moveRight = function() {
						if($element.is(':not(:animated)')){
							if ((total > settings.visivel) && (atual < total)) {
								atual++;
								$element.animate({scrollLeft:'+='+settings.larguraFoto},{queue:false});
							} else {
								atual = settings.visivel;
								$element.animate({scrollLeft:0},{queue:false});
							}
						}
					};
				break;
			}
			this.init();
		});
	};
	
	$(function() {
			
		// MAIN
			
	});

})(jQuery);
