var delay_time = 10 * 1000 // Tab looping delay time
var t;

var tab_toggle = function(num, loop){
    if (!loop) 
        clearTimeout(t);
    $$('.tab').each(function(obj, index){
        num == index ? obj.show() : obj.hide();
    });
    $$('.tabbed_box_top li a').each(function(obj, index){
        num == index ? obj.addClassName('active') : obj.removeClassName('active');
    })
}

var tab_next = function(){
    var last_tab = $$('#highlight_box .tab').length - 1
    var next_tab;
    $$('.tab').each(function(obj, index){
        if (obj.style.display == '') {
            next_tab = (index == last_tab ? 0 : index + 1);
        }
    })
    tab_toggle(next_tab, true);
}

var tabs_loop = function(){
    t = setTimeout("tabs_loop(); tab_next();", delay_time);
}

function get_chief_email(){
    new Ajax.Request('/contact/get_chief_email', {
        asynchronous: true,
        evalScripts: true,
        method: 'get',
        onSuccess: function(transport){
            $('c_container').innerHTML = transport.responseText;
        }
    })
}

function get_ads_email(){
    new Ajax.Request('/contact/get_ads_email', {
        asynchronous: true,
        evalScripts: true,
        method: 'get',
        onSuccess: function(transport){
            $('a_container').innerHTML = transport.responseText;
        }
    })
}

function get_subscription_email(){
    new Ajax.Request('/contact/get_subscription_email', {
        asynchronous: true,
        evalScripts: true,
        method: 'get',
        onSuccess: function(transport){
            $('s_container').innerHTML = transport.responseText;
        }
    })
}

function get_office_email(){
    new Ajax.Request('/contact/get_office_email', {
        asynchronous: true,
        evalScripts: true,
        method: 'get',
        onSuccess: function(transport){
            $('o_container').innerHTML = transport.responseText;
        }
    })
}

function nWYSIWIG(obj){
    if (document.selection) {
        obj.bound = function(prefix, sufix){
            this.focus();
            var str = document.selection.createRange().text;
            var sel = document.selection.createRange();
            sel.text = prefix + str + sufix;
            this.focus();
            return false;
        }
        
        obj.insert = function(content){
            this.focus();
            document.selection.createRange().text = content;
            this.focus();
            return false;
        }
    }
    else {
        obj.bound = function(prefix, sufix){
            this.focus();
            startPos = this.selectionStart;
            endPos = this.selectionEnd;
            before = this.value.substr(0, startPos);
            selected = this.value.substr(this.selectionStart, (this.selectionEnd - this.selectionStart));
            after = this.value.substr(this.selectionEnd, (this.value.length - this.selectionEnd));
            this.value = before + prefix + selected + sufix + after;
            return false;
        }
        
        obj.insert = function(content){
            this.focus();
            startPos = this.selectionStart;
            endPos = this.selectionEnd;
            before = this.value.substr(0, startPos);
            selected = this.value.substr(this.selectionStart, (this.selectionEnd - this.selectionStart));
            after = this.value.substr(this.selectionEnd, (this.value.length - this.selectionEnd));
            this.value = before + content + after;
            return false;
        }
    }
    
    obj.bold = function(){
        return obj.bound('[b]', '[/b]');
    }
    
    obj.link = function(){
        return obj.bound('[link]', '[/link]');
    }
    
    obj.img = function(){
        return obj.bound('[img]', '[/img]')
    }
    
    obj.quote = function(){
        return obj.bound('[quote]', '[/quote]')
    }
}

var quote_post = function(id){
    new Ajax.Request('/post/get_body/' + id, {
        asynchronous: true,
        evalScripts: false,
        method: 'get',
        onSuccess: function(transport){
            $('forum_post_body').value = transport.responseText;
        }
    });
    Effect.ScrollTo('forum_post_body');
}
