$(function()
{
    $('#menu > li:not(.current_page_ancestor)')
            .addClass('collapsed')
            .children('ul').hide();
    $('#menu > li > ul')
            .css({paddingTop: 0, paddingBottom: 0})
            .map(function()
                {
                    $(this).children('li:first').css({paddingTop: 5}).end()
                           .children('li:last').css({paddingBottom: 20});
                });
    $('#menu > li > a').click(function()
    {
        $(this).blur()
               .parent().toggleClass('collapsed').end()
               .next().slideToggle('fast');
        return false;
    });

    $('.tooltip .back').css('opacity','0.7');
/*
    $('#ie #infotooltip')
        .hover( function() { $(this).children('span.tooltip').show(); },
                function() { $(this).children('span.tooltip').hide(); });
*/
    $('#infotooltip').mousemove(function(e)
        {
            var off = $(this).offset();
            var newRight = 35 - (e.pageX - off.left);
            var newtop = e.pageY - off.top + 2;
            if(newRight < 35 && newtop < 35)
                $(this).children('span.tooltip').css({ right: newRight, top: newtop });
        });
//                .children('span').removeClass('css');

/*
            $('#photo')
                .hover(function() { $('#phototooltip').show(); },
                       function() { $('#phototooltip').hide(); })
                .mousemove(function(e)
                    {
                        var off = $(this).offset();
                        var newleft = e.pageX - off.left + 2;
                        var newtop = e.pageY  + 2;
                        $('#phototooltip').css({ left: newleft, top: newtop });
                    });
*/
    if($('#info').length)
    {
        var gallery =
        {
            files: ['1.png','2.png','3.png','4.png','5.png','6.png','7.png','8.png'],
            path: '',
            images: [],
            loaded: 0,
            current: 0,
            prevdate: new Date(),
            activeimage: 81,
            autoscroll: true,
            dragging: false,
            pos: -6235,
            delta: 0,
            mousepos: 0,
            safemode: false,
            loader: function(e)
            {
                if(e !== undefined)
                {
                    $('#bullets span').eq(gallery.files.length - gallery.loaded - 1).addClass('loaded');
//                    $('#photo').css('background-image', 'url(gallery.path+gallery.files[gallery.loaded]+'")');
                    gallery.loaded++;
                }
                if(gallery.loaded < gallery.files.length)
                {
                    gallery.images[gallery.loaded] =
                        $("<img/>").load(gallery.loader).attr('src', gallery.path + gallery.files[gallery.loaded]);
                }
            },
            gotoimage: function(n)
            {
                gallery.prevdate = new Date();

                var len = gallery.files.length;
                if((gallery.loaded != len && n % len == 0) || n % len > gallery.loaded) return;

                gallery.pos = -(77*n-2);
                if(!gallery.safemode)
                    $('#ribbon').stop().animate({backgroundPosition: gallery.pos+"px 0" }, 300, 'swing');
                else
                    $('#ribbon').css({backgroundPosition: gallery.pos+"px 0" });

                var nn = (n - 1) % len;
                var from = len - (gallery.activeimage - 1) % len - 1;
                var to = len - nn - 1;

                $('#bullets span').eq(from).removeClass('active');
                $('#bullets span').eq(to).addClass('active');


                var image = gallery.files[nn];

                if(!gallery.safemode)
                    $('#photo2')
                        .css('backgroundImage', 'url("' + gallery.path + image + '")')
                        .fadeIn(300, function() {
                            $('#photo').css('backgroundImage', 'url("' + gallery.path + image + '")');
                            $('#photo2').hide();
                        });
                else $('#photo').css('backgroundImage', 'url("' + gallery.path + image + '")');

                gallery.activeimage = n;
            },
            bullet_click: function()
            {
                var picnum = $(this).nextAll().length + 1;
                if(picnum <= gallery.loaded)
                {
                    picnum += gallery.activeimage - (gallery.activeimage - 1) % gallery.files.length - 1;
                    gallery.gotoimage(picnum);
                }
//                $('#photo').text($('#bullets span').index(this) + ' ' + $(this).nextAll().length);
            },
            animation: function()
            {
                if(!gallery.autoscroll || gallery.dragging || new Date() - gallery.prevdate < 5000) return;

                gallery.gotoimage(gallery.activeimage+1);
            },
            mousedown: function(e)
            {
                gallery.dragging = true;
                gallery.mousepos = e.clientX;
            },
            mouseup: function()
            {
                if(gallery.dragging)
                {
                    gallery.dragging = false;
                    gallery.pos -= gallery.delta;
                    gallery.gotoimage(Math.floor((-gallery.pos - 2) / 77) + 1);
                    gallery.delta = 0;
                }
            },
            mousemove: function(e)
            {
                if(gallery.dragging)
                {
                    gallery.delta = gallery.mousepos - e.clientX;
                    var posX = gallery.pos - gallery.delta;
                    $('#ribbon').css('backgroundPosition', posX + 'px 0px');
                }
            },
            start: function()
            {
                gallery.path = $('script:first').attr('src');
                gallery.path = gallery.path.substr(0, gallery.path.lastIndexOf('/')) + '/images/';

//                if(jQuery.browser.msie && jQuery.browser.version.substr(0, 1) < 7)  gallery.safemode = true;

                $('#ribbon').css({backgroundPosition: gallery.pos+"px 0" });

                if(!gallery.safemode)
                {
                    $('#ribbon').mousedown(gallery.mousedown);
                    $(document).mouseup(gallery.mouseup)
                               .mousemove(gallery.mousemove);
                }

                $('#photo').click(function(){ gallery.gotoimage(gallery.activeimage+1); })
                           .hover(function(){ gallery.autoscroll = false; },
                                  function(){ gallery.autoscroll = true; });
                $('#bullets span').click(gallery.bullet_click);
                gallery.loader();
                setInterval(gallery.animation, 50);
            }
        };
        $(window).load(gallery.start);
    }

    function updatecounter()
    {
        var t = new Date(2011, 0, 30, 0, 0, 0);
        var ms = t - new Date();
        var t2 = new Date(ms);
        var fulldays = Math.floor(ms/(60*60*24*1000));
        fulldays = "000".substr(0, 3-(''+fulldays).length) + fulldays;
        var hours = t2.getUTCHours();
//            var hours = t2.getUTCMinutes();
        if(hours < 10) hours = "0"+hours;
        var minutes = t2.getUTCMinutes();
//            var minutes = t2.getUTCSeconds();
        if(minutes < 10) minutes = "0"+minutes;

        var s = fulldays+" "+hours+":"+minutes;
//            $('#lang').text(s);

        var digits = $('.large_digit');
        digits[0].className= 'large_digit digit_' + s.substr(0, 1);
        digits[1].className= 'large_digit digit_' + s.substr(1, 1);
        digits[2].className= 'large_digit digit_' + s.substr(2, 1);

        digits = $('.small_digit');
        digits[0].className= 'small_digit sdigit_' + s.substr(4, 1);
        digits[1].className= 'small_digit sdigit_' + s.substr(5, 1);
        digits[2].className= 'small_digit sdigit_' + s.substr(7, 1);
        digits[3].className= 'small_digit sdigit_' + s.substr(8, 1);
    }
    setInterval(updatecounter, 1000);
});
