window.addEvent('domready', function(){

	var opera = Browser.Engine.presto;
	var panels = $$('#console .panel');
	// Init transitions
	var panelFx = new Fx.Elements(panels, {wait: false, duration: 1000});
	panelFx.options.transition = new Fx.Transition(Fx.Transitions.Back, 0.7).easeOut;
	panelFx.options.unit = 'em';
	
	if (Browser.Engine.trident) {var div = 1; } else { var div =10; }
	
	// Calculate handle width (with a hack for Opera)
	if (Browser.Engine.presto) var interval = 2.4; 
	else var interval = $('console').getElement('.handle').getStyle('width').toFloat()/div -0.1;

	// Amount by which to move each panel, this should be the same as the content viewport
	
	var moveAmount = $('console').getStyle('width').toFloat()/div - interval * ($$('#console .panel .handle').length-1);

	/*
	 * IMPORTANT!
	 * Remember that the "home" panel doesn't have a visible handle, thus the "-1" off all handle-based calcualtions
	 */

	$$('#console .panel .handle').each(function(handle, panelNum) {
		var panel = handle.getParent('.panel');

		handle.addEvent('mouseover', function() { this.setStyle('background', '#f9f5ee url(images/console/handle_bg_on.gif)'); });
		handle.addEvent('mouseout', function() { this.setStyle('background', '#F6E8C8  url(images/console/handle_bg_off.gif)'); });
		
		// Add handle click event
		handle.addEvent('mouseup', function(event) {
			var effects = {};
			var leftVal = panel.getStyle('left').toFloat();

			// If the "active" panel was clicked on, move backwards instead of forwards
			if (panel.hasClass('active')) {
				previousPanel = panel.getPrevious().getElement('.handle');
				if (previousPanel) previousPanel.fireEvent('mouseup');
				return;
			}
			
			// Move active class to clicked on handle
			panel.addClass('active');
			
			// Move the "clicked" panel
			effects[panelNum] = {left: [leftVal, (panelNum-1) * interval]}
			
			panels.each(function(otherPanel, otherPanelNum) 
			{
				// Move the panels before the clicked panel
				var otherLeftVal = otherPanel.getStyle('left');
				otherLeftVal = (otherLeftVal.contains('px')) ? otherLeftVal.toFloat() / 10 : otherLeftVal;
				if (panelNum > otherPanelNum) effects[otherPanelNum] = {left: [otherLeftVal, (otherPanelNum-1) * interval]};
				if (panelNum < otherPanelNum)  effects[otherPanelNum] = {left: [otherLeftVal, ((otherPanelNum-1) * interval) + moveAmount]};
				
				if (otherPanel.hasClass('active') && otherPanelNum != panelNum)	{
					otherPanel.removeClass('active');
					otherPanel.fireEvent('hide');
				}				
			});
				
			panelFx.start(effects).chain(
			   function(){ $$('#console .panel.active')[0].fireEvent('reveal'); }
			);

		});
	});
	
	startPanel = $('start');
	newsPanel = $('news');
	
	startPanel.addEvent('hide', function(event)
	{
		this.removeEvents('reveal');
	});
	
	var injectIntroMovie = function() { 
		var obj = new Swiff('images/console/start/console-v'+$random(2,3)+'.swf', {
					    width: 920,
					    height: 230,
					    container: startPanel.getFirst('.content'),
					    params: {
					        bgcolor: '#000'
					    }
		}); 
	}
	
	// Simulate click on home panel
	seenMovieToday = Cookie.read('seenMovieToday');
	dontShowAgain = Cookie.read('dontShowAgain');
	if ((seenMovieToday != 1) && (dontShowAgain != 'true') && !opera) {
		startPanel.addClass('active');
		injectIntroMovie();
		newsPanel.setStyle('left', '87.6em');
	} else {
		startPanel.addEvent('reveal', injectIntroMovie);
		newsPanel.getFirst('.handle').fireEvent('mouseup');
	}

	if (opera) { startPanel.getFirst('.handle').removeEvents(); }
	
});

function consoleMovieCompleted() {
	Cookie.write("seenMovieToday", 1);
	if ($('start').hasClass('active')) $('news').getFirst('.handle').fireEvent('mouseup'); 
}

function setShowMovieStatus(show) {
	Cookie.write('dontShowAgain', show, {duration: 365});
}

function getShowMovieStatus() {
	return Cookie.read('dontShowAgain');
}



