// Define variables

// Ready
jQuery().ready(function() {
    // Background
    $.fn.supersized.options = {
        startwidth: 800,
        startheight: 600,
        vertical_center: 1,
        slideshow: 0
    };
    $('#supersize').supersized();

    // Make box other elements
    $('.container').each(function() {
        var el = $(this);
        el.wrap('<div class="holder"></div>');
        el.wrap('<div class="wrapper"></div>');
        el.before('<div class="top"></div>');
        el.wrap('<div class="wrapper"></div>');
        el.wrap('<div class="wrapper"></div>');
    });

    // Draggable and close
    $(".box").each(function() {
        var box = $(this);
        
        if ($('.with_header', box).length)
        {
            box.draggable({scroll: false, handle: $('.header', box), stack: {group: '.box', min: 1}});
        }
        else
        {
            box.draggable({scroll: false, stack: {group: '.box', min: 1}});
        }
    });
    $('.box .header a').click(function() {
        var box = $(this).parents('.box');

        $('#tools span.'+ box.attr('id')).parent().toggleClass('active');
        box.toggle();
    });

    // Toggle elements
    $('#tools a').click(function() {
        var element = $('span', this).attr('class');
        var item = $('#' + element);

        if (item.length)
        {
            $(this).toggleClass('active');
            item.toggle();
        }
    });

    // App Short URL
    $('#app_short_url form .gogo').click(function() {
        app_short_url();
    });
    $('#long_url').keypress(function(e) {
        if (e.which == 13)
        {
            app_short_url();
            return false;
        }
    }).focus(function() {
        $(this).val('');
    });

    // Clock
    if ($('#clock').length)
    {
        setTimeout(function() {
            time_machine();
        }, 10);
    }

    // My Rss
    setTimeout(function() {
        borisDelevRSS();
    }, 1000);
});

function borisDelevRSS()
{
    $.ajax({
        type: "GET",
        url: _root + "information/getrss?url=http://borisdelev.blogspot.com/feeds/posts/default?alt=rss",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('item').each(function(){
                var id = $(this).attr('id')
                var title = $(this).find('title').text();
                var link = $(this).find('link').text();

                var summary = '';
                var categories = $(this).find('category');
                categories.each(function() {
                    summary = summary + '<a href="http://borisdelev.blogspot.com/search/label/' + $(this).text() + '">' + $(this).text() + '</a> ';
                });

                $("#feeds ul").append('<li><a href="'+link+'">'+title+'</a><div class="tags">'+summary+'</div></li>');
            });

            $("#feeds ul li.loading").remove();
            $("#feeds ul li").fadeIn();
        }
    });
}

// Application
function app_short_url()
{
    $("#app_short_url form .buttons span").css('display', 'block');

    $.post(_root + "application/xhr?method=create_short_url", $("#app_short_url form").serialize(), function(answer) {
        
        // Hide previous notify message
        if (answer.error < 2) $('#app_short_url .notify').remove();

        if (answer.error == 0)
        {
            $('#app_short_url .header').after('<div class="notify">' + answer.message + '<br /> <strong>http://bs-d.us/' + answer.code + '</strong></div>');
            $("#long_url").val('http://');
        }
        else if (answer.error == 1)
        {
            $('#app_short_url .header').after('<div class="notify error">' + answer.message + '</div>');
        }

        $("#app_short_url form .buttons span").css('display', 'none');
    }, "json");
}

// Clock
function time_machine()
{
    var hours = $('#clock .hours');
    var minutes = $('#clock .minutes');
    var seconds = $('#clock .seconds');

    var currentTime = new Date();
    var h = currentTime.getHours();
    var m = currentTime.getMinutes();
    var s = currentTime.getSeconds();

    if (h < 10) h = '0' + h;
    if (m < 10) m = '0' + m;
    if (s < 10) s = '0' + s;

    hours.text(h);
    minutes.text(m);
    seconds.text(s);

    setTimeout(function() {
        time_machine();
    }, 1000);
}