

// trida -- oblacek
function Cloud( container, caption, hoverCaption, initialAngle, targetPage )
{
    //
    // promenne

    // nadrizeny element
    this.container = container;

    // muj element
    this.me = null;

    // prepocitana pozice, nastavuje se pomoci setAngle
    this.x = 0;
    this.y = 0;

    // "uhel" -- 0 = nahore, < 360
    this.angle = 0;
    
    // "normalni text"
    this.caption = caption;

    // text po najeti mysi
    this.hoverCaption = hoverCaption;

    // jmeno stranky pro nahrani
    this.targetPage = targetPage;
  
    // sirka drahy (nastaveni v inicializaci)
    this.trackWidth = 0;

    // vyska drahy (taktez)
    this.trackHeight = 0;

    // vybrani nejakeho cisla oblacku
    this.cloudImgNo = Math.floor( Math.random() * CLOUDS_BG );

    //
    // funkce

    // nastavi novy uhel -- 0 = nahore, podle hod. rucicek, < 360
    this.setAngle = function( angle )
    {
	angle = 90 - angle;
	if ( angle < 0 ){
	    angle += 360;
	}
	this.angle = angle * ( 2 * Math.PI / 360 );

	// update sirky a vysky drahy (muze se menit kvuli resize okna)
        this.trackWidth = this.container.clientWidth - this.me.clientWidth;
	this.trackHeight = this.container.clientHeight - this.me.clientHeight;

	// prava stena
	if (( this.angle < Math.PI / 4 ) || ( this.angle >= 7 * Math.PI / 4 ))
	{
	    //alert( "prava stena" );
	    this.x = this.trackWidth;
	    this.y = ( Math.tan( - this.angle ) * this.trackHeight / 2 ) 
		+ ( this.trackHeight / 2 );
	}
	// horni
	else if (( this.angle >= Math.PI / 4 ) && ( this.angle < 3 * Math.PI / 4 ))
	{
	    //alert( "horni stena" );
	    this.y = 0;
	    this.x = ( Math.tan( Math.PI / 2 - this.angle ) * this.trackWidth / 2 ) 
		+ ( this.trackWidth / 2 );
	}
	// leva
	else if (( this.angle >= 3 * Math.PI / 4 ) && ( this.angle < 5 * Math.PI / 4 ))
	{    
	    //alert( "leva stena" );
	    this.x = 0;
	    this.y = ( Math.tan( this.angle - Math.PI ) * this.trackHeight / 2 ) 
		+ ( this.trackHeight / 2 );
	}
	// spodni
	else 
	{
	    //alert( "spodni stena" );
	    this.y = this.trackHeight;
	    this.x = ( Math.tan( this.angle - 3 * Math.PI / 2 ) * this.trackWidth / 2 ) 
		+ ( this.trackWidth / 2 );
	}
	this.me.style.left = this.x + 'px';
	this.me.style.top = this.y + 'px';
    };

    // zjisti uhel ve stupnich
    this.getAngle = function()
    {
	var ret = this.angle;

	ret = ret * ( 360 / (2 * Math.PI ));
	ret = 90 - ret;
	if ( ret < 0 )
	    ret += 360;

	return ret;
    };

    // pri zmene velikosti okna se tohle musi zavolat!
    this.refreshView = function()
    {
	this.setAngle( this.getAngle() );
    }

    //
    // inicializace

    // vytvoreni meho elementu
    this.me = document.createElement( "DIV" );
    this.me.className = "cloud";
    this.me.style.position = "absolute";
    this.me.innerHTML = '<div class="cloudbg" >'
	+ '<img src="style/cloud' + this.cloudImgNo + '.png" alt="" /></div>'
	+ '<a href="javascript:loadPage('
        + targetPage + ');"><span class="repl"><span class="normal">'
	+ this.caption + '</span><span class="hover">' + this.hoverCaption
	+ '</span></span></a>';
    this.me.style.left = 0;
    this.me.style.top = 0;
    this.container.appendChild( this.me ); 

    // nastaveni pozice
    this.setAngle( initialAngle );
};

