$(function()
{
	$('.hidableTrigger').click(function(e)
	{
		e.preventDefault();
		
		var selector = $(this).attr('href');
		
		$(selector).filter(':not(:animated)').trigger('toggle', [this]);
	});
	
	// Perform toggling of the object.
	$('.hidable').bind('toggle', function(e, trigger)
	{
		$hidable = $(this);
		var hidden = $hidable.hasClass('hidden');
		
		// Move up / down.
		$hidable.toggleClass('hidden', !hidden);
		$hidable.animate({height: hidden ? $hidable.data('naturalHeight') : 0}, 400);
		
		// Modify trigger.
		$(trigger).toggleClass('maximise');
	});
	
	// Initialise heights.
	$('.hidable').each(function()
	{
		$(this).data('naturalHeight', $(this).height());
	});
});

$(document).ready(function(){
	$("a.controlToggle").bind("click", function() {
		scriptActionEffects('','',$(this));
	});
	
	$("a.controlToggle").removeAttr("href");

});

function scriptActionEffects(id,fx,trigger) {
	var btn, obj, rel, relSplit, relObj, relFx, animSpeed;
	animSpeed 		= 200;
	animSpeedFast 	= 100;
	animSpeedMed 	= 400;
	btn				= trigger;

	if ((id != '') && (fx != '')) {
		relFx 	= fx;
		obj 	= $("#"+id);
		
	} else {

		rel 		= btn.attr("rel");
		relSplit 	= rel.indexOf(",");
		
		if (relSplit > 0) {
			relObj 	= rel.substr(0,(relSplit));
			relFx		= rel.substr(relSplit+1);
		} else {
			relObj 	= rel;
		}
		obj 	= $("#"+relObj);
	}
	
	if (obj != $(":animated")) {
	
		switch (relFx) {
			case 'fade' : 
					if (obj.hasClass("active")) {
						obj.animate({
								opacity:0
							},animSpeed,function(){
								obj.removeClass("active").css({display:"none"});
								btn.removeClass("active");
						});
					} else {
						obj.css({display:"block",opacity:0})
							.animate({
								opacity:1
							},animSpeed,function(){
								obj.addClass("active");
								btn.addClass("active");
						});
					}
				break;
			case 'slide' : 
					if (obj.hasClass("active")) {
						obj.slideUp(animSpeed,function(){
								obj.removeClass("active").css({display:"none"});
								btn.removeClass("active");
						});
					} else {
						btn.addClass("active");
						obj.slideDown(animSpeed,function(){
								obj.addClass("active").css({display:"block"});
						});
					}
				break;
			case 'move' :
					var moveHeight = obj.height();
					moveHeight = moveHeight-30;
					if (obj.hasClass("active")) {
						obj.css({display:"block",top:"0px"})
							.animate({
									// move up
									top:"-"+moveHeight+"px"
								},animSpeedMed,function(){
									obj.removeClass("active").css({display:"none"});
									btn.removeClass("active");
							});
					} else {
						obj.css({display:"block",top:"-"+moveHeight+"px"})
							.animate({
									// move down
									top:"0px"
								},animSpeedMed,function(){
									obj.addClass("active");
									btn.addClass("active");
							});
					}
				break;
			default : 
					if (obj.hasClass("active")) {
						obj.css({"display":"none"}).removeClass("active");
						btn.removeClass("active");
					} else {
						obj.css({"display":"block"}).addClass("active");
						btn.addClass("active");
					}
				break;
		}
	}	
}
