﻿/// <reference path="Helper.js" />

function feiRateObj() {
    this.CountHTMLRate = 0;

    this.setRate = function(o, indexForced) {
        var currentIndex = 0;
        if(indexForced) currentIndex = indexForced;
        else currentIndex = parseFloat(o.getAttribute('index'));
        var i = 0;
        //var parentSpan = o.parentElement;
        var parentSpan = o.parentNode;
        for(i = 0; i < 5;i++) {
            var inStar = parentSpan.children[i];
            if(i < currentIndex+1) inStar.src = nvaFx.Server.AppPath + '/Style%20Library/Images/StarOn.gif';
            else inStar.src = nvaFx.Server.AppPath + '/Style%20Library/Images/StarOff.gif';
        }
        if(!indexForced) fei.Rate.UpdateRate(parentSpan.getAttribute('ContentURL'), currentIndex + 1, o)
    }
    
    this.UpdateRate = function(urlContent, rate, objRateStar) {
        if(nuova.FEI.Portal.Web.WebServices.Rate) {
            var wsRate = nuova.FEI.Portal.Web.WebServices.Rate;
            wsRate.UpdateRate(urlContent,rate,function(result, userContext, methodName){fei.Rate.UpdateRateCallBack(result, userContext, methodName, objRateStar)}, function(error){fei.Error.TrapError(error)}) 
        } else {
            nvaFx.ShowError("Erro interno: Web Service Rate não disponível.");
        }
    }

    this.WSGetRateHTML = function(urlContent, objRateArea) {
        if (nuova.FEI.Portal.Web.WebServices.Rate) {
            var wsRate = nuova.FEI.Portal.Web.WebServices.Rate;
            wsRate.GetHTMLRate(urlContent, function(result, userContext, methodName) { fei.Rate.divAuxRateLoad(result, objRateArea) }, function(error) { fei.Error.TrapError(error) })
            
        } else {
            nvaFx.ShowError("Erro interno: Web Service Rate não disponível.");
        }
    }
    
    this.UpdateRateCallBack = function(result, userContext, methodName, objRateStar)  {
        // Atualizar o Rate na hora fica estranho... retirei.
        //fei.Rate.setRate(objRateStar,parseFloat(result));
    }

    this.GetRateHTML = function(urlContent) {
        this.CountHTMLRate = this.CountHTMLRate + 1;
        document.write("<span style='white-space: nowrap;' id='feiHRate_id_" + this.CountHTMLRate + "' urlContent='" + urlContent + "'><!-- teste --></span>");
        //<script>fei.Rate.divAuxRateLoad(document.getElementById(\"feiHRate_id_" + this.CountHTMLRate + "\"));</script>
        this.WSGetRateHTML(urlContent, document.getElementById("feiHRate_id_" + this.CountHTMLRate));
    }

    this.divAuxRateLoad = function(content, o) {
        o.innerHTML = "&nbsp;| " + content;
    }
    
}

function feiObj() 
{
    this.Rate = null;
}

try {
    var x= fei;
} catch(ex) {
    fei = new feiObj();
}

if(fei.Rate == null) fei.Rate = new feiRateObj();
