/* 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              : "/api/",
    methodClassName     : 'Rating',
    uid                 : '',
    moduleId            : 0,
    moduleTypeId        : 0,
    containerId         : 'rating',
    login               : null,
    params              : null,
    rateSet             : null,
    userRateSet         : null,
    profile_id          : 0,
    hooverStartPos      : -17,
    selectStartPos      : -34,
    starWidth           : 21,
    loginMsg            : 'You must be logged in to rate.',
    forceFAIL			: 0,
    
    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;              }

    },
    setRating : function(pos) {
        if (pos == null) return; 
        if(this.uid.length != 0){
            this.setRatingByUid(pos);
        }else{
            this.setRatingByModuleId(pos);
        }
    },
    setRatingByUid : function(pos) {
        queryString  = "method="        + "Rating.SetRating";
        queryString += "&uid="          + this.uid;
        queryString += "&e_id="         + this.login.e_id;
        queryString += "&rating="       + pos;
        this.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;
        this.loadJSON(this.apiURL,'callback='+this.name+".display&"+queryString,'script');
    },
    getRating : function(id, moduleTypeId, FF) {
        
        if (FF) {this.forceFAIL = FF;}
        
        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;
        this.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;

        this.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();
    },
    /**
    *  Creates a new script node in the HEAD pointing to a php file that will process the url's 
    *  query string params and return a JSON result.
    *
    *  @param string url A url to the php page that will produce the required javascript code.
    */
    loadJSON : function loadJSON(url, queryString, type) {
        var headId = document.getElementsByTagName("head")[0];
        if (typeof(type) == "undefined") type = 'script';

        url += (url.indexOf('?') >= 0) ? "&" : "?";
        url += "noCacheIE=" + (new Date()).getTime() + "&" + queryString;

        if (type == 'link') {
            var link = document.createElement('link');
            link.rel = 'alternate';
            link.type = 'application/rss+xml';
            link.href = url;
        } else {
            var link = document.createElement('script');
            link.type = 'text/javascript';
            link.src = url;
        }
        headId.appendChild(link);
    }
}

okRating.prototype.display = function(data)
{	
    if (typeof(data.profileId) == 'undefined') return;

    var container = document.getElementById(this.containerId);
    if (container == undefined) {
        alert("Container ("+this.containerId+") not found.");
        return false;
    }

    while (container.firstChild) {
        container.removeChild(container.firstChild);
    }
            
    this.profile_id = data.profile_id;
    /*
    html = "<div class='rating-hover-stars' id='"+this.name+"userRating'>";
    if (this.e_id == '') {
	    html += "<a id='"+this.name+"rate1' title='Click to Login' href='javascript:void(0);' onclick='alert(\""+this.loginMsg+"\"); return false' onmouseover='"+this.name+".setStars(1);' onmouseout='"+this.name+".resetStars();'>1 Stars</a>";
	    html += "<a id='"+this.name+"rate2' title='Click to Login' href='javascript:void(0);' onclick='alert(\""+this.loginMsg+"\"); return false' onmouseover='"+this.name+".setStars(2);' onmouseout='"+this.name+".resetStars();'>2 Stars</a>";
	    html += "<a id='"+this.name+"rate3' title='Click to Login' href='javascript:void(0);' onclick='alert(\""+this.loginMsg+"\"); return false' onmouseover='"+this.name+".setStars(3);' onmouseout='"+this.name+".resetStars();'>3 Stars</a>";
	    html += "<a id='"+this.name+"rate4' title='Click to Login' href='javascript:void(0);' onclick='alert(\""+this.loginMsg+"\"); return false' onmouseover='"+this.name+".setStars(4);' onmouseout='"+this.name+".resetStars();'>4 Stars</a>";
	    html += "<a id='"+this.name+"rate5' title='Click to Login' href='javascript:void(0);' onclick='alert(\""+this.loginMsg+"\"); return false' onmouseover='"+this.name+".setStars(5);' onmouseout='"+this.name+".resetStars();'>5 Stars</a>";
    } else {
	    html += "<a id='"+this.name+"rate1' title='Click to Rate 1 Star or Press 1' href='javascript:void(0);' onclick='"+this.name+".setRate(1);' onmouseover='"+this.name+".setStars(1);' onmouseout='"+this.name+".resetStars();'>1 Stars</a>";
	    html += "<a id='"+this.name+"rate2' title='Click to Rate 2 Star or Press 2' href='javascript:void(0);' onclick='"+this.name+".setRate(2);' onmouseover='"+this.name+".setStars(2);' onmouseout='"+this.name+".resetStars();'>2 Stars</a>";
	    html += "<a id='"+this.name+"rate3' title='Click to Rate 3 Star or Press 3' href='javascript:void(0);' onclick='"+this.name+".setRate(3);' onmouseover='"+this.name+".setStars(3);' onmouseout='"+this.name+".resetStars();'>3 Stars</a>";
	    html += "<a id='"+this.name+"rate4' title='Click to Rate 4 Star or Press 4' href='javascript:void(0);' onclick='"+this.name+".setRate(4);' onmouseover='"+this.name+".setStars(4);' onmouseout='"+this.name+".resetStars();'>4 Stars</a>";
	    html += "<a id='"+this.name+"rate5' title='Click to Rate 5 Star or Press 5' href='javascript:void(0);' onclick='"+this.name+".setRate(5);' onmouseover='"+this.name+".setStars(5);' onmouseout='"+this.name+".resetStars();'>5 Stars</a>";
    }
    html += "</div>";
    */
    html = "<div id='"+this.name+"userHover' class='rating-user'></div>";
   /* html += "<span class='color10 txt-sub rating-message'>";
    Moving this logic to the template for google purposes.
   if(data.votes == 0){
        html += "Be the first to rate";
    }else{
        if(data.votes == 1){
            html += "Based on "+data.votes+" rating";
        }else{
            html += "Based on "+data.votes+" ratings";
        }
    }
  
    if(data.profileRating > 0){
        html += " You rated <b id='rating-message-number'>" + data.profileRating + "</b>";
    }  
    html += "</span>";
    */
    
    container.innerHTML = html;
   
   	if (this.forceFAIL) {
   		//FORCES NO STAR RATING IF NO TEXTUAL REVIEWS ARE PRESENT : 2/18/2011 rJONES
   		this.rateSet = 0;
   		this.setRate(0, 0); 
    } else if (data.user_rating > 0) {   	
        this.setRate(data.user_rating, 0);
    } else {   
        this.rateSet = data.average;
        this.setRate(data.average, 0);
    }
}

