var oStarOnSmall = new Image;
oStarOnSmall.src = "/communityProfileWeb/img/oslopuls/userstar_small_on.gif";
var oStarOffSmall = new Image;
oStarOffSmall.src = "/communityProfileWeb/img/oslopuls/userstar_small_off.gif";


var oStarOnMedium = new Image;
oStarOnMedium.src = "/communityProfileWeb/img/oslopuls/userstar_medium_on.gif";
var oStarOffMedium = new Image;
oStarOffMedium.src = "/communityProfileWeb/img/oslopuls/userstar_medium_off.gif";

var oStarOnLarge = new Image;
oStarOnLarge.src = "/communityProfileWeb/img/oslopuls/userstar_on.gif";
var oStarOffLarge = new Image;
oStarOffLarge.src = "/communityProfileWeb/img/oslopuls/userstar_off.gif";


function turnonstarsForSingleObject(iObjectId, iValue, sSize ) {

    var thisOStarOn = oStarOnMedium;
    if( sSize ) {
        if( sSize === 'SMALL' ) {
            thisOStarOn = oStarOnSmall;
        }
        else if( sSize === 'LARGE' ) {
            thisOStarOn = oStarOnLarge;
        }
    }

    turnoffstarsForSingleObject(iObjectId, 6, sSize);
    for (var i = 1; i <= iValue; i++) {
        document.getElementById( iObjectId + '_userstar_' + i).src = thisOStarOn.src;
    }
}



function turnoffstarsForSingleObject(iObjectId, iValue, sSize) {
    var thisOStarOff = oStarOffMedium;
    if( sSize ) {
        if( sSize === 'SMALL' ) {
            thisOStarOff = oStarOffSmall;
        }
        else if( sSize === 'LARGE' ) {
            thisOStarOff = oStarOffLarge;
        }
    }

    for (var i = 1; i <= iValue; i++) {
        document.getElementById( iObjectId + '_userstar_' + i).src = thisOStarOff.src;
    }
}


function rateSingleObject(iValue, iObjectId, iObjectTypeId, iRatingId, sSize) {
    var callbackSimpleRating = function callbackSimpleRating(data) {
        setSimpleRating(iObjectId, data, sSize);
        refreshRatingCounterForSingleObject(iObjectId, iObjectTypeId, iRatingId)
    }

    document.getElementById(iObjectId + '_rating').value = iValue;
    dwrRatingFacade.setRatingObject(iObjectId, iObjectTypeId, iRatingId, iValue, 0, callbackSimpleRating);
}


function rateSingleVenue(iValue, iVenueId, sCategoryList, iRatingId, sSize) {
    var callbackSimpleRating = function callbackSimpleRating(data) {
        setSimpleRating(iVenueId, data, sSize);
    }

    document.getElementById(iVenueId + '_rating').value = iValue;
    dwrRatingFacade.setRatingObjectUsingCategories(iVenueId, sCategoryList, iRatingId, iValue, 0, callbackSimpleRating);
}



function refreshUserRatingForSingleObject(iObjectId, iObjectGroupId, iRatingId, sSize) {
    var callbackRefreshUserRating = function callbackRefreshUserRating(data) {
        turnonstarsForSingleObject(iObjectId, data.ratingValue, sSize);
        document.getElementById( iObjectId + '_rating').value = data.ratingValue;        
    }

    dwrRatingFacade.getRatingObject(iObjectId, iObjectGroupId, iRatingId, callbackRefreshUserRating);
}



function refreshAverageRatingForSingleObject(iObjectId, iObjectGroupId, iRatingId, sSize, sIdAddOn ) {

    var callbackSimpleRating = function callbackSimpleRating(data) {
        setSimpleRating(iObjectId, data, sSize, sIdAddOn);
    }

    dwrRatingFacade.getAverageRating(iObjectId, iObjectGroupId, iRatingId, callbackSimpleRating);
}

function refreshRatingCounterForSingleObject(iObjectId, iObjectTypeId, iRatingId) {

    var callbackRatingCounter = function callbackRatingCounter(data) {
        document.getElementById( iObjectId + '_ratingCount').innerHTML = data;
    }
    dwrRatingFacade.getRatingCount(iObjectId, iObjectTypeId, iRatingId, callbackRatingCounter);
}

function resetstarsForSingleObject(iObjectId, sSize) {
    var iValue = document.getElementById( iObjectId + '_rating').value;
    turnoffstarsForSingleObject(iObjectId, 6, sSize);
    turnonstarsForSingleObject(iObjectId, iValue, sSize);
}


function setSimpleRating(iObjectId, data, sSize, sIdAddOn ) {

    var thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_medium_on.gif\" alt=\"\">";
    var thisSStarHalf = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_medium_half.gif\" alt=\"\">";
    var thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_medium_off.gif\" alt=\"\">"
    if( sSize ) {
        if( sSize === 'SMALL' ) {
            thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_small_on.gif\" alt=\"\">";
            thisSStarHalf = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_small_half.gif\" alt=\"\">";
            thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_small_off.gif\" alt=\"\">"
        }
        else if( sSize === 'LARGE' ) {
            thisSStarOn = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_on.gif\" alt=\"\">";
            thisSStarHalf = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_half.gif\" alt=\"\">";
            thisSStarOff = "<img src=\"/communityProfileWeb/img/oslopuls/userstar_off.gif\" alt=\"\">"
        }
    }

    if (data.length>0){

        sRatingElementIdName = iObjectId + "_ratingUsers";
        if( sIdAddOn )
            sRatingElementIdName += sIdAddOn;

        var oRatingOutput = document.getElementById( sRatingElementIdName );
        var sHtml = "";
        var avgRating = parseFloat(data.replace(",", "."));
        var tempRating = data.split(",");
        var wholeNum = parseInt(tempRating[0]);
        var decimals = avgRating - wholeNum;
        sHtml += ""
        for (var i = 1; i <= wholeNum; i++) {
            sHtml += thisSStarOn;
        }
        if ((0.25 < decimals) && (decimals < 0.75)) {
            sHtml += thisSStarHalf;
            wholeNum = wholeNum + 1;

        }
        if (0.75 <= decimals) {
            sHtml += thisSStarOn;
            wholeNum = wholeNum + 1;

        }

        for (var y = 1; y <= (6 - wholeNum); y++) {
            sHtml += thisSStarOff;
        }
        oRatingOutput.innerHTML = sHtml;
    }
}


function createOsloCookieIfNeeded() {

    var callbackCreateOsloCookieIfNeeded = function callbackCreateOsloCookieIfNeeded(data) {
        // This will be true if a new anonumous user was created and a cookie needs to be created.
        if( data ) {
            var expdate = new Date();
            FixCookieDate(expdate);
            expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365 * 10));
            SetCookie("OSLOPULSID", data, expdate, "/");
        }
    }

    dwrRatingFacade.newAnonymousUser( callbackCreateOsloCookieIfNeeded);

}
