$(document).ready(function() {

	var currentPanelID = "#panel-1";
	//Get the height of the first item
	$('#mask').css({'height':$('#panel-1').height()});	
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div.panel-items').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div.panel-items').width($('#mask').width());
	
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
	
		currentPanelID = $(this).attr('href');
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		
		//Discard the link default behavior
		return false;
	});
	
	$('div.scrolled-contents div.left-btn').click(function() {
		var currentIDNumber = currentPanelID.substr(7, 1);
		var prevNumber = parseInt(currentIDNumber)-1;
		
		if(prevNumber>0)
		{
			currentPanelID = '#panel-'+prevNumber;
			var panelheight = $('#panel-'+prevNumber).height();
			$('#mask').animate({'height':panelheight},{queue:false, duration:500});
			$('#mask').scrollTo('#panel-'+prevNumber, 800);
		}
	});
	
	$('div.scrolled-contents div.right-btn').click(function() {
		var currentIDNumber = currentPanelID.substr(7, 1);
		var nextNumber = parseInt(currentIDNumber)+1;
		
		if($('#panel-'+nextNumber).length>0)
		{
			currentPanelID = '#panel-'+nextNumber;
			var panelheight = $('#panel-'+nextNumber).height();
			$('#mask').animate({'height':panelheight},{queue:false, duration:500});
			$('#mask').scrollTo('#panel-'+nextNumber, 800);
		}
	});	
});
