﻿function VoteControl(elemId, itemNumber, itemType, serverKey, initialText, loadingText) 
{
    var result =
    {
        itemId: itemNumber,
        itemType: itemType,
        serverKey: serverKey,
        container: document.getElementById(elemId),
        initialText: initialText,
        loadingText: loadingText,
        init: function() {
            this.container.innerHTML = '<a id="voteButton"><img src="/images/icons/plus.gif" alt="" align="middle"/></a>' + this.initialText;
            var button = document.getElementById('voteButton');
            button.onclick = function() {
                if (voteControl) voteControl.click();
                return false;
            }
        },

        click: function() {
            this.container.innerHTML = this.loadingText;
            AdWiser.Fashion.Web.Services.PageService.LogItemVote(this.itemId, this.itemType, this.serverKey, this.onVoteCompleted, this.onVoteError, this);
            return false;
        },

        onVoteCompleted: function(result, userState) {
            var oStatus = null;
            eval('oStatus = ' + result);
            if (null != oStatus) {
                if (oStatus.status >= 0) {
                    userState.container.innerHTML = '<span class="voteSuccess">' + oStatus.message + '</span>';
                }
                else {
                    alert(oStatus.message);
                    userState.init();
                }
            }
            else {
                alert('Unknown error has occured. Please, try again.');
                userState.init();
            }
        },

        onVoteError: function(error, userState) {
            alert('Unknown error has occured. Please, try again.');
            userState.init();
        }
    };
    result.init();        
    return result;    
}



