/*
 * @(#)WeaverBoard.js
 */

/*
 * Author: Jianmin Liu
 * Created: 2010/07/02
 */

function WeaverBoard()
{
this.cfg = {
    src: '/weaver.html'
};

this.flags = {
    reload: false,
    disabled: false,
    pinit: false,
    wid: '_wb',
    nodoctype: false,
    isie6: false,
    resize_start: 0,
    resize_pos: 0,
    loaded: 0,
    cinit: false,
    ticktime1: 150,
    ticktime2: 15,
    expand: 0,
    cupdates: 0,
    ltimer: 0,
    ltimer2: 0,
    ltimer3: 0
};
this.wpos = {
    animated: false,
    visible: false,
    wwidth: 420,
    wheight: 160,
    wminwidth: 380,
    target: 0,
    ohash: '',
    stime: 0,
    ttime: 0
};

this.init_onload = function()
{
    if (this.flags['loaded'])
        return;

    this.flags['loaded']++;

    var d = document; 
    if (!this.gel(this.wname('tb_wcont')))
        this.init();

    if (this.flags['disabled'])
        return;

    this.set_timer(1);
};

this.init = function()
{
    this.flags['isie6'] = (navigator.userAgent.indexOf('MSIE 6') >= 0);
    if (navigator.userAgent.indexOf('MSIE') >= 0)
        if (this.flags['isie6'] || (document.firstChild.nodeValue == null))
            this.flags['nodoctype'] = true;

    this.flags['expand'] = 1;
    this.icon_insert();

    if (this.flags['nodoctype'])
        this.gel(this.wname('tb_wcont')).style.position = 'absolute';
};

this.init_params = function()
{
    this.flags['pinit'] = true;
    if (this.flags['expand'])
    {
        this.flags['expand'] = 0;
        this.icon_click();
    }
};

this.gel = function(elid)
{
    return document.getElementById(elid);
};

this.add_el = function(tic)
{
    var b = document.body; 
    try 
    {
        if (b.childNodes.length > 0)
            b.insertBefore(tic, b.childNodes[0]);
        else
            b.appendChild(tic);
    }
    catch (e) 
    {
        return false;
    }

    return true;
};

this.change_class = function(elid, cname, onoff) 
{
    var el = this.gel(elid);
    if (onoff && (onoff != 0))
    {
        if (el.className.indexOf(cname) < 0)
            el.className += ' '+cname;
    }
    else
    {
        for (var cn = ''; cn != el.className;)
        {
            cn = el.className;
            el.className = el.className.replace(cname, '');
        }
        el.className = el.className.replace(/ +/g, ' ');
    }
};

this.cssload = function(cssText)
{
    var d = document;
    var styleText = d.createTextNode(cssText);
    var styleNode = d.createElement("style");
    styleNode.type = 'text/css';
    if (styleNode.styleSheet)
        styleNode.styleSheet.cssText = styleText.nodeValue;
    else
        styleNode.appendChild(styleText);
    d.getElementsByTagName("head")[0].appendChild(styleNode);
};

this.jsload = function(jsurl, jsid, override)
{
    var d = document;
    var head = d.getElementsByTagName('head')[0];
    var u = jsurl;
    if (this.gel(jsid))
    {
        if (!override)
            return false;
        else
            head.removeChild(this.gel(jsid));
    }
    var s = d.createElement('script');
    s.type= 'text/javascript';
    s.src = u;
    s.id  = jsid;
    head.appendChild(s);
    return true;
};

this.add_event = function(ev, func, wnd)
{
    var scope = wnd ? window : document;
    if (typeof scope.addEventListener != 'undefined')
    {
        scope.addEventListener(ev, func, false);
    }
    else if (typeof scope.attachEvent != 'undefined')
    {
        scope.attachEvent('on' + ev, func);
    }
    else
        return false;

    return true;
};

this.wname = function(elid)
{
    return elid + '_wb';
};

/*
#tb_resizer {
  position: absolute;
  background-image: url(/images/weaver/dots.png);
  width: 7px;
  height: 60px;
  right: 1px;
  top: 48%;
  cursor: e-resize;
}
*/

this.icon_insert = function()
{
    var d = document;
    this.cssload(
    ".tb_passive_container {bottom:0;padding:0px;margin:0px;position:fixed;top:44%;cursor:pointer;left:0px;padding:0px;margin:0px;height:101px;z-index:7000;background:none;line-height:normal;}" + 
    "#tb_passive_tweet {" + 
        "background:transparent url(/images/weaver/passive_weaver_gray.png) 0px 0px no-repeat;" +
        "width:14px;" + 
        "height:63px;" + 
        "position:absolute;" + 
        "left:13px;" +
        "top:19px;" + 
        "z-index:1;" + 
    "}" +
    "#tb_passive_large {" + 
        "position:absolute;" + 
        "top:0px;" + 
        "left:0px;" + 
        "height:101px;" + 
        "width:38px;" + 
        "border: none;" + 
        "margin:0;" + 
        "padding:0;" + 
        "background:transparent url(/images/weaver/passive_tab.png) 0px 0px no-repeat;" + 
        "opacity:.95;" + 
    "}" +
    ".tb_passive_container.tb_open #tb_passive_tweet { left:8px; }" +
    "#tb_passive_tweet.tb_open { background-image:url(/images/weaver/passive_weaver_dark.png); }" + 
    "#tb_passive_large.tb_open { background-position: 0px -101px !important; }" + 
    ".tb_passive_container.tb_open #tb_passive_large { background-position: -5px -202px !important; }" + 
    ".tb_passive_container.tb_open #tb_passive_large.tb_open { background-position: -5px -303px !important; }" +
    ".tb_overlay {" +
        "width: 420px;" +
        "min-width: 289px;" +
        "z-index: 1000000000;" +
        "opacity: 0.95;" + 
        "background-color: #333333;" + 
        "border-right: #333 1px solid;" + 
        "position: fixed;" + 
        "top: 0px;" + 
        "left: -420px;" +
        "padding: 0px;" + 
        "margin: 0px;" + 
        "height: 100%;" + 
    "}"+
    "#tb_resizer {" +
        "position: absolute;" +
        "background-color: #3F3F3F;" + 
        "background-image: url(/images/weaver/separator_h.png);" +
        "background-position: 0 50%;" +
        "width: 7px;" +
        "height: 100%;" +
        "right: 1px;" +
        "top: 0;" +
        "line-height: 0;" +
        "cursor: e-resize;" +
    "}" +
    ".tb_content {" +
        "z-index: 2000000000;" +
        "margin: 0px;" +
        "padding: 0px;" +
        "position: fixed;" +
        "width: 400px;" +
        "scrolling: no;" +
        "min-width: 269px;" +
        "top: 0px;" +
        "left: -410px;" +
        "height: 100%;" +
        "border: none;" +
    "}" +
    "#tb_waitload {" +
        "display: none;" +
        "position: absolute;" +
        "top: 39px;" +
        "left: 11px;" +
    "}" +
    ".tb_n_load {" + 
        "opacity: 0.6;" + 
        "filter2: alpha(opacity=60);" +
    "}" +
    ".tb_n_load #tb_waitload {" + 
        "display: inline;" +
    "}" +
    "a:focus {" +
        "outline: none;" +
    "}"
    );
    
    if (!this.gel(this.wname('tb_wcont')))
    {
        var tic = d.createElement('div');
        tic.innerHTML = "<div id=\"tb_wcont_wb\" class=\"tb_passive_container\">" +
                          "<div onclick=\"return theWeaverBoard.icon_click()\" onmouseover=\"theWeaverBoard.icon_over()\" onmouseout=\"theWeaverBoard.icon_out()\">" + 
                            "<div id=\"tb_passive_tweet\"></div>" + 
                            "<div id=\"tb_passive_large\"></div>" + 
                          "</div>" + 
                          "<img id=\"tb_waitload\" src=\"/images/weaver/ajax-loader.gif\" />" + 
                        "</div>";
        tic.id = 'TB_icon';
        tic.style.display = 'none';
        this.add_el(tic);

        d.body.replaceChild(this.gel(this.wname('tb_wcont')), this.gel('TB_icon'));
    }    
};

this.init_pos = function(tic, shift)
{
    tic.style.left = '-' + (this.wpos['wwidth'] - shift) + 'px';
    tic.style.top = '0px';
    tic.className += ' tb_slide';
};

this.init_content = function()
{
    var d = document;
    this.flags['cinit'] = true;
    if (!this.gel(this.wname('tb_cont')) )
    {
        var tic = d.createElement('iframe');
        tic.id = this.wname('tb_cont');
        tic.name = this.wname('tb_cont');
        tic.className = 'tb_content';
        // Jianmin - 2010/07/06
        //this.init_pos(tic, 10);
        this.init_pos(tic, 0);
        tic.style.display = 'block';
        tic.style.zIndex = 2000000000;
        if (this.flags['nodoctype'])
            tic.style.position = 'absolute';
        if (this.flags['isie6'])
            tic.style.height = document.documentElement.clientHeight;
        tic.src = this.cfg['src'];
        tic.scrolling = 'auto';
        tic.frameBorder = 0;
        tic.setAttribute("allowTransparency","true");
        this.add_el(tic);

        return true;
    }
    return false;
};

this.slide_prop = function(el, setto)
{
    if (typeof setto == 'undefined')
        return parseInt(el.style.left);
    else
    {
        el.style.left = setto;
    }
};

this.wswitch = function()
{
    this.wswitch_state(!this.wpos['visible']);
};

this.wswitch_state = function(state)
{
    if (state || (this.wpos['visible'] == state))
    {
        this.change_class('tb_passive_tweet', 'tb_active', state);
        this.change_class('tb_passive_large', 'tb_active', state);
    }
    if (this.wpos['visible'] == state)
        return;

    if (!this.wpos['animated'])
    {
        this.set_timer(2);
        this.wpos['visible'] = state;
        this.wpos['target'] = this.wpos['wwidth'];
        if (!state)
            this.wpos['target'] = -this.wpos['target'];
        this.wpos['stime'] = this.ctime();
        this.wpos['ttime'] = 500;

        this.epos = {};
        this.wpos['animated'] = true;

        if (this.wpos['visible'])
            this.change_class(this.wname('tb_wcont'), 'tb_open', true);
    }
};

this.icon_over = function()
{
    this.change_class('tb_passive_tweet', 'tb_open', true);
    this.change_class('tb_passive_large', 'tb_open', true);
};

this.icon_out = function()
{
    this.change_class('tb_passive_tweet', 'tb_open', false);
    this.change_class('tb_passive_large', 'tb_open', false);
};

this.set_widget_width = function(wd, mv) 
{
    wd = Math.min(parseInt(document.body.clientWidth*0.9), wd);
    wd = Math.max(theWeaverBoard.wpos['wminwidth'], wd);
    theWeaverBoard.wpos['wwidth'] = wd;

    var e1 = theWeaverBoard.gel(this.wname('tb_over'));
    var e2 = theWeaverBoard.gel(this.wname('tb_cont'));
    if (e1 && e2)
    {
        e1.style.width = (wd - 1) + 'px';
        // Jianmin - 2010/07/06
        //e2.style.width = (wd - 20) + 'px';
        e2.style.width = (wd - 10) + 'px';

        if (parseInt(e1.style.left)<0) e1.style.left = '-' + (wd) + 'px';
        // Jianmin - 2010/07/06
        //if (parseInt(e2.style.left)<0) e2.style.left = '-' + (wd-10) + 'px';
        if (parseInt(e2.style.left)<0) e2.style.left = '-' + (wd-0) + 'px';
    }

    if (mv)
    {
        this.gel(this.wname('tb_wcont')).style.left = (wd) + 'px';
    }
};

this.resize_move = function(ev)
{
    if (theWeaverBoard.flags['resize_start'])
    {
        theWeaverBoard.set_widget_width(theWeaverBoard.flags['resize_start'] - theWeaverBoard.flags['resize_pos'] + ev.clientX, true);
    }
    return false;
};

this.ctime = function() 
{
    return +new Date;
};

this.icon_click = function()
{
    if (!this.flags['pinit']) 
        return false;

    var d = document;
    this.set_widget_width(this.wpos['wwidth'], false);

    var cinit = this.init_content();
    if (!this.gel(this.wname('tb_over')))
    {
        this.change_class(this.wname('tb_wcont'), 'tb_n_load', true);

        var tic = d.createElement('div');

        tic.id = this.wname('tb_over');
        tic.className = 'tb_overlay';
        this.init_pos(tic, 0);
        if (this.flags['nodoctype'])
            tic.style.position = 'absolute';
        this.add_el(tic);
        if (this.flags['isie6'])
            tic.style.height = document.documentElement.clientHeight;

        var tic2 = d.createElement('a');
        tic2.id = 'tb_resizer';
        tic2.onmousedown = function(ev)
        {
            if (typeof ev == 'undefined')
                ev = window.event;
            theWeaverBoard.flags['resize_pos'] = ev.clientX;
            theWeaverBoard.flags['resize_start'] = theWeaverBoard.wpos['wwidth'];

            var tic = d.createElement('div');
            tic.id = theWeaverBoard.wname('tb_over2');
            tic.className = 'tb_slide';
            tic.style.left = '0px';
            tic.style.width = '100%';
            tic.style.position = 'fixed';
            tic.style.height = theWeaverBoard.flags['isie6'] ? document.documentElement.clientHeight : '100%';
            tic.style.zIndex = '2000000001';
            theWeaverBoard.add_el(tic);
            return false;
        };

        this.add_event('mouseup', function()
        {
            var ov2id = theWeaverBoard.gel(theWeaverBoard.wname('tb_over2'));
            if (ov2id) 
                document.body.removeChild(ov2id);
            theWeaverBoard.flags['resize_start'] = 0;
            return false;
        }, false);
        this.add_event('mousemove', theWeaverBoard.resize_move, false);

        tic2.href = '#';
        tic.appendChild(tic2);

        if (!cinit)
            this.gel(this.wname('tb_cont')).src = '';

        this.set_widget_width(this.wpos['wwidth'], false);
    }
    else
    {
        theWeaverBoard.wswitch();
    }

    return false;
};

this.set_timer = function(tp) 
{
    if (this.flags['ltimer'])
        window.clearInterval(this.flags['ltimer']);
   
    this.flags['ltimer'] = null;
    this.flags['ltimer'] = window.setInterval('theWeaverBoard.check_timer()', this.flags['ticktime' + tp]);
};

this.load_pagelet = function()
{
    if (this.flags['reload'])
        this.gel(this.wname('tb_cont')).src = this.cfg['src'] + '?r=' + Math.random();
};

this.check_timer = function()
{
    var curtime = this.ctime();
    this.change_class(this.wname('tb_wcont'), 'tb_n_load', false);

    if (!this.flags['cinit'] && ((curtime - this.flags['ltimer2']) > 60000) )
    {
        this.flags['ltimer2'] = curtime;
        theWeaverBoard.count_timer();
    }

    if (this.wpos['animated'])
    {
        var ael = [this.wname('tb_cont'), this.wname('tb_over')];

        ael[ael.length] = this.wname('tb_wcont');

        var tdiff = curtime - this.wpos['stime'];
        var pshift = 1;
        if (tdiff >= this.wpos['ttime'])
        {
            this.set_timer(1);
            this.wpos['animated'] = false;
            if (!this.wpos['visible'])
            {
                this.change_class(this.wname('tb_wcont'), 'tb_open', false);
            }
            this.wswitch_state(this.wpos['visible']);

            // reload the page
            this.load_pagelet('open_' + (this.wpos['visible']?1:0));
            // Jianmin - 2010/07/06 disabled hash.
            //document.location.hash = this.wpos['visible'] ? '#newsboard' : '#news';
        }
        else
            pshift = tdiff/this.wpos['ttime'];

        pshift = ((-Math.cos(pshift * Math.PI)/2) + 0.5) * this.wpos['target'];            

        for (var i=0;i<ael.length;i++)
        if (this.gel(ael[i]))
        {
            el = ael[i];

            if (typeof this.epos[el] == 'undefined')
            {
                this.epos[el] = this.slide_prop(this.gel(el));
                if (isNaN(this.epos[el]))
                    this.epos[el] = 0;
            }
            this.slide_prop(this.gel(el), this.epos[el] + pshift + 'px');
        }
    }
};

this.count_timer = function()
{
    this.flags['cupdates']++;

    if (this.flags['cupdates'] > 1)
        return;

    if ((this.flags['cupdates'] > 3) && (this.flags['cupdates']%30 != 0))
        return;

    this.jsload('/js/weaver_counter.js?r=' + Math.random(), 'tb_js_counter', true);
};

// end of object definitions
}

/*if (typeof theWeaverBoard == 'undefined')
{
   var theWeaverBoard = new WeaverBoard();

   if (document.body)
   {
	   theWeaverBoard.init_onload();
   }
}*/

