﻿$(document).ready(function(){        
    var config = new RateConfig();
    
    $.getJSON(config.getUrl, function(obj){
        config.clcode = obj.Rated.client; 
        if(obj.Rated.stars < 1 && !obj.Rated.isset)
        {
            $('#doRating').css('display', 'block');    
            $('#uRate').css('display', 'block'); 
            config.links.each(function(){
                $(this).mouseenter(function(){
                    var n = parseInt(this.id); $(this).toggleClass('aStard');
                    for(var i = 1; i < n; i++) $('#'+i+'r').toggleClass('aStard');
                });
                $(this).mouseleave(function(){
                    var n = parseInt(this.id); $(this).toggleClass('aStard');
                    for(var i = 1; i < n; i++) $('#'+i+'r').toggleClass('aStard');
                });
                $(this).click(function(){
                    $('#uRate').addClass('uActive');
                    RatePick(config, parseInt(this.id), true);
                    return false;
                });
            });
        }
        else
        {
            $('#uRate').css('display', 'block');  
            RatePick(config, obj.Rated.stars, false);
            RateStore(config);
        }
    });

}); 

function RateConfig()
{
    this.links = $('#uRate a');
    this.starimg = new Image();
    this.starimg.src = '/App_Themes/Egglands/Images/star.gif';
    this.ccode = 'ebr8x3'; 
    this.clcode = null;
    this.baseUrl = '/Clientside/Ratings.ashx?r=' + recipe;
    this.getUrl = this.baseUrl; 
    this.postUrl = this.baseUrl;
    
    if($.cookie(this.ccode) != null)
    {
        this.clcode = $.cookie(this.ccode);
        this.getUrl = this.getUrl + '&c=' + this.clcode;
    }
}

function RatePick(config, stars, save)
{
     config.links.each(function(){
        var lnk = $(this);
        lnk.unbind();
        if(parseInt(this.id)<=stars) 
            lnk.toggleClass('aStarp');
        else 
            lnk.toggleClass('aUnstarp');        
        
        lnk.click(function(){
            return false;
        });
        $('#uRate').removeClass('uActive');
        $('#uRate').addClass('uSet');
        $('#doRating').css('display', 'none');    
        $('#doneRating').css('display', 'block');   
        if (save) RateSave(config, stars); 
     });
}

function RateSave(config, stars)
{
    postUrl = config.postUrl + '&a=sv&c=' + config.clcode + '&s=' + stars;
    $.getJSON(postUrl, function(obj){
        RateStore(config);
    });
}

function RateStore(config)
{
    $.cookie(config.ccode, config.clcode, { expires: 365, path: '/' });
}

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
