


if (!Array.prototype.indexOf) {
 Array.prototype.indexOf = function(text) {
  for (var i = 0; i < this.length; i++) {
   if (this[i] == text) {
    return i;
   }
  }
  return -1;
 }
}

function addEvent(node, event, ref) {
 if (node.addEventListener) {
  return node.addEventListener(event, ref, false);
 }
 else if(node.attachEvent) {
  return node.attachEvent('on'+event, ref);
 }
}

function removeEvent(node, event, ref) {
 if (node.addEventListener) {
  return node.removeEventListener(event, ref, false);
 }
 else if(node.attachEvent) {
  return node.detachEvent('on'+event, ref);
 }
}

function dispatchEvent(node, event)
{

    if (node.dispatchEvent) {
        var evt = node.ownerDocument.createEvent("HTMLEvents");
        evt.initEvent(event, true, true);
        node.dispatchEvent(evt);
    } else if (node.fireEvent) {
        node.fireEvent("on" + event);
    }
}

function preventDefault(e)
{
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

function stopPropagation(e) {
 if (!e) var e = window.event;
 if (e.stopPropagation) {
  e.stopPropagation();
 }
 else {
  e.cancelBubble = true;
 }
}

if (document.getElementsByClassName) {
   getElementsByClassName = function (className, elm, tag) {
        elm = elm || document;
        var elements = elm.getElementsByClassName(className),
            nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
            returnElements = [],
            current;
        for(var i=0, il=elements.length; i<il; i+=1){
            current = elements[i];
            if(!nodeName || nodeName.test(current.nodeName)) {
                returnElements.push(current);
            }
            }
            return returnElements;
    };
} else if (document.evaluate) {
    getElementsByClassName = function (className, elm, tag) {
        tag = tag || "*";
        elm = elm || document;
        var classes = className.split(" "),
        classesToCheck = "",
        xhtmlNamespace = "http://www.w3.org/1999/xhtml",
        namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
        returnElements = [],
        elements,
            node;
        for(var j=0, jl=classes.length; j<jl; j+=1){
            classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
        }
        try {
            elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
        }
        catch (e) {
            elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
        }
        while ((node = elements.iterateNext())) {
            returnElements.push(node);
        }
        return returnElements;
    };
} else {
    getElementsByClassName = function (className, elm, tag) {
        tag = tag || "*";
        elm = elm || document;
        var classes = className.split(" "),
            classesToCheck = [],
            elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
            current,
            returnElements = [],
            match;
        for(var k=0, kl=classes.length; k<kl; k+=1){
            classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
        }
        for(var l=0, ll=elements.length; l<ll; l+=1){
            current = elements[l];
            match = false;
            for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                match = classesToCheck[m].test(current.className);
                if (!match) {
                    break;
                }
            }
            if (match) {
                returnElements.push(current);
            }
        }
        return returnElements;
    };
}















var vwwin_stack = new Array();
var vwwin_list  = new Object();

addEvent(document, 'keydown', vwwin_escape);

function vwwin(name, id) {
    this.underlay = false;
    this.underlayNode = null;
    this.align = null;
    this.name  = name ? name : 'default'; 
    this.id = id;
    this.onclose = null;
    this.escape = true;
    this.parent = null;
	

    this.open  = vwwin_open;
    this.close  = vwwin_close;

    this.relativeNode = null;
    this.relativeId = null;
    this.top = this.left = 0;

    this.mouseIsOver = false; 
  

    if (!id) { 
        var cont = this.cont = document.createElement('div');
        cont.className = 'vw_window';
//        cont.style.zIndex = 101 + vwwin_stack.length;


        var head = this.head = vw_node(cont, "div");
        head.className = 'head';


        var obj = this;
        var a = vw_node(head, 'a', 'Close Window');
        a.className = 'close_window';
        a.href = '#';
        a.onclick = function() {obj.close(); return false;};

        var errors = this.errors = vw_node(cont, 'div');
        errors.style.display = 'none';
        errors.className = 'error_spot';

        var success = this.success = vw_node(cont, 'div');
        success.style.display = 'none';
        success.className = 'success_spot';
 
        var body = this.body = vw_node(cont, "div");
        body.className = 'body';
    }
}


function vwwin_open(node) {
    /* Close Window if already Open 
    */
    var index = vwwin_stack.indexOf(this.name);
    if (index >= 0) {
        vwwin_list[this.name].close();
    }
    
    if (vwwin_stack[vwwin_stack.length - 1]) {
        this.parent = vwwin_list[vwwin_stack[vwwin_stack.length - 1]];
    }
    vwwin_stack.push(this.name);
    vwwin_list[this.name] = this;

    if (this.ref) {
        this.ref.win = this;
    }

    var obj = this;
    if (this.id) {
        this.cont = document.getElementById(this.id);
        this.body = document.getElementById(this.id+'_body');
        this.head = document.getElementById(this.id+'_head');
    }
    else if (this.title) {
        var div = vw_node(this.head, 'div', this.title);
        div.className = 'vw_title';
    }
    
        /* Detect when mouse is over window 
    
    this.cont.onmouseover = function(e) {
        var evt = e ? e : event;
        var relTarget = evt.relatedTarget || evt.fromElement;
        if (!vw_isParent(obj.cont, relTarget)) {       
            obj.mouseIsOver = true; 
        }
    }*/

    /* Detect when mouse exits window 
    
    this.cont.onmouseout = function(e) {
        var evt = e ? e : event;
        var relTarget = evt.relatedTarget || evt.toElement;
        if (!vw_isParent(obj.cont, relTarget)) {       
            obj.mouseIsOver = false; 
            if (obj.ref && obj.ref.mouseout) {
                obj.ref.mouseout(evt);
            }
        }
    }*/


    if (!this.id) { 
        document.body.appendChild(this.cont);
    }

    this.cont.style.display = 'block';

    if (this.ref && this.ref.open) {
        this.ref.open();
    }
    if (this.onopen) {
        this.onopen();
    }

    if (this.relativeId) {
        this.relativeNode = document.getElementById(this.relativeId);
    }

    switch (this.align) {
        case 'parent':
            if (this.parent) {
                this.cont.style.top  = this.parent.cont.offsetTop + 30  + 'px';
                this.cont.style.left = this.parent.cont.offsetLeft + 10 + 'px';
                break;
            }
        case 'center':
            var win = this.cont;
            vwCenter(this.cont);
            window.onresize = function() {vwCenter(win)}
            window.orientationchange = function() {vwCenter(win)}
        break;
        case 'absolute':
            this.cont.style.top   = this.top + 'px';
            this.cont.style.left  = this.left + 'px';
        break;
        case 'relative':
            var pos = getPos(this.relativeNode);
            var left = (pos.left - this.cont.offsetWidth - 20);
            if (left <= 0) {
                this.cont.style.left = (pos.left + 20 + this.relativeNode.offsetWidth) + "px";
            }
            else if(pos.left > 20) {
                this.cont.style.left = left+"px";
            }
            else {
                this.cont.style.left = "0px";
            }
            var top = (pos.top - this.cont.offsetHeight / 2);
            if (top < 25) {
                top = 25;
            }
            this.cont.style.top = (top > 0 ? top : 0) + "px";
        break;
        case 'relativeCenter':
            var pos = getPos(this.relativeNode);
            var left = (pos.left + this.relativeNode.offsetWidth / 2)- this.cont.offsetWidth / 2;

            if (left <= 0) {
                this.cont.style.left = 0
            }
            else {
                this.cont.style.left = left+"px";
            }
            this.cont.style.top = pos.bottom + "px";
        break;
        case 'fillY':
            vwCenter(this.cont);
            var offset = 0;
            if (document.getElementById('vw_toolbar')) {
                offset = document.getElementById('vw_toolbar').offsetHeight;
            }
     
//            this.cont.style.position = 'fixed';
            this.cont.style.top = 30 + offset + 'px';
            this.cont.style.bottom = '30px';
            
            this.body.style.height = this.cont.offsetHeight - this.head.offsetHeight + 'px';
            this.body.style.overflow = 'auto';
        break;
        case 'conter':
            var width   = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
            var height  = window.innerHeight ? window.innerHeight: document.documentElement.clientHeight;
            var scrollY = document.documentElement.scrollTop;

            this.cont.style.top = (height - this.cont.offsetHeight) / 2 + scrollY + 'px';
            this.cont.style.left = (width - this.cont.offsetWidth) / 2 + 'px';
        break;
    }


    if (this.underlay == true) {
        var obj = this;
        var underlay = this.underlayNode = document.createElement('div');
        underlay.className = 'vw_window_underlay';
        underlay.onclick = function() {obj.close()};
        document.body.appendChild(underlay);
    }

    setOpacity(this.cont, 0);
    this.cont.style.visibility = 'visible';
    var t = new vw_transition(this.cont, 'opacity', 1);
    t.engage(300);
}

function vwwin_close() {
    if (this.underlayNode) {
        document.body.removeChild(this.underlayNode);
        this.underlayNode = null;
    }

    if (this.ref && this.ref.close) {
        this.ref.close();
    }

    var index = vwwin_stack.indexOf(this.name);
    if (vwwin_stack.length > index + 1) {
       var child =  vwwin_list[vwwin_stack[index + 1]];
       child.close();
    }
    vwwin_list[this.name] = null;   
    vwwin_stack.splice(index, 1);

    if (this.onclose) {
        this.onclose();
    }
    stop_flash(this.cont);

    var t = new vw_transition(this.cont, 'opacity', 0);
    var cont = this.cont
    if (this.id) {
        t.engage(300, function() 
        {
            cont.style.visibility = 'hidden';
            cont.style.display = 'none';
        });
    }
    else {
        t.engage(300, function() 
        {
            vw_node_delete(cont);
        });
    }
}

function vwwin_escape(e) {
    var evt = e ? e : event;
    if (evt.keyCode == '27' && vwwin_stack.length && !evt.vwHandled && !contextMenu) {
        var name = vwwin_stack[vwwin_stack.length - 1];
        var win  = vwwin_list[name];
        if (win.escape) {
            evt.vwHandled = true;
            win.close();
        }
    }
}

function stop_flash(node) {
    if (typeof(node) === 'undefined') {
        var node = document;
    }
    // If we contain an object, assume it's Flash and send it a STOP event.
    // Checks for sendEvent in case the Flash object isn't a JW player. - ltw
    var objects = node.getElementsByTagName('object');
    for (var i = 0; i < objects.length; i++) {
        if (objects[i] && typeof(objects[i].sendEvent) === 'function') {
            objects[i].sendEvent('STOP');
        }
    }
}

