﻿var Ratings = {
    _oObserver: null,
    _elObeserver: null,
    _elDiv: null,
    _elUL: null,

    Rate: function(e) {
        var el = Event.findElement(e, 'a');
        if (!el.tagName == "A") {
            return false
        }
        var l = el.readAttribute('href')
        var arr = l.split('?')
        var arr1 = arr[1].split('=')
        // alert(arr1[1]);
        _elDiv = Event.findElement(e, 'div')
        _elUL = Event.findElement(e, 'UL')
        _elDiv.down(0).hide();
        _elDiv.down(1).hide();
        _elUL.addClassName('rfSaving')
        var _elType = 0; // 1 = Inline, 2 = Bellow

        if (_elUL.hasClassName("rfOlNi")) {
            _elType = 2
        }

        if (_elUL.hasClassName("rfIlNi")) {
            _elType = 1
        }

        if (_elType == 0) {
            alert("Something is wrong!");
            return false
        }

        if (_elType == 2) {
            _elUL.addClassName('rfOlNirfSaving')
        }
        if (_elType == 1) {
            _elUL.addClassName('rfIlNirfSaving')
        }

        if (!arr1[1] == "") {
            if (FilmAjax.RateFilm(arr1[1], RateFilm_CallBack)) {

            }
        } else {
            alert("No rating value provided.");
            Event.stop(e);
        }
        function RateFilm_CallBack(response) {
            if (response.error != null) {
                alert(response.error);
                RateFilm_CallBackFinish();
                return false;
            }
            if (!response.value) {
                alert("Action Failed");
                RateFilm_CallBackFinish();
                return false;
            }
            if (response.value == -1) {
                PopUp.logIn(1);
                RateFilm_CallBackFinish();
                return false;
            }

            RateFilm_CallBackFinish();
            if (response.value == 0) {
                if (_elType == 2) {
                    _elDiv.down(1).setStyle({ backgroundPosition: '0px 0px', width: '0px' });
                    _elUL.addClassName('rfOl0Active')
                }
                if (_elType == 1) {
                    _elDiv.down(1).setStyle({ backgroundPosition: '20px 0px', width: '0px' });
                    _elUL.addClassName('rfIlNiActive')
                }
            } else {
                if (_elType == 2) {
                    _elDiv.down(1).setStyle({ backgroundPosition: '0px 0px', width: '' + response.value + 'px' });
                    _elUL.removeClassName('rfOl0Active')
                }
                if (_elType == 1) {
                    _elDiv.down(1).setStyle({ backgroundPosition: '20px 0px', width: '' + (parseInt(response.value) + 20) + 'px' });
                    _elUL.removeClassName('rfIlNiActive')
                }

            }
        }
        function RateFilm_CallBackFinish() {
            _elDiv.down(0).show();
            _elDiv.down(1).show();
            _elUL.removeClassName('rfSaving')
            if (_elType == 2) {
                _elUL.removeClassName('rfOlNirfSaving')
            }
            if (_elType == 1) {
                _elUL.removeClassName('rfIlNirfSaving')
            }

        }
        Event.stop(e);
    },

    OnMouseRate: function(e) {
        switch (e.type) {
            case 'mouseover':
                _elObeserver = Event.findElement(e, 'UL');
                _oObserver = Ratings.Rate;
                Event.observe(_elObeserver, 'click', _oObserver);
                break;

            case 'mouseout':
                Event.stopObserving(_elObeserver, 'click', _oObserver);
                break;
        }
    }

}

var rO = Ratings.OnMouseRate;
