var Sidebox = {
	init:function() {
		$('.sidebox').each(function(i,element){
			$('.sidebox-handle', element).each(function(ind,e){
				handle = $(e);
				// hook on the click event for each of the sidebox handles
				handle.bind('click', function() {
					if (!$('.sidebox-element', element).is(':animated') ) { 
						Sidebox.swap(this, element);
					}
				});
				// init opacity
				if (!handle.hasClass('active')) handle.fadeTo(0, 0.5);
			});
		
			//$('#sidebox-featured', element).css('height', $('#sidebox-container-1', element).height()+15);
		});
	},
	swap:function(target, element) {
		// find the target number
		id = target.id.match('[0-9]+');
		handle = $('.sidebox-handle.active', element).get(0);

		if (handle.id != target.id) {
			// animate handle changes
			$(handle).removeClass('active').fadeTo(500, 0.5);
			$(target).addClass('active').fadeTo(500, 1);
			
			current = $('.sidebox-element:visible', element);
			next = $('#sidebox-container-'+id, element);
			// animate picture changes
			//$('#sidebox-featured', element).animate({height:next.height()+15}, 1000);
			next.fadeIn(250);
			current.fadeOut(500);
		}

	}
};

$(document).ready(function () {
	
	Sidebox.init();
	
});