var bc = {}

bc.filters = {
	$div: null,
	change: function($me,e) {
		if ($me.parent().hasClass('active'))
			return false;
		this.$div.find('li').removeClass('active');
		$me.parent().addClass('active');
		var type = $me.attr('href').replace('#','').replace('Tasks','');
		var cs;
		switch(type) {
			case 'outstanding': cs = ['alert','incomplete']; break;
			case 'urgent': cs = ['alert']; break;
			case 'complete': cs = ['done']; break;
			case 'all': cs = ['alert','task','done']; break;
		}
		bc.filters.filter(cs);
	},
	filter: function(cs) {
		var $toHide = $('.task:visible');
		var $toShow = $();
		for (var i=0;i<cs.length;i++) {
			var c = cs[i];
			$toHide = $toHide.not('.'+c);
			$toShow = $toShow.add('.'+c+':hidden');
		}
		$toHide.slideUp(250);
		$toShow.slideDown(250);
	},
	init: function() {
		this.$div = $('#filters').eq(0);
		this.$div.find('ul a').click(function(e) {
			$me = $(this);
			bc.filters.change($me,e);
			return false;
		});
		$('div.task div.description').hide();
		$('div.task h5').css({cursor: 'pointer'}).click(function() {
			var $d = $(this).siblings('div.description');
			if ($d.hasClass('open')) {
				$d.stop().removeClass('open').slideUp(250);
			} else {
				$d.stop().addClass('open').slideDown(250);
			}
		});
	}
}

bc.crumbnav = {
	timer: null,
	init: function() {
		$('a.crumbnav').click(function() {
			window.clearTimeout(bc.timer);
			bc.crumbnav.hideAll();
			var id = $(this).attr('href').replace('#','');
			var $el = $('#'+id)
			if ($el.children().length > 1) {
				$el.toggle();
				$(this).addClass('crumbon');
			}
			return false;
		}).blur(function() {
			var id = $(this).attr('href').replace('#','');
			bc.timer = window.setTimeout(function() {
				bc.crumbnav.hideAll();
			},200);
		});
		$('.crumbnav a').click(function() {
			window.clearTimeout(bc.crumbnav.timer);
			return true;
		});
	},
	hideAll: function() {
		$('.crumbon').removeClass('crumbon');
		$('span.crumbnav').hide();
	}
}

jQuery.fn.posFix = function(options) {
	var settings = {
		speed: 500,
		minGap: 50,
		offset: null
	};
	var findPos = function(obj,axis) {
		var curtop = 0;
		var curLeft = 0;
		if (obj.offsetParent) {
			do {
				curtop += obj.offsetTop;
				curLeft += obj.offsetLeft;
			} while (obj = obj.offsetParent);
			if ('y' == axis) {
				return curtop;
			} else {
				return curLeft;
			}
		}
		return 0;
	};
	return this.each(function(){
		if (options) { 
			jQuery.extend(settings, options);
		}
		var $this = jQuery(this);
		var y = findPos(this,'y');
		var x = findPos(this,'x');
		var offset = 0;
		$this.css({ position: "absolute", top: y + "px", left: x + "px" });
		$(window).scroll(function() {
			if (($(document).scrollTop() + settings.minGap) > y) {
				offset = $(document).scrollTop() + settings.minGap;
			} else {
				offset = y;
			}
			$this.animate({ top: offset + "px" }, { duration: settings.speed, queue: false });
		});
		return true;
	});
}

$(document).ready(function() {
	bc.filters.init();
	bc.crumbnav.init();
	$('#filters').posFix({speed: 250});
});
