if (typeof(Rating) == 'undefined') {

    Rating = {};
    
    Rating.ajaxLoading = "/media/rating/ajax-loader.gif";

    Rating.delegate = function(id, backup) {
        return function(result, response) {
            Rating.setOK(id, backup);
        }
    }
    
    Rating.get = function(id, items) {
        var r = {};
        r.values = {};
        r.errors = [];
        
        r.fillable = 0;
        r.filled = 0;

        for(i in items) {
            var item = items[i];
            var val = null;

            if (item.type == 'school') {
                r.fillable++;
                for(mark = 1; mark <= 5; mark++) {
                    if ($('#' + id + '-item-' + i + '-' + mark)[0].checked) {
                        val = mark;
                        r.filled++;
                        break;
                    }
                }
            } else if(item.type == 'bool') {
                r.fillable++;
                if ($('#' + id + '-item-' + i + '-yes')[0].checked) {
                    val = true;
                    r.filled++;
                }
                if ($('#' + id + '-item-' + i + '-no')[0].checked) {
                    val = false;
                    r.filled++;
                }
            } else if(item.type == 'text' || item.type == 'email') {
                r.fillable++;
                val = $('#' + id + '-item-' + i)[0].value;
                if (val == '') {
                    val = null;
                }
                
                if (item.type == 'email') {
                    if(val == '@') {
                        val = null;
                    }
                }
                
                if (val) {
                    r.filled++;
                }

            } else if(item.type == 'session') {
                val = $('#' + id + '-item-' + i)[0].value;
            } else if(item.type == 'ip') {
                val = $('#' + id + '-item-' + i)[0].value;
            } else if(item.type == 'url') {
                val = '' + window.location.pathname + window.location.search;
            }

            if (item.required && val == null) {
                r.errors.push(i);
            }
            
            r.values[i] = val;
        }

        return r;
    }
    
    Rating.setSending = function(id, messages, delegates) {
        var r = {};
        r.id = id;
        r.messages = messages;
        r.delegates = delegates;
        
        r.div = $('#' + id);
        r.backup = r.div.html();
        r.div.html(messages.loading || ("<img src='" + Rating.ajaxLoading + "' />" + 'Ukládá se, chvíli strpení'));

        return r;
    }
    
    Rating.setOK = function(id, backup) {
        backup.div.html(backup.messages.sent || "Děkujeme za Váš hlas!");
        
        //backup.div.html(backup.backup);
        
        delegate = backup.delegates.sent || '';
        
        eval(delegate);
    }

    Rating.submit = function(id, worker, items, messages, delegates) {
        var data = Rating.get(id, items);
        
        if (data.filled == 0) {
            var text = messages.atleastone || "Odpovězte, prosím, alespoň na jednu otázku.";
            alert(text);
            return;
        }
        
        if (data.errors.length > 0) {
            var text = messages.answer || "Odpovězte, prosím, na následující otázky" + ":\n\n";
            for(i in data.errors) {
                text += items[data.errors[i]].title + "\n";
            }
            
            alert(text);
            return;
        }
        
        var backup = Rating.setSending(id, messages, delegates);

        worker.submit(data, Rating.delegate(id, backup));
    }

    Rating.image = new Image();
    Rating.image.src = Rating.ajaxLoading;
}
