var D_Question = Class.create({
    options:{
        root:''
    },
    translates:{
        'organizations_error':'Please select at least one organization',
        'captcha_error':'Please enter captcha value',
        'name_error':'Please enter your name',
        'email_error':'Please enter your email',
        'question_error':'Please enter your question'
    },
    errors:{},
	initialize: function(options, translates)
    {
        Object.extend(this.options, options || {});
        Object.extend(this.translates, translates || {});
        $('dialogue_category').observe('change', function()
        {
            new Ajax.Request(this.options.root + '/ajax/dialogue/load-organizations/',{
                method: 'get',
                parameters: {cat_id:$('dialogue_category').value},
                onComplete: function (transport) {
                    response = transport.responseJSON;
                    if (response.organizations[0].url) {
                        $('dialogue_values').hide();
                        $('dialogue_url').show();
                        $('dialogue_url_link').update(response.organizations[0].url).writeAttribute('href', response.organizations[0].url)
                    } else {
                        $('dialogue_values').show();
                        $('dialogue_url').hide();
                    }
                    $('dialogue_institutes').update(this.buildOrganizationsTemplate(response.organizations, 'organization'));
                }.bind(this)
            });
        }.bind(this));
        if ($('dialogue_search_category')) {
            $('dialogue_search_category').observe('change', function()
            {
                new Ajax.Request(this.options.root + '/ajax/dialogue/load-organizations/',{
                    method: 'get',
                    parameters: {cat_id:$('dialogue_search_category').value},
                    onComplete: function (transport) {
                        response = transport.responseJSON;
                        $('dialogue_search_institutes').update(this.buildOrganizationsTemplate(response.organizations, 'search_organization'));
                    }.bind(this)
                });
            }.bind(this));
        }

        $('dialogue_submit').observe('click', function(){
            this.errors = {};
//            if ($('dialogueForm').select('input[type=checkbox].org:checked').length == 0) {
//                this.errors.organizations = this.translates.organizations_error;
//            }
            if ($('dialogue_name').value == '') {
                this.errors.name = this.translates.name_error;
            }
            if ($('dialogue_email').value == '') {
                this.errors.email = this.translates.email_error;
            }
            if ($('dialogue_question').value == '') {
                this.errors.question = this.translates.question_error;
            }
            if ($('dialogue_captcha').value == '') {
                this.errors.captcha = this.translates.captcha_error;
            }

            if ($H(this.errors).size() > 0) {
                $('dialogueForm').select('.err').each(function(err){
                    err.hide();
                });
                $H(this.errors).each(function(error){
                    $(error[0] + '_error').update(error[1]).show();
                });
            } else {
                $('dialogueForm').submit();
            }
        }.bind(this));
    },
    buildOrganizationsTemplate:function(organizations, id_pref)
    {
        var xhtml = '<ul>';
        $A(organizations).each(function(item){
            xhtml += '<li><input type="checkbox" name="organization[' + item.id + ']" class="org" value="' + item.id + '" id="' + id_pref + '_' + item.id + '" />';
            xhtml += '<label for="' + id_pref + '_' + item.id + '">' + item.title + '</label></li>';
        });
        xhtml += '</ul>';
        return xhtml;
    }
});




function Dialogue_QuestionVoting(question_id, root)
{
    $('question-vote-up-' + question_id).observe('click', function(){
        new Ajax.Request(root + '/ajax/dialogue/vote-question/id/' + question_id, {
            method:'get',
            parameters:{'vote':'up'},
            onComplete:function(transport){
                var response = transport.responseJSON;
                if (response.voted == 0) {
                    return;
                }
                $('question-rating-' + question_id).update(response.rating);
                $('question-votes-' + question_id).update(parseInt($('question-votes-' + question_id).innerHTML) + 1);
                $('question-vote-up-' + question_id).stopObserving('click');
                $('question-vote-down-' + question_id).stopObserving('click');
            }
        });
    });
    $('question-vote-down-' + question_id).observe('click', function(){
        new Ajax.Request(root + '/ajax/dialogue/vote-question/id/' + question_id, {
            method:'get',
            parameters:{'vote':'down'},
            onComplete:function(transport){
                var response = transport.responseJSON;
                if (response.voted == 0) {
                    return;
                }
                $('question-rating-' + question_id).update(response.rating);
                $('question-votes-' + question_id).update(parseInt($('question-votes-' + question_id).innerHTML) + 1);
                $('question-vote-up-' + question_id).stopObserving('click');
                $('question-vote-down-' + question_id).stopObserving('click');
            }
        });
    });
}