/* @version $Id: v 0.1 02.03.2010 - 17:10:37 Exp $
 *
 * Project:     chimera2.local_webservers
 * File:        votes.js *
 *
 * This library is commercial distributed software; you can't
 * redistribute it and/or modify it without owner (or author) approval.
 *
 * @link http://bestartdesign.com
 * @copyright (C) 2009
 *
 * @author Seyar Chapuh <seyarchapuh@gmail.com>, <sc@bestitsolutions.biz>
 */

function showPreloader( thisObj )
{
    if( $(thisObj).parent().find('.preloader').html() != undefined ) return;
    
    $(thisObj).css('display','none');
    var pre = '<div class="tCenter preloader" style="height:14px;"><img src="/templates/kontext/images/preloader.gif" alt="Loading..."/></div>';
    $(thisObj).before( pre );
    $('.preloader').css('paddingTop', ($(thisObj).height() / 2)+'px' );
    $('.preloader').css('paddingBottom', ($(thisObj).height() / 2 - 14)+'px' );
}

function hidePreloader( thisObj )
{
    $(thisObj).css('display','block');
    $('.preloader').remove();
}

function showVoteResults( withVotesLink )
{
    id = $('input[name=question_id]').val();
    showPreloader( $('#votes') );
    if( withVotesLink == undefined ) withVotesLink = 1;
    
    var url = '/'+lang+'/ajax/votes/'+id+'/showVoteResults';
    $.get(
        url.replace('//','/'),
        function(data)
        {
            $('#votes').html(data);
            hidePreloader( $('#votes') );
            //$('#showVoteQuestions').bind('click',function(){showVoteQuestions();return false;});
        }
    );
}

function showVoteQuestions()
{
    id = $('input[name=question_id]').val();
    showPreloader( $('#votes') );
    var url = '/'+lang+'/ajax/votes/'+id+'/showVoteResults';
    $.get(
        url.replace('//','/'),
        function(data)
        {
            $('#votes').html(data);
            hidePreloader($('#votes'));
        }
    );
}

function makeVote()
{
    var text = $('.quizTxt').val();
    var var_id = $('input[name=var]:checked').val();

    if(!text && var_id == -1 )  // if user choosed own war and dont fill text
    {
        alert('Введите свой вариант');
        return false;
    }
    id = $('input[name=question_id]').val();

    showPreloader( $('#votes') );
    var url = '/'+lang+'/ajax/votes/'+id+'/makeVote';

    $.get(
        url.replace('//','/'),
        {var_id:var_id, text:text},
        function(data)
        {
            if(data == 1)
            {
                showVoteResults(0);
                
            }else if(data = 'youvoted'){
                alert('Вы уже принимали участие в голосовании');
                showVoteResults();

            }else{
                hidePreloader( $('#votes') );
                alert('Server error');
            }
        }
    );
}
