
// "oblacky"
var clouds = null;

// aktualni stranka
var actPage = -1;

// indikator pohybu
var moving = false;

// cas posledniho zavolani moveClouds
var lastMove = 0;

// "nacludovani" skriptu az po nahrani souboru
function include( scriptFile )
{
    var script = document.createElement( 'SCRIPT' );
    script.src = scriptFile;

    document.body.appendChild( script );
}


// podle "actPage" se snazi, aby ji prislusny oblacek 
// byl na uhlu MAIN_ANGLE
function moveClouds()
{
    var delta;
    var move;
    var actAngle = clouds[actPage-1].getAngle();
    var localDate = new Date();
    var curTime = localDate.getTime();

    // spocitani vzdalenosti od cile
    delta = MAIN_ANGLE - actAngle;
    if ( delta < 0 )
	delta += 360;

    // spocitani o kolik by se mel pohnout
    if ( delta < 180 )
	move =  2 * ( ( curTime - lastMove ) / 50 );
    else 
	move = -2 * ( ( curTime - lastMove ) / 50 );
   
    // konec (prejizdim MAIN_ANGLE)
    if ( Math.max( actAngle, actAngle + move ) >= MAIN_ANGLE 
	    && Math.min( actAngle, actAngle + move ) < MAIN_ANGLE )
    {
	moving = false;
	lastMove = 0;
	// document.getElementById( 'debug' ).innerHTML = '';
	for( var i = 0; i < clouds.length; ++i )
	{	    
	    clouds[i].setAngle( ( clouds[i].getAngle() + delta ) % 360 );
	    /*document.getElementById( 'debug' ).innerHTML += clouds[i].getAngle() + ' ' 
		    + clouds[i].x + ' ' + clouds[i].y + ' ' + clouds[i].angle 
		    + ' ' + ( clouds[i].angle * 360 / ( 2 * Math.PI ) ) +  '<br/>';*/
	}
	return;
    }

    // posun a naplanovani dalsiho
    for( var i = 0; i < clouds.length; ++i )
	clouds[i].setAngle( (clouds[i].getAngle() + move) % 360 );

    lastMove = curTime;
    setTimeout( "moveClouds();", 50 ); 
}


// nahraje stranku -- vola showSection a moveClouds
function loadPage( section )
{
    var localDate;
    section = section + '';
    var pageNo = section.indexOf( "#" ) == -1 ? section : section.substring( 0, section.indexOf("#") );

    if ( actPage == pageNo )
    {
	pages.showSection( section );
	return;
    }

    actPage = pageNo;
    if ( !moving )
    {
	localDate = new Date();
	lastMove = localDate.getTime();
	moveClouds();
    }
    moving = true;

    pages.showSection( section );
}


// prida novy stylesheet (jen pro javascript -- rozlozeni stranky)
// rozhazuje layout :-(
/*function loadStyleSheet( styleSheet )
{
    if ( document.createStyleSheet ) 
    {
	document.createStyleSheet( styleSheet );
    }
    else 
    {
	var newSS=document.createElement( 'link' );

	newSS.rel = 'stylesheet';
	newSS.href = styleSheet;

	document.getElementsByTagName( "head" )[0].appendChild( newSS );
    }

}*/

// zmeni drahy oblacku podle nove velikosti viewPortu
// v IE vola updateLayout
function updateView()
{
    if ( window.navigator.appName.indexOf( 'Microsoft Internet Explorer' ) != -1  )
    {
	updateLayout();
    }
    for( var i = 0; i < clouds.length; ++i )
	clouds[i].refreshView();
}

// (c) A List Apart
// nastavi aktivni Stylesheet
function setActiveStyleSheet(title) 
{
    var i, a, main;

    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
    {	
	if(a.getAttribute("rel").indexOf("style") != -1
		&& a.getAttribute("title")) 
	{
	    a.disabled = true;
	    if(a.getAttribute("title") == title) 
		a.disabled = false;
	}
    }
}


// inicializace
function init()
{
    var main = document.getElementById( 'main' );

    // nahraje stylesheet
    //loadStyleSheet( 'style/js.css' );
    setActiveStyleSheet( 'js' );

    // nahraje potrebne skripty
    include( 'script/loader.js' );

    // upravi layout v IE
    if ( window.navigator.appName.indexOf( 'Microsoft Internet Explorer' ) != -1  )
    {
	updateLayout();
    }
    window.onresize = updateView;

    // vyrobi oblacky
    clouds = new Array();
    for( var i = 0; i < PAGES_NUM; ++i )
    {
	var angle;
	var tooNear;
	
	do {
	    angle = Math.random() * 360;
	    tooNear = false;
	    for( var j = 0; j < i; ++j )
		if ( (Math.abs( clouds[j].getAngle() - angle ) % 360) < CLOUDS_PROXIMITY )
		    tooNear = true;
	}
	while ( tooNear );
    
	clouds[i] = new Cloud( main, CLOUDS_NAMES[i], CLOUDS_HOVER[i],
		angle, CLOUDS_SECTIONS[i] );
    }

    initRepl();  // inicializace "hovers"

    // spravi PNG v IE
    if ( window.navigator.appName.indexOf( 'Microsoft Internet Explorer' ) != -1 )
    {
	if (document.all && document.styleSheets && document.styleSheets[0] &&
	    document.styleSheets[0].addRule)
	{
	    // Feel free to add rules for specific tags only, you just have to call it several times.
	    document.styleSheets[0].addRule('img', 'behavior: url(iepngfix.htc)');
	    document.styleSheets[0].addRule('div', 'behavior: url(iepngfix.htc)');
	}
	
    }

    // nastavi nahodny font nadpisu
    setCaption();

    
    // schova "Loading..."
    document.getElementById( "loading_message" ).style.display = 'none';
}


/*
	var x = document.getElementById( "caption" ).style;
	var y = '';
	var j = 0;

	for (var i in x )
	{
	  j++;
	  if ( j == 20 )
	  {
	    alert( y );
	    y = '';
	    j = 0;
	  }
	  y += '\n' + i + ': ' + x[i];
	}
	alert( y );
*/

