/* Behaviours list */
var PornMage = {
    behaviours : {
        '#reportform' : function(element) {
            element.innerHTML = "<center>[ Report bad content ]</center>";
            element.onclick = function(element) {
                PornMage.showReportForm(this);
            };
        },
        '#sitevote' : function(element) {
             var rating = new RatingControl("sitevote");
        },
        '#addcomment' : function(element) {
             element.onclick = function( element ) {
                 Element.hide($('addcomment'));
                 Element.show($('comment_form'));
             }
        }
   }
};

/* Site rating control */
var RatingControl = new Class.create();
RatingControl.prototype = {
    initialize:function(containerId) {
        this.container = $(containerId);
        this.buildForm();
    },

    buildForm:function() {
        this.select = document.createElement('select');
    this.select.options[0] = new Option('--- Rate the site ---', -1, 0, true);
    this.select.options[1] = new Option('5 - Amazing', 0, 0, false);
    this.select.options[2] = new Option('4 - Good', 1, 0, false);
    this.select.options[3] = new Option('3 - Satisfactory', 2, 0, false);
    this.select.options[4] = new Option('2 - Poor', 3, 0, false);
    this.select.options[5] = new Option('1 - Shit', 4, 0, false);
    Event.observe( this.select, 'change', this.onVote.bind(this), true );
        this.container.appendChild( this.select );



    },

    onVote:function(event) {

        var element = Event.element(event);

        element.disabled = 'true';

        document.cookie="site"+$('siteId').value+"=1";

        new Ajax.Request( '/vote/',
                              {
                                method: 'get',
                                parameters: 'id=' + $('siteId').value + '&q=' + element.value,
                                onComplete : this.onCompleteVote.bind(this)
                              }
                            );

    },

    onCompleteVote:function(response) {
        alert('Thank you for your vote!');
    }
};

PornMage.showReportForm = function(element) {

    element.onclick = Prototype.emptyFunction;
    element.innerHTML = "<center><select name='reportType' id='reportType' onchange='PornMage.sendReportForm(this);'>\
    <option value='none' selected='selected'>Choose appropriate report type</option>\
    <option value='bad-siteshot'>-- Report bad site thumbnail</option>\
    <option value='bad-description'>-- Report bad description</option>\
    <option value='bad-prices'>-- Report wrong prices</option>\
    </select>\
    </center>";
};

PornMage.onCompleteReportForm = function(element) {
    $('reportform').innerHTML = "<center>Thank you for the report...</center>"
    alert('Your report has been sent');
};

PornMage.sendReportForm = function(element) {
    if ( confirm( "Do you really want to send report?\nJust press Ok and we'll receive your notice immediately.") ) {
            $('reportform').innerHTML = "<center>\
            <img src='/app/webroot/img/li.gif' border='0'> Please wait...<center>";
            new Ajax.Request( '/about/badcontent/' + element.value,
                              {
                                method: 'get',
                                parameters: 'jsResponse=1&siteId=' + $('siteId').value,
                                onComplete : PornMage.onCompleteReportForm
                              } );
    }
};

Behaviour.register(PornMage.behaviours);
