window.addEvent('domready', function(){

	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 browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);

	var lenminus = 1;
	if(browser == "Microsoft Internet Explorer")
	    lenminus = 1;
	else
	    lenminus = 2;

	var moveAmount = $('console').getStyle('width').toFloat()/div - interval * ($$('#console .panel .handle').length-lenminus);

	/*
	 * IMPORTANT!
	 * Remember that the "home" panel doesn't have a visible handle, thus the "-1" off all handle-based calcualtions
	 */
	$$('#console .panel .handle span').each(function(handle, panelNum) {
        var panel = handle.getParent('.panel');

        switch(panel.id)
        {
            case "start":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g1over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g1.jpg)'); });
                break;
            case "news":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g1over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g1.jpg)'); });
		        break;
            case "research":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g2over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g2.jpg)'); });
		        break;
            case "study":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g3over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g3.jpg)'); });
		        break;
            case "international":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g4over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g4.jpg)'); });
		        break;
            case "events":
           		handle.addEvent('mouseover', function() { this.setStyle('background', 'Green url(img/g5over.jpg)'); });
		        handle.addEvent('mouseout', function() { this.setStyle('background', 'Green url(img/g5.jpg)'); });
		        break;
        }
    });

	$$('#console .panel .handle').each(function(handle, panelNum) {
		var panel = handle.getParent('.panel');
//        alert(panel.id);
//        switch(panel.id)
//        {
//            case "start":
//           		handle.addEvent('mouseover', function() { this.setStyle('background', '#f9f5ee url(img/g2over.jpg)'); });
//		        handle.addEvent('mouseout', function() { this.setStyle('background', '#F6E8C8  url(img/g2.jpg)'); });
//                break;
//            case "news":                
//           		handle.addEvent('mouseover', function() { this.setStyle('background', '#f9f5ee url(img/g2over.jpg)'); });
//		        handle.addEvent('mouseout', function() { this.setStyle('background', '#F6E8C8  url(img/g2.jpg)'); });
//		        break;
//        }
           		//handle.addEvent('mouseover', function() { this.setStyle('background', '#f9f5ee url(img/g2over.jpg)'); });
		        //handle.addEvent('mouseout', function() { this.setStyle('background', '#F6E8C8  url(img/g2.jpg)'); });

		
		// 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');
			    if(leftVal != 87.6)
				    return;
			}
			if(leftVal == -2.4)
			    leftVal = 0;

			// Move active class to clicked on handle
			panel.addClass('active');
			
			// Move the "clicked" panel
			if(panelNum == 0)			
			    effects[panelNum] = {left: [leftVal, 0]}
			else
			    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 = $('news');
	newsPanel = $('news');
	
	startPanel.addEvent('hide', function(event)
	{
		this.removeEvents('reveal');
	});
	
	var injectIntroMovie = function() { 
		var obj = new Swiff('img/banner.swf', {
					    width: 876,
					    height: 236,
					    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')) {
		startPanel.addClass('active');
		injectIntroMovie();
		newsPanel.setStyle('left', '87.6em');
	} else {
		startPanel.addEvent('reveal', injectIntroMovie);
		newsPanel.getFirst('.handle').fireEvent('mouseup');
	}
	
});

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');
}