/**
 * @author simon
 * 
 * Use Module Pattern: http://yuiblog.com/blog/2007/06/12/module-pattern/
 */

var BA = function($) {
	
	// Private methods and properties
	
	var cycle_timer, click_timer;
	
	var resizeLists = function(e) {
		var height = 0;
		$.each(e, function(){
			height = Math.max(height, $(this).height());
		});
		$('#project-lists').css('overflow', 'hidden').animate({height: height + 'px'});
	}
	
	var filter = function() {
		var visible_categories = [];
		
		$('#categories li:not(.off)').each(function(){
			visible_categories.push(this.id.substr(9));
		});
		
		if (visible_categories.length > 0) {
			$.cookie('visible_categories', "#categories li." + visible_categories.join(', #categories li.'), 1);
		} else {
			$.cookie('visible_categories', null);
		}
		
		if (visible_categories.length > 0) {
			$('.project-list li:not(.' + visible_categories.join(', .') + ')').fadeTo('normal',.2);
			$('.project-list li.' + visible_categories.join(', .project-list li.')).fadeTo('normal', 1);
		} else {
			$('.project-list li').fadeTo('normal', 1);
		}
	}
	
	// Public methods and properties
	return {
		init : function() {
			$('#menu>li').mouseover(function(){
			    $(this).addClass("hover");
		    }).mouseout(function(){
				$(this).removeClass("hover");
		    });
		},
		initProjects : function() {
			$('#project-lists').jCarouselLite({ 
				btnNext: "#next",
				btnPrev: "#prev",
				circular: false,
				visible: 5,
				start : $('#project-lists>ul>li').length - 5
//				afterEnd : function(e){resizeLists(e);},
//				afterSetup : function(e){resizeLists(e);}
			});
			
			if ($.cookie('visible_categories')) {
				$($.cookie('visible_categories')).removeClass('off');
				filter();
			}
			
			$('#categories>li').click(function(){
				$('#categories>li').addClass('off');
				$(this).removeClass('off');
				filter();
				$('#click').fadeOut();
				clearTimeout(cycle_timer);
				clearTimeout(click_timer);
				$.cookie('category_click', 1, {path: '/', expires: 100});
				
			}).css('cursor', 'pointer');
			
			var $click = $('<img id="click">').attr('src', '/images/click.png').css({position:'absolute', display:'none', bottom:'-80px', right:'260px'});
			$click.appendTo('#header');
			if (!$.cookie('category_click')) {
				BA.cycle();
			}
			click_timer = setTimeout('$("#click").fadeIn()', 2000);
			
		},
		initGallery : function() {
			$('#thumbs a').lightBox({
				fixedNavigation:true,
				imageLoading: '/images/lightbox-ico-loading.gif',
				imageBtnPrev: '/images/lightbox-btn-prev.gif',
				imageBtnNext: '/images/lightbox-btn-next.gif',
				imageBtnClose: '/images/lightbox-btn-close.gif',
				imageBlank: '/images/lightbox-blank.gif',
				containerBorderSize: 5
			});
		},
		
		cycle : function() {
			// Get the current category selection
			var $next_cat = $('#categories>li:not(.off):first').next();
			if ($next_cat.length < 1) {
				$next_cat = $('#categories>li:first');
			}
			
			$('#categories>li').addClass('off');
			$next_cat.removeClass('off');
			filter();
			cycle_timer = setTimeout('BA.cycle()', 2500);
		}
	}
	
}(jQuery);

$(document).ready(function(){
	BA.init();
});

