function WWin(file, content, styles, drag, startPos, startDim, title) {

    this.ready = false;
    this.showed = false;
    this.drag = drag;
    this.anim = startPos!=null;
    this.xml = (file!=null && file!="");

    this.startPos=startPos;
    this.startDim=startDim;
    this.endPos=new Array();

    this.ifrm=null;
    this.titleEl=null;
    this.title=title;
    this.loadedF=this.changeF=this.closeF=null;

    this.win = document.createElement("div");
    this.contEl = document.createElement("div");
    Element.setUnVisible(this.contEl);
    this.lastR=1;

    WWinMan.setActive(this);

    if (styles!=null) {
        for(var key in styles) {
            if (key=="styleClass")
                this.win.className = styles[key];
            else if (key=="id")
                this.win.id = styles[key];
            else if (startPos!=null && key.match(/(top|left|right|bottom)/gi)!=null)
                this.endPos[key]=styles[key];
            else
                Element.setStyle(this.win, key, styles[key]);
        };
    };
    if (this.xml) {
        XMLlib.foreignMainNode = this.win;
        XMLlib.loadFunc = function() { WWinMan.setLoaded(); };;
        XMLlib.loadXML(file);
    }
    else {
        this.makeContent();
        if (content!=null)
            this.contEl.appendChild(content); //.innerHTML
        this.loaded();
    };

    return this;
};
WWin.prototype.loaded=function() {
    Browser.getDocBody().appendChild(this.win);

    if (this.endPos) {
        var ePos = this.endPos;
        if (this.endPos['top']!=null && this.endPos['left']!=null)
            this.endPos=[parseInt(ePos['left']),parseInt(ePos['top'])];
        else if (this.endPos['right']!=null && this.endPos['bottom']!=null) {
            this.endPos=[parseInt(ePos['right'])-this.win.offsetWidth,parseInt(ePos['bottom'])-this.win.offsetHeight];
        };
    };
    //this.startPos = Pos.correctCoords(this.startPos, Pos.getDim(this.win), true);
    //this.endPos = Pos.correctCoords(this.endPos, Pos.getDim(this.win), true);
    Pos.setCoords(this.win, this.startPos);

    if (this.anim)
        WWinMan.animate();
/*
    else
        this.makeContent();
*/
    if (Browser.isIE6())
        this.makeIframe();

    this.ready=true;
    this.showed=true;

};
WWin.prototype.getDragEl=function() {
    //return this.title!=""?this.titleEl:this.win;
    return this.win;
};
WWin.prototype.move=function(e) {
    var el = EventTools.getElement(e, "wwin");
    if (el!=null) {
        WWinMan.setActive(el.wwin);
        if (el.wwin.drag) {
            DragDrop.init(el.wwin.getDragEl(),e,false, el.wwin.win);
        };
    };
    return EventTools.stopEvent(e);
};
WWin.prototype.makeContent=function(content) {

    if (this.xml)
        XMLlib.copyNodes(null, this.contEl);

    this.win.appendChild(this.contEl);
    if (this.drag) {
        this.getDragEl().wwin = this;
        EventTools.setEvent( this.getDragEl(), "mousedown", this.move);
        DragDrop.setDragable(this.getDragEl());
    };

    Element.setVisible(this.contEl);
    if (!this.anim) {
        this.makeTitle();
        this.runOnLoad();
    };
};
WWin.prototype.setContent=function(content) {
    this.win.innerHTML = content;
};
WWin.prototype.addContent=function(content) {
    this.win.innerHTML += content;
};
WWin.prototype.makeTitle=function() {
    this.titleEl=document.createElement("div");
    var cA = document.createElement("a");
    cA.className="close";
    cA.innerHTML = "Zavřít";
    cA.href="javascript://nop/";
    cA.wwin=this;
    Element.setStyles(cA,{position:"absolute",zIndex:5});
    Pos.setCoords(cA, {right:"0",bottom:"0"});
    this.titleEl.appendChild(cA);
    this.win.appendChild(this.titleEl);
    Element.setOpacity(cA,70);
    EventTools.setEvent(cA,"click", this.closeWin);
};
WWin.prototype.closeWin=function(e) {
    var el = EventTools.getElement(e, "wwin");
    if (el!=null)
        el.wwin.close();

    return EventTools.stopEvent(e);
};
WWin.prototype.close=function() {
    if (this.anim) {
        Element.setUnVisible(this.contEl);
        Element.setStyle(this.titleEl,"display","none");
        WWinMan.closeWin(this);
    }
    else
        this.closeIt(this);

};
WWin.prototype.closeIt=function(wwin) {
    if (this.closeF!=null)
        this.closeF();
        
    Browser.getDocBody().removeChild(wwin.win);
    if (Browser.isIE6()) {
        Browser.getDocBody().removeChild(wwin.ifrm);
        window.clearInterval(this.timeOut);
    };
    WWinMan.count--;
};
WWin.prototype.showWin=function() {
    var s = (this.showed) ? "hidden" : "visible";
    Element.setStyle(this.win,"visibility",s);
    if (this.ifrm!=null) {
        Element.setStyles(this.ifrm,"visibility",s);
    };
    this.showed = !this.showed;
};
WWin.prototype.getRootNode=function() {
    return this.win;
};
WWin.prototype.makeIframe=function() {
    this.ifrm = document.createElement("iframe");
    Element.setStyles(this.ifrm,{position:"absolute",zIndex:WWinMan.zIndex-2});
    Pos.setCoords(this.ifrm, [this.win.offsetLeft,this.win.offsetTop]);
    Pos.setDim(this.ifrm, [this.win.offsetWidth,this.win.offsetHeight]);

    Browser.getDocBody().appendChild(this.ifrm);
    this.timeOut = self.setInterval("WWinMan.moveIframe()", 10);
};
WWin.prototype.moveIframe=function() {
    Pos.setCoords(this.ifrm, [this.win.offsetLeft,this.win.offsetTop]);
    Pos.setDim(this.ifrm, [this.win.offsetWidth,this.win.offsetHeight]);
};
WWin.prototype.runOnLoad=function() {
    if (this.loadedF!=null)
        this.loadedF();
};
WWin.prototype.runOnChange=function(param) {
    if (this.changeF!=null)
        this.changeF(param);
};
WWin.prototype.onLoad=function(fce) {
    this.loadedF=fce;
};
WWin.prototype.onClose=function(fce) {
    this.closeF = fce;
};
WWin.prototype.onChange=function(fce) {
    this.changeF = fce;
};

var DragDrop = {
    obj:null,
    dragObj:null,
    mpos: new Array(),
    opos: new Array(),
    init:function(obj,event,out,dragObj) {
        DragDrop.obj = obj;
        if (DragDrop.obj==null)
            return false;

        DragDrop.dragObj = (dragObj==null) ? obj : dragObj;
        DragDrop.opos = Pos.findPos(DragDrop.obj);
        if (out && DragDrop.obj.style.position!="absolute") {

            var div=document.createElement("div");
            Pos.setDim(div, [DragDrop.obj.offsetHeight,DragDrop.obj.offsetHeight] );
            div.style.background="red";

            DragDrop.obj.parentNode.replaceChild(div, DragDrop.obj);

            DragDrop.obj.style.position="absolute";
            Pos.setCoords(DragDrop.obj, DragDrop.opos);

            document.getElementsByTagName("body")[0].style.position ="relative";
            document.getElementsByTagName("body")[0].appendChild(DragDrop.obj);
        };

        DragDrop.mpos = EventTools.getEventPos(event);
        EventTools.setEvent(DragDrop.obj, "mousemove", DragDrop.mmove);
        EventTools.setEvent(DragDrop.obj, "mouseup", DragDrop.release);
        EventTools.setEvent(DragDrop.obj, "mouseout", DragDrop.release);
        DragDrop.setDragable(DragDrop.obj);
        return EventTools.stopEvent(event);
    },
    mmove:function(event) {
        DragDrop.move(EventTools.getEventPos(event));
        return EventTools.stopEvent(event);
    },
    release:function(event) {
        EventTools.removeEvent(DragDrop.obj, "mousemove", DragDrop.mmove);
        EventTools.removeEvent(DragDrop.obj, "mouseup", DragDrop.release);
        EventTools.removeEvent(DragDrop.obj, "mouseout", DragDrop.release);
        DragDrop.obj=null;
    },
    move:function(newpos) {
        Pos.setCoords(DragDrop.dragObj, [DragDrop.opos[0]+newpos[0]-DragDrop.mpos[0],DragDrop.opos[1]+newpos[1]-DragDrop.mpos[1]]);
    },
    setDragable:function(obj) {
        obj.dragable=true;
        EventTools.setEvent(obj, "mouseover", DragDrop.showDragCur);
        EventTools.setEvent(obj, "mouseout", DragDrop.showDragCur);
    },
    showDragCur:function(e) {
        var el = EventTools.getElement(e, "dragable");
        if (el!=null) {
            el.style.cursor= el.dragable ? "move" : "default";
        };
        return EventTools.stopEvent(e);
    }
};

var EventTools = {
    setEvent: function(el, action, fce) {
        if (window.attachEvent)
            el.attachEvent("on"+action, fce);
        else
            el.addEventListener(action, fce, true);
    },
    removeEvent: function(el, action, fce) {
        if (window.detachEvent)
            el.detachEvent("on"+action, fce);
        else
            el.removeEventListener(action, fce, true);
    },
    stopEvent: function(event) {
        if (window.event)
            event = window.event;
        if (event.stopPropagation) {
            event.stopPropagation();
            event.preventDefault();
        }
        else
            event.cancelBubble = true;

        return false;
    },
    getKeyCode: function(event) {
        var code;
        if(event.keyCode)
            code = event.keyCode;
        else if(event.which)
            code = event.which;

        return code;
    },
    getElement: function(event, prop) {
        var el=null;
        if (window.event) {
            var elem = window.event.srcElement;
            do {
                if (elem[prop]!=null)
                    el=elem;
                else
                    elem = elem.parentNode;
            } while (!(elem.nodeName=="BODY" || el!=null));
        }
        else if (event!=null)
            el = event.currentTarget;

        return el;
    },
    getEventPos: function(event) {
        if (window.event)
            event = window.event;
        return [parseInt(event.clientX),parseInt(event.clientY)];
    },
    getAbsEventPos: function(event) {
        if (window.event) {
            e = window.event;
            return [parseInt(document.body.scrollLeft+e.clientX),parseInt(document.body.scrollTop+e.clientY)]
        }
        else
            return [parseInt(event.screenX),parseInt(event.screenY)];
    },
    getMouseButton: function(event) {
        return event.button;
    },
    isAltKey: function(e) {
        if (window.event)
            e = window.event;

        return e.altKey;
    },
    isCtrlKey: function(e) {
        if (window.event)
            e = window.event;

        return e.ctrlKey;
    },
    isShiftKey: function(e) {
        if (window.event)
            e = window.event;

        return e.shiftKey;
    }
};
var Browser = {
    bType: null,
    ie6: false,
    ie7: false,
    gecko: false,
    opera: false,
    init: function() {
        var agent = navigator.userAgent;
        if (agent.match(/opera/gi)!=null) {
            this.bType="Opera";
            this.opera=true;
        }
        else if (agent.match(/MSIE/gi)!=null && document.getElementById && document.childNodes && !document.addEventListener) {
            this.bType="IE";
            this.ie6 = (agent.indexOf("MSIE 6.0")!=-1);
            this.ie7 = (agent.indexOf("MSIE 7.0")!=-1);
        }
        else if (agent.match(/gecko/gi)!=null && agent.indexOf("netscape")==-1) {
            this.bType="Gecko";
            this.gecko=true;
        };
        return this.bType;
    },
    isIE: function() {
        if (this.bType==null) this.init();
        return this.bType=="IE";
    },
    isIE6: function() {
        if (this.bType==null) this.init();
        return this.ie6;
    },
    isIE7: function() {
        if (this.bType==null) this.init();
        return this.ie7;
    },
    isGecko: function() {
        if (this.bType==null) this.init();
        return this.gecko;
    },
    isOpera: function() {
        if (this.bType==null) this.init();
        return this.opera;
    },
    getType: function() {
        if (this.bType==null) this.init();
        return this.bType;
    },
    getDocBody:function() {
        return document.getElementsByTagName("body")[0];
        //return document.compatMode && document.compatMode != "BackCompat" ? document.documentElement : document.body;
    }
};
var Pos = {
    pageWidth:0,
    pageHeight:0,
    pageOffsetWidth:0,
    pageOffsetHeight:0,
    init:function() {
        Pos.pageWidth = Browser.isIE() ? document.documentElement.clientWidth : self.innerWidth;
        Pos.pageHeight = Browser.isIE() ? document.documentElement.clientHeight : self.innerHeight;
        Pos.scrollLeft = Browser.isIE() ? document.documentElement.scrollLeft : window.pageXOffset;
        Pos.scrollTop = Browser.isIE() ? document.documentElement.scrollTop : window.pageYOffset;
        if (window.innerHeight && window.scrollMaxY) {
            Pos.pageOffsetWidth = window.innerWidth + window.scrollMaxX;
            Pos.pageOffsetHeight = window.innerHeight + window.scrollMaxY;
        }
        else if(document.body.scrollHeight>document.body.offsetHeight) {
            Pos.pageOffsetWidth = document.body.scrollWidth;
            Pos.pageOffsetHeight = document.body.scrollHeight;
        }
        else {
            Pos.pageOffsetWidth = document.body.offsetWidth+document.body.offsetLeft;
            Pos.pageOffsetHeight = document.body.offsetHeight+document.body.offsetTop;
        };
    },
    findPos: function(obj) {
        var curleft=0,curtop=0;
        if (obj.offsetParent) {
            curleft=obj.offsetLeft;
            curtop=obj.offsetTop;
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            };
        };
        return [parseInt(curleft),parseInt(curtop)];
    },
    getMaxDim:function(full) {
        Pos.init();
        if (full)
            return [Pos.pageOffsetWidth,Pos.pageOffsetHeight];
        else
            return [Pos.pageWidth,Pos.pageHeight];
    },
    setCoords:function(el,coords) {
        var akey;
        for(var key in coords) {
            if (key==0) akey="left";
            else if (key==1) akey="top";
            else akey=key;
            try {
                el.style[akey]=parseInt(coords[key])+"px";
            } catch(ex) { };
        };
    },
    setDim:function(el,dim) {
        Element.setStyles(el,{width:dim[0]+"px",height:dim[1]+"px"});
    },
    getDim:function(el) {
        return [el.offsetWidth,el.offsetHeight];
    },
    correctCoords:function(coords, dim, center) {
        Pos.init();
        var bdim = [Pos.pageWidth,Pos.pageHeight],sc=[Pos.scrollLeft,Pos.scrollTop];
        for (var i in coords) {
            if (coords[i]<sc[i])
                coords[i] = parseInt(center ? sc[i]+(bdim[i]-dim[i])/2 : sc[i]+5);
            else if (coords[i]+dim[i]>sc[i]+bdim[i])
                coords[i] = parseInt(center ? sc[i]+(bdim[i]-dim[i])/2 :bdim[i]+sc[i]-dim[i]-5);
        };
        return coords;
    },
    getEl:function(id) {
        return document.getElementById(id);
    },
    focusById:function(id) {
        var el = Pos.getEl(id);
        if (el!=null)
            Pos.focus(el);
    },
    focus:function(el) {
        el.focus();
    },
    sameHeight:function(elems){
        var max=-1;
        for(var i=0;i<elems.length;i++) {
            var h=parseInt(elems[i].offsetHeight);
            if (h>max)
                max=h;
        };
        for(var i=0;i<elems.length;i++)
            Element.setStyle(elems[i],"height",max+"px");
    }
};
var Element={
    doc:document,
    getEl:function(id) {
        return this.doc.getElementById(id);
    },
    setStyles:function(elem, styles) {
        for(var key in styles) {
            try {
                elem.style[key] = styles[key];
            } catch(ex) { };
        };
    },
    setStyle:function(elem, key,style) {
        try {
            elem.style[key]=style;
        } catch(ex) { };
    },
    setOpacity:function(elem, opacity) {
        elem.style.opacity=opacity/100;
        elem.style.filter="alpha(opacity="+opacity+")";
    },
    setVisible:function(elem) {
        Element.setStyle(elem,"visibility","visible");
    },
    setUnVisible:function(elem) {
        Element.setStyle(elem,"visibility","hidden");
    },
    fadeOut:function(elem) {
        Element.setOpacity(elem,0);
       self.setTimeout(function() { Element.fade(elem, 10); }, 50)

    },
    fade:function(elem, op) {
        Element.setOpacity(elem, op);
        if (op<100)
            self.setTimeout(function() { Element.fade(elem, op+10); }, 50)
    }
};

var WWinMan={
    zIndex:10000,
    activeW:null,
    aniData:new Array(),
    aniSpeed:0.3,
    count:0,
    getZIndex:function() {
        return WWinMan.zIndex;
    },
    createWin:function(file, content, styles, drag, startPos, startDim, title) {
        var w = new WWin(file, content, styles, drag, startPos, startDim, title);
        WWinMan.setActive(w);
        WWinMan.count++;
        return w;
    },
    setActive:function(wwin) {
        if (WWinMan.activeW!=null)
            Element.setStyles(WWinMan.activeW.win, {zIndex:WWinMan.zIndex-1});
        Element.setStyles(wwin.win, {zIndex:WWinMan.zIndex});
        WWinMan.activeW=wwin;
        return wwin;
    },
    setLoaded:function() {
        if (WWinMan.activeW!=null && WWinMan.activeW instanceof WWin)
            WWinMan.activeW.loaded();
    },
    moveIframe:function() {
        if (WWinMan.activeW!=null && WWinMan.activeW instanceof WWin)
            WWinMan.activeW.moveIframe();
    },
    animate:function() {
        var win=WWinMan.activeW;
        var dx=Math.abs(win.startPos[0]-win.endPos[0]),dy=Math.abs(win.startPos[1]-win.endPos[1]);
        var z=Math.sqrt(dx*dx+dy*dy);
        var startDim = (win.startDim!=null) ? win.startDim : [1,1];
        var aD = {
            dir: new Array((win.endPos[0]-win.startPos[0]>0 ? 1 : -1), (win.endPos[1]-win.startPos[1]>0 ? 1 : -1)),
            z:z,sin:dy/z,cos:dx/z,add:7,step:1,r:0,endPos:win.endPos,
            dim:[parseInt(win.win.offsetWidth),parseInt(win.win.offsetHeight)]
        };;
        Pos.setCoords(win.win, win.startPos);
        Pos.setDim(win.win, startDim);
        self.setTimeout(function() { WWinMan.animove(win, aD); }, 50)
    },
    animove:function(win, aD) {
        var r = (aD.z - aD.r) * WWinMan.aniSpeed;
        if (Math.abs(r)<WWinMan.aniSpeed)
            r = (aD.z - aD.r);

        if (Math.abs(r) > 0)
            aD.r += r;

        var dx = aD.cos*aD.r,dy=aD.sin*aD.r,z = Math.sqrt( dx*dx + dy*dy ),dimP=z/aD.z,x,y;
        if (z>=(aD.z-5)) {
            win.lastR = r;
            Pos.setDim(win.win, aD.dim);
            x = win.endPos[0];
            y = win.endPos[1];
            win.makeTitle();
            win.runOnLoad();
        }
        else {
            x = Math.round(dx*aD['dir'][0] + win.startPos[0]);
            y = Math.round(dy*aD['dir'][1] + win.startPos[1]);
            Pos.setDim(win.win, [dimP*aD.dim[0],dimP*aD.dim[1]])
            win.runOnChange(1-(r/aD.z));
            self.setTimeout( function() { WWinMan.animove(win, aD); }, 50);
        };
        Pos.setCoords(win.win, [x,y]);
    },
    
    closeWin:function(win) {
        WWinMan.setActive(win);
        var winPos = Pos.findPos(win.win);
        var dx=Math.abs(win.startPos[0]-winPos[0]),dy=Math.abs(win.startPos[1]-winPos[1]);
        var z=Math.sqrt(dx*dx+dy*dy);
        var aD = {
            dir: new Array((winPos[0]-win.startPos[0]>0 ? 1 : -1), (winPos[1]-win.startPos[1]>0 ? 1 : -1)),
            z:z,sin:dy/z,cos:dx/z,add:7,step:1,r:win.lastR,winPos:win.endPos,
            dim:[parseInt(win.win.offsetWidth),parseInt(win.win.offsetHeight)]
        };;
        self.setTimeout(function() { WWinMan.animoveback(win, aD); }, 50)
    },

    animoveback:function(win, aD) {
        var r = (aD.r * 1/WWinMan.aniSpeed);
        if (Math.abs(r)<(1/WWinMan.aniSpeed))
            r = aD.r;
        if (Math.abs(r) > 0)
            aD.r += r;
        var dx = aD.cos*(aD.z - aD.r),dy=aD.sin*(aD.z - aD.r);
        var z = Math.sqrt( dx*dx + dy*dy ),dimP=z/aD.z,x,y;
        if (z<(aD.r * 1/WWinMan.aniSpeed)) {
            win.closeIt(win);
        }
        else {
            x = Math.round(dx*aD['dir'][0] + win.startPos[0]);
            y = Math.round(dy*aD['dir'][1] + win.startPos[1]);
            Pos.setDim(win.win, [parseInt(dimP*aD.dim[0]),parseInt(dimP*aD.dim[1])]);
            win.runOnChange((z/aD.z));
            self.setTimeout( function() { WWinMan.animoveback(win, aD); }, 50);
        };
        Pos.setCoords(win.win, [x,y]);
    }
};

