﻿
var dhtmlWin = Class.create({
    
    initialize: function(linkID, centered, right_align){     
        this.classLink = $$(linkID) || false;
        this.elLink = $(linkID) || false;
        // console.log("class link: " + this.classLink);
        // console.log("id link: " + this.elLink);
        this.centered = (centered) ? centered : false;
        this.right_align = (right_align) ? right_align : false;
        this.elDhtmlWin = null;
        this.elCloseLink = null;
        
        if (this.classLink[0]) {
            for (var i=0; i < this.classLink.length; i++) {
                var linkURL = this.classLink[i].href;
                var startSlice = linkURL.indexOf("#") + 1;
                var winID = linkURL.slice(startSlice);

                if (this.classLink[i].hasClassName("dhtml_win_link")) {
                    this.elDhtmlWin = this.elLink.next("div.dhtml_window");
                } else {
                    this.elDhtmlWin = $(winID) || false;
                }

                if (this.elDhtmlWin) {
                    this.elCloseLink = this.elDhtmlWin.down("div.close a");
                    this.elCloseLink.observe('click', this.__closeHandler.bindAsEventListener(this));
                    this.classLink[i].observe('click', this.__clickHandler.bindAsEventListener(this));
                }
            }
            
        } else if (this.elLink) {
            
            var linkURL = this.elLink.href;
            var startSlice = linkURL.indexOf("#") + 1;
            var winID = linkURL.slice(startSlice);
            
            if (this.elLink.hasClassName("dhtml_win_link")) {
                this.elDhtmlWin = this.elLink.next("div.dhtml_window");
            } else {
                this.elDhtmlWin = $(winID) || false;
            }
            
            if (this.elDhtmlWin) {
                this.elCloseLink = this.elDhtmlWin.down("div.close a");
                this.elCloseLink.observe('click', this.__closeHandler.bindAsEventListener(this));
                this.elLink.observe('click', this.__clickHandler.bindAsEventListener(this));
            }
        }
        function getLinkProps() {
            }
    },
    
    __clickHandler: function(e) {
        e.stop();
        console.log('clicked');
        if (this.centered) {
            this.positionWin();
        } else if (this.right_align) {
            this.rightPositionWin(e);
        } else {
            var leftPos = e.element().positionedOffset().left + 40;
            var topPos = e.pointerY();
            this.elDhtmlWin.setStyle({
                top: topPos+"px",
                left: leftPos+"px"
            });
        }
        if (Prototype.Browser.IE6) {
            $$("select").invoke('hide');
        }
        this.elDhtmlWin.appear({ duration: 0.5 });
    },
    
    __closeHandler: function(e) {
        this.elDhtmlWin.fade({
            duration: 0.5,
            afterUpdate: function() {
                if (Prototype.Browser.IE6) {
                    $$("select").invoke('show');
                }
            }
        });
    },
    
    positionWin: function(e) {
        if (this.elDhtmlWin.getHeight() >= document.viewport.getHeight()) {
            var topPos = document.viewport.getScrollOffsets().top;
        } else {
            var topPos = document.viewport.getScrollOffsets().top + ((document.viewport.getHeight() - this.elDhtmlWin.getHeight()) / 2) - 80;
        }
        var leftPos = (document.viewport.getWidth() - this.elDhtmlWin.getWidth()) / 2 - 100;
        this.elDhtmlWin.setStyle({
            top: topPos+"px",
            left: leftPos+"px"
        });
    },
    
     rightPositionWin: function(e) {
        var rightPos = e.element().positionedOffset().left - 230;
        var topPos = e.pointerY() + 10;
        this.elDhtmlWin.setStyle({
            top: topPos+"px",
            left: rightPos+"px"
        });
    }
    
});
