/*****************************************************************************
 *
 *  Project
 *                 ___  \ /  ___   ___ |\  /| |
 *                |___|  X  |___| |    | \/ | |
 *                |___  / \ |___  |___ |    | |___
 * eXecML Executable Markup Language
 * Copyright (C) 2003,2004, Kepler Group, Inc.
 *
 * LEGAL NOTICE
 * The source code is Copyright 2004, Kepler Group, Inc. and
 * has been distributed to you for demonstration purposes only
 * and may not be copied, used, sold, or distributed without
 * the express written consent of Kepler Group, Inc.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 *****************************************************************************/
function requireJS(name){document.write('<script language="javascript" type="text/javascript" src="' + '/xweb_client/javascript/'+name+'.js"></script>');}

requireJS('browser');
requireJS('xxml');
requireJS('xmodule');
requireJS('xpage');
requireJS('ig_trans');

var m_page;
var mouseX=0;
var mouseY=0;

var _debug = function(){};

window.onload = function(){
    if(!m_browser.moz){
       m_browser.moz=(document.implementation && document.implementation.createDocument)?1:0;
    }    
    m_page = new XPage();

    document.onscroll = getMouseXY;
    document.onmousemove = getMouseXY;    
    if(m_browser.moz){
        document.body.onmousedown=captureOffsetXY;
    } 
    if(document.getElementById('jsoutput')!=null){
	    _debug = _debugconsole;
	}
};

function getScrollTop(){
    var scrolltop = document.body.scrollTop;
    if(document.documentElement){
        scrolltop = scrolltop==0 ? document.documentElement.scrollTop : scrolltop;
    }
    return scrolltop;					    
}
function getScrollLeft(){
    var scrollleft = document.body.scrollLeft;
    if(document.documentElement){
        scrollleft = scrollleft==0 ? document.documentElement.scrollLeft : scrollleft;
    }
    return scrollleft;					    
}
function captureOffsetXY(e) {
	if(m_browser.ie5) {
		cur_x=window.event.offsetX;
		cur_y=window.event.offsetY;
	} else if(m_browser.moz && cmvid!=null) {
		cur_x = e.pageX;
		cur_y = e.pageY;
		setCellMov(cmvid);
	}
}
function getMouseXY(e) {
    if(document.all) {
        mouseX = event.clientX + getScrollLeft();
        mouseY = event.clientY + getScrollTop();
    }else if(m_browser.moz) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }  
    if (mouseX < 0){mouseX = 0;}
    if (mouseY < 0){mouseY = 0;}  
}

document.onmouseup = function(){
    if(colsizing)
        endSizeCol();
    else if(cmv)
        endCellMov();
}



var cur_x
var cur_y;
var cmv     = null;
var cmvid   = null;
var cmvs    = null;
var tdtar   = null;
var tdtari  = -1;


function hasFilters(el){
    if(el.filters && el.filters.length)
        return true;
    else
        return false;
}

//MOVABLE MODULES DHTML FUNCTIONS
function setCellMov(celid){

    if(m_browser.ie5){
        cmvid=celid;
        document.onselectstart=new Function ("return false");
        captureOffsetXY(event);
    }else if(m_browser.moz && cmvid==null){
	    document.onmousedown=function(){
	        return false;
	    };        
        cmvid=celid;
        document.onmousemove=moveCell;
        return;
    }

    document.body.style.cursor = 'move';

    cmv = document.getElementById(celid+'_w');
    cmvs = cmv.style;

    cmvs.width = cmv.offsetWidth + 'px';
    cmvs.height = cmv.offsetHeight + 'px';

    if(m_browser.ie5){
        cmv.parentElement.style.width = cmvs.width;
        cmv.parentElement.style.height = cmvs.height;
    }else if(m_browser.moz){
        cmv.parentNode.style.width = cmvs.width;
        cmv.parentNode.style.height = cmvs.height;
        cmv.onmousemove=null;
    }
    document.body.onmousemove=moveCell;
    cmvs.position='absolute';
    document.getElementById(cmvid+'_cd').style.overflow ='hidden';
    cmvs.zIndex = 10;

    if(m_browser.ie5){
        cmvs.filter='progid:DXImageTransform.Microsoft.Alpha( Opacity=50, FinishOpacity=50, Style=1, StartX=0, FinishX=100, StartY=0, FinishY=100) progid:DXImageTransform.Microsoft.Shadow(color=#000000,direction=135);';
        moveCell();
    }else if(m_browser.moz){
        cmvs.left = cur_x + 'px';
        cmvs.top = cur_y + 'px';
    }
    
    if(cmv.getElementsByTagName('IFRAME').length>0){
        var ifr = cmv.getElementsByTagName('IFRAME')[0];
        ifr.parentNode.removeChild(ifr);
    }
    document.onmousemove=moveCell;
}
function endCellMov(){

    document.body.onmousemove=null;
    var xrow = null;

    if(tdtar && 'emp_'+cmvid!=tdtar.id){
        xrow = m_page.layout.selectSingleNode('//row[@id="'+cmvid+'"]');
        var targetid = tdtar.id;

        if(targetid.indexOf('_999')>-1){
            var colnum = targetid.split('_');
            var col = m_page.layout.selectSingleNode('//section['+colnum[1]+']/column['+colnum[2]+']');
            col.appendChild(xrow);
        }else if(targetid.indexOf('mcol_')==0){
            var colnum = targetid.split('_');
            var col = m_page.layout.selectSingleNode('//section['+colnum[1]+']/column['+colnum[2]+']');
            var ncol = m_page.layout.ownerDocument.createElement('column');
            if(col.nextSibling){
                col.parentNode.insertBefore(ncol,col.nextSibling);
            }else{
                col.parentNode.appendChild(ncol);
            }
            ncol.appendChild(xrow);
            m_page.saveLayout();
            location.href = location.href;
        }else{
            var nrx = m_page.layout.selectSingleNode('//row[@id=\''+targetid.substring(4)+'\']');
            nrx.parentNode.insertBefore(xrow,nrx);
        }


        var ctrow=cmv;
        while(ctrow.parentNode){
            ctrow = ctrow.parentNode;
            if(ctrow.tagName=='TR')break;
        }
        
        if(document.all){
            var empcel=ctrow.previousSibling;
            var tbody=tdtar.parentElement.previousSibling;
            if(!tbody){
                tbody=tdtar.parentElement.parentElement;
                tbody.insertAdjacentElement('afterBegin',empcel);
            }else{
                tbody.insertAdjacentElement('afterEnd',empcel);
            }

            empcel.insertAdjacentElement('afterEnd',ctrow);

        }else if(m_browser.moz){

            var empcel=document.getElementById('emp_'+cmvid).parentNode;
            var trtar = tdtar.parentNode;
            ctrow = trtar.parentNode.insertBefore(ctrow,trtar);
            empcel=trtar.parentNode.insertBefore(empcel,ctrow);
        }
    }
    if(tdtar)tdtar.style.backgroundColor='';
    tdtar=null;
    cmvs.position='relative';
    cmvs.left = '0px';
    cmvs.top = '0px';
    cmvs.height = null;
    cmvs.width = '100%';
    cmvs.filter=null;
    cmvs.zIndex = 0;
    document.getElementById(cmvid+'_cd').style.overflow ='auto';

    if(m_browser.ie5){
        cmv.parentElement.style.width = null;
        cmv.parentElement.style.height = null;
    }else if(m_browser.moz){
        cmv.parentNode.style.width = null;
        cmv.parentNode.style.height = null;
    }

    document.body.style.cursor = 'default';
    document.onselectstart=null;
    document.onmousedown=null;    

    doModAction(cmvid,'hideHeader');
    m_page.saveLayout();
    document.onmousemove = getMouseXY;
    if(m_browser.moz){
        document.body.onmousedown=captureOffsetXY;
    } 
        
    for(var mod in m_page.modules){
        m_page.modules[mod].top=null;
        m_page.modules[mod].left=null;
    }
    cmvs=null;
    cmvid=null;
    cmv=null;    
}

function moveCell(e){

    var eg;
	if(document.all){
	    eg = event;
	}else{
	    eg = e;
    }

	if(!eg){
	    document.body.onmousemove=null;
	    return;
	}

	if(!cmvs)return;

	var x2 = eg.clientX;
	var y2 = eg.clientY+getScrollTop();

	if(document.all){
    	x2 = x2 - (cur_x+2);
	    y2 = y2 - (cur_y+2);
    }
    cmvs.left = x2 + 'px';
    cmvs.top = y2 + 'px';
    
    if(y2-getScrollTop()>=document.documentElement.offsetHeight*.875){
        window.scrollBy(0,10)
    }
    if(y2-getScrollTop()<=document.documentElement.offsetHeight*.125){
        window.scrollBy(0,-10)
    }

    for(i=0;i<m_page.empty_cells.length;i++){
        var td = m_page.empty_cells[i];
        if(document.all){
            var wo = td.parentElement.parentElement.parentElement.parentElement.offsetLeft;
//            var ho = td.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.offsetTop;
            var elpar = td.parentElement;
            while(elpar){
                if(elpar.id.indexOf('sec_')==0){
                    break;
                }
                elpar = elpar.parentElement;   
            }
            var ho = elpar.offsetTop;
        }else if(m_browser.moz){
            var wo = td.parentNode.parentNode.parentNode.parentNode.offsetLeft;
            var ho = td.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop;
        }   
        if( eg.clientX+(td.offsetWidth) > td.offsetLeft + wo &&
            eg.clientX-(td.offsetWidth/4) < td.offsetLeft + td.offsetWidth + wo &&
            eg.clientY+10 > td.offsetTop - getScrollTop() + ho &&
            eg.clientY-10 < td.offsetTop - getScrollTop() + td.offsetHeight + ho ){
                td.style.backgroundColor='blue';
                tdtar=td;
                tdtari = i;
                break;
         }
    }

    for(i=0;i<m_page.empty_cells.length;i++){
        var td = m_page.empty_cells[i];
        if(i!=tdtari)
            td.style.backgroundColor='';
    }

}


//ADUSTABLE COLUMN SIZES
var colsizing=null;
var colsizing2=null;
var mcid = null;
var mintds;
function movr_colsep(el){
    if(colsizing==null){el.style.backgroundColor='blue';}
}
function mout_colsep(el){
    if(colsizing==null){el.style.backgroundColor='';}
}
function sizeCol(e){
    if(colsizing){
        if(m_browser.moz){
            var origw =parseFloat(colsizing.style.width);
            colsizing.style.width = e.pageX-colsizing.offsetLeft + 'px';
            var neww = e.pageX-colsizing.offsetLeft;
            var diff = origw-neww;
            _jso('sizeCol diff:'+diff);
            //if(colsizing2){
                colsizing2.style.width = (parseFloat(colsizing2.offsetWidth)+diff) + 'px';
            //}

        }else{
            var origw =colsizing.offsetWidth;
            //alert(colsizing.offsetLeft);
            colsizing.style.width = event.x-colsizing.offsetLeft + 'px';
            var neww = colsizing.offsetWidth;
            var diff = origw-neww;
            _jso('sizeCol diff:'+diff);
            if(colsizing2){
                colsizing2.style.width = colsizing2.offsetWidth+diff + 'px';
            }

        }
    }
}
function beginSizeCol(el){
    mcid = el.id.split('_');
    colsizing= document.getElementById('mcol_'+mcid[1]+'_'+mcid[2]);
    colsizing2=document.getElementById('mcol_'+mcid[1]+'_'+(parseFloat(mcid[2])+1));
    document.onselectstart=new Function ("return false");

    var aTDs = document.getElementById('sec_'+mcid[1]).getElementsByTagName("td");
    mintds = new Array();
    var j=0;
    for(i=0;i<aTDs.length;i++){
        var td = aTDs[i];
        if( td.className=="cell" && td.style.visibility!='hidden'){
            td.style.visibility='hidden';
            mintds[j] = td;
            j++;
        }
    }
    if(m_browser.moz){
        el.onmousemove=null;
        document.body.onmousemove=sizeCol;
        document.body.onmouseup=endSizeCol;
    }
}
function endSizeCol(){
    if(m_browser.moz){
        document.body.onmousemove=null;
        document.body.onmouseup=null;
    }
    if(colsizing){
        if(mintds.length){
            for(i=0;i<mintds.length;i++){
                mintds[i].style.visibility='visible';
            }
        }
        mintds=null;

        var xcol = m_page.layout.selectSingleNode('//section['+mcid[1]+']/column['+(parseFloat(mcid[2]))+']');
        xcol.setAttribute('width',colsizing.style.width);
        var xcol = m_page.layout.selectSingleNode('//section['+mcid[1]+']/column['+(parseFloat(mcid[2])+1)+']');
        xcol.setAttribute('width',colsizing2.style.width);
        
        var sepid = colsizing.id+'_sep';
        colsizing=null;
        mout_colsep(document.getElementById(sepid));
        document.onselectstart=null;
        m_page.saveLayout();
        for(var mod in m_page.modules){
            m_page.modules[mod].top=null;
            m_page.modules[mod].left=null;
        }        
    }

}
function _jso(s){
	_debug('_jso call','s',s);
}

var _dbgw = window;
function _debugconsole () {
    
    if(_dbgw==null){
        _dbgw = window.open('about:blank');
        _dbgw.document.write('<body></body>');
    }
    
    var _dl = _dbgw.document.getElementById('_dbgel');
    if(_dl==null){
        var dbgrhtml = '<table id="_dbgel" cellspacing="0" cellpadding="0" border="0" style="padding: 0px 0px 2px 4px;"' +
                       'style="border:1pt solid #cc0000; ' +
                       'color:#cc0000; ' +
                       'font-size:10pt; ' +                       
                       'background-color:#ffffff; ' +
                       'zposition:absolute; ' +
                       'width:100%' +                    
                       'ztop:100px; ' +
                       'zleft:300px;"><tr bgcolor="#cc0000">'+
                       '<td><b style="color:white;">JAVASCRIPT CONSOLE</b><td>' + 
                       '<td align="right" style="color:white;"><i>&copy;David Krause 2005,2006</i></td>'+
                       '</tr></table>';
		document.getElementById('jsoutput').innerHTML = dbgrhtml;            
        //_dbgw.document.body.insertAdjacentHTML('beforeEnd',dbgrhtml);      
        _dl = _dbgw.document.getElementById('_dbgel');
    }

	//TODO: make firefox compatible
	
    if(arguments[0]!=null){
	    var rh = _dl.insertRow();
	    var ch = rh.insertCell();
	    ch.colSpan=3;
	    ch.innerHTML = '<b>'+arguments[0]+':</b>';
	    ch.style.borderTop = '1pt solid #cc0000';
	}
	
    var names = arguments[1].split(',');    

    for(var i=2;i<arguments.length;i++){
        
        var val = arguments[i];
       	var name = names[i-2];

        if(typeof(val)=='object'){
        	if(arguments[0]==null){
        		val = val.toString().replace(/\&/g,' &');
        	}else{
	        	_debug(null,name,'[object]');
	        	var obj = arguments[i];
	        	for(var s in obj){
	        		try{
	        			if(obj[s]!=null){
		        			_debug(null,'&nbsp;&nbsp;&nbsp;&nbsp;.'+s,obj[s]);
		        		}
	        		}catch(e){}
	        	}
	        	name = null;
	        }
        }else if(typeof(val)=='function'){
            val = '<pre>'+arguments[i]+'</pre>';
        }else if(typeof(val)=='string' && name.indexOf('HTML')!=-1){
            val = val.replace(/</g,'&lt;').replace(/>/g,'&gt;');
        }
                
        if(name!=null){
	        var r = _dl.insertRow();
	        var c1 = r.insertCell();
	        var c2 = r.insertCell();
	        var c3 = r.insertCell();
        
	        c1.innerHTML = '<nobr>'+name+'</nobr>';        
	        c2.innerHTML = '<i>'+(val=='[object]'?val:typeof(arguments[i]))+'</i>';
	        c3.innerHTML = val+'&nbsp;';
			
			c2.style.color = '#000077';        
			c3.style.color = '#000000';        
			
	        c1.style.width = c2.style.width = '20px';
	        c1.style.verticalAlign = c2.style.verticalAlign = c3.style.verticalAlign = 'top';
	        c1.style.borderTop = c2.style.borderTop = c3.style.borderTop = '1pt dashed #cc0000';
	        c1.style.borderRight = c2.style.borderRight = '1pt dashed #cc0000';
	    }
    }    
    
    
}    
window.onerror = function (m,u,l){
	_debug('ERROR','message,url,line',m,u,l);
	return true;
}
