var version = 'v3.0.2 Non-Distributable Release - 19/01/2010';

// Promo config
var promo_enabled = true;
var promo_id = 'BRAND-100205'; // upload swf to /images/console/start/promos/PROMO_ID.swf

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 (opera) { 
		var interval = 2.5; 
		var consoleWidth = $('console').getStyle('width').toFloat() - 15; 
	} else { 
		var interval = $('console').getElement('.handle').getStyle('width').toFloat()/div -0.0;
		var consoleWidth = $('console').getStyle('width').toFloat();
	}

	// Amount by which to move each panel, this should be the same as the content viewport	
	var moveAmount = consoleWidth / 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-color', '#3ea973'); });
		handle.addEvent('mouseout', function() { this.setStyle('background-color', '#49be82'); });
		
		// 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]};
					otherPanel.addClass("close");
				} 
				if (panelNum < otherPanelNum) { 
					effects[otherPanelNum] = {left: [otherLeftVal, ((otherPanelNum-1) * interval) + moveAmount]};
					otherPanel.removeClass("close");
				} 
				
				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() { 
		if (!promo_enabled) {
			var swf_file = 'console-v'+$random(2,3)+'.swf';
		} else {
			var swf_file = 'promos/'+promo_id+'.swf';
		}
		
		// New width 836px
		var obj = new Swiff('images/console/start/'+swf_file, {
					    width: 900,
					    height: 200,
					    container: startPanel.getFirst('.content'),
					    params: {
					        bgcolor: '#000',
					        wmode : "transparent",
						salign : "lt",
						loop : "true"
					    }
		}); 
	}
	
	seenMovieToday = Cookie.read('seenMovieToday');
	//firefseenMovieToday = 0;
	dontShowAgain = Cookie.read('dontShowAgain');	
	currentPromoExposure = getPromoExposure();
	
	if ( (((seenMovieToday != 1) && (dontShowAgain != 'true')) || (promo_enabled && !currentPromoExposure)) && !opera ) {
		startPanel.addClass('active');
		injectIntroMovie();
		//newsPanel.setStyle('left', '84.1em');
		newsPanel.setStyle('left', '75.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');
}


function getPromoExposure() {
	if (promo_enabled) {
		lastPromo = Cookie.read('consolePromo');
		Cookie.write('consolePromo', promo_id, {duration: 365});
		if (lastPromo == promo_id || promo_id == '') return true;
		else return false;
	} else return true;
}
