var Widget = {

    popup: function(url, name, width, height) {
        var left = Math.round(screen.availWidth  / 2 - width  / 2);
        var top  = Math.round(screen.availHeight / 2 - height / 2);

        return window.open(url, name, ['height=' + height, 'width=' + width, 'left=' + left, 'top=' + top, 'scrollbars=yes'].join(','));
    }
    
};

Widget.Weather = {

    update: function(code) {
        $('#cities').hide();
        
        User.Location.set(code);
        Widget.Weather.current();
    },
    
    current: function() {
        var code     = User.Location.get();
        var city     = weather.cities[code];
        var forecast = weather.forecast[code];
            
        $('.weather .city').text(city);
        $('.weather .temp').text(forecast.current.low + 'º');
    }
    
};

Widget.Login = {
    referer: '/',

    service: function(name, callback) {
        callback = callback || 'Widget.Login.complete';
        var callback_param = callback ? '?callback=' + callback : '';
        var login = Widget.popup('/connect/' + name + callback_param, 'login', 600, 400);
    },
    
    complete: function() {
        document.location.href = Widget.Login.referer || '/';
    },
    
    connect: function(service) {
        Widget.Login.service(service, 'Widget.Login.afterConnect');
        return false;
    },
    
    afterConnect: function(service) {
        service.id = service.id.replace('http://', '').replace('www.', '');
    
        var element = jQuery('li.' + service.name);
        element.html('<span class="ic16"></span> <span class="identifier">' + service.id + '</span>');
        element.addClass('linked');
    }

};

Widget.Comments = {

    checkUser: function() {
    
        if(User.logged) {
            $('.not-logged').hide();
            $('.logged').show();
            

            var today = new Date();
            var textdate = today.getDate() + " de " + Locale.months[today.getMonth()] + " de " + today.getFullYear() + ", a las " + today.getHours() + ":" + today.getMinutes();

            if(User.info.avatar)
                $('.user-info .avatar img').attr('src', User.info.avatar);
                
            $('.user-info .realname').text(User.info.realname);
            $('.user-info .date').text(textdate);
            
        } else {
            $('.logged').hide();
            $('.not-logged').show();
        }

    },
    
    login: function(service) {
        Widget.Login.service(service, 'Widget.Comments.checkLogin');
    },
    
    checkLogin: function() {
        User.check();
        Widget.Comments.checkUser();
    },
    
    reply: function(id, author) {
        $('#compose h3').html('respuesta a <cite class="in-reply-to">' + author + '</cite>:');
        $('#reply-id').val(id);
    },
    
    vote: function(comment_id, vote) {
        jQuery.post('/wp-content/plugins/karma/process.php', {'type': 'comment', 'id': comment_id, 'vote': vote}, function(result) {}, 'json');
            
        jQuery('#comment-' + comment_id + ' .comment-actions li.vote').fadeOut('fast');
        
        if(vote == 'down')
            jQuery('#comment-' + comment_id).addClass('collapsed');
        

    },
    
    expand: function(comment_id) {
        jQuery('#comment-' + comment_id).removeClass('collapsed').children('ul.children').show();
    }
    
};

Widget.Share = {
    Facebook: function() {
        var url = 'http://www.facebook.com/sharer.php?src=sp&u=' + encodeURIComponent(document.location.href);
        var opened = Widget.popup(url, 'share', 626, 264);
    }

};

Widget.Print = {
    normal: function() {
        latebox.close();
        window.print();
    },
    
    compact: function() {
        Widget.Print.normal();
    }
};

Widget.HotNews = {

    render: function() {
    
        var container = jQuery('.breaking-news > div.group');
    
        for(var i=0; i < hotnews.length; i++) {
            var data = hotnews[i];
            var item = jQuery('<div><span></span><h4 class="gsf"><a href="#"></a></h4></div>');
                        
            item.children('span').text(data.date + ' | ');
            item.find('h4 > a').text(data.title).attr('href', data.link);
            
            (i > 0) ? item.hide() : false;
            
            container.prepend(item);
        }
    
        new Timer(6000, function(tick) {
            var index = tick % hotnews.length;
            
            container.children(':visible').slideUp();
            container.children().eq(index).slideDown();
            
            container.children('div').hover(this.stop.bind(this), this.start.bind(this));
        }).start();
  
    }  

};

var Timer = new Class({
    ticks: 0, callback: function() {}, interval: 1000, timer: null,

    initialize: function(interval, callback) { this.interval = interval; this.callback = callback; },
    start: function() { this.stop(); this.timer = setInterval(this.tick.bind(this), this.interval); },
    stop: function() { clearInterval(this.timer); },
    tick: function() { this.callback.apply(this, [this.ticks++]); }
});

