$(document).ready( function() {

    checkCookie();

    // --
    // vote action
    $("#btn-vote-men").live('click', function() {
        votePoll('men');
    });

    $("#btn-vote-women").live('click', function() {
        votePoll('women');
    });
    // --

    $("#btn-results").live('click', function() {
        getResults();
    });
    // --



    // --
    // results action
    $("#btn-results-all").live('click', function() {

        $("#results-place-all").show();
        $("#results-place-men").hide();
        $("#results-place-women").hide();
    });

    $("#btn-results-men").live('click', function() {

        $("#results-place-all").hide();
        $("#results-place-men").show();
        $("#results-place-women").hide();
    });

    $("#btn-results-women").live('click', function() {

        $("#results-place-all").hide();
        $("#results-place-men").hide();
        $("#results-place-women").show();
    });
    // --

});



    // --
    // votePoll
    // --
    votePoll = function( spol ) {

        var AnswerId = 0;

        $('input[type=radio]').each(

        	function(ix, obj) {

        		if( $(obj).attr('checked') == true ) {
                    AnswerId = $(obj).attr('id');
                }

        	}

  		);

        if( AnswerId == '' ) {
            alert('Izberi odgovor!');
            return;
        }

        var data = {
            QuestionId: $("#QuestionId").val(),
            AnswerId: AnswerId,
            Spol: ''+spol+''
        };

        jsonText = JSON.stringify( data );

        $.ajax({
  		    type: "POST",
  		    url: URLROOT+'ajax.php?module=poll&action=votePoll',
            data: {inputData: jsonText},
  		    success: function(msg) {
  			    if (msg) {
  				    votePollCallback(msg);
  			    }
  		    }
  	    });

    }

    votePollCallback = function( msg ) {
        checkCookie();
    }
    // --



    // --
    // getPoll
    // --
    getPoll = function() {

        $.ajax({
  		    type: "POST",
  		    url: URLROOT+'ajax.php?module=poll&action=getPoll',
            data: {},
  		    success: function(msg) {
  			    if (msg) {
  				    getPollCallback(msg);
  			    }
  		    }
  	    });
    }

    getPollCallback = function( html ) {
        $("#pollPlace").html( html );
    }
    // --



    // --
    // getResults
    // --
    getResults = function() {

        $.ajax({
  		    type: "POST",
  		    url: URLROOT+'ajax.php?module=poll&action=getResults',
            data: {},
  		    success: function(msg) {
  			    if (msg) {
  				    getResultsCallback(msg);
  			    }
  		    }
  	    });
    }

    getResultsCallback = function( html ) {

        $("#pollPlace").html( html );

        $("#results-place-all").show();
        $("#results-place-men").hide();
        $("#results-place-women").hide();
    }
    // --


    // --
    // checkCookie
    // --
    checkCookie = function() {

        $.ajax({
  		    type: "POST",
  		    url: URLROOT+'ajax.php?module=poll&action=checkCookie',
            data: {},
  		    success: function(msg) {
  			    if (msg) {
  				    checkCookieCallback(msg);
  			    }
  		    }
  	    });
    }

    checkCookieCallback = function( msg ) {

        if(msg == 1) {
            getResults();
        } else {
            getPoll();
        }

    }
    // --

