var webroot = '';
var webroot_url = '';
var flash_mode = true;
var lang = 'pl';
var you_are_allowed_to_start = true;
var flash_is_loaded = false;

var occupied = false; //kiedy nie anlezy ruszac niczego ;[

var page_state; // 0 -normalna, 1 - content na wierzchu

function initialize()
{

	page_state = 0;

	//$('loading_screen').style.width = $('main_container').getWidth() + "px";
	//$('loading_screen').style.height = $('main_container').getHeight() + "px";
	
	$('loading_screen').hide();
	$('loading_screen').style.visibility = "visible";
	$('loading_screen').show();
	$('loading_background').setOpacity(0.8);
	
	//$('skip_first_intro').show();

	resize();

	if (flash_mode)
	{
		var flashvars = {};
		flashvars.url = webroot_url;
		flashvars.lang = 0;
		if (lang == 'pl') {
			flashvars.lang = 0;
		}
		else
		{
			flashvars.lang = 1;
		}
		var params = {};
		params.wmode = "opaque";
		params.bgcolor = "000000";
		params.allowscriptaccess = "always";
		var attributes = {};
		attributes.align = "middle";
		swfobject.embedSWF(webroot+"simplebox/simplebox.swf", "flash_container", "100%", "100%", "9.0.0", webroot+"expressInstall.swf", flashvars, params, attributes);
		
		//wstawiamy interko
		/*flashvars = {};
		flashvars.url = webroot_url;
		params = {};
		params.bgcolor = "FFFFFF";
		params.allowscriptaccess = "always";
		params.wmode = "transparent";
		attributes = {};
		attributes.align = "middle";
		swfobject.embedSWF(webroot+"intro/main.swf", "floating_intro_inner", 640, 480, "9.0.0", webroot+"expressInstall.swf", flashvars, params, attributes);*/
	}
	else
	{
		flashLoaded();
	}
	
}

function flashPreloaded()
{
	you_are_allowed_to_start = true;
}

function flashLoaded()
{
	flash_is_loaded = true;
	//$('flash_container').allow_me();
	//$('volume_reg').appear();
	$('loading_screen').fade();
	//$('skip_first_intro').hide();
	$('skip_intro').show();
	
	if (!flash_mode)
	{
		the_fly_is_over(); //DEBUG MODE!
	}
}

function resize()
{
	//if (page_state == 0)
	{
		//$('flash_table').width = document.viewport.getWidth();
		//$('flash_table').height = document.viewport.getHeight();
		
		$('main_cell').width = Math.round(document.viewport.getWidth() * 0.8);;
		$('main_cell').height = document.viewport.getHeight() - 200;
		
		$('flash_container').width = document.viewport.getWidth() - 100;
		$('flash_container').height = document.viewport.getHeight() - 120;
		
		//jeszcze przestawiamy loadinga
		$('loading_screen').style.width = $('main_container').getWidth() + "px";
		$('loading_screen').style.height = $('main_container').getHeight() + "px";
		
		//centrujemy intro
		if ($('floating_intro'))
		{
			$('floating_intro').style.left = Math.round(($('main_cell').getWidth() / 2) - ($('floating_intro').getWidth() / 2)) + "px";
			$('floating_intro').style.top = Math.round(($('main_cell').getHeight() / 2) - ($('floating_intro').getHeight() / 2)) + "px";
		}
		//console.log($('main_cell').getWidth());
		//$('loading_screen').style.lineHeight = $('main_container').getHeight() + "px";
	}
}

//callbacki dla flasha

function door_opened(action)
{
	/*new Ajax.Request(webroot+'news', {
  		onSuccess: function(transport) {
      		
      		$('content_container').innerHTML = transport.responseText;
      		$('overdiv').appear();
      		
  		},
  		
  		evalScripts:true
	});*/
	
	//ukrywamy flasha i pokazujemy lodaer
	$('loading_screen').show();
	
	//teraz na podstawie ustawionych zmiennych ustalamy co pokazac
	var url = webroot + 'news/';
	var header = 'news_header';
	
	switch (action)
	{
		case 1 : header = 'news_header'; break;
		case 2 : header = 'performances_header'; break;
		case 3 : header = 'history_header'; break;
		case 4 : header = 'contact_header'; break;
	}
	
	if (next_action == 0)
	{
		switch (action)
		{
			case 1 : url = webroot + 'news/'; break;
			case 2 : url = webroot + 'performances/'; break;
			case 3 : url = webroot + 'performances/history/'; break;
			case 4 : url = webroot + 'stills/'; break;
		}
	}
	else
	{
		//pokazanie newsa
		if ( (action == 1) && (next_action == 1) )
		{
			url = webroot + 'news/view/'+next_id;
		}
		
		//pokazanie spektaklu
		if ( (action == 2) && (next_action == 1) )
		{
			url = webroot + 'performances/view/'+next_id;
		}
		
		//pokazanie podrozy
		if ( (action == 2) && (next_action == 2) )
		{
			url = webroot + 'travels/view/'+next_id;
		}
		
		//pokazanie historii o teatrze
		if ( (action == 3) && (next_action == 1) )
		{
			url = webroot + 'abouts/view/'+next_id;
		}
		
		//pokazanie wywiadu
		if ( (action == 3) && (next_action == 2) )
		{
			url = webroot + 'interviews/view/'+next_id;
		}
	}
	
	$('header_img').src = $(header).src;
	
	new Ajax.Updater('content_container', url,
	 
		{ 
			asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'content_container'],
			onComplete: function(transport) { 
				$('loading_screen').hide();
				$('overdiv').appear( { afterFinish: function() 
					{ resize_return_button(); $('return').style.display = 'block'; page_state = 1; occupied = false; setTimeout('resize_return_button()', 100); } } ); 
				resize_return_button();
				Lightbox.prototype.updateImageList();
			}
      		
		}
	);
}

function door_closed()
{
	page_state = 0;
	occupied = false;
	
	if (next_index_to_go > 0)
	{
		go(next_index_to_go,next_action,next_id);
	}
}

function the_fly_is_over()
{
	resize_menu();
	$('top_menu').hide();
	$('top_menu').style.visibility = "visible";
	$('skip_intro').hide();
	
	if (flash_mode)
	{
		$('top_menu').appear();
		$('events').appear();
	}
	else
	{
		$('top_menu').show();
		$('events').show();
	}
}

//some hot new stuff!
//jak sie zagra poczatkowa animacja!
function loader_stopped()
{
	if (you_are_allowed_to_start)
	{
		$('floating_intro').remove();
		$('flash_container').allow_me();
	}
	else
	{
		$('loading_screen').show();
		setTimeout('loader_stopped()', 100);
	}
	//alert("ajzda z tym pieprzem!");
	
	//$('floating_intro').remove();
	//$('flash_container').allow_me();
}

function skip_pre_intro()
{
	$('skip_first_intro').hide();
	
	you_are_allowed_to_start = true;
	loader_stopped();
	
	if (!flash_is_loaded)
	{
		$('loading_screen').show();
	}
}

function intro_started()
{
	$('loading_screen').hide();
}



