$(document).ready(function(){
    $('#homePoll .options div, #homePoll .options div div').click(function(){
        var dv_curHolder = $(this).parent().attr('class') == 'options' ? $(this) : $(this).parent();
        $('#homePoll .options div').removeClass('active');
        $('div',dv_curHolder).addClass('active');
        $('#homePoll .options input').val($(dv_curHolder).index()+1);
    });
    $('#homePoll a').click(function(){
        var i_awnser = $('#homePoll .options input').val();
        if(i_awnser != ''){
            $.ajax({
                type: "POST",
                url: s_siteUrl+"customs/homePollSave.php",
                data: "a="+i_awnser,
                success: function(o_return){
                    if(o_return.saved == 'y' && o_return.loaded == 'y'&& o_return.allready == 'n'){
                        var i_total = o_return.stats.total;
                        $('#homePoll .awnsers div').each(function(){
                            var i_val = o_return.stats.votes[$(this).index()];
                            var i_pro = Math.round(i_total > 0 && i_val > 0 ? (100/i_total)*i_val : 0);
                            $('div',this).html(i_pro+'%').attr('title',i_val+' stemmen');
                            $(this).attr('title',i_val+' stemmen');
                        });
                        $('#homePoll .options, #homePoll a').hide();
                        $('#homePoll .awnsers').show();
                        $('#homePoll .total span').html(i_total);
                        $('#homePoll .total').show();
                    } else {
                        if(o_return.allready == 'y'){
                            $('#homePoll .mess').html('Je hebt al een keer gestemd, klik hier om de uitslag te bekijken)').show().click(function(){
                                $('#homePoll .options, #homePoll a').hide();
                                $('#homePoll .awnsers').show();
                                $('#homePoll .total').show();
                            });
                        } else {
                            $('#homePoll .mess').html('Er is iets fout gegaan, probeer het nog eens of neem contact met ons op.<br /><br />(klik om terug te gaan)').show();
                        }
                    }
                },
                dataType: 'json'
            });
        } else {
            $('#homePoll .mess').html('Je hebt nog geen keuze gemaakt.<br /><br />(klik om terug te gaan)').show();
        }
        return(false);
    });
    $('#homePoll .mess').click(function(){
        $(this).html('').hide();
    });
});
