function load_vote() {
    var bag = $('#vote span');
    bag.html('');
    jQuery.getJSON(SITE_URL+'/ajax/load_vote&id='+item_id, function(json){
        var total = 0;
        $(json).each(function() {
            total += parseInt(this.num);
            var html = '<div class="voteRow clearfix">\
              <div class="voteName">'+this.name+'：</div>\
              <div class="voteBar"><div class="vbWarp"><div class="vbIs" style="width:'+this.num+'px"></div></div></div>\
              <div class="voteNum">'+this.num+'</div>\
              <div class="voteInput"><input type="checkbox" name="vote[]" value="1" /></div>\
            </div>';
            bag.append(html);
        });

        $('.vbIs', bag).each(function() {
            var old = parseInt($(this).css('width'));
            if (total == 0) {
                var per = 0;
            } else {
                var per = parseInt(100 * old / total);
            }

            $(this).css('width', '0').show().animate({width:per+'%'}, 'slow');
            $(this).parent().parent().next().append(' ('+per+'%)');
        });

    });
}

$(function() {
    load_vote();
    $('.voteBtn input').click(function() {
        var bag = $('#vote span');
        var i = 1;
        var string = '';
        $('input[type=checkbox]').each(function(){
            if (this.checked) {
                string += '&vote[]='+i;
            }
            i++;
        });
        if (string == '') {
            alert('请至少选择一个选项!');
            return false;
        }

        // if (confirm('提醒好友?')) {
        //   string += '&notice=1';
        // }

        $(this).attr('disabled', 'disabled');

        jQuery.getJSON(SITE_URL+'/ajax/vote&id='+item_id+string, function(json) {
            if (json.info == '') {
                load_vote();
                $('.voteBtn').remove();
            } else {
              if (json.info == 'login') {
                jQuery.facebox({ ajax: 'login.html' });
              } else {
                alert(json.info);
              }
              $('.voteBtn input').removeAttr('disabled');
            }
        });
    });
    $('#giftBox input[type=button]').click(function(){
        if ($('input[name=cfIds[]]').length == 0) {
            alert('你必须选择至少一位好友');
        } else {
            if ($('input[name=cfIds[]]').length > 30) {
                alert('你最多只能选择30位好友!');
            } else {
                this.form.submit();
            }
        }
    });

    $('#giftBtn').toggle(function(){
        $('#giftBox').slideDown('fast');
    },function(){
        $('#giftBox').slideUp('fast');
    });

    hit_item(item_id);
});
