﻿(function($){
	$.fn.pgallery = function(options){
		var defaults = {
			interval: 4000,
			animar: true
		};
		var gallery=$(this);
		var options = $.extend(defaults, options);
		var size=$(".images img",gallery).size();
		var index=0;
		var animation=true;
		return this.each(function(){						
			loadphoto(0);
			//var id = setInterval("loadphoto("+index+")",1000);
			//			loadphoto(index);
			//setTimeout("clearInterval("+id+")",15000);
			
			$(".pcontrols a.next", gallery).click(function(){
				index++;
				loadphoto(index);
			});
			$(".pcontrols a.prev", gallery).click(function(){
				index--;
				loadphoto(index);
			});
		});
		function loadphoto(i){
			animation=true;
			/*if(i<0){ index=i=0; animation=false;}
			if(i>(size-1)) {index=i=size-1; animation=false;}
			
			if(i>=0&&i<size) $(".pcontrols a", gallery).removeClass("disabled");
			
			if(i==(size-1)) $(".pcontrols a.next", gallery).addClass("disabled");
			if(i==0) $(".pcontrols a.prev", gallery).addClass("disabled");
			*/
			if(index==size){ index=0; i=0; }
			if(animation){
			//$(".pload", gallery).show();
			$("<img/>").load(function(){
				$(".bimage", gallery).html($(this));
				$(this).hide().fadeIn(800);
				//$(".pload", gallery).hide();
				$(".pcont", gallery).text((i+1)+" de "+size);
				$(".title", gallery).html($($(".images img", gallery).get(i)).attr("alt")).animate({top:"260px"},600);
				index++;
				setTimeout(function(){$(".title").css("top","300px"); loadphoto(index); },options.interval); 
			}).attr("src",""+$($(".images img", gallery).get(i)).attr("src")+"");
			}
			
			
		}
		
	}
})(jQuery);

