(function($) {

    if (!window.burocratos) { return false; }
    if (!window.burocratos.actions) { return false; }
    if (!window.burocratos.actions.upload) { return false; }
    if (window.burocratos.actions.upload.votes) { return false; }
    
    var
            _VOTE_URL       = './'
        ,   _VOTE_PARAMS    = {
                    'page':     'actions.upload.vote'
            }
        ,   _IN_PROCESS     = '<p class="message">Ваш голос обрабатывается <img src="./images/vote.ajax.loader.gif" alt="LOADING" width="16" height="11" /></p>'
        ,   _COOKIE_NAME    = 'actions_upload_votes'
        ,   _EXPIRE_DAYS    = 365
        ;
    
    var
            rating = function(element) {
                var
                        $now    = $(element)
                    ,   rating  = 0
                    ;
                while ($now.size() && $now.is('span.vote-star')) {
                    rating++;
                    $now = $now.prev();
                }
                
                return rating;
            }
            
        ,   $container = function($cell) {
                return $cell.find('.vote-container');
            }
            
        ,   $rating = function($cell) {
                return $cell.find('.vote-rating');
            }
            
        ,   $cell = function(element) {
                return $(element).closest('td');
            }
            
        ,   params = function(rating, uploadId, actionId) {
                var params = _VOTE_PARAMS;
                params.rating = rating;
                params.uploadId = uploadId;
                params.actionId = actionId;
                return params;
            }
            
        ,   before = function($cell) {
                $container($cell).html(_IN_PROCESS);
            }
            
        ,   errMessage = function(params) {
                return '<p class="errors">Не удалось учесть ваш голос. ' + params.cause;
            }
            
        ,   success = function($cell, params) {
                $container($cell).html('<p class="message">Ваш голос принят!</p>');
                $rating($cell).html('Рейтинг: ' + (params.avg ? params.avg : 'нет') + ', голосов: ' + (params.count ? params.count : 'нет'));
                setCookie(
                        _COOKIE_NAME
                    ,   getCookie(_COOKIE_NAME) + '-' + params.uploadId + '/' + params.rating
                    ,   _EXPIRE_DAYS
                );
            }
            
        ,   got = function($cell, data) {
                var
                        $data   = $(data)
                    ,   params = {
                                'result':   $data.find('#result').html()
                            ,   'count':    $data.find('#count').html()
                            ,   'avg':      $data.find('#avg').html()
                            ,   'rating':   $data.find('#rating').html()
                            ,   'cause':    $data.find('#cause').html()
                            ,   'uploadId': $data.find('#uploadId').html()
                            ,   'actionId': $data.find('#actionId').html()
                        }
                    ;
                    
                if (!$data.size()) {
                    params.cause = 'Сервер вернул некорректный ответ';
                    error($cell, params);
                } if ('1' != params.result) {
                    error($cell, params);
                } else {
                    success($cell, params);
                }
            }
            
        ,   error = function($cell, params) {
                $container($cell).html(errMessage(params))
            }
        ;
    
    window.burocratos.actions.upload.votes = {
    
            hl: function(element) {
                var
                        $now = $(element)
                    ;
                while ($now.size() && $now.is('span.vote-star')) {
                    $now.addClass('vote-star-hl');
                    $now = $now.prev();
                }
            }
    
        ,   dsbl: function(element) {
                $(element).parent().find('span.vote-star-hl').removeClass('vote-star-hl');
            }
            
        ,   vote: function(uploadId, actionId, element) {
                var _$cell = $cell(element);
                $.ajax({
                        'url':          _VOTE_URL
                    ,   'dataType':     'html'
                    ,   'data':         params(rating(element), uploadId, actionId)
                    ,   'beforeSend':   function(jqXHR, settings) { before(_$cell); }
                    ,   'success':      function(data, textStatus, jqXHR) { got(_$cell, data); }
                    ,   'error':        function(jqXHR, textStatus, errorThrown) { error(_$cell, { 'cause': textStatus }); }
                });
            }
    };
    
})(jQuery);

