/**
 * Multislider tool for the Meteotest homepage
 * 
 * @author Christian Studer <christian.studer@meteotest.ch>
 */
var currentProject = '';

// Set up buttons and the first slider once the document is ready
$(document).ready(function() {
	// Buttons
	$("#multisliderbuttons").buttonset();
	$("#multisliderbuttons label").click(switchCurrentProject);

	// Store current project
	currentProject = project;

	// Treat options
	if (multiOptions.hideControls) {
		$("#slidercontrols").hide();
	}
	
	// Check for anchor
	if($(location).attr('hash')) {
		var hash = $(location).attr('hash').substr(1);
		
		// First: Go by id
		$('label[for=radio_' + hash + ']').click();
		
		// Second: Go by title
		$.each(projects, function (index, value) {
			if(value.title == hash) {
				$('label[for=radio_' + value.project +  ']').click();
			}
		});
	}
});

/**
 * Switch current project to a new one
 */
function switchCurrentProject() {
	switchToProject(($(this).attr('for')).substr(6));
}

/**
 * Switch to a new project
 * 
 * @param string newProject
 */
function switchToProject(newProject) {
	if (currentProject != newProject) {
		// New project selected
		project = currentProject = newProject;
		$.getJSON('/meteotest-extras/slider/service.php?project=' + project,
				installProject);
		
		// Add hash
		$(location).attr('hash', project);
	}
}

