// {{{ Header
/**
 *
 * filename:     swbUtils.js
 *
 * @autor:       JMueller
 * @contact:     info at swbulletin dot de
 * @modified:    25/10/05
 * @version:     1.0.0.1
 */
// }}}

// Javascript Framework SWBulletin.De

/*-------------------------------------*/

var d         = document;
var mouse     = new Object;
var evt       = new Object;
var nodeArray = new Array;
var mouseX, mouseY;

/*-------------------------------------*/

/*----------- prototype.js ------------*/

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Object.prototype.extend = function(object) {
  return Object.extend.apply(this, [this, object]);
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    __method.apply(object, arguments);
  }
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    __method.call(object, event || window.event);
  }
}

if (!Array.prototype.push) {
  Array.prototype.push = function() {
		var startLength = this.length;
		for (var i = 0; i < arguments.length; i++)
      this[startLength + i] = arguments[i];
	  return this.length;
  }
}


/*-------------------------------------*/

var swB = function() {};
swB.prototype = {
    
    Version: '0.1.1',
    
    Client: function() {
        if (navigator.appName == 'Microsoft Internet Explorer') {
            return (
            new ActiveXObject('Msxml2.XMLHTTP') ? new ActiveXObject('Msxml2.XMLHTTP') :
            new ActiveXObject('Microsoft.XMLHTTP')) || false;
        } else {
            return (
            new XMLHttpRequest()) || false;
        }
    }
}

/*-------------------------------------*/

swB.Base = function() {};
swB.Base.prototype = (new swB).extend({
    thisout: function() {
        expSwB.Client;
    },
    
    Evt: function(evt) {
        
        evt = evt ? evt : window.event;
        evt.pos = this.fnMousePosition(evt); 
        evt.source = evt.target ? evt.target : evt.srcElement;
        
        return evt;
    },
    
    fnMousePosition: function(evt) {
        
        if(typeof(evt.pageX) == 'number') {
            mouseX = evt.pageX;
            mouseY = evt.pageY;
        } else {
            mouseX = evt.clientX+d.body.scrollLeft-1;
            mouseY = evt.clientY+d.body.scrollTop-1;
        }
        return {x:mouseX,y:mouseY}
    },
    
    fnEvtadd: function(target,type,func,bubbles) {
        if (d.addEventListener) {
            target.addEventListener(type,func,bubbles);
        } else if (d.attachEvent) {
            target.attachEvent("on"+type,func,bubbles);
        } else {
            target["on"+type] = func;
        }
    },
    
    fnEvtrem: function(target,type,func,bubbles) {
        if (d.removeEventListener) {
            target.removeEventListener(type,func,bubbles);
        } else if (document.detachEvent) {
            target.detachEvent("on"+type,func,bubbles);
        } else {
            target["on"+type] = null;
        }
    },
    
    fnTermEvent: function(evt) {
        if ( evt.stopPropagation != undefined )
            evt.stopPropagation();
        else if ( evt.cancelBubble != undefined )
            evt.cancelBubble = true;
        
        if ( evt.preventDefault != undefined )
            evt.preventDefault();
        else
            evt.returnValue = false;
    },
    
    fnIniEvent: function() {
      if ( typeof d.implementation != "undefined" &&
         d.implementation.hasFeature("HTML",   "1.0") &&
         d.implementation.hasFeature("Events", "2.0") &&
         d.implementation.hasFeature("CSS",    "2.0") ) {
         d.addEventListener("mouseup",   this.fnEvtadd(d,"mouseup",this.fnEndDrag,false).bindAsEventListener(this),  false);
         d.addEventListener("mousemove", this.fnEvtadd(d,"mousemove",this.fnDragLoop,false).bindAsEventListener(this), false);
      }
      else {
         d.attachEvent( "onmouseup",   this.fnEvtadd(d,"mouseup",this.fnEndDrag,false).bindAsEventListener(this) );
         d.attachEvent( "onmousemove", this.fnEvtadd(d,"mousemove",this.fnDragLoop,false).bindAsEventListener(this) );
      }
    },
    
    fnViewPort: function(element) {
        this.element = element;
        var left=this.element.offsetLeft;
        var top=this.element.offsetTop;
        if(this.element.offsetParent
           &&this.element.offsetParent.clientLeft
           ||this.element.offsetParent.clientTop) {
            
            left+=this.element.offsetParent.clientLeft;
            top+=this.element.offsetParent.clientTop;
        }
        while((this.element = this.element.offsetParent) != null) {
            left+=this.element.offsetLeft;
            top+=this.element.offsetTop;
        }
        return{x:left,y:top};
    }
});

/*-------------------------------------*/

swB.Util = function() {};
swB.Util.prototype = (new swB.Base).extend({
    
    sizeof: function(thearray) {
        for (i = 0; i < thearray.length; i++) {
            if (!this.arrayisgood(thearray, i))
                return i;
        }
        return thearray.length;
    },
    
    arrayisgood: function(thearray, i) {
        if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null)) {
            return false;
        }
        return true;
    }
    
});

/*-------------------------------------*/

swB.FadeOpacity = function() {};
swB.FadeOpacity.prototype = (new swB.Util).extend({
    
    fnFadeOut: function(evt) {
        evt = expSwB.Evt(evt);
        this.fnEvtadd(evt.source, "mouseout", expSwB.fnFadeIn,false);
        evt.source.style.filter="alpha(opacity=50);";
        evt.source.style.opacity="0.5;";
    },
    
    fnFadeIn: function(evt) {
        evt = expSwB.Evt(evt);
        expSwB.fnEvtrem(evt.source, "mouseout", expSwB.fnFadeIn,false);
        evt.source.style.filter="alpha(opacity=100);";
        evt.source.style.opacity="1.0;";
    },
    
    fnFadeAut: function(evt) {
        this.evt = expSwB.Evt(evt);
        
        this.fnFadeLoop(evt);
    },
    
    fnFadeLoop: function(evt) {
        if (evt.source.style.opacity == '') {
            var t = evt.source.getAttribute('style');
            evt.source.setAttribute("style",t+"opacity:0.2;filter:alpha(opacity=20)");
        }
        
    },
    
    fnIcoff: function() {
        var obj = d.getElementById('ic');
        obj.style.display="none";
    }
});

/*-------------------------------------*/

swB.Toggle = function() {};
swB.Toggle.prototype = (new swB.FadeOpacity).extend({
    
    fnToggleIni: function(evt) {
        this.evt      = new Object;
        this.evt      = evt;
        this.objid    = null;
        var toggleIni = d.getElementById("togini");
        var ini       = toggleIni.value.split(',');
        
        if (ini[0]  != null && ini[1] != null) {
            this.direc    = ini[0];
            this.posY     = ini[1];
            this.step     = ini[2];
            this.duration = ini[3];
            
            switch (this.direc) {
                case "up": {
                    this.fnUP();
                    break;
                }
                case "down": {
                    this.fnDOWN();
                    break;
                }
            }
        }
    },
    
    fnUP: function() {
        if (this.objid == null) {
            evt = this.Evt(this.evt);
            this.objid = evt.source.id;
        }
        
        evt.source.style.top = parseInt(evt.source.style.top.replace('px',''))-this.step + "px";
        
        if (parseInt(evt.source.style.top.replace('px','')) > this.posY) {
            this.timer = setTimeout(this.fnUP.bind(this), this.duration);
        } else {
            clearTimeout(this.timer);
        }
    },
    
    fnLoop: function() {
        // dummy
    },
    
    fnDOWN: function() {
        if (this.objid == null) {
            evt = this.Evt(this.evt);
            this.objid = evt.source.id;
        }
        
        evt.source.style.top = parseInt(evt.source.style.top.replace('px',''))+this.step + "px";
        
        if (parseInt(evt.source.style.top.replace('px','')) < this.posY) {
            this.timer = setTimeout(this.fnUP.bind(this), this.duration);
        } else {
            clearTimeout(this.timer);
        }
    },
    
    /*----------- temp -----------*/
    
    fnToggleOpen: function(evt) {
        evt = this.Evt(evt);
        var t = evt.source.parentNode;
        var c = t.parentNode;
        var ch = d.getElementById('ec');
        if (parseInt(c.style.height.replace('px',''))>500) {
            c.style.height=19 + "px";
            ch.src='yes.gif';
        } else {
            c.style.height=600 + "px";
            ch.src='no.gif';
        }
    }
});

/*-------------------------------------*/

swB.Draggable = function() {};
swB.Draggable.prototype = (new swB.Toggle).extend ({
    
    fnStartDrag: function(evt) {
        this.evt = this.Evt(evt);

        this.fnEvtadd(this.evt.source, "mousemove", expSwB.fnDragLoop.bind(this),false);
        this.fnEvtadd(this.evt.source, "mouseup", expSwB.fnEndDrag,false);
        this.fnEvtadd(this.evt.source, "mouseout", expSwB.fnEndDrag,false);
    },
    
    fnDragLoop: function(evt) {
        expSwB.fnMousePosition(evt);
        this.evt = expSwB.Evt(evt);
        
        this.evt.source.style.left = mouseX - this.evt.source.offsetWidth/2 + "px";
        this.evt.source.style.top  = mouseY - this.evt.source.offsetHeight/2 + "px";
        
    },
    
    fnEndDrag: function(evt) {
        this.evt = expSwB.Evt(evt);
        expSwB.fnEvtrem(this.evt.source, "mousemove", expSwB.fnDragLoop,false);
        expSwB.fnEvtrem(this.evt.source, "mouseup", expSwB.fnEndDrag,false);
        expSwB.fnEvtrem(this.evt.source, "mouseout", expSwB.fnEndDrag,false);
    }
});

/*-------------------------------------*/

swB.DragAndDrop = function() {};
swB.DragAndDrop.prototype = (new swB.Draggable).extend ({
    
    fnRegDraggables: function() {
        this.idname = 'draggables';
        
        var parNode = d.getElementById(this.idname);
        this.draggables = parNode.childNodes;
        
        for (var i=0; i < this.draggables.length; i +=1) {
            if (typeof(this.draggables[i].id) == 'string') {
                nodeArray.push(this.draggables[i]);
                expSwB.fnEvtadd(this.draggables[i],"mousedown",expSwB.fnSelDragNode,false);
            }
        }
    },
    
    fnRegDropZones: function() {
        
    },
    
    fnSelDragNode: function(evt) {
        this.evt = expSwB.Evt(arguments[0]);
        expSwB.fnEvtadd(this.evt.source,"mousemove",expSwB.fnSelStartDrag,false);
        expSwB.fnEvtadd(this.evt.source,"mouseup",expSwB.fnCancel,false);
        expSwB.fnEvtadd(this.evt.source,"mouseout",expSwB.fnCancel,false);
        
        this.evt.source.style.background="#00ff00";
        this.evt.source.style.position="absolute";
    },
    
    fnSelStartDrag: function(evt) {
        expSwB.fnMousePosition(evt);
        this.evt = expSwB.Evt(arguments[0]);
        
        this.evt.source.style.background="#00ff00";
        this.evt.source.style.top = mouseY - this.evt.source.offsetHeight/2 + "px";
        this.evt.source.style.left = mouseX - this.evt.source.offsetWidth/2 + "px";
        expSwB.fnCheckDropZone(arguments[0]);
    },
    
    fnCancel: function(evt) {
        this.evt = expSwB.Evt(arguments[0]);
        
        if (true==expSwB.fnCheckDropZone(arguments[0])) {
            var drz = d.getElementById('dropzone');
            var br = d.createElement("br");
            drz.appendChild(evt.source);
            drz.appendChild(br);
            drz.style.background="#ffffff";
            
            expSwB.fnEvtrem(this.evt.source,"mousedown",expSwB.fnSelDragNode,false);
            expSwB.fnEvtrem(this.evt.source,"mouseout",expSwB.fnCancel,false);
            expSwB.fnEvtrem(this.evt.source,"mouseup",expSwB.fnCancel,false);
            expSwB.fnEvtrem(this.evt.source,"mousemove",expSwB.fnSelStartDrag,false);
        }

        this.evt.source.style.position="";
        this.evt.source.style.background="#ffffff";
    },
    
    fnCheckDropZone: function(evt) {
        expSwB.fnMousePosition(arguments[0]);
        this.evt = expSwB.Evt(arguments[0]);
        var drz = d.getElementById('dropzone');
        
        
        if (parseInt(this.evt.source.style.top) > parseInt(drz.style.top)
         && parseInt(this.evt.source.style.top) < (parseInt(drz.style.top)+drz.offsetHeight)
         && parseInt(this.evt.source.style.left) > parseInt(drz.style.left)
         && parseInt(this.evt.source.style.left) < (parseInt(drz.style.left)+drz.offsetWidth)) {
            drz.style.background="#E1E1E1";
            return true;
        } else {
            drz.style.background="#FFFFFF";
            return false;
        }
        return false;
    }
    
});

/*-------------------------------------*/

swB.DivScroll = function() {};
swB.DivScroll.prototype = (new swB.DragAndDrop).extend ({
    
    fnRegisterScrollElements: function() {
        for (var key=0;key < arguments.length; key++) {
            eval('this.'+ arguments[key] +'=document.getElementById(arguments[key])');
        }
        this.jsDrag.pos  = expSwB.fnViewPort(this.jsDrag);
        this.nullPoint = parseInt(this.jsDrag.style.top);
        
    },
    
    fnStartScroll: function(evt) {
        
        expSwB.fnMousePosition(evt);
        this.evt = expSwB.Evt(evt);
        
        expSwB.jsDrag.pos  = expSwB.fnViewPort(expSwB.jsDrag);
        expSwB.jsInner.pos = expSwB.fnViewPort(expSwB.jsInner);
        
        this.dragDiff = 0;
        this.dragDiff  = this.dragDiff > 0 ? this.dragDiff : (this.evt.pos.y - expSwB.jsDrag.pos.y);
        var expos = (mouseY - this.evt.pos.y) - (expSwB.jsDrag.offsetHeight + this.dragDiff);
        
        expSwB.fnEvtadd(d,"mousemove",expSwB.fnScrollLoop,false);
        expSwB.fnEvtadd(d,"mouseup",expSwB.fnEndScroll,false);
        
    },
    
    fnScrollLoop: function(evt) {
        expSwB.fnMousePosition(evt);
        this.evt = expSwB.Evt(evt);
        
        expSwB.jsOuter.pos = expSwB.fnViewPort(expSwB.jsOuter);
        expSwB.jsDrag.pos  = expSwB.fnViewPort(expSwB.jsDrag);
        expSwB.jsInner.pos = expSwB.fnViewPort(expSwB.jsInner);
        
        var expos = (mouseY - this.evt.pos.y) - (expSwB.jsDrag.offsetHeight + expSwB.dragDiff);
        
        if (expSwB.jsDrag.pos.y > expSwB.nullPoint && (expSwB.jsDrag.pos.y - expSwB.jsDrag.offsetHeight) <  parseInt(expSwB.jsSlide.style.top) + expSwB.jsSlide.offsetHeight) {
            expSwB.jsInner.style.top = - (expSwB.jsInner.offsetHeight / (expSwB.jsSlide.offsetHeight - expSwB.jsDrag.offsetHeight)*(mouseY-expSwB.jsOuter.pos.y)) + "px";
            expSwB.jsDrag.style.top  =  mouseY + expos + "px";
        }
        
        
        if ((parseInt(expSwB.jsDrag.style.top) + 1) < parseInt(expSwB.jsSlide.style.top)) {
            expSwB.jsInner.style.top = 0 + "px";
            expSwB.jsDrag.style.top  = parseInt(expSwB.jsSlide.style.top) + 1 + "px";
        }
        
        if ((parseInt(expSwB.jsDrag.style.top) + expSwB.jsDrag.offsetHeight) > (parseInt(expSwB.jsSlide.style.top) + expSwB.jsSlide.offsetHeight)) {
            expSwB.jsInner.style.top = expSwB.jsInner.style.top;
            expSwB.jsDrag.style.top  = (parseInt(expSwB.jsSlide.style.top) + expSwB.jsSlide.offsetHeight - 1) - expSwB.jsDrag.offsetHeight + "px";
        }
        
        if (navigator.appName == 'Microsoft Internet Explorer')
            return false;
        
    },
    
    fnEndScroll: function(evt) {
        this.evt = expSwB.Evt(evt);
        expSwB.fnEvtrem(d,"mousemove",expSwB.fnScrollLoop,false);
        expSwB.fnEvtrem(d,"mouseup",expSwB.fnEndScroll,false);
        expSwB.fnClearScrollBar(evt);
    },
    
    fnClearScrollBar: function(evt) {
        this.evt = expSwB.Evt(evt);
        expSwB.fnEvtrem(d,"mousemove",expSwB.fnScrollLoop,false);
        expSwB.fnEvtrem(d,"mouseup",expSwB.fnEndScroll,false);
    }
});

/*-------------------------------------*/

swB.AjaxEngine = function() {};
swB.AjaxEngine.prototype = (new swB.DivScroll).extend ({
    
    fnAjaxIni: function(evt) {
        this.Events          = new Array;
        this.Events          = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
        this.requestObject   = expSwB.Client(evt);
        this.requestElements = new Array;
        this.requestFile     = null;
        this.queryString     = null;
        this.sendMethod      = 'get';
        this.returnBody      = null;
        this.requestIds      = new Array;
    },
    
    fnRegAjaxElements: function(evt, idname, reqFile) {
        this.fnAjaxIni(evt);
        this.requestElements[idname] = reqFile;
        this.requestIds.push(idname);
        this.requestFile = reqFile;
    },
    
    fnAjaxSendRequest: function(evt, sourceChange) {
        this.evt = expSwB.Evt(evt);
        
        for (var i=0; i < expSwB.requestIds.length; i++) {
            expSwB.requestObject.open(expSwB.sendMethod, expSwB.requestElements[expSwB.requestIds[i]], false);
        }
        expSwB.requestObject.onreadystatechange = expSwB.fnStateChange;
        expSwB.requestObject.send(expSwB.sendMethod == 'post' ? body : null);
        if (parseInt(sourceChange) == 1) {
            this.evt.source.innerHTML = expSwB.requestObject.responseText;
        }
        if (typeof(sourceChange) == 'string') {
            var anySource = d.getElementById(sourceChange);
            anySource.innerHTML = expSwB.requestObject.responseText;
        }
    },
    
    fnStateChange: function() {
        var readyState = expSwB.requestObject.readyState;
        if (expSwB.Events[readyState] == 'Complete') {
            expSwB.requestObject.onreadystatechange = function() {};
        }
    }
    
});

/*-------------------------------------*/

var expSwB = new swB.AjaxEngine;

/*-------------------------------------*/

function getReady(evt) {
    var evsrc = expSwB.Evt(evt);
    evsrc.pos = expSwB.fnViewPort(evsrc.source);
//    evsrc.source.style.position="absolute";
}

var newWin    = new Array();
var winname,where,wwidth,wheight, properties;

function openWindow(winname,where,wwidth,wheight) {
  properties = 'width=' + wwidth + ',height=' + wheight +',resizable=no,location=no,status=no,menubar=no,scrollbars=yes,toolbar=no,directories=no';
  var nwindex;
  nwindex = winname + (newWin.length + 1);
  newWin[nwindex] = window.open('', nwindex, properties);
  newWin[nwindex].location.href=where;
}

function gotoLocation(where) {
    if (where != '') {
        self.location=where;
    }
}

function showUserInfo(para, evt) {
    evt = expSwB.Evt(evt);
    expSwB.fnEvtadd(evt.source,"mouseout",hideUserInfo,false);
    
    objHC = d.getElementById('helpcontainer');
    objHC.style.top=mouseY + 10 + "px";
    objHC.style.left=mouseX + 10 + "px";
    objHC.style.zIndex=5000;
    objHC.style.display="";
    
    objHC.innerHTML=para;
}

function hideUserInfo(evt) {
    evt = expSwB.Evt(evt);
    objHC = d.getElementById('helpcontainer');
    objHC.style.display="none";
}
