$(document).ready(function() {
	
	// IMAGES OVERLAY
	$("div.imgbox a").fancybox({
		'hideOnContentClick': true,
		'overlayShow': true,
		'overlayOpacity': 0.8
	});
	
	// SubFolders MouseOver
	$("div#subfolders div").hover(function(){
		$(this).css('background-color','#B9D473');	
	},function(){
		$(this).css('background-color','#A7C950');			
	});
	
	//Home Scroller
	var homeScroll = {};
	homeScroll.list = [];
	homeScroll.dict = {};
	homeScroll.activeIx = 0;
	homeScroll.intervalId = 0;
	
	$('.bannersMain').each(function(ix) {
		var obj = {},
			ref = $(this);
		
		obj.ix = ix;
		obj.id = ref.attr('id');
		obj.title = ref.children('h1').text();
		obj.descr = ref.find('.thumbsZone p').text();
		obj.img = ref.css('background-image').split("?").pop();
		obj.ref = ref.css('background-image').split("?").pop();
		obj.dom = this;
		
		homeScroll.list.push(obj);
		homeScroll.dict[obj.id] = obj;
		
		if (ix) ref.remove();
	});

	
	$('.thumbs a').click(function() {
		var id = $(this).parent().attr('id').replace(/thumb/, 'banner');
		resetInterval();
		activateBanner(id);
		return false;
	});
	
	function activateBanner(id) {
		var dom = homeScroll.dict[id].dom;
		var existing = $('.bannersMain').eq(0);
		$(dom).appendTo('.bannerDestaques .border').hide().fadeIn('slow', function() {
			existing.remove();
		});
	}
	
	function activateNextBanner() {
		homeScroll.activeIx++;
		if (homeScroll.activeIx >= homeScroll.list.length) {
			homeScroll.activeIx = 0;
		}
		var id = homeScroll.list[homeScroll.activeIx].id;
		activateBanner(id);		
	}
	
	function startLoop() {
		homeScroll.intervalId = setInterval(function(){
			activateNextBanner();
		}, 5000);
	}
	
	function resetInterval() {
		if (homeScroll.intervalId) {
			clearInterval(homeScroll.intervalId);
		}
		startLoop();
	}
	startLoop();
	
	
	
	(function() {
	   var SELECTOR = "#bannersRot a",
		   TIME = 3000,
		   items = $(SELECTOR),
		   total = items.length,
		   activeItem = 0;
	   
	   items.hide();
	   
	   function tick() {
		   items.filter(':visible').fadeOut();
		   items.eq(activeItem++).fadeIn();
		   if (activeItem >= total) activeItem = 0;
	   }
	   
	   setInterval(tick, TIME);
	   tick();
	})();
	
});
