/**
 * @author Jay
 */

/****************************************/
/*				 Votes					*/
/****************************************/
$(function(){
    $("#log_in_button").click(function () {
        $("div#hidden_log").toggle("fast");
        jQuery.each(jQuery.browser, function(i, val) {
            if(i=="msie")
                $("input.search").toggle();
        });
    });
    var loader=$('#loader');
    var pollcontainer=$('#pollcontainer');
    loader.fadeIn();
    //Load the poll form
    $.get('/poll/poll.php', '', function(data, status){
        pollcontainer.html(data);
        animateResults(pollcontainer);
        pollcontainer.find('#viewresult').click(function(){
            //if user wants to see result
            loader.fadeIn('2',function(){
                $.get('/poll/poll.php', 'result=1', function(data,status){
                    pollcontainer.fadeOut(1000, function(){
                        $(this).html(data);
                        animateResults(this);
                    });
                });
            });
            //prevent default behavior
            return false;
        }).end()
        .find('#pollform').submit(function(){
            var selected_val=$(this).find('input[name=poll]:checked').val();
            if(selected_val!=''){
                //post data only if a value is selected
                loader.fadeIn();
                $.post('/poll/poll.php', $(this).serialize(), function(data, status){
                    $('#formcontainer').fadeOut(100, function(){
                        $(this).html(data);
                        animateResults(this);
                        loader.fadeOut();
                    });
                });
            }
            //prevent form default behavior
            return false;
        });
        loader.fadeOut();
    });
	
    /*News Articles*/
    $(document).ready(function(){
        $("#small_news li.normal_article").click(function () {
            $(this).next().toggle("fast");
        });
    });
    $(document).ready(function(){
        $("div.mainarticletext h2").click(function () {
            $(this).hide();
            $("div#hidden_news").slideDown(80);
        });
        $("div#hidden_news").click(function () {
            $("div.mainarticletext h2").show();
            $("div#hidden_news").slideUp(80);
        });
    });
		
		
    function animateResults(data){
        $(data).find('.bar').hide().end().fadeIn('slow', function(){
            $(this).find('.bar').each(function(){
                var bar_width=$(this).css('width');
                $(this).css('width', '0').animate({
                    width: bar_width
                }, 1000);
            });
        });
    }
	
});
/****************************************/
/*				End Votes				*/
/****************************************/
