// -------------------------------------------------------------------
// -------------------------------------------------------------------
var openMyWindow={
imagefiles:['openmywindowfiles/close.gif', 'openmywindowfiles/resize.gif'], //Path to 4 images used by script, in that order

zIndexvalue:100,
tobjects: [], //object to contain references to the window divs, for cleanup purposes
lastactivet: {}, //reference to last active the window
dark: {}, //reference to last active the window

init:function(t){
    var domwindow=document.createElement("div") //create the window div
    domwindow.id=t
    domwindow.className="openMyWindow"
    var domwindowdata=''
    domwindowdata='<div class="drag-handle">'
    domwindowdata+='open my window <div class="drag-controls"><img src="'+this.imagefiles[0]+'" title="Close" /></div>'
    domwindowdata+='</div>'
    domwindowdata+='<div class="drag-contentarea"></div>'
    domwindowdata+='<div class="drag-statusarea"><div class="drag-resizearea" style="background: transparent url('+this.imagefiles[1]+') top right no-repeat;">&nbsp;</div></div>'
    domwindowdata+='</div>'
    domwindow.innerHTML=domwindowdata
    document.getElementById("openMyWindowholder").appendChild(domwindow)
    //this.zIndexvalue=(this.zIndexvalue)? this.zIndexvalue+1 : 100 //z-index value for the window: starts at 0, increments whenever a window has focus
    var t=document.getElementById(t)
    var divs=t.getElementsByTagName("div")
    for (var i=0; i<divs.length; i++){ //go through divs inside the window and extract all those with class="drag-" prefix
        if (/drag-/.test(divs[i].className))
            t[divs[i].className.replace(/drag-/, "")]=divs[i] //take out the "drag-" prefix for shorter access by name
    }
    dark=document.getElementById('darkenOpenMyWindow')
    if (!dark){
        var tbody = document.getElementsByTagName("body")[0]
        var tnode = document.createElement('div')           // Create the layer.
        tnode.style.position='absolute'                     // Position absolutely
        tnode.style.top='0px'                               // In the top
        tnode.style.left='0px'                              // Left corner of the page
        tnode.style.overflow='hidden'                       // Try to avoid making scroll bars
        tnode.style.display='none'                          // Start out Hidden
        tnode.id='darkenOpenMyWindow'                       // Name it so we can find it later
        tbody.appendChild(tnode)                            // Add it to the web page
        dark=document.getElementById('darkenOpenMyWindow')  // Get the object.
    }
    this.dark = dark
    //t.style.zIndex=this.zIndexvalue //set z-index of this the window
    t.handle._parent=t //store back reference to the window
    t.resizearea._parent=t //same
    t.controls._parent=t //same
    t.onclose=function(){return true} //custom event handler "onclose"
    t.onmousedown=function(){openMyWindow.setfocus(this)} //Increase z-index of window when focus is on it
    t.handle.onmousedown=openMyWindow.setupdrag //set up drag behavior when mouse down on handle div
    t.resizearea.onmousedown=openMyWindow.setupdrag //set up drag behavior when mouse down on resize div
    t.controls.onclick=openMyWindow.enablecontrols
    t.show=function(){openMyWindow.show(this)} //public function for showing the window
    t.hide=function(){openMyWindow.hide(this)} //public function for hiding the window
    t.close=function(){openMyWindow.close(this)} //public function for closing the window (also empties the window content)
    t.setSize=function(w, h){openMyWindow.setSize(this, w, h)} //public function for setting window dimensions
    t.moveTo=function(x, y){openMyWindow.moveTo(this, x, y)} //public function for moving the window (relative to viewpoint)
    t.isResize=function(bol){openMyWindow.isResize(this, bol)} //public function for specifying if window is resizable
    t.isScrolling=function(bol){openMyWindow.isScrolling(this, bol)} //public function for specifying if window content contains scrollbars
    t.load=function(contenttype, contentsource, title){openMyWindow.load(this, contenttype, contentsource, title)} //public function for loading content into window
    this.tobjects[this.tobjects.length]=t
    return t //return reference to the window div
},

open:function(t, contenttype, contentsource, title, attr, recalonload){
    var d=openMyWindow //reference the window object
    function getValue(Name){
        var config=new RegExp(Name+"=([^,]+)", "i") //get name/value config pair (ie: width=400px,)
        return (config.test(attr))? parseInt(RegExp.$1) : 0 //return value portion (int), or 0 (false) if none found
    }
    if (document.getElementById(t)==null) //if window doesn't exist yet, create it
        t=this.init(t) //return reference to the window div
    else
        t=document.getElementById(t)
    openMyWindow.grayOut(true)
    this.setfocus(t)
    t.setSize(getValue(("width")), (getValue("height"))) //Set dimensions of window
    var xpos=getValue("center")? "middle" : getValue("left") //Get x coord of window
    var ypos=getValue("center")? "middle" : getValue("top") //Get y coord of window
    //t.moveTo(xpos, ypos) //Position window
    if (typeof recalonload!="undefined" && recalonload=="recal" && this.scroll_top==0){ //reposition window when page fully loads with updated window viewpoints?
        if (window.attachEvent && !window.opera) //In IE, add another 400 milisecs on page load (viewpoint properties may return 0 b4 then)
            this.addEvent(window, function(){setTimeout(function(){t.moveTo(xpos, ypos)}, 400)}, "load")
        else
            this.addEvent(window, function(){t.moveTo(xpos, ypos)}, "load")
    }
    t.isResize(getValue("resize")) //Set whether window is resizable
    t.isScrolling(getValue("scrolling")) //Set whether window should contain scrollbars
    t.style.visibility="visible"
    t.style.display="block"
    t.contentarea.style.display="block"
    t.moveTo(xpos, ypos) //Position window
    t.load(contenttype, contentsource, title)
    return t
},
//set window size (min is 150px wide by 100px tall)
setSize:function(t, w, h){
    t.style.width=Math.max(parseInt(w), 150)+"px"
    t.contentarea.style.height=Math.max(parseInt(h), 100)+"px"
},
//move window. Position includes current viewpoint of document
moveTo:function(t, x, y){
    this.getviewpoint() //Get current viewpoint numbers
    t.style.left=(x=="middle")? this.scroll_left+(this.docwidth-t.offsetWidth)/2+"px" : this.scroll_left+parseInt(x)+"px"
    t.style.top=(y=="middle")? this.scroll_top+(this.docheight-t.offsetHeight)/2+"px" : this.scroll_top+parseInt(y)+"px"
},
//show or hide resize interface (part of the status bar)
isResize:function(t, bol){
    t.statusarea.style.display=(bol)? "block" : "none"
    t.resizeBool=(bol)? 1 : 0
},
//set whether loaded content contains scrollbars
isScrolling:function(t, bol){
    t.contentarea.style.overflow=(bol)? "auto" : "hidden"
},
//loads content into window plus set its title (content types: "inline", "iframe")
load:function(t, contenttype, contentsource, title){
    if (t.isClosed){
        alert("The window has been closed, so no window to load contents into. Open/Create the window again.")
        return
    }
    var contenttype=contenttype.toLowerCase()
    if (typeof title!="undefined") {
        t.handle.firstChild.nodeValue=title
    }
    if (contenttype=="inline")
        t.contentarea.innerHTML=contentsource
    else if (contenttype=="iframe"){
        t.contentarea.style.overflow="hidden" //disable window scrollbars, as iframe already contains scrollbars
        if (!t.contentarea.firstChild || t.contentarea.firstChild.tagName!="IFRAME") //If iframe tag doesn't exist already, create it first
            t.contentarea.innerHTML='<iframe src="" style="margin:0; padding:0; width:100%; height: 100%" name="_iframe-'+t.id+'"></iframe>'
        window.frames["_iframe-"+t.id].location.replace(contentsource) //set location of iframe window to specified URL
    }
    t.contentarea.datatype=contenttype //store contenttype of current window for future reference
},

setupdrag:function(e){
    var d=openMyWindow //reference the window object
    var t=this._parent //reference the window div
    d.etarget=this //remember div mouse is currently held down on ("handle" or "resize" div)
    var e=window.event || e
    d.initmousex=e.clientX //store x position of mouse onmousedown
    d.initmousey=e.clientY
    d.initx=parseInt(t.offsetLeft) //store offset x of window div onmousedown
    d.inity=parseInt(t.offsetTop)
    d.width=parseInt(t.offsetWidth) //store width of window div
    d.contentheight=parseInt(t.contentarea.offsetHeight) //store height of window div's content div
    if (t.contentarea.datatype=="iframe"){ //if content of this window div is "iframe"
        t.style.backgroundColor="#F8F8F8" //colorize and hide content div (while window is being dragged)
        t.contentarea.style.visibility="hidden"
    }
    document.onmousemove=d.getdistance //get distance travelled by mouse as it moves
    document.onmouseup=function(){
        if (t.contentarea.datatype=="iframe"){ //restore color and visibility of content div onmouseup
            t.contentarea.style.backgroundColor="white"
            t.contentarea.style.visibility="visible"
        }
        d.stop()
    }
    return false
},

getdistance:function(e){
    var d=openMyWindow
    var etarget=d.etarget
    var e=window.event || e
    d.distancex=e.clientX-d.initmousex //horizontal distance travelled relative to starting point
    d.distancey=e.clientY-d.initmousey
    if (etarget.className=="drag-handle") //if target element is "handle" div
        d.move(etarget._parent, e)
    else if (etarget.className=="drag-resizearea") //if target element is "resize" div
        d.resize(etarget._parent, e)
    return false //cancel default dragging behavior
},
//get window viewpoint numbers
getviewpoint:function(){
    var ie=document.all && !window.opera
    var domclientWidth=document.documentElement && parseInt(document.documentElement.clientWidth) || 100000 //Preliminary doc width in non IE browsers
    this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
    this.scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
    this.scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
    this.docwidth=(ie)? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(domclientWidth, window.innerWidth-16)
    this.docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
},

move:function(t, e){
    t.style.left=openMyWindow.distancex+openMyWindow.initx+"px"
    t.style.top=openMyWindow.distancey+openMyWindow.inity+"px"
},

resize:function(t, e){
    t.style.width=Math.max(openMyWindow.width+openMyWindow.distancex, 150)+"px"
    t.contentarea.style.height=Math.max(openMyWindow.contentheight+openMyWindow.distancey, 100)+"px"
},

enablecontrols:function(e){
    var d=openMyWindow
    var sourceobj=window.event? window.event.srcElement : e.target //Get element within "handle" div mouse is currently on (the controls)
    if (/Close/i.test(sourceobj.getAttribute("title"))) //if this is the "close" control
        d.close(this._parent)
    return false
},

close:function(t){
    try{
        var closewinbol=t.onclose()
    }
    catch(err){ //In non IE browsers, all errors are caught, so just run the below
        var closewinbol=true
    }
    finally{ //In IE, not all errors are caught, so check if variable isn't defined in IE in those cases
        if (typeof closewinbol=="undefined"){
            alert("An error has occured somwhere inside your \"onclose\" event handler")
            var closewinbol=true
        }
    }
    if (closewinbol){ //if custom event handler function returns true
        if (window.frames["_iframe-"+t.id]) //if this is an IFRAME the window
            window.frames["_iframe-"+t.id].location.replace("about:blank")
        else
            t.contentarea.innerHTML=""
        t.style.display="none"
        t.isClosed=true //tell script this window is closed (for detection in t.show())
        openMyWindow.grayOut(false)
    }
    return closewinbol
},
//Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
setopacity:function(targetobject, value){
    if (!targetobject)
        return
    if (targetobject.filters && targetobject.filters[0]){ //IE syntax
        if (typeof targetobject.filters[0].opacity=="number") //IE6
            targetobject.filters[0].opacity=value*100
        else //IE 5.5
            targetobject.style.filter="alpha(opacity="+value*100+")"
    }
    else if (typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
        targetobject.style.MozOpacity=value
    else if (typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
        targetobject.style.opacity=value
},
//Sets focus to the currently active window
setfocus:function(t){
    this.zIndexvalue++
    t.style.zIndex=this.zIndexvalue
    t.isClosed=false //tell script this window isn't closed (for detection in t.show())
    this.setopacity(this.lastactivet.handle, 0.5) //unfocus last active window
    this.setopacity(t.handle, 1) //focus currently active window
    this.lastactivet=t //remember last active window
},

show:function(t){
    if (t.isClosed){
        alert("The window has been closed, so nothing to show. Open/Create the window again.")
        return
    }
    t.style.display="block"
    this.setfocus(t)
    t.state="fullview" //indicate the state of the window as being "fullview"
},

hide:function(t){
    t.style.display="none"
},

stop:function(){
    openMyWindow.etarget=null //clean up
    document.onmousemove=null
    document.onmouseup=null
},
//assign a function to execute to an event handler (ie: onunload)
addEvent:function(target, functionref, tasktype){
    var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
    if (target.addEventListener)
        target.addEventListener(tasktype, functionref, false)
    else if (target.attachEvent)
        target.attachEvent(tasktype, functionref)
},

cleanup:function(){
    for (var i=0; i<openMyWindow.tobjects.length; i++){
        openMyWindow.tobjects[i].handle._parent=openMyWindow.tobjects[i].resizearea._parent=openMyWindow.tobjects[i].controls._parent=null
    }
    window.onload=null
},

grayOut:function(bol){
    if (bol){
        var zindex = 50
        var opacity = 70
        var opaque = (opacity / 100)
        var bgcolor = '#000000'
        // Calculate the page width and height
        if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
            var pageWidth = document.body.scrollWidth+'px'
            var pageHeight = document.body.scrollHeight+'px'
        } else if( document.body.offsetWidth ) {
            var pageWidth = document.body.offsetWidth+'px'
            var pageHeight = document.body.offsetHeight+'px'
        } else {
            var pageWidth='100%'
            var pageHeight='100%'
        }

        //set the shader to cover the entire page and make it visible.
        this.dark.style.opacity=opaque
        this.dark.style.MozOpacity=opaque
        this.dark.style.filter='alpha(opacity='+opacity+')'
        this.dark.style.zIndex=zindex
        this.dark.style.backgroundColor=bgcolor
        this.dark.style.width=pageWidth
        this.dark.style.height=pageHeight
        this.dark.style.overflow ='hidden'
        this.dark.style.display='block'
        //document.body.style.overflow =  'hidden';
    } else {
        this.dark.style.display='none'
    }
}



} //End openMyWindow object
//container that holds all the window divs on page
document.write('<div id="openMyWindowholder"><span style="display:none">.</span></div>')
window.onunload=openMyWindow.cleanup
