/* statick class to create an okRating object */
function getOkRating(params)
{
    return getOkRating.initialize(params);
}
getOkRating.ratings = []
getOkRating.initialize = function(params)
{
    if (typeof(params)=='undefined') params = {};
    params.name = 'getOkRating.ratings['+getOkRating.ratings.length+']';
    return getOkRating.ratings[getOkRating.ratings.length] = new okRating(params);
}
/* -------------------------------------------------------------------------- */

function okRating(params) 
{
    this.initialize(params);
}
okRating.names = [];
okRating.prototype = {
    name                : "okRating",
    apiURL              : "http://stage-api.connectok.com/json/",
    methodClassName     : 'Rating',
    uid                 : '',
    moduleId            : 0,
    moduleTypeId        : 0,
    containerId         : '',
    login               : null,
    params              : null,
    rateSet             : null,
    userRateSet         : null,
    profile_id          : 0,
    hooverStartPos      : -14,
    selectStartPos      : -28,
    starWidth           : 16,
    e_id                : '',
    loginMsg            : 'You must be logged in to rate.',
    
    initialize : function (params)
    {
        this.params = params;

        okRating.names[params.name] = this;
        if(typeof(params.name) != 'undefined')              { this.name             = params.name;              }
        if(typeof(params.currentPhotoId) != 'undefined')    { this.currentPhotoId   = params.currentPhotoId;    }
        if(typeof(params.uid) != 'undefined')               { this.uid              = params.uid;               }
        if(typeof(params.moduleId) != 'undefined')          { this.moduleId         = params.moduleId;          }
        if(typeof(params.moduleTypeId) != 'undefined')      { this.moduleTypeId     = params.moduleTypeId;      }
        if(typeof(params.type_id) != 'undefined')           { this.type_id          = params.type_id;           }
        if(typeof(params.apiURL) != 'undefined')            { this.apiURL           = params.apiURL;            }
        if(typeof(params.methodClassName) != 'undefined')   { this.methodClassName  = params.methodClassName    }
        if(typeof(params.containerId) != 'undefined')       { this.containerId      = params.containerId;       }
        if(typeof(params.hooverStartPos) != 'undefined')    { this.hooverStartPos   = params.hooverStartPos;    }
        if(typeof(params.selectStartPos) != 'undefined')    { this.selectStartPos   = params.selectStartPos;    }
        if(typeof(params.starWidth) != 'undefined')         { this.starWidth        = params.starWidth;         }
        if(typeof(params.e_id) != 'undefined')              { this.e_id             = params.e_id;              }

        /*
        if(this.e_id.length == 0){
            if (this.login == null) this.login = okProfile;
            Broadcaster.subscribe(okProfile, 'onLoggedIn', this, 'loggedIn')
        }
        */
    },
    setRating : function(pos)
    {
        if (pos == null) return; 
        if(this.uid.length != 0){
            this.setRatingByUid(pos);
        }else{
            this.setRatingByModuleId(pos);
        }
        document.getElementById('user_rated').innerHTML = pos;
    },
    setRatingByUid : function(pos)
    {
        queryString  = "method="        + "Rating.SetRating";
        queryString += "&uid="          + this.uid;
        queryString += "&e_id="         + this.login.e_id;
        queryString += "&rating="       + pos;
        loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    setRatingByModuleId : function(pos)
    {
        queryString  = "method="        + "Rating.SetRating";
        queryString += "&moduleId="     + this.moduleId;
        queryString += "&moduleTypeId=" + this.moduleTypeId;
        queryString += "&e_id="         + this.e_id;
        queryString += "&rating="       + pos;
        loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    getRating : function(id, moduleTypeId)
    {
        if (typeof(moduleTypeId) == 'undefined') {
            this.getRatingByUid(id);
        }else{
            this.getRatingByModule(id, moduleTypeId);
        }
    },
    getRatingByModule : function(moduleId, moduleTypeId)
    {
        if (typeof(moduleId) == 'undefined' && typeof(moduleTypeId) == 'undefined') return;
        this.moduleId       = moduleId;
        this.moduleTypeId   = moduleTypeId;

        queryString  = "method=Rating.GetRating";
        queryString += "&moduleId="      + moduleId;
        queryString += "&moduleTypeId="  + moduleTypeId;
        queryString += "&e_id="          + this.e_id;

        loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    /* From Backward Compatibility */
    getRatingByUid : function(uid)
    {
        if (typeof(uid) == 'undefined') return;
        this.uid = uid;

        queryString  = "method=Rating.GetRating";
        queryString += "&uid="      + uid;
        queryString += "&e_id="     + this.login.e_id;

        loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    display : function(data)
    {
    },
    resizeDiv   : function(pos) 
    {
        findRate = parseInt(pos,10); // strip off 'rate' at the begining to get the rating number
        document.getElementById(this.name+'userHover').style.width = (findRate * this.starWidth)+'px';          // set the div to the width of the hover star
    },
    setStars    : function(pos) 
    {
        if (this.rateSet == null) this.rateSet = pos;
        this.resizeDiv(pos);
        document.getElementById(this.name+'userHover').style.backgroundPosition = '0 '+this.selectStartPos+'px';        // set the position of the sprite to show the dimmed star
    },
    setRate : function(pos, callJSON) 
    {
        if (typeof(callJSON) == 'undefined') callJSON = 1;

        if (this.profile_id == 0) {
            this.rateSet = pos;
            this.loginPrompt(pos);
        } else {
            this.rateSet = pos;
            this.resizeDiv(pos);
            this.userRateSet = 1;
            document.getElementById(this.name+'userHover').style.backgroundPosition = '0 '+this.hooverStartPos+'px';        // set the position of the sprite back to the hover star

            if (callJSON) {
                this.setRating(pos);
            }
        }
    },
    
    resetStars  : function() 
    {
        if (this.userRateSet > 0) {
            this.resizeDiv(this.rateSet);
            document.getElementById(this.name+'userHover').style.backgroundPosition = '0 '+this.hooverStartPos+'px';    // set the position of the sprite back to the hover star
        } else if (this.rateSet > 0) {
            this.resizeDiv(this.rateSet);
            document.getElementById(this.name+'userHover').style.backgroundPosition = '0 '+this.selectStartPos+'px';    // set the position of the sprite back to the hover star
        } else {
            document.getElementById(this.name+'userHover').style.width = '0px';                     // set the div to the width of 0 because nothing was clicked
        }
    },
    loggedIn : function()
    {
        this.getRating(this.uid);
        this.login.toggle(0);
        this.setRating(this.rateSet);
    },
    loginPrompt : function()
    {
        this.login.login();
    }
}
