// Função de Timeout
(function($) {

	if (typeof $.timeout != "undefined") return; 

	$.extend({
	  timeout : function (func,delay) {
			// init
			if (typeof $.timeout.count == "undefined") $.timeout.count = 0; 	
			if (typeof $.timeout.funcs == "undefined") $.timeout.funcs = new Array(); 
			// set timeout
			if (typeof func =='string') return setTimeout(func, delay); 
			if (typeof func =='function') {
				$.timeout.count++;
				$.timeout.funcs[$.timeout.count] = func;
				return setTimeout("$.timeout.funcs['"+$.timeout.count+"']();", delay);
			}
		},
	  interval : function (func,delay) {
			// init
			if (typeof $.interval.count == "undefined") $.interval.count = 0; 	
			if (typeof $.interval.funcs == "undefined") $.interval.funcs = new Array(); 
			// set interval
			if (typeof func =='string') return setInterval(func, delay); 
			if (typeof func =='function') {
				$.interval.count++;
				$.interval.funcs[$.interval.count] = func;
				return setInterval("$.interval.funcs['"+$.interval.count+"']();", delay);
			}
		},
	  idle : function (func,delay) {
			// init
			if (typeof $.idle.lasttimeout == "undefined") $.idle.lasttimeout = null;
			if (typeof $.idle.lastfunc == "undefined") $.idle.lastfunc = null;
			// set idle timeout
			if ($.idle.timeout) { clearTimeout($.idle.timeout); $.idle.timeout = null; $.idle.lastfunc = null; }
			if (typeof(func)=='string') { 
				$.idle.timeout = setTimeout(func, delay); 
				return $.idle.timeout;
			}		
			if (typeof(func)=='function') { 
				$.idle.lastfunc = func;
				$.idle.timeout = setTimeout("$.idle.lastfunc();", delay);
				return $.idle.timeout;
			}
		},
	  clear : function (countdown) {
		clearInterval(countdown);
		clearTimeout(countdown);
	  }	
		
	});
})(jQuery);

(function($){
	$.B86slidePortfolio = function (options) {
		var defaults = {
			images: ['a.jpg', 'b.jpg', 'c.jpg']
		};
		var options = $.extend(defaults, options);

		// CLIQUE
		$("#B86slide-list li a").click(function(){
			index_click = $("#B86slide-list li a").index(this);
			$next = $("#B86slide-list li:eq("+ index_click +")");
			$curr = $("#B86slide-list li.active");

			// 3. realiza a troca
			$("#B86slide-image").toggleClass('load');

			// carregar imagem (ajax)
			// http://jqueryfordesigners.com/image-loading/
   			$(function () {
       			var img = new Image();
       			$(img).load(function () {
           			//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
           			$(this).hide();
           			$("#B86slide-image").toggleClass('load').html(this);
           			$(this).fadeIn();
       			}).error(function () {
            		// notify the user that the image could not be loaded
        		}).attr('src', options.images[index_click]);
    		});

			// alterar estilo
			$curr.toggleClass("active");
			$next.toggleClass("active");

			// ao clicar, reiniciar timeout
			clearInterval(interval);
			initInterval();

			// não redirecionar para link do href
			return false;
			 
		}); // fim do clique

		// TIMEOUT
		function initInterval(){
			interval = $.interval(function(){
				// 1. identifica index de elementos
				index_active = $("#B86slide-list li").index($("#B86slide-list li.active"));
				index_ultimo = $("#B86slide-list li").index($("#B86slide-list li:last"));
	
				// 2. define elemento a ser trocado
				if(index_active==index_ultimo){
					$next = $("#B86slide-list li:first");
					index_next = 0;
				}else{
					index_next = index_active + 1;
					$next = $("#B86slide-list li:eq("+ index_next +")");
				}
				$curr = $("#B86slide-list li.active");
	
				// 3. realiza a troca
				$("#B86slide-image").toggleClass('load');
	
				// function load image
				// http://jqueryfordesigners.com/image-loading/
	   			$(function () {
	       			var img = new Image();
	       			$(img).load(function () {
	           			//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
	           			$(this).hide();
	           			$("#B86slide-image").toggleClass('load').html(this);
	           			$(this).fadeIn();
	       			}).error(function () {
	            		// notify the user that the image could not be loaded
	        		}).attr('src', options.images[index_next]);
	    		});
					
				$curr.toggleClass("active");
				$next.toggleClass("active");
			}, 5000); // fim do timeout
		}
		initInterval();
	} // fim da função
	
})(jQuery);