var showboxIsAutoplaying = true;
var showboxAutoplayIntervall = null;
var showboxItems = ['idle','bottom','open','case','hardware'];
var showboxCurrentItem = 0;
var showboxTimings = {'side': 1, 'hardware': 6};
var showboxBoxxes = {'case': 'showbox_desc_case'
					,'hardware': 'showbox_desc_hardware' 
					,'idle': 'showbox_desc_idle' 
					,'bottom': 'showbox_desc_bottom' 
					};
var showBoxMenuEnabled = ['idle', 'bottom', 'bottom_idle', 'case', 'case_idle', 'hardware', 'hardware_idle', 'to_idle' ];
var showboxLastButton;
var showboxLastBox;
var showboxAutoplayTarget;
var showboxAutoplaySpeed = 3000;

window.addEvent('domready', function(){

	if( swfobject.getFlashPlayerVersion().major < 9 )
		return;
	
	// hide menu
	$$('ul.showbox_menu').setStyle('display', 'none');
	
	$$('.showbox_menu_wrap').removeClass('plain');
	
	// save boxxes
	for( part in showboxBoxxes ){
		var box = $( showboxBoxxes[part] );
		box.setStyle('display','none');
		box.addClass('flash');
		box.injectTop('showbox_stage_wrap');
	}
	
	$('showbox_stage').setHTML('');
	var flashvars = {srcPath:'/images/showbox/images'};
	var params = {allowScriptAccess:'sameDomain', wmode: 'transparent'};
	var attributes = {};
	swfobject.embedSWF('images/showbox/pyrexx_showbox_px1.swf', 'showbox_stage', '800', '450', '9.0.0', 'images/showbox/expressInstall.swf', flashvars, params, attributes );


	function showbox_queue( srcID ){
		$('showbox_stage').queue( srcID );
	}
	function showbox_queue_manual( srcID ){
		if( showboxLastBox && showboxLastBox != srcID ){
			var boxFX = new Fx.Style( $( showboxBoxxes[ showboxLastBox ] ) ,'opacity'
					,{duration: 200, onComplete: function(e){
						e.setStyle('display','none');
					}
			}).start(1,0);
		}
		
		$('showbox_stage').queue( srcID, true );
	}
	
	function preload_done(){

		$('wrap_showbox_autoplay').setStyle('display','inline');
		$('btn_showbox_autoplay').addEvent( 'click', function(e){
			var event = new Event(e);
			event.stop(); 
			showboxAutoplayToggle(); 
		});
		
		showboxItems.each( function(part){
			var btn = $('btn_showbox_'+ part );
			btn.addClass('loading');
			if( btn )
				btn.addEvent( 'click', function(e){
					var event = new Event(e);
					event.stop();
					
					showboxLastButton.removeClass('active');
					
					showbox_queue_manual(part);
					showboxAutoplayStop();
					
					if( part == 'open' ){
//						showboxIsAutoplaying = true;
//						showboxAutoplayAdvance();
						showboxAutoplayStart();
					}
				});

		});
	};

	preload_done();
	
});








 
function showboxAutoplayToggle(){
	if( showboxIsAutoplaying ){
		showboxAutoplayStop();
	}else{
		showboxAutoplayStart();
	}
}
	
function showboxAutoplayStop(){
	showboxIsAutoplaying = false;
	clearInterval( showboxAutoplayIntervall );
}

function showboxAutoplayStart(){
	showboxAutoplayStop();
	showboxIsAutoplaying = true;
	showboxAutoplayIntervall = setInterval( showboxAutoplayAdvance, showboxAutoplaySpeed );
}

function showboxAutoplayAdvance(){
	if( !showboxIsAutoplaying )
		return;
	
	clearInterval( showboxAutoplayIntervall );
	
	showboxCurrentItem++;
	if( showboxCurrentItem >= showboxItems.length )
		showboxCurrentItem = 0;
	
	showboxAutoplayTarget = showboxItems[ showboxCurrentItem ];
	$('showbox_stage').queue( showboxItems[ showboxCurrentItem ] );
}
	
function preloadUpdate( srcID ){
	var btn = $('btn_showbox_'+ srcID);
	if( !btn )
		return;
	
	btn.removeClass('loading');
	btn.addClass('ready');
	
	if( srcID == 'hardware' )
		showboxAutoplayStart();
	
}
function preloadError( srcID ){
}
function playingStaticLoop(){
	playingLoop();
}
function playingLoop(){
}
function playingUpdate( srcID ){
	
	if( showBoxMenuEnabled.indexOf( srcID ) > -1 ){
		if( $$('ul.showbox_menu')[0].getStyle('opacity') != 1 || $$('ul.showbox_menu')[0].getStyle('display') == 'none' ){
			$$('ul.showbox_menu').setStyle('display', '');
			var menuFX = new Fx.Style( $$('ul.showbox_menu')[0] ,'opacity' ).start(0,1);
		}
	}else{
		$$('ul.showbox_menu').setStyle('display', 'none');
		var menuFX = new Fx.Style( $$('ul.showbox_menu')[0] ,'opacity'
				,{duration: 200, onComplete: function(e){
						e.setStyle('display','none');
					}
				}
		).start(1,0);
	}
	
	// update autoplay position
	var foundPosition = showboxItems.indexOf( srcID );
	if( foundPosition > -1 )
		showboxCurrentItem = foundPosition;
	
	if( showboxIsAutoplaying && showboxAutoplayTarget == srcID ){
		var delay = showboxAutoplaySpeed;
		if( showboxTimings[srcID] !== undefined )
			delay = showboxTimings[srcID] * 1000;
		
		showboxAutoplayIntervall = setInterval( showboxAutoplayAdvance, delay );
	}
	
	if( showboxLastButton )
		showboxLastButton.removeClass('active');
	
	if( srcID.indexOf('_') > 0 )
		srcID = srcID.substr(0, srcID.indexOf('_') );
	
	showboxLastButton = $('btn_showbox_'+ srcID );
	if( showboxLastButton )
		showboxLastButton.addClass('active');

	if( showboxLastBox && showboxLastBox != srcID ){
		var boxFX = new Fx.Style( $( showboxBoxxes[ showboxLastBox ] ) ,'opacity'
				,{duration: 200, onComplete: function(e){
					e.setStyle('display','none');
				}
		}).start(1,0);
	}	
	if( showboxBoxxes[ srcID ] && showboxLastBox != srcID ){
		showboxLastBox = srcID;
		var boxFX = new Fx.Style( $( showboxBoxxes[ showboxLastBox ] ) ,'opacity',{
				wait: 1000, duration: 300, onStart: function(e){
					e.setStyle('opacity',0);
					e.setStyle('display','');
				}
				,onComplete: function(e){
					e.setStyle('display','');
					e.setStyle('opacity',1);
					e.setStyle('visibility','visible');
				}
		}).start(0,1);
		
	}
	
	
}
