$(document).ready(function(){
	$('a.tblank').bind('click', function (event){
		window.open(this.href);
        return false;
	});

	// fotogalerie
	$('#gallery li').wrapInner('<div class="around"><div class="top"><div class="bottom"><div class="inner"></div></div></div></div>');
	var vclass = 0;
	$('#gallery li').each(function (i){
		var vjustcss = 'vjustcss'+vclass;
		$(this).addClass(vjustcss);
		if((i + 1)%4 == 0)	{
			$(this).addClass('last');
			$('#gallery li.'+vjustcss+' div.inner').vjustify();
			vclass += 1;
		}
	});
	$("#gallery a").addClass("thickbox");
	$("#gallery a").attr("rel", "gall");
	$("div.photos ul a").addClass("thickbox");
	$("div.photos ul a").attr("rel", "gall");
    TB_init();

	// fotogalerie (GalerieListPage)
	vclass = 0;
	$('#GalerieListPage li').each(function (i){
		var vjustcss = 'vjustcss'+vclass;
		$(this).addClass(vjustcss);
		if((i + 1)%3 == 0)	{
			$(this).addClass('last');
			$('#GalerieListPage li.'+vjustcss+' div.inner').vjustify();
			vclass += 1;
		}
	});
    if(jQuery.cookie('screensaver') == 'true') {
        Pozadi.show();
        Screensaver.restart(true);
    }
});

Pozadi = new function() 
{
        this.interval = null;
        this.show = function()
        {
            jQuery.cookie('screensaver','true',{expires : 1});
            var pozadi = $('div.pozadi');
            if(pozadi.length) {
                pozadi = pozadi[0];
                showHodiny(pozadi,$('body'),false,false);
                this.interval = setInterval(function() {
                    showHodiny(pozadi,$('body'),false,false);
                },1000*60);
            }
        }
        this.close = function()
        {
            jQuery.cookie('screensaver','false',{expires : 31});
            var pozadi = $('div.pozadi');
            if(pozadi.length) {
                $(pozadi[0]).css('background','none');
            }
            $('body').css('background',"url('/sally/obr/obsah_bg.jpg') repeat-x #d2d1d7");
            clearInterval(this.interval);
        }
}

function showHodiny(pozadi,body,minuty,zeme)
{
    var date = new Date();
    var sec = date.getHours()*3600+date.getMinutes()*60+date.getSeconds();
 //   var sec = 3600*7;
    var alfa = 270-sec/240;
    var polomer = $('body').width() - 248;
    if(polomer > 700) polomer = 700;

    var x = Math.round(polomer*Math.cos(alfa*Math.PI/180));
    var y = Math.round(polomer*Math.sin(alfa*Math.PI/180));
    x = Math.round($(pozadi).width()/2)+x - 248/2;
    y = polomer - y - 240/2;

    var xy = ''+x+'px '+y+'px';
    $(pozadi).css({
         'background' : "url('/sally/obr/slunce-anim.gif')",
         'background-position': xy,
         'background-repeat' : 'no-repeat'
    });
    $(body).css({
        'background-image' : "url('/sally/obr/stars.gif')",
        'background-repeat': 'repeat'
    });
    if(minuty)
    for(var i = 0; i < 24; i++)
    {
        var alfa =180+90 - 360/24*i;
    
        var x = Math.round(polomer*Math.cos(alfa*Math.PI/180));
        var y = Math.round(polomer*Math.sin(alfa*Math.PI/180));
        x = Math.round($(pozadi).width()/2)+x;
        y = polomer - y;
        var t = document.createElement('div');
        t.innerHTML = i;
        var size = 16;
        if(!(i% 3))
            size = 22;
        if(!(i % 6))
            size = 28;
        $(t).css({
            'color' : 'white',
            'font-size' : size +'px',
            'font-weight' : 'bold',
            'float': 'left',
            'text-align' : 'center',
            'position' : 'absolute',
            'left' : x,
            'top' : y,
            'z-index' : -1
        });
        $(body).append(t);
    }
    
    if(zeme) {
        var earth = document.createElement('img');
        earth.src='/sally/obr/earth.gif';
        $(earth).css({
            'position' : 'absolute',
            'left' : Math.round($(pozadi).width()/2)-50,
            'top' : polomer-50,
            'width' : '100px',
            'height' : '100px'
        });
        $(body).append(earth);
    }
}

Screensaver = new function()
{
    this.sec =  60*5; // po kolika vterinach se objevi
    this.interval = null; // timer pro zobrazeni
    this.moveInterval = null; //timmer pro posun hodin
    this.poz = null;
    this.scr = null;
    this.show = function()
    {
        if(this.interval) {
            clearInterval(this.interval);
            this.interval = null;
        }

        if(this.scr) {
            $(scr).css('display','block');
            if(!this.moveInterval)
                this.moveInterval = setInterval(function() { showHodiny(this.poz,this.scr,true,true);},60000);
            return;
        }

        poz = document.createElement('div');
        scr = document.createElement('div');
        $(scr).append(poz);
        $(poz).css({
            width : '100%',
            height : $('body').height() + 25,
            'cursor' : 'pointer'
        });
        $(scr).css({
            position: 'absolute',
            display : 'block',
            top : 0,
            left : 0,
            background : 'black',
            zIndex : 2,
            width : '100%',
            height : $('body').height() + 25
        });
        $('body').append(scr);
        showHodiny(poz,scr,true,true);

        $(scr).click(function() {
            Screensaver.close();
        });
        this.poz = poz;
        this.scr = scr;
        this.moveInterval = setInterval(function() { showHodiny(this.poz,this.scr,true,true);},60000);
    }

    this.close = function(remove)
    {
        $(this.scr).css('display','none');
        if(!remove) {
            this.restart();
            var scr = this;
            $('body').mousemove(function() {
                scr.restart(true);
            });
        }
        else if(this.interval) {
            clearInterval(this.interval);
            this.interval = null;
        }
        if(this.moveInterval) {
            clearInterval(this.moveInterval);
            this.moveInterval = null;
        }
    }
    this.restart = function(start)
    {
        var scr = this;
        if(this.interval) {
            clearInterval(this.interval);
        }
        if(this.interval || start)
            this.interval = setInterval(function() { scr.show()},1000*this.sec);
    }
}
