/* <![CDATA[ */
// poll tease initialization
function initPollTease(teaseObj,teasePostPath)
{
    cmg.query(teaseObj + ' form.poll_form').submit(function(){
        var checked =  cmg.query(teaseObj + ' form.poll_form input:radio[name=choice]:checked').val();
        if (checked == undefined) {
            // show poll tease message
            cmg.query(teaseObj + ' .cmPollMessage.tease').addClass('cmErrorBox round').show();
            cmg.query(teaseObj + ' .cmPollMessage.tease p').addClass('sprite iconAlert').html('Please choose an answer.');
            return false;
        } else {
            // reset poll tease message
            resetPollTeaseMsg(teaseObj);
            // submit vote choices to AJAX view
            cmg.query.ajax({
                url: teasePostPath,
                type: 'POST',
                data: cmg.query(teaseObj + ' form.poll_form').serialize(),
                dataType: 'json',
                success: function(data){
                    renderPollTease(data);
                }
            });
            return false;
        }
    });
}

// get width of col
function getPollBarScale(pollObject){
    var barscale =0.9;
    var cmWidth = cmg.query('#cmPollTeaseId_' + pollObject).width();
    if (cmWidth <= 195) { barscale=0.75 }
    if (cmWidth > 195 && cmWidth <= 300 ) { barscale=0.8 }
    if (cmWidth > 300 && cmWidth <= 405 ) { barscale=0.85  }
    if (cmWidth >= 615 ) { barscale=0.9 }
    return barscale;
}

// reset poll tease msg
function resetPollTeaseMsg(teaseObj)
{
    cmg.query(teaseObj + ' .cmPollMessage.tease p').removeClass('sprite iconAlert iconSuccess').html('');
    cmg.query(teaseObj + ' .cmPollMessage.tease').removeClass('cmErrorBox cmMessageBox round').hide();
}

// poll tease render
function renderPollTease(jsonData)
{
    // set a variable for the poll id from the JSON data object
    var poll_id = jsonData.poll_id;
    // test for jsonData.error and render a stock error message
    if (jsonData.error != undefined)
    {
        cmg.query('#cmPollTeaseId_' + poll_id + ' .cmPollMessage.tease').addClass('cmErrorBox round').show();
        cmg.query('#cmPollTeaseId_' + poll_id + ' .cmPollMessage.tease p').addClass('sprite iconAlert').html(jsonData.error);
    }
    // render a voting success messsage
    else
    {
        cmg.query('#cmPollTeaseId_' + poll_id + ' .cmPollMessage.tease').addClass('cmMessageBox round').show();
        cmg.query('#cmPollTeaseId_' + poll_id + ' .cmPollMessage.tease p').addClass('sprite iconSuccess').html('Thank you for voting!');
    }
    // hide the poll form container
    cmg.query('#cmPollTeaseId_' + poll_id + ' div.cmPollForm').css({display: 'none'});
    // show the poll results container
    cmg.query('#cmPollTeaseId_' + poll_id + ' div.cmPollResults').css({display: 'block'});
    // loop through the choice containers and set the number of votes, the bar, and the percentage

    barscale = getPollBarScale(poll_id);

    for (var i in jsonData.choices)
    {
       cmg.query('#cmPollTeaseId_' + poll_id + ' #choiceId_' + jsonData.choices[i].id).each(function(){
           votes = cmg.query('span.votes', this), bar = cmg.query('div.bar_percent', this), percent = cmg.query('div.percent', this);
           // fill in the votes using the json data
           var votes_suffix = (jsonData.choices[i].votes == 1) ? ' vote' : ' votes';
           votes.html(' ' + jsonData.choices[i].votes + ' ' + votes_suffix);
           // fill in the percent using the json data
           percent.html(Math.round(jsonData.choices[i].percentage) + '%');
           vote_percent = percent.html().slice(0,percent.html().indexOf('%'));
           if (vote_percent < 1) {
               cmg.query('div.bar_percent', this).remove('div.bar_percent');
               percent.removeClass("percent");
           }
           // animate the bar using the json data
           bar.animate({width: Math.round(jsonData.choices[i].percentage)*barscale + '%'}, 600);
       });
    }
    // show the total votes below the results bar
    var total_votes_suffix = (jsonData.total_votes == 1) ? ' total vote' : ' total votes';
    cmg.query('#cmPollTeaseId_' + poll_id + ' p.total_votes').html(jsonData.total_votes + total_votes_suffix);
}

// inactive (expired or closed to multiple votes) poll tease render
function renderInactivePollTease(teaseId)
{
	// show the poll results container
    cmg.query('#cmPollTeaseId_' + teaseId + ' div.cmPollResults').css({display: 'block'});
	// render the poll results
	cmg.query('#cmPollTeaseId_' + teaseId + ' div.choice').each(function(){
		// create jQuery objects for the bar and percent container;
		bar = cmg.query('div.bar_percent', this), percent = cmg.query('div.percent', this);
		// extract the percent from the template tag from the percent container
		vote_percent = percent.html().slice(0,percent.html().indexOf('%'));
		// if there are no votes, hide the bar and the margin
	    if (vote_percent < 1) {
            cmg.query('div.bar_percent', this).remove('div.bar_percent');
            percent.removeClass("percent");
        }

        var barscale = getPollBarScale(teaseId);
        bar.animate({width: vote_percent*barscale + '%'}, 600);
	});
}
/* ]]> */

