var ratingWsdl = '/wsdl/rating.php';

if (typeof(ratingUpdater) == 'undefined')
{
    ratingUpdater = function(type)
    {
        this.type = type;

        this.ratings = new Array();

        this.authorized = false;
        this.checkAuthorized();
    };
}

ratingUpdater.prototype.addRating = function (rating){
    this.ratings.push(rating);
};

ratingUpdater.prototype.init = function (){
    ownerId = new Array();
    currObject = this;

    for(i in this.ratings) {
        ownerId.push(this.ratings[i].ownerId);
    }

    postData = {'action' : 'getRatings', 'ownerId[]': ownerId, type: this.type};

    $.post(ratingWsdl,
           postData,
            function(data){

                if ((typeof(showAJAXDebugInfo) != 'undefined') && data.PHPAJAXDebug != null)
                {
                    showAJAXDebugInfo(data.PHPAJAXDebug.Info, data.PHPAJAXDebug.Owner);
                }

                if (data.Response.RatingResults) {
                    for (i in currObject.ratings) {
                        currObject.ratings[i].initHandlers();

                        if (data.Response.RatingResults[currObject.ratings[i].ownerId]) {

                            result = data.Response.RatingResults[currObject.ratings[i].ownerId];

                            currObject.ratings[i].update(result.Result, result.CanSend,  result.RatingCount);
                        } else {
                            currObject.ratings[i].update(0, true, 0);
                        }
                    }
                } else {
                    for (i in currObject.ratings) {
                        currObject.ratings[i].initHandlers();
                        currObject.ratings[i].update(0, true, 0);
                    }
                }

            }, 'json');

};

ratingUpdater.prototype.checkAuthorized = function () {
    currObject = this;

    postData = {'action' : 'checkAuthorized'};

    $.post(ratingWsdl,
            postData,
            function(data){

                if ((typeof(showAJAXDebugInfo) != 'undefined') && data.PHPAJAXDebug != null)
                {
                    showAJAXDebugInfo(data.PHPAJAXDebug.Info, data.PHPAJAXDebug.Owner);
                }

                if (data.Response.Authorized && data.Response.Authorized == 1) {
                    currObject.authorized = true;
                }

            }, 'json');
};

if (typeof(rating) == 'undefined')
{
    ratingComponent = function(parentTagId, ownerId, type, updaterObject)
    {
        this.parentTagId   = parentTagId;
        this.ownerId       = ownerId;
        this.type          = type;
        this.isInitiated   = false;

        if (updaterObject) {
            this.updaterObject = updaterObject;
        } else {
            this.updaterObject = new ratingUpdater(type);
            this.updaterObject.addRating(this);
            this.updaterObject.init();
        }

        classes = ['s05', 's1', 's15', 's2', 's25', 's3', 's35', 's4', 's45', 's5'];

        this.content = '';

        for (i in classes) {
            this.content += '<li><a href="javascript:void(0);" class="'+classes[i]+'">'+(parseInt(i)+1)+'</a></li>';
        }

        $('#'+parentTagId).html(this.content);

        var curObject = this;
    };
}

ratingComponent.prototype.initHandlers = function (){
    var curObject = this;
    if (!this.isInitiated) {

        if (true/*this.updaterObject.authorized*/) {
        	
            $('#'+this.parentTagId+' li').mouseover(function(){
                //$('#'+curObject.parentTagId+' li').removeClass('active');
            });
            
            $('#'+this.parentTagId+' li').parent().mouseleave(function(){
                if (false && curObject.rating) {
                    var index = curObject.rating - 1;
                    var elem = $('#'+curObject.parentTagId+' li').get(index);
                    $(elem).addClass('active');
                }
            });
        }

        $('#'+this.parentTagId+' li').each(function(){
            $(this).click(function(){
                if (curObject.updaterObject.authorized) {
                    curObject.sendRating($(this).text());
                } else {
                    systemAlert('You have to be logged in to do that.');
                }
            });
        });

        this.isInitiated = true;
    }
};

ratingComponent.prototype.sendRating = function(rating){
    var currentObject = this;

    postData = {action: 'setRating', ownerId: this.ownerId, rating: rating, type: this.type};

    $.post(ratingWsdl,
            postData,
            function(data){

            if ((typeof(showAJAXDebugInfo) != 'undefined') && data.PHPAJAXDebug != null)
            {
                showAJAXDebugInfo(data.PHPAJAXDebug.Info, data.PHPAJAXDebug.Owner);
            }

            if (data.Response.Success) {
                currentObject.updaterObject.init();
                systemAlert(data.Response.Success);
            } else {
                systemAlert(data.Response.Error);
            }

            }, 'json');
};

ratingComponent.prototype.update = function(result, canSend, ratingCount){
    this.rating = result;

    if (result != 0) {
        var index = this.rating - 1;
        var elem = $('#'+this.parentTagId+' li').get(index);
        $(elem).addClass('active');
    }

    $('#ratingCount').text(ratingCount);
};


