/* #################################################################
Copyright © 2008 BBB Systems, LLC, All rights reserved
################################################################## */

//used for the resizing mechanism
var cur_resizerDiv = null;
var r_mouseStatus = 'up';
var md_ResizerArray = new Array( );

//make sure and include pscript first.

window.special_init = function( ){
    if(window.js_initialized){ return; }

    //window.js_initialized set in init( )
    window.init( );

    //admin specific
    initTableIndex( );
    initMDControls( );
    initAdminHelp( );
}

window.special_xmlInit = function( ){
    window.xmlInit( );
}

function initMDControls( ){
    var divs = document.getElementById('content').getElementsByTagName('div');

    var j = 0;
    for(var i = 0; i < divs.length; i++){
        switch(divs[i].className){
        case 'md_resizer':
            md_ResizerArray[j] = new mdResizer(divs[i]);
            j++;
            break;
        case 'md_imp_exp_button':
            var a_button = divs[i].firstChild;
            var d_control = a_button.nextSibling;

            if(!a_button || ! d_control){
                continue;
            }

            d_control.style.display = 'none';

            a_button.d_control = d_control;
            d_control.a_button = a_button;

            a_button.onclick = dControlHandler;
            a_button.onmouseover = dControlOver;
            a_button.onmouseout = dControlOut;
            break;
        case 'md_imp_exp_loader':
            var frm = divs[i].getElementsByTagName('form');
            if(!frm || !frm[0]){
                break;
            }
            frm = frm[0];
            frm.onsubmit = importExportFormCheck;

            break;
        default:
            break;
        }
    }

    document.onmousemove = mdResizer_getPos;
    document.onmouseup = setMouseStatusUp;
}

//takes in an element
mdResizer = function(rObj){
    this.rObj = rObj;

    //this is the parent of the object
    this.rc = rObj.parentNode;
    this.sib = rObj.previousSibling;

    this.tbody = new Array( );
    if(this.sib.getElementsByTagName){
        this.tbl = this.sib.getElementsByTagName('table');
        this.tbody = this.sib.getElementsByTagName('tbody');
        this.thead = this.sib.getElementsByTagName('thead');
    }

    if(!this.tbody[0]){
        this.tbl = null;
        this.tbody = null;
        this.thead = null;
    }
    else{
        this.tbl = this.tbl[0];
        this.tbody = this.tbody[0];
        this.thead = this.thead[0];

        if(this.thead && this.thead.className != 'md_locked'){
            //make sure all dimensions are auto;
            this.surrender( );
            this.rc.removeChild(this.rObj);
            return;
        }
    }



    if(!this.rObj || !this.rc){
        return;//something went wrong
    }

    this.rObj.style.left = '0px';

    this.r_startHeight = 0;

    if(this.rc.clientHeight){
        this.rc.style.height = this.rc.clientHeight + 'px';
        this.sib.style.overflowX = 'auto';
        this.sib.style.overflowY = 'hidden';
        this.r_startHeight = this.rc.clientHeight;
    }
    else if(this.rc.offsetHeight){
        if(window.ActiveXObject && this.tbody){
            //it's a bummer it has to be this way.
            this.rc.style.height = '250px';
            this.rc.style.overflow = 'auto';

            this.sib.style.display = 'none';

            //fix the big ie stretch... match the width the md_search, it shouldn't be stretched
            var md_search = this.sib.parentNode.previousSibling;
            this.sib.style.width = md_search.offsetWidth + 'px';

            this.sib.style.display = 'block';
        }
        this.rc.style.height = this.rc.offsetHeight + 'px';
        this.r_startHeight = this.rc.offsetHeight;
    }
    else{
        this.surrender( );
        return;
    }

    this.sib_pad = 14;
    this.tbody_pad = 0;

    if(this.tbody){
        this.sib_pad = 0;
        this.tbody_pad = 41;
    }

    if(this.tbody){
        this.r_startScrollHeight = this.tbody.scrollHeight + this.tbody_pad;
    }

    this.sib.style.height = parseInt(this.r_startHeight - this.sib_pad) + 'px';
    if(this.tbody){
        this.tbody.style.overflowX = 'hidden';
        this.tbody.style.overflowY = 'auto';

        this.tbody.style.height = parseInt(this.r_startHeight - this.tbody_pad) + 'px';

        if(window.ActiveXObject){
            //ie has it's own way
            this.surrender2( );
        }
        else if( ( (window.xml && window.xml.toString() && navigator.mimeTypes["image/svg+xml"]) || window.Iterator ) && document.designMode){
            if(navigator.oscpu){
                //it's firefox 3... default behavior is kind of weird in ff3 when it comes to table cell borders, throw it to the table hack
                this.tableHack( );
            }
            //else it's firefox 2, let it slide... Firefox 2 don't support the table hack... weird because, it works in 3.
            this.scroller = new fakeScroll(this.tbody, this.sib);
        }
        else{
            this.tableHack( );
            this.scroller = new fakeScroll(this.tbody, this.sib);
        }

    }

    this.r_curHeight = 0;
    this.r_curPos = 0;
    this.r_newPos = 0;

    this.rObj.resizer = this;
    this.rObj.onmousedown = this.setPos;

    this.sib.resizer = this;

}

mdResizer.prototype = {
/* ---------------------------------------------- */

tableHack: function( ){

    var ths = this.thead.getElementsByTagName('th');

    var tr = this.tbody.getElementsByTagName('tr')[0];
    var tds = tr.getElementsByTagName('td');

    var total_style_width = 0;
    var total_client_width = 0;
    for(var i = 0; i < ths.length; i++){
        var th = ths[i];
        var td = tds[i];

        var t_width = 0;
        if(th.clientWidth > td.clientWidth){
            t_width = th.clientWidth;
        }
        else{
            t_width = td.clientWidth;
        }

        //if(t_width > 200){//can't do this.
            //t_width = 200;
        //}

        total_style_width += t_width;

        th.style.width = t_width + 'px';
        td.style.width = t_width + 'px';

        th.style.minWidth = t_width + 'px';
        td.style.minWidth = t_width + 'px';

        th.style.maxWidth = t_width + 'px';
        td.style.maxWidth = t_width + 'px';
    }

    //this.tbl.style.display = 'block';

    this.tbody.style.display = 'block';
    this.tbody.style.overflowX = 'hidden';
    this.tbody.style.overflowY = 'auto';
    this.tbody.style.minWidth = '100%';

    this.thead.style.display = 'block';
    var th_tr = ths[0].parentNode;
    th_tr.style.display = 'block';

    this.tbody.style.width = this.tbody.scrollWidth + 'px';
    //alert(this.tbl.clientWidth);

    if(this.thead.scrollWidth > this.thead.clientWidth){
        this.thead.style.width = this.thead.scrollWidth + 'px';
    }

    //alert(this.tbody.scrollWidth);
    //alert(this.rc.clientWidth);
    if(this.tbody.scrollWidth - this.rc.clientWidth < 20){
        //get rid of the bottom scroller, don't need it.
        this.sib.style.overflow = 'hidden';
        this.rc.style.height = parseInt(this.rc.clientHeight - 14) + 'px';
    }

    this.tbody.style.height = parseInt(this.r_startHeight - this.tbody_pad) + 'px';

    //this.applySticky( );
},

/*
//I'll revisit this someday when I have more time
applySticky: function( ){
    //find the offsets
    var node = this.tbl;


    var trs = this.tbl.getElementsByTagName('tr');
    for(var i = 0; i < trs.length; i++){
        tr = trs[i];
        //tr.style.position = 'relative';
        var tds = null;
        if(i == 0){
            tds = tr.getElementsByTagName('th');
        }
        else{
            tds = tr.getElementsByTagName('td');
        }

        var td_offset = 0;
        var foundBreak = false;
        for(var j = 0; j < tds.length; j++){
            var td = tds[j];
            if(td.className && td.className.match(/md_sticky/)){
                td.style.position = 'fixed';
                td.style.display = 'block';
                td.style.marginLeft = td_offset + 'px';
                td.style.marginTop = '-1px';
                td_offset += td.clientWidth;
            }
            else{
                td.style.paddingLeft = parseInt(td_offset + 8) + 'px';
                td_offset += td.clientWidth;
                break;
            }
        }
    }
},
*/

surrender: function( ){
    this.sib.style.width = 'auto';
    this.rc.style.width = 'auto';

    this.sib.style.height = 'auto';
    this.rc.style.height = 'auto';

    this.sib.style.overflow = 'visible';

    this.rc.style.overflow = 'visible';
    this.rObj.style.cursor = 'default';

    if(this.tbody){
        this.tbody.style.width = 'auto';
        this.tbody.style.height = 'auto';
        this.tbody.style.overflow = 'visible';
    }
},

surrender2: function( ){
    //only partially surrender, let them keep their scrolls, just header don't stick.
    this.rc.style.overflow = 'visible';
    this.sib.style.overflow = 'auto';

    if(this.tbody){
        this.tbody.style.width = 'auto';
        this.tbody.style.height = 'auto';
        this.tbody.style.overflow = 'visible';
    }

    this.tbody = null;
},

setPos: function(e){
    //firefox 3 creates a selection when you drag the bar which creates some nasty
    //functionality, so get rid of them.
    if(window.getSelection){
        var selObj = window.getSelection();
        selObj.removeAllRanges();
    }

    //for handling events in ie vs. w3c
    var curevent = (typeof event == 'undefined' ? e : event);
    r_mouseStatus = 'down';

    cur_resizerDiv = this;

    this.resizer.r_curPos = curevent.clientY;
    tempHeight = this.resizer.rc.style.height;
    this.resizer.r_curHeight = parseInt(tempHeight);
}//end setPos

/* ---------------------------------------------- */
}//end mdResizer.prototype


//this function must be global scope, as it is shared among all mdResizers
function setMouseStatusUp( ){
    if(r_mouseStatus == 'down'){
        //firefox 3 creates a selection when you drag the bar which creates some nasty
        //functionality, so get rid of them.
        if(window.getSelection){
            var selObj = window.getSelection();
            selObj.removeAllRanges();
        }

        r_mouseStatus = 'up';
        cur_resizerDiv = null;
    }
}

function mdResizer_getPos(e){
    if(!cur_resizerDiv){
        return;
    }

    if(r_mouseStatus == 'down'){
        //firefox 3 creates a selection when you drag the bar which creates some nasty
        //functionality, so get rid of them.
        if(window.getSelection){
            var selObj = window.getSelection();
            selObj.removeAllRanges();
        }

        var curevent = (typeof event == 'undefined' ? e : event);

        //get new mouse position
        r_newPos = curevent.clientY;

        //calculate movement in pixels
        var pxMove = parseInt(r_newPos - cur_resizerDiv.resizer.r_curPos);

        //determine new height
        var newHeight = parseInt(pxMove + cur_resizerDiv.resizer.r_curHeight);

        //if(newHeight < cur_resizerDiv.resizer.r_startHeight){
        if(newHeight < cur_resizerDiv.resizer.r_startHeight){
            newHeight = cur_resizerDiv.resizer.r_startHeight;
        }

        cur_resizerDiv.resizer.rc.style.height = newHeight + 'px';

        if(!window.ActiveXObject){
            if(cur_resizerDiv.resizer.tbody.scrollWidth - cur_resizerDiv.resizer.rc.clientWidth < 20){
            //if(cur_resizerDiv.resizer.sib.style.overflow == 'hidden'){//safari didn't play nice with this one
                cur_resizerDiv.resizer.rc.style.height = parseInt(cur_resizerDiv.resizer.rc.clientHeight - 14) + 'px';
            }
        }

        cur_resizerDiv.resizer.sib.style.height = parseInt(newHeight - cur_resizerDiv.resizer.sib_pad) + 'px';

        if(cur_resizerDiv.resizer.tbody){
            if(newHeight > cur_resizerDiv.resizer.r_startScrollHeight){
                newHeight = cur_resizerDiv.resizer.r_startScrollHeight;
            }
            cur_resizerDiv.resizer.tbody.style.height = parseInt(newHeight - cur_resizerDiv.resizer.tbody_pad) + 'px';
            cur_resizerDiv.resizer.scroller.resizeScrollBar( );
        }
    }
}//end mdResizer_getPos

//End Resizer Stuff #########################################################

//start scroll bar stuff

//this object creates an object div that is placed on the side of the target div
//that is passed into it, and if one scrolls, the other one scrolls.  relObj
//is the div that the scroll bar is attached to as a child.  relObj must be positioned
fakeScroll = function(targetObj, relObj){

    this.targetObj = targetObj;
    this.relObj = relObj;
    this.scroller_outer = null;
    this.scroller_inner = null;

    this.scroller_width = 18;

    this.scroller_outer_height = 0;
    this.scroller_inner_height = 0;

    this.rel_top = 0;

    this.setScrollerHeight( );
    this.createScroller( );
}

fakeScroll.prototype = {
/* ---------------------------------------------- */

setScrollerHeight: function( ){
    this.scroller_inner_height = this.targetObj.scrollHeight;
    //this.rel_top = this.relObj.clientHeight - this.targetObj.clientHeight;
    this.rel_top = this.targetObj.offsetTop;
    this.scroller_outer_height = this.targetObj.clientHeight;

},//end setTargetHeight

createScroller: function( ){
    var tmp = document.createElement('span');

    //alert(this.scroller_outer_height);
    //alert(this.scroller_inner_height);
    //alert(this.targetObj.clientHeight);
    //alert(this.targetObj.scrollHeight);
    //alert(this.relObj.offsetTop);

    tmp.style.display = 'block';
    tmp.style.width = this.scroller_width + 'px';
    tmp.style.height = this.scroller_outer_height + 'px';
    tmp.style.position = 'absolute';
    tmp.style.top = this.rel_top + 'px';
    tmp.style.right = '0px';
    tmp.style.overflowX = 'hidden';
    tmp.style.overflowY = 'auto';
    tmp.style.zIndex = '2000';
    //tmp.style.border = '1px solid #ff0000';

    var tmp2 = document.createElement('span');
    tmp2.style.display = 'block';
    tmp2.style.width = this.scroller_width + 'px';
    tmp2.style.height = this.scroller_inner_height + 'px';
    tmp2.innerHTML = '&nbsp;';

    this.scroller_outer = this.relObj.parentNode.appendChild(tmp);
    this.scroller_inner = this.scroller_outer.appendChild(tmp2);

    this.targetObj.scroller_outer = this.scroller_outer;
    this.scroller_outer.targetObj = this.targetObj;

    this.targetObj.onscroll = fakeScrollTarget;
    this.scroller_outer.onscroll = fakeScrollScroller;

},//end createScroller

resizeScrollBar: function( ){
    this.setScrollerHeight( );
    this.scroller_outer.style.height = this.scroller_outer_height + 'px';

    if(this.scroller_inner_height > this.scroller_outer_height){
        this.scroller_outer.style.overflowY = 'auto';
    }
    else{
        this.scroller_outer.style.overflowY = 'hidden';
    }
}

}//end fakeScroll prototype

function fakeScrollTarget(e){
    if(cur_resizerDiv){
        return false;
    }
    this.scroller_outer.scrollTop = this.scrollTop;
}

function fakeScrollScroller(e){
    if(cur_resizerDiv){
        return false;
    }
    this.targetObj.scrollTop = this.scrollTop;
}

function recordsScroll(e){
    var thead = this.getElementsByTagName('thead');
    if(thead[0]){
        thead = thead[0];
    }
    else{
        return;
    }

    //thead.style.top = this.scrollTop + 'px';
    //alert(thead.style.top);
}//end tbody scroller

//end scrollbar stuff

//Start Table Index #########################################################
function initTableIndex( ){
    if(!window.cur_table_index){
        return;
    }

    var content = document.getElementById('content');

    cur_indx = window.cur_table_index;

    var divs = content.getElementsByTagName('div');

    for(var i = 0; i < divs.length; i++){
        var div = divs[i];
        if(div.className != 'md_title'){
            continue;
        }

        var t_id = div.id;

        if(t_id == ''){
            continue;
        }

        t_id = t_id.replace(/^table\_index\_/, '');

        var start_open = false;

        if(t_id == cur_indx){
            start_open = true;
        }

        var md_content = get_md_content(div);

        if(start_open){
            div.className = 'md_title md_minus';
            md_content.style.display = 'block';
        }
        else{
            div.className = 'md_title md_plus';
            md_content.style.display = 'none';
        }
        div.onclick = toggleTableIndexVisible;

    }

}

function toggleTableIndexVisible( ){
    var md_content = get_md_content(this);

    if(md_content.style.display == 'block'){
        this.className = 'md_title md_plus';
        md_content.style.display = 'none';
    }
    else{
        this.className = 'md_title md_minus';
        md_content.style.display = 'block';
    }
}

function get_md_content(node){
    while(node.nextSibling){
        node = node.nextSibling;
        if(node.className){
            if(node.className == 'md_content'){
                return node;
            }
        }
    }
}

//End Table Index ###########################################################

function initAdminHelp( ){
    var imgs = document.getElementsByTagName('img');
    var imgs_len = imgs.length;
    for(var i = 0; i < imgs_len; i++){
        var img = imgs[i];
        if(img.className != 'admin_help_icon'){
            continue;
        }

        img.onclick = function( ){
            idA = this.id.split(/_/);
            var span_id = '';
            if(idA[0]){
                span_id = idA[0] + '_help';
            }

            //toggle help div
            if(span_id != '' && document.getElementById(span_id)){
                t_help = document.getElementById(span_id);
                if(t_help.style.display != 'inline'){
                    t_help.style.display = 'inline';
                }
                else{
                    t_help.style.display = 'none';
                }
            }

            //close other help divs
            var spans = document.getElementsByTagName('span');
            var spans_img = spans.length;
            for(var i = 0; i < spans_img; i++){
                span = spans[i];
                if(span.className != 'admin_help_info'){
                    continue;
                }

                if(span.id != span_id){
                    span.style.display = 'none';
                }
            }
        }
    }

    //close parent div
    as = document.getElementsByTagName('a');
    as_len = as.length;
    for(var j = 0; j < as_len; j++){
        a = as[j];
        if(a.className != 'close_help'){
            continue;
        }

        a.onclick = function( ){
            this.parentNode.style.display = 'none';
            return false;
        }
    }
}

