function handler_vote( guid )
{
    if ( guid == '' )
    {
        jQuery.get
        (
            '/cgi-bin/vote/list' ,
            {
            }                    ,
            function( data )
            {
                core_vote_list(data);

            } ///;~
        );

        jQuery.get
        (
            '/cgi-bin/vote/current' ,
            {
            }                       ,
            function( data )
            {
                core_vote_item(data, false);
                core_vote_tuning(false);

            } ///;~
        );
    }
    else if ( isNaN(parseInt(guid)) == true )
    {
        jQuery.get
        (
            '/cgi-bin/vote/list' ,
            {
            }                    ,
            function( data )
            {
                core_vote_catalogue(data);

            } ///;~
        );
    }
    else
    {
        jQuery.get
        (
            '/cgi-bin/vote/list' ,
            {
            }                    ,
            function( data )
            {
                core_vote_list(data);

            } ///;~
        );

        jQuery.get
        (
            '/cgi-bin/vote/item' ,
            {
                guid: guid
            }                    ,
            function( data )
            {
                core_vote_item(data, false);
                core_vote_tuning(false);

            } ///;~
        );
    }

} ///;~

function core_vote_current( data )
{
    var vote   = $('item'   , data);
    var parent = $('guid'   , vote).text();
    var title  = $('title'  , vote).text();
    var choice = $('choice' , vote).text();
    var code   = $('data'   , vote).text();
    var total  = $('voice'  , vote).text();
    var size   = $('option' , data).size();

    var head     = $('div.core_vote_current:last').find('div.question:last');
    var item     = $('div.core_vote_current:last').find('div.option:last');
    var accepted = $('div.core_vote_current:last').find('div.accepted:last');
    var summary  = $('div.core_vote_current:last').find('div.summary:last');
    var archive  = $('div.core_vote_current:last').find('div.archive:last');

    $('div.core_vote_current:last').html(head);
    $('div.core_vote_current:last').append(summary);
    $('div.core_vote_current:last').append(accepted);
    $('div.core_vote_current:last').append(item);

    if ( choice == 'NULL' )
    {
        $(accepted).hide();
    }
    else
    {
        $(accepted).show();
    }

    $('table.core_vote_current').find('div.question')
                                .text(title);

    $('table.core_vote_current').find('div.summary')
                                .find('span')
                                .text(total);

    $('option' , data).each(function( position )
    {
        var guid  = $('guid'  , this).text();
        var data  = $('data'  , this).text();
        var voice = $('voice' , this).text();
        var pct   = Math.ceil((100 * voice) / total);

        $('div.core_vote_current:last').find('div.option:last')
                                       .find('a')
                                       .text(data);

        $('div.core_vote_current:last').find('div.option:last')
                                       .find('div.percentage')
                                       .text(pct + '%');

        $('div.core_vote_current:last').find('div.option:last')
                                       .find('div.progressbar')
                                       .css
        ({
            width: pct + '%'
        });

        $('div.core_vote_current:last').find('div.option:last')
                                       .find('a')
                                       .click(function()
        {
            core_vote_submit
            (
                guid   ,
                parent ,
                code
            );
        });

        if ( (position + 1) != size )
        {
            $('div.core_vote_current:last').find('div.option:last')
                                           .clone()
                                           .appendTo('div.core_vote_current:last');
        }
    });

    $('div.core_vote_current:last').append(archive);
    $('table.core_vote_current').show();

} ///;~

function core_vote_catalogue( data )
{
    var size = $('item', data).size();
    var item = $('div.core_vote_catalogue:last');
    var link = $('div.core_vote_link:last');

    if ( size == 0 )
    {
        $('#core_mainframe_container').html('');
    }
    else
    {
        $('#core_mainframe_container').html(item);
    }

    $('item', data).each(function( position )
    {
        var guid = $('guid', this).text();
        var data = $('data', this).text();

        $('div.core_vote_catalogue:last').find('h3.core_vote_title')
                                         .text(data);

        $('div.core_vote_catalogue:last').find('ul.core_vote_item')
                                         .attr('id', 'opt_' + guid);

        $('div.core_vote_catalogue:last').attr('id', guid);
        $('div.core_vote_catalogue:last').ready(function()
        {
            jQuery.get
            (
                '/cgi-bin/vote/item' ,
                {
                    guid: guid
                }                    ,
                function( data )
                {
                    core_vote_item(data, guid);
                    core_vote_tuning(guid);

                } ///;~
            );
        });

        if ( (position + 1) != size )
        {
            $('div.core_vote_catalogue:last').clone()
                                             .appendTo('#core_mainframe_container');
        }
    });

    $(link).appendTo('#core_mainframe_container');

} ///;~

function core_vote_list( data )
{
    var size = $('item', data).size();
    var item = $('ul.core_vote_list').find('li:last');

    if ( size == 0 )
    {
        $('ul.core_vote_list').hide();
    }
    else
    {
        $('ul.core_vote_list').html(item);
    }

    $('item', data).each(function( position )
    {
        var guid = $('guid', this).text();
        var data = $('data', this).text();

        $('ul.core_vote_list').find('li:last')
                              .find('a')
                              .text(data);

        $('ul.core_vote_list').find('li:last')
                              .find('a')
                              .attr
        ({
            href: '/vote/' + guid + '/' ,
            title: data                 ,
            alt:   data
        });

        if ( (position + 1) != size )
        {
            $('ul.core_vote_list').find('li:last')
                                  .clone()
                                  .appendTo('ul.core_vote_list');
        }
    });

    $('ul.core_vote_list').show();

} ///;~

function core_vote_item( data ,
                         dvid )
{
    var vote   = $('item'   , data);
    var parent = $('guid'   , vote).text();
    var title  = $('title'  , vote).text();
    var choice = $('choice' , vote).text();
    var code   = $('data'   , vote).text();
    var total  = $('voice'  , vote).text();
    var size   = $('option' , data).size();

    if ( dvid == false )
    {
        var item   = $('ul.core_vote_item').find('li:last');
    }
    else
    {
        var item   = $('#' + dvid).find('ul.core_vote_item')
                                  .find('li:last');
    }

    if ( dvid == false )
    {
        $('h3.core_vote_title').text(title);
        $('p.core_vote_voice').find('span')
                              .text(total);
    }
    else
    {
        $('#' + dvid).find('h3.core_vote_title')
                     .text(title);

        $('#' + dvid).find('p.core_vote_voice')
                     .find('span')
                     .text(total);
    }

    if ( size == 0 )
    {
        if ( dvid == false )
        {
            $('ul.core_vote_item').hide();
        }
        else
        {
            $('#' + dvid).find('ul.core_vote_item')
                         .hide();
        }
    }
    else
    {
        if ( dvid == false )
        {
            $('ul.core_vote_item').html(item);
        }
        else
        {
            $('' + dvid).find('ul.core_vote_item')
                        .html(item);
        }
    }

    $('option', data).each(function( position )
    {
        var guid  = $('guid'  , this).text();
        var data  = $('data'  , this).text();
        var voice = $('voice' , this).text();
        var pct   = Math.ceil((100 * voice) / total);

        if ( dvid == false )
        {
            $('ul.core_vote_item').find('li:last')
                                  .find('div.core_vote_item_data')
                                  .text(data);

            $('ul.core_vote_item').find('li:last')
                                  .find('div.core_vote_item_voice')
                                  .find('span')
                                  .text(voice);

            $('ul.core_vote_item').find('li:last')
                                  .find('div.percentage')
                                  .text(pct + '%');

            $('ul.core_vote_item').find('li:last')
                                  .find('div.progressbar')
                                  .css
            ({
                width: pct + '%'
            });
        }
        else
        {
            $('#' + dvid).find('ul.core_vote_item')
                         .find('li:last')
                         .find('div.core_vote_item_data')
                         .text(data);

            $('#' + dvid).find('ul.core_vote_item')
                         .find('li:last')
                         .find('div.core_vote_item_voice')
                         .find('span')
                         .text(voice);

            $('#' + dvid).find('ul.core_vote_item')
                         .find('li:last')
                         .find('div.percentage')
                         .text(pct + '%');

            $('#' + dvid).find('ul.core_vote_item')
                         .find('li:last')
                         .find('div.progressbar')
                         .css
            ({
                width: pct + '%'
            });
        }

        if ( (position + 1) != size )
        {
            if ( dvid == false )
            {
                $('ul.core_vote_item').find('li:last')
                                      .clone()
                                      .appendTo('ul.core_vote_item');
            }
            else
            {
                $('#' + dvid).find('ul.core_vote_item')
                             .find('li:last')
                             .clone()
                             .appendTo('#opt_' + dvid);
            }
        }
    });

    if ( dvid == false )
    {
        $('ul.core_vote_item').show();
        $('h3.core_vote_title').show();
        $('p.core_vote_voice').show();
    }
    else
    {
        $('#' + dvid).find('ul.core_vote_item')
                     .show();

        $('#' + dvid).find('h3.core_vote_title')
                     .show();

        $('#' + dvid).find('p.core_vote_voice')
                     .show();
    }

} ///;~

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function core_vote_submit( choice ,
                           parent ,
                           data   )
{
    
if(getCookie("ivote2")!=parent) {
jQuery.post
    (
        '/operator/vote/submit.php' ,
        {
            choice: choice ,
            parent: parent ,
            data:   data
        }                           ,
        function( data )
        {
            core_vote_current(data);

        } ///;~
    );
setCookie("ivote2", parent);
} else alert("Вы уже принимали участие в данном опросе. Спасибо! ");
} ///;~

function core_vote_tuning( dvid )
{
    var maxoption     = 0;
    var maxpercentage = 0;
    var maxsummary    = 0;

    if ( dvid == false )
    {
        $('ul.core_vote_item').find('div.option')
                              .each(function( position )
        {
            if ( $(this).width() > maxoption )
            {
                maxoption = $(this).width();
            }
        });

        $('ul.core_vote_item').find('div.percentage')
                              .each(function( position )
        {
            if ( $(this).width() > maxpercentage )
            {
                maxpercentage = $(this).width();
            }
        });

        $('ul.core_vote_item').find('div.summary')
                              .each(function( position )
        {
            if ( $(this).width() > maxsummary )
            {
                maxsummary = $(this).width();
            }
        });

        $('ul.core_vote_item').find('div.option')
                              .width(maxoption);

        $('ul.core_vote_item').find('div.percentage')
                              .width(maxpercentage);

        $('ul.core_vote_item').find('div.summary')
                              .width(maxsummary);
    }
    else
    {
        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.option')
                     .each(function( position )
        {
            if ( $(this).width() > maxoption )
            {
                maxoption = $(this).width();
            }
        });

        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.percentage')
                     .each(function( position )
        {
            if ( $(this).width() > maxpercentage )
            {
                maxpercentage = $(this).width();
            }
        });

        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.summary')
                     .each(function( position )
        {
            if ( $(this).width() > maxsummary )
            {
                maxsummary = $(this).width();
            }
        });

        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.option')
                     .width(maxoption);

        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.percentage')
                     .width(maxpercentage);

        $('#' + dvid).find('ul.core_vote_item')
                     .find('div.summary')
                     .width(maxsummary);
    }

} ///;~
