/**
 * Jolicloud Website 2.0 - http://www.jolicloud.com/
 * Download Page
 */

$(function() {
	// Init
	$('#download-sections').css({ backgroundPosition: '0px 0px' });
	$('#download-container .content').css({ marginTop: '0px' });
	var containerHeight = $('#download-container').outerHeight();
	var sectionHeight = $('#download-sections li').outerHeight();
	var containerHeight = $('#download-container').outerHeight();

	// Set the correct section
	function setSection(element) {
		// Selected states for notes
		$(element).parent().siblings().removeClass('selected');
		$(element).parent().addClass('selected');
		// Calculations
		var index = $('#download-sections li:first-child a')[0] === $(element)[0] ? 0 : 1;
		var sectionTop = sectionHeight * index;
		var containerTop = containerHeight * index;
		// Animations
		$('#download-sections').animate({
			backgroundPosition: '0 ' + sectionTop + 'px'
		}, 500, 'easeInOutQuart');
		$('#download-container .content').animate({
			marginTop: '-' + containerTop + 'px'
		}, 500, 'easeInOutQuart');
		// Anchor
		var anchor = $(element).attr('href');
		window.location.hash = anchor.substr(1, anchor.length);
	}

	// Set the correct section by checking the location hash
	function setSectionFromAnchor() {
		var anchor = window.location.hash;
		if (anchor && anchor != '') {
			$('#download-sections li a').each(function(i, element) {
				if ($(element).attr('href') == anchor && !$(element).parent().hasClass('selected')) {
					setSection($(this));
				}
			});
		} else {
			setSection($('#download-sections li.jolicloud-express a'));
		}
	}

	// Initial state
	setSectionFromAnchor();

	// Bind click events on sections
	$('#download-sections li a').click(function() {
		setSection($(this), true);
		return false;
	});

	// Bind hash change event
	$(window).bind('hashchange', function() {
		setSectionFromAnchor();
		return false;
	});
});