(function($){
	$.DivopenBlog = {
		atual : 0
		,porPagina:5
		,fotos:[]
		,_init:function(){
			var me = this;
			me.setasFoto();
			$('.foto img').bind("load", function (e) {
				$(this).fadeTo('fast', 1);
			});
			$('#left')
				.click(function(e){
					me.paginacao(-1);
				});
			$('#right')
				.click(function(e){
					me.paginacao(1);
				});
			me.criarThumbs(0);
			me.trocarImagem(0);
		}
		,bindObjs:function(){
			var me = this;
			$('.thumbs li a')
				.unbind('click')
				.bind('click',function(e){
					var id = $(this).parents("li").attr("id");
					me.trocarImagem(id);
				});
		}
		,paginacao:function(inc){
			var me = this;
			me.criarThumbs(me.atual+(inc*me.porPagina));
		}
		,criarThumbs:function(pos){
			var me=this;
			pos=pos>=me.fotos.length-1?me.fotos.length-1:pos;
			pos=pos==0?0:Math.floor(pos/me.porPagina);
			pos=Math.max(pos,0);

			if(pos <= Math.floor(me.fotos.length/me.porPagina)){
				var html=""
					,ini=pos*me.porPagina
					,fim=(pos*me.porPagina)+me.porPagina
				;
				html = '<ul class="thumbs">';
				for(var x=ini;x<fim;x++)
					if(me.fotos[x])
						html += '<li id="'+x+'"><a href="javascript:;"><img src="'+me.fotos[x].p+'" alt="" /></a></li>';
				html += "</ul>";
				$('.box').html(html);
				me.bindObjs();
				if(pos!=Math.floor(me.atual/me.porPagina)){
					me.trocarImagem(me.porPagina * pos);
				}
			}
		}
		,trocarImagem:function(i){
			var 
				me = this
				,pos=i
				,i=i>=0?i:me.atual*me.porPagina
			;
			if(me.fotos[i] && me.fotos[i].g!=$('.foto img').attr('src')){
				$('.foto img')
					.fadeTo('fast', .01)
					.queue(function () {
						$(this)
						.dequeue()
						.attr('src', me.fotos[i].g);
					})
				;
				$('.container-foto .descricao').text(me.fotos[i].descricao);
				me.atual = parseInt(pos);
				me.criarThumbs(pos);
			}
		}
		,setasFoto:function() {
			var me = this;
			$(".arrow-foto")
				.css("opacity",0)
			;
			$(".arrow-foto")
				.hover(
					function() {
						$(this).animate({opacity:.6})
					},
					function() {
						$(this).animate({opacity:0})
					}
				)
				.click(function() {
					var id = $(this).attr('id');
					var atual = parseInt(me.atual);
					if (id == "arrowleft-foto") {
						var i = (((atual-1 % me.fotos.length) + me.fotos.length) % me.fotos.length);
						me.atual = i;
						me.trocarImagem(i);
					} else {
						var i = (((atual+1 % me.fotos.length) + me.fotos.length) % me.fotos.length);
						me.atual = i;
						me.trocarImagem(i);
					}
				})
			;
		}
	}
})(jQuery);