// UKNova link classes - auto-popup, auto-bookmark and prevent-double-clicks

// open links with class="popup" in a separate window
//
function links_popup() {
    var features= 'width=640,height=480,resizable=yes,scrollbars=yes';
    if (dom_isClass(this, 'radio'))
        features= 'width=196,height=324,resizable=yes,scrollbars=no';
    var w= open(this.href, '_blank', features);
    return (!w);
}

// send links with class="bookmark" to bookmark UI, if available in this browser
//
function links_bookmark() {
    if (window.external && window.external.addFavorite)
        window.external.addFavorite(location.href, this.title);
    else
        alert('Bookmark this page using your web browser\'s bookmark function (eg. ctrl-D for Firefox) to retain this selection of categories');
    return false;
}

// warn user before allowing buttons of class="danger" to go ahead
//
function links_danger() {
    if (document.body.id=='page-account-view') { // hack, don't alert danger if it wouldn't work anyway
        var current= document.getElementById('f-current');
        if (current && current.value=='') {
            alert('To delete the account you must enter your current password');
            return false;
        }
    }
    return confirm(this.title);
}

// replace topic owner of posts, ask what topic title is through AJAX before confirming.
// fall back to "danger" behaviour.
//
var links_replace_bypass= false;
function links_replace() {
    if (links_replace_bypass)
        return true;
    if (!document.getElementById('f-replacetopic') || !document.getElementById('f-replacetopic').checked)
        return confirm(this.title);
    var topic= parseInt(document.getElementById('f-replacementtopic').value);
    if (!topic)
        alert('Please enter a topic ID number to which to move');
    else
        new TopicCheckRequest().send(topic);
    return false;
}

js_class('TopicCheckRequest', 'AsyncRequest', function() {
    this.send= function(topic) {
        this.open(false, '/wsgi/movetopic/'+topic, null, 5000);
    };
    this.oncancel= function() {
        var button= document.getElementById('f-delete');
        links_replace_bypass= true;
        button.click();
    };
    this.oncomplete= function(results) {
        if (!results.ok)
            alert(results.error);
        else if (confirm(results.error))
            this.oncancel();
    };
});

// stop buttons with class="nodouble" from taking a double-click as two separate submissions
//
js_class('NoDoubleClicker', null, function() {
    var DELAY= 1000; // no second click within 1s of first
    this._init= function(element) {
        this.lastclick= 0;
        element.onclick= js_callback(this, 'onclick');
    };
    this.onclick= function() {
        var now= new Date().getTime();
        if (now<this.lastclick+DELAY)
            return false;
        this.lastclick= now;
        return true;
    };
})


// Bind to appropriate elements
//
function links_bind() {
    for (var linki= document.links.length; linki-->0;) { var link= document.links[linki];
        if (dom_isClass(link, 'popup'))
            link.onclick= links_popup;
        else if (dom_isClass(link, 'bookmark'))
            link.onclick= links_bookmark;
        else if (dom_isClass(link, 'nodouble'))
            new NoDoubleClicker(link);
    }
    var inputs= document.getElementsByTagName('input');
    for (var inputi= inputs.length; inputi-->0;) { var input= inputs[inputi];
        if (input.type=='submit') {
            if (dom_isClass(input, 'nodouble'))
                new NoDoubleClicker(input);
            else if (dom_isClass(input, 'replace'))
                input.onclick= links_replace;
            else if (dom_isClass(input, 'danger'))
                input.onclick= links_danger;
            else if (dom_isClass(input, 'fakedefault'))
                input.onclick= js_returnfalse;
        }
    }
}

links_bind();
