window.addEvent('domready', function(){    
    news.init();
});

var news = {
    init: function(){
        $$('#news_items .news_item').each(function(el){
            var title = el.getElement('.title');
            title.set('morph',{duration:'short'});
            title.addEvent('click',function(e){
                e.stop();
                var link = this.getElement('h3 a');
                location.href = link.get('href');
            });
            el.addEvents({
                'mouseover': function(){
                    title.morph('.news_over');
                },
                'mouseout': function(){
                    title.morph('.news_out');
                }
            });
        });
    }
}

var poll = {
    init: function(){
        this.hideShow =  new Fx.Tween('s_poll',{property:'opacity', link:'chain'});
        if ($('live_data')) {$('live_data').addEvent('click', function(){poll.update()})};
    },    
    send_poll: function(poll_id){
        new Request({
            url: '/cs/clear/main',
            data: $('poll_form'),
            onSuccess: function(txt){
                $('s_poll').set('html',txt);
                $('live_data').addEvent('click', function(){poll.update()});
                Cookie.write('poll_taken', poll_id , {domain:'e-myth.com', duration:365, path:'/'});                
                poll.hideShow.start(1);
            }
        }).send();
    },
    check_ans: function(poll_id){
        var checked = 0;
        $$('table#poll_opt input').each(function(input){
            if (input.checked){
                checked++;
            }
        });
        if ( checked == 1 ) {
            poll.hideShow.start(0).chain(function(){
                poll.send_poll(poll_id);
            });
        } else {
            alert('Please choose an option.');
        }
    },
    resize_poll: function(bars){
        bars.each(function(bar){
            $('poll_bar_'+bar.option_id).tween('width', bar.width);
            $('poll_percent_'+bar.option_id).set('text',bar.percent+'%');
            //$('poll_count_'+bar.option_id).setText('('+bar.count+')');
        })
    },
    timer: function(){
        var sec = $('poll_update').get('text');
        $('poll_update').set('text',sec - 1);
        if (sec == 0){
            $('poll_update').removeClass('not_ready').set('text','Update');
            $('poll_msg').set('text','Thank you for responding.').removeClass('new_msg');
            $('live_data').addEvent('click', function(){poll.update()});
        } else {
            poll.timer.delay(1000);
        }
    },
    update: function(){
        $('poll_update').addClass('not_ready').set('text',10);
        $('live_data').removeEvents('click');
        poll.timer();
        new Request({
            url: '/cs/clear/main',
            data: 'x-p=poll/json_results',
            onSuccess: function(txt,xml) {
                var jsonObj = JSON.decode(txt);
                poll.resize_poll(jsonObj.bars);
                var total = $('poll_total').get('text');
                if (jsonObj.total == total) {
                    $('poll_msg').set('text','No new data.').addClass('new_msg');
                } else {
                    var diff = jsonObj.total - total;
                    $('poll_total').set('text',jsonObj.total);
                    $('poll_msg').set('text',diff+' vote(s) since last update').addClass('new_msg');
                };
            }
        }).send();
    }
};