$(document).ready(function(){

	if ($("#home-page").length>0){
		// Set up homepage according to values in cookie
		if (lm1=$.cookie('praxis_home_lm1')){		// Cookie is set, so read all values
			lm3=$.cookie('praxis_home_lm3')
		} else {							// No cookie, so initialise as empty arrays
			lm1 = '[]';
			$.cookie('praxis_home_lm1', lm1);
			lm3 = '[]';
			$.cookie('praxis_home_lm3', lm3);
		}
		lm1=eval(lm1);						// Parse the string received from the cookie
		lm3=eval(lm3);	

		// Loop through each item, and set it to it's saved value - also add an id tag, so we can track movement
		c=$(".list-min1").length;
		do {c--;n=$(".list-min1").eq(c);n.attr('id', 'lm1_'+c);n=n.children();if (!lm1[c]){lm1[c]=1};for (i=1; i<lm1[c];i++){n=n.next();};n.addClass('last');n.next().hide();} while (c>0);

		c=$(".list-min3").length;
		do {c--;n=$(".list-min3").eq(c);n.attr('id', 'lm3_'+c);n=n.children();if (!lm3[c]){lm3[c]=4};for (i=1; i<lm3[c];i++){n=n.next();};n.addClass('last');n.next().hide();} while (c>0);
	}

	// Home Page
	$("#home-page div.control ul li.minus a").click(function(){
		$(this).parent().parent().parent().next().reduceList();
	});

	$("#home-page div.control ul li.plus a").click(function(){
		$(this).parent().parent().parent().next().increaseList();
	});

	$(".home-list").find(".home-list-item:odd").addClass('stripe');

	// Collapsable items
	$("#collapsable div.control").click(function(){
		if ($(this).hasClass('open')){
			$(this).find(".minus").hide();
			$(this).find(".plus").show();
			$(this).next().slideUp("fast");
			$(this).removeClass('open');
		} else {
			$(this).find(".minus").show();
			$(this).find(".plus").hide();
			$(this).next().slideDown("fast");
			$(this).addClass('open');
		}
	});


	$("#collapsable div.details").hide();

	// Tooltips
	$(".info-tooltip").hover(
		function(){
			$(this).next().css("z-index", 1000);
			$(this).next().fadeIn("fast");
		},
		function(){
			$(this).next().fadeOut(0);
	});
});

jQuery.fn.reduceList = function(){
	min = ($(this).hasClass('list-min1')) ? 0 : 3;
	if (!$(this).children().eq(min).hasClass('last')){
		$(this).find(".last").eq(0).slideUp("fast");
		$(this).find(".last").eq(0).prev().addClass('last');
		d=$(this).attr("id").split("_");
		eval(d[0])[d[1]]--;	
		saveState();
	}
}
jQuery.fn.increaseList = function(){
	if ($(this).children(":last").prev().hasClass('last')){
		$(this).find(".last").eq(1).slideDown("fast");
		$(this).find(".last").eq(0).removeClass('last');
		d=$(this).attr("id").split("_");
		eval(d[0])[d[1]]++;
		saveState();
	}
}
saveState = function(){
	$.cookie('praxis_home_lm1', '['+lm1.toString()+']');
	$.cookie('praxis_home_lm3', '['+lm3.toString()+']');
}



