var last_src = '';
var last_tab = 1;

//bawimy sie datami
var date = new Date();
var today_month = date.getMonth();
var today_year = date.getFullYear();

var current_month = today_month;
var current_year = today_year;

function get_days_in_month(year, month)
{
	return 32 - new Date(year, month, 32).getDate();
}

function get_month_list(year, month)
{
	var days = get_days_in_month(year, month);
	//2009-12-09
	//miesiace sa od 0 liczone! 0 == styczen!
	var full_month = (month - (-1));
	if (full_month < 10) { full_month = "0"+full_month; }
	var string_date = year + "-" + full_month + "-";
	
	var result = months[month] + " " + year + ":&nbsp;&nbsp;";
	for (var i = 0 ; i < days ; i++)
	{
		var final_day = i-(-1);
		if (final_day < 10) { final_day = "0"+final_day; }
		
		var this_date = string_date + final_day;
		
		//sprawdzamy czy mamy gdzies wydarzenie o takiej dacie
		for (var j = 0 ; j < events.length ; j++)
		{
			//console.log(events[j], this_date);
			if (events[j] == this_date)
			{
				final_day = "<a href=\"javascript:showEvent("+events_ids[j]+");\"><i>"+final_day+"</i></a>";
				break;
			}
		}
		
		var d = new Date();
		d.setFullYear(year,month,(i-(-1)));
		
		//jezeli to niedziela to pgorubiamy
		if (d.getDay() == 0)
		{
			final_day = "<b>" + final_day + "</b>";
		}
		
		result = result + final_day + "&nbsp;&nbsp;";
	}
	
	return result;
}

function img_change(handle)
{
	last_src = handle.src;
	handle.src = $(handle.id+"_").src;
	
	if (flash_mode)
	{
		$('flash_container').reset_camera_angles();
	}
}

function img_return(handle)
{
	handle.src = last_src;
}

var mouseDown = 0;
var mouseCounter = 0;

function init_menu()
{
	//$('top_menu').hide();
	//$('top_menu').style.visibility = "visible";
	
	resize_menu();
	$('return').setOpacity(0.7);
	resize_return_button();
	
	document.body.onmousedown = function() { 
	  ++mouseDown;
	}
	document.body.onmouseup = function() {
	  --mouseDown;
	  mouseCounter = 0;
	}

	$('events_dates').innerHTML = get_month_list(current_year, current_month);
	//setInterval('scroll_events()', 10);
}

function scroll_events()
{
	if (mouseDown)
	{
		if (mouseCounter == 0)
		{
			if (events_previous_on) {
				events_previous();
			}
			
			if (events_next_on) {
				events_next();
			}
			
			
		}
		else if (mouseCounter > 15)
		{
			if (events_previous_on) {
				events_previous();
			}
			
			if (events_next_on) {
				events_next();
			}
		}
		
		mouseCounter++;
	}
}

function resize_menu()
{
	$('top_menu').style.left = (Math.round(document.viewport.getWidth() / 2) - Math.round($('top_menu_table').getWidth() / 2)) + "px";
	$('overdiv').style.left = (Math.round(document.viewport.getWidth() / 2) - Math.round(768 / 2)) + "px";
	resize_return_button();
	resize_events();
	
	//$('events').style.top = (document.viewport.getHeight() - (-document.viewport.getScrollOffsets().top) - $('events').getHeight()) + 'px';
}

var next_index_to_go = 0;
var next_action = 0;
var next_id = 0;

function go(entrance, action, id)
{
	//resetujemy taby
	var last_tab = 1;
	
	next_action = action;
	next_id = id;

	if (occupied) { return; }
	
	closeBubble();
	
	occupied = true;
	
	if (page_state == 0)
	{
		next_index_to_go = 0;
		
		if (flash_mode)
		{
			$('flash_container').start_finding_entrance(entrance);
		}
		else
		{
			door_opened(entrance);
		}
	}
	else
	{
		next_index_to_go = entrance;
		return_page();
	}
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}


function resize_return_button()
{
	$('return').style.left = (document.viewport.getWidth() - 130) + "px";
	$('return').style.top = (document.viewport.getHeight() - 100 - (-document.viewport.getScrollOffsets().top)) + "px";
	resize_events();
}

function resize_events()
{
	if ($('bubble').style.display != 'none')
	{
		$('bubble').style.top = Math.round( (document.viewport.getHeight()/2) - ($('bubble').getHeight()/2) - (-document.viewport.getScrollOffsets().top)) + "px";
		$('bubble').style.left = Math.round( (document.viewport.getWidth()/2) - ($('bubble').getWidth()/2) - (-document.viewport.getScrollOffsets().left)) + "px";
	}
	//$('events').style.top = (document.viewport.getHeight() - 30 - (-document.viewport.getScrollOffsets().top)) + "px";
	//$('events_scroll').style.width = (document.viewport.getWidth() - 200) + "px";
	//$('bubble').style.width = (document.viewport.getWidth() - 200) + "px";
}

var events_previous_on = false;
var events_next_on = false;

function events_next()
{
	//$('events_scroll').scrollLeft = $('events_scroll').scrollLeft + 10;
	
	current_month = current_month + 1;
	
	if (current_month > 11)
	{
		current_year++;
		current_month = 0;
	}
	
	$('events_dates').innerHTML = get_month_list(current_year, current_month);
}

function events_previous()
{
	//$('events_scroll').scrollLeft = $('events_scroll').scrollLeft - 10;
	
	current_month = current_month - 1;
	
	//mozemy cofnac max 2 miesiace
	//if ((current_year == today_year) && ((today_month - current_month) > 2)) { current_month++; }
	if (((today_year * 365 + (today_month+1) * 31) - (current_year * 365 + (current_month+1) * 31)) > 62) { current_month++; }
	else
	{
		if (current_month < 0)
		{
			current_year--;
			current_month = 11;
		}
	}
	
	$('events_dates').innerHTML = get_month_list(current_year, current_month);
}

//po kliknieciu powrotu
function return_page()
{
	$('return').style.display = 'none';
	$('overdiv').fade( { afterFinish: function () { scroll(0,0); if (flash_mode) { $('flash_container').return_on_track(); } else { occupied = false; page_state = 0; } } } );
}

//do newsow
function reset_news()
{
	var news_list = $$('.news_long_content');
	
	for (i = 0 ; i < news_list.length ; i++)
	{
		if (news_list[i].style.display != 'none')
		{
			news_list[i].hide();
			//new Effect.SlideUp(news_list[i]);
		}
	}
	
	news_list = $$('.more');
	
	for (i = 0 ; i < news_list.length ; i++)
	{
		news_list[i].show();
	}
	
	news_list = $$('.news_short_content');
	
	for (i = 0 ; i < news_list.length ; i++)
	{
		news_list[i].show();
	}
}

function show_news(id)
{
	reset_news();
	
	$("news_more_"+id).hide();
	$("news_long_"+id).show();
	$("news_short_"+id).hide();
	//new Effect.SlideDown("news_long_"+id);
}

//////////////////////////////////////
//TABY
function shut_down_everything(max_tabs)
{
	for (var i = 0 ; i < max_tabs ; i++) {
		$('tab_'+(i-(-1))).className = 'tab_content';
		$('tab_'+(i-(-1))).up().className = 'tab';
		$('tab_content_'+(i-(-1))).hide();
	}
}

//do spektakli
var block_links = false;

function switch_tab(id, max_tabs)
{
	if (id > max_tabs) { id = 1; }
	
	//usuwamy zawartosc jesli cos jest w ogole
	if ($('performance_container'))
	{
		$('performance_container').innerHTML = '';
	}
	
	last_tab = id;
	shut_down_everything(max_tabs);
	$('tab_'+id).className = 'tab_selected';
	$('tab_'+id).up().className = 'tab_active';
	$('tab_content_'+id).show();
	$('tab_'+id).scrollTo();
}
///////////////////////////////////////

function show_performance(id)
{
	if (!block_links)
	{
		//zamykamy wszystkie taby
		shut_down_everything(3);
		
		var url = webroot + "performances/view/"+id;
		
		$('perf_loader_'+id).show();
		
		new Ajax.Updater('performance_container', url,
		 
			{ 
				asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'content_container'],
				onComplete: function(transport) { 
					
					block_links = false;
					$('performance_top').scrollTo();
					//ukrywamy lopadera jesli jakis jest
					if ($('perf_loader_'+id)) { $('perf_loader_'+id).hide(); }
				}
	      		
			}
		);
	}
}

function switch_event(handle) {
	if (handle.className == ("on")) {
		//new Effect.SlideDown('event_more');
		$('event_more').show();
		resize_return_button();
		handle.className = "off";
		handle.src = (webroot + 'img/ar_down.jpg');
	} else {
		//new Effect.SlideUp('event_more');
		$('event_more').hide();
		resize_return_button();
		handle.className = "on";
		handle.src = (webroot + 'img/ar_up.jpg');
	}
}

function switch_sound(handle) {
	//$('flash_container').skip_fly();
	if (handle.className == ("on")) {
		$('flash_container').sound_off();
		if ($('floating_intro_inner')) { $('floating_intro_inner').sound_off(); } 
		handle.className = "off";
		handle.src = $('img_sound_off').src;
	} else {
		$('flash_container').sound_on();
		if ($('floating_intro_inner')) { $('floating_intro_inner').sound_on(); } 
		handle.className = "on";
		handle.src = webroot+"img/snd_on.jpg";
	}
}

function closeBubble() {
	var other_links = $$('.event_link');
	for (var i = 0 ; i < other_links.length ; i++) {
		other_links[i].style.fontWeight = 'normal';
	}
	scroll(0,0);
	$('bubble').hide();
}

function showEvent(id) {
	$('bubble').show();
	resize_events();
	$('event_text').innerHTML = "<img src=\""+webroot+"img/loading.gif\" />";
	$('flash_container').reset_camera_angles();
	
	//var other_links = $$('.event_link');
	//for (var i = 0 ; i < other_links.length ; i++) {
	//	other_links[i].style.fontWeight = 'normal';
	//}
	//
	//$('event_link_'+id).style.fontWeight = 'bold';
	
	new Ajax.Updater('event_text', webroot+'events/view/'+id,
		{ 
			asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'event_text'],
			onComplete: function(transport) { 
				
				Lightbox.prototype.updateImageList();
			}
	    		
		}
	);
}

