/**** /js/deprecated/topics.js ****/

/**
 * Initiate voting routines on page. 
 * Send vote data to server, 
 * update voting results on page (including styles change), 
 * disable buttons, hide or show blocks.
 * @param {String} topMessage New tooltip value for top votable element on page.
 * @param {String} otherMessage New tooltip value for other votable elements on page, defaults to topMessage.
 */
function initVote(topMessage, otherMessage) {
    var VotingBlock = Class.create();
    VotingBlock.prototype = {
        initialize: function(forButton, againstButton, id, message) {
            // save data
            this.forButton = $(forButton);
            this.againstButton = $(againstButton);
            this.id = id;
            this.message = message;
            
            // create bound handlers
            this.forHandler = function(event){ 
                this.vote('recommend', event); 
            }.bindAsEventListener(this);
            
            this.againstHandler = function(event){ 
                this.vote('complain', event); 
                // hide bad comment
                var comment_oneitem = $(this.againstButton.parentNode.parentNode.parentNode);
                if (comment_oneitem.hasClassName('unhidable')) return;
                comment_oneitem.addClassName('hide_bad voted');
            }.bindAsEventListener(this);
            
            // attach handlers
            this.forButton.observe('click', this.forHandler);
            this.againstButton.observe('click', this.againstHandler);
        },
        disableButton: function(button, handler) {
            button.rel = '';
            Event.observe(button, 'click', Event.stop);
            Event.stopObserving(button, 'click', handler);
            button.setAttribute('title',  this.message);
            var img = button.getElementsByTagName('img')[0];
            if (img) {
                img.src = img.src.replace(/\.gif/i, '_no.gif');
            }
        },
        vote: function(action, event) {
            // prevent going to link
            Event.stop(event || window.event);
            
            // quit if already voted
            if (!this.id || this.id.length == 0) return;

            // disable both related buttons 
            this.disableButton(this.forButton,     this.forHandler);
            this.disableButton(this.againstButton, this.againstHandler);

            // send request
            var req = new JsHttpRequest();
            req.caching = false;
            req.open('GET', window.location.protocol + '//' + window.location.host + '/widgets/ajax/' + action, true);
            req.onreadystatechange = function () {
                if ( req.readyState != 4 ) return;
                if ( !req.responseJS || !req.responseJS.weight ) return;
                
                var weight = req.responseJS.weight;
                if (weight > 0) {
                    weight = '+' + weight;
                }

                // update voting data
                $$('span.weight' + this.id).each(function(hWeight) {
                    hWeight.innerHTML = weight;
                    if (weight > 0) {
                        hWeight.parentNode.className = 'vote_up_bg';
                    } else if (weight == 0) {
                        hWeight.parentNode.className = 'vote_no_bg';
                    } else {
                        hWeight.parentNode.className = 'vote_down_bg';
                    }
                });
            }.bind(this);
            req.send({q: this.id});
        }
    };

    otherMessage = otherMessage || topMessage;
    
    var forButtons = document.getElementsByName('votefor');
    var againstButtons = document.getElementsByName('voteagainst');
    
    // initiate voting block for every pair of buttons:
    for (var index = 0; index < forButtons.length; index++) {
        new VotingBlock(forButtons[index], againstButtons[index], forButtons[index].rel, (index == 0) ? topMessage : otherMessage);
    }
   
    // assign handlers to show hidden items
    var showersOfHidden = document.getElementsByName('show_hidden');
    $A(showersOfHidden).each(function(shower){
        $(shower).observe('click', function(event) {
            Event.stop(event);
            var comment_oneitem = $(shower.parentNode.parentNode.parentNode);
            comment_oneitem.removeClassName('hide_bad');
            comment_oneitem.addClassName('');
        }.bindAsEventListener(shower));
    })
    
    // assign handlers to hide again hidden items
    var hidersOfHidden = document.getElementsByName('hide_hidden');
    $A(hidersOfHidden).each(function(hider){
        $(hider).observe('click', function(event) {
            Event.stop(event);
            var comment_oneitem = $(hider.parentNode.parentNode.parentNode);
            comment_oneitem.removeClassName('show_bad');
            comment_oneitem.addClassName('hide_bad');
        }.bindAsEventListener(hider));
    })
    
    // assign handlers to hide viewed items
    var hideViewers = document.getElementsByName('hide_viewed');
    $A(hideViewers).each(function(hider){
        $(hider).observe('click', function(event) {
            var comment_oneitem = $(hider.parentNode.parentNode.parentNode.parentNode);
            comment_oneitem.removeClassName('show_bad');
            comment_oneitem.addClassName('hide_bad');
            comment_oneitem.addClassName('hide_viewed');
        }.bindAsEventListener(hider));
    })
}


function initTopicsOneitem()
{
    var link = document.getElementsByName('topic_delete_link')[0];
    if (link) {
        Event.observe(link, 'click', function (e) {
            if (!confirm(DIC.topic_delete_link_confirm)) {
                Event.stop(e);
                return false;
            }
        })
    }
    var links = document.getElementsByName('comment_delete_link');
    for (var i = 0; i < links.length; i++) {
        Event.observe(links[i], 'click', function (e) {
            if (!confirm(DIC.comment_delete_link_confirm)) {
                Event.stop(e);
                return false;
            }
        })
    }
}


/**
 * Initialize topic watch/unwatch checkbox.
 */
function initObjWatch()
{
    var elts = document.getElementsByName('obj_checkbox_watch');
    for (var i = 0; i < elts.length; i++) { (function() { 
        var chk = elts[i];
        var div = chk.parentNode;
        var img = div.getElementsByTagName('img')[0];
        Event.observe(chk, 'click', function(event) {
            chk.blur();
            img.style.visibility = 'visible';
            // Do not use POST because of the MetaForm!
            JsHttpRequest.query(
                'GET ' + window.location.protocol + '//' + window.location.host + '/widgets/ajax/objWatch',
                { 
                    obj_id: chk.value, 
                    watch: (chk.checked? 1 : 0) 
                },
                function(js, err) {
                    img.style.visibility = 'hidden';
                },
                true
            );
        })
    })() }
    if ($('mark_as_read_button')) {
        Event.observe($('mark_as_read_button'), 'click', function(event) {
            if (!confirm(DIC.mark_all_as_read_confirm)) {
                Event.stop(e);
                return false;
            }
        });
    }
}

;
