// Dialog.js - provides lightboxes/modal dialogs
// License: This file is entirely BSD licensed.
// Screen size class modified from Sébastien Gruhier
// Written by Brian Miedlar (c) 2006

var Screen = Class.create();
Screen.ScrollSize = function() {
    var w = window;
    var T, L, W, H;
    with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
    }
    return { top: T, left: L, width: W, height: H };
};

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
// Edit by bmiedlar for scrolling 
//
Screen.PageSize = function(){
    var xScroll, yScroll, isScrollY, isScrollX;

    if (window.innerHeight && window.scrollMaxY) {	
	    xScroll = document.body.scrollWidth;
	    yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	    xScroll = document.body.scrollWidth;
	    yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	    xScroll = document.body.offsetWidth;
	    yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {	// all except Explorer
	    windowWidth = self.innerWidth;
	    windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	    windowWidth = document.documentElement.clientWidth;
	    windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
	    windowWidth = document.body.clientWidth;
	    windowHeight = document.body.clientHeight;
    }	
    var pageHeight, pageWidth;

    // for small pages with total height less then height of the viewport
    isScrollY = false;
    if(yScroll < windowHeight){
	    pageHeight = windowHeight;
    } else { 
	    pageHeight = yScroll;
    }
    if(windowHeight < yScroll) isScrollY = true;

    // for small pages with total width less then width of the viewport
    isScrollX = false;
    if(xScroll < windowWidth){	
	    pageWidth = windowWidth;
    } else {
	    pageWidth = xScroll;
    }
    if(windowWidth < xScroll) isScrollX = true;

    return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight, isScrollX:isScrollX, isScrollY:isScrollY};
};

var DialogInstance = Class.create();
DialogInstance.prototype = {
    initialize: function(element, finishCallback) {
        this.element = element;
        this.finishCallback = finishCallback;
    },
    open: function() {
        Element.show(this.element);
    },
    close: function() {
        Element.hide(this.element);
        if(this.finishCallback) this.finishCallback();
    }    
};

var Dialog = Class.create();
Dialog.FrozenDiv = null;
Dialog.prototype = {
    initialize: function() {
        this.openedDialogs = new Array();
        this.openedDialogCount = 0;
        this.finishCallback = null;
    },

    show: function(elementName, frozenCallback, finishCallback) {
        var instance = new DialogInstance(elementName, finishCallback);
        this.openedDialogs.push(instance);
        this.openedDialogCount++;
        instance.open();
        frozenCallback();
        this.finishCallback = finishCallback;
    },
    closeAll: function(finishCallback) {
        this.openedDialogs.each( function(oDialogInstance) {
            oDialogInstance.close();
            this.openedDialogCount--;
            if(this.openedDialogCount <= 0) this.openedDialogCount = 0;
        });
        this.openedDialogs.clear();
        if(finishCallback) finishCallback();
    }
};

Dialog.FreezeApplication = function(containerElement, frozenCss) {
    //if it is already frozen 
    if (Dialog.FrozenDiv != null) return;
    if ($('FrozenStatic')) { if (Element.visible('FrozenStatic')) return; }
    var sId = 'Frozen_' + containerElement;
    if ($(sId)) { if (Element.visible(sId)) return; }

    //Create a frozen layer
    Dialog.UnfreezeApplication(containerElement);
    var oSize = Screen.PageSize();
    var div = $(sId);
    if (!div) {
        div = document.createElement("div");
        div.id = sId;
        Dialog.FrozenDiv = $(containerElement).appendChild(div);
    }
    if (!div.frozenInit) {
        Element.setOpacity(sId, 0);
        Element.setStyle(sId, {
            display: 'block', 'background': '#999999', position: 'absolute',
            top: 0, left: 0, 'z-index': 25000, width: '100%'
        });
        Element.addClassName(sId, frozenCss);
        div.frozenInit = true;
    }
    var iFreezeHeight = oSize.pageHeight;
    if (containerElement == 'App_Ticketing') iFreezeHeight = parseInt(Element.getHeight(containerElement)) || 0;
    Element.setStyle(sId, { height: iFreezeHeight + 'px' });

    new Effect.Appear(sId, { duration: 1.3, from: .2, to: .6 }); ;
    //Element.setOpacity(sId, .7);
};
Dialog.UnfreezeApplication = function(containerElement) {
    //Create a frozen layer
    if($('FrozenStatic')) { Element.hide('FrozenStatic'); return; }
    if(Dialog.FrozenDiv != null) $(containerElement).removeChild(Dialog.FrozenDiv);
    Dialog.FrozenDiv = null;
    var sId = 'Frozen_' + containerElement;
    if($(sId)) { Element.hide(sId); }
};



var _dialogs = new Dialog();

DialogHandler = Class.create();
DialogHandler.prototype = {
    'initialize': function(element, options) {
        this.element = $(element);
        this.pos = null;
        this.options = options;
        this.freeze = false;
    },
    'show': function(options) {
        options = Object.extend(options || {});
        moveX = options.moveX;
        moveY = options.moveY;
        freeze = options.freeze;
        this.freeze = freeze;
        this.freezeElement = 'UI';
        if (options.freezeElement) this.freezeElement = options.freezeElement;
        position = options.position;
        //moveX and moveY show dynamic movement in loading
        if (!moveX) moveX = 0; if (!moveY) moveY = 0;

        if (position == null) position = 'center';
        if (this.options.onBeginShow) this.options.onBeginShow();

        if (!this.pos) {
            this.element.show();
            this.element.makePositioned();
            this.pos = [parseFloat(this.element.getStyle('left')) || 0, parseFloat(this.element.getStyle('top')) || 0];
            this.element.hide();
        }
        var szElement = [parseFloat(this.element.getStyle('width')) || '0', parseFloat(this.element.getStyle('height')) || 0];
        if (options.width) szElement[0] = options.width;
        if (options.height) szElement[1] = options.height;

        var x = this.pos[0];
        var y = this.pos[1];
        var szPage = Screen.PageSize();

        var offsetX = szPage.windowWidth / 2 - (szElement[0] / 2);
        var offsetY = (szPage.windowHeight / 2) - (szElement[1]);
        if (position.indexOf('top') >= 0) offsetY = 0;
        if (position.indexOf('left') >= 0) offsetX = 0;
        if (position.indexOf('right') >= 0) offsetX = szPage.windowWidth - szElement[0] / 2 - 20;
        if (position.indexOf('bottom') >= 0) offsetY = szPage.windowHeight - szElement[1] - 20;

        //movement from starting point to scroll
        var oScroll = Screen.ScrollSize();
        var left = x - moveX;
        var top = y - moveY;

        if (position != 'fixed') {
            left = x + offsetX - moveX + oScroll.left;
            top = y + offsetY - moveY + oScroll.top;
        }
        if (options.fixedLeft) left = options.fixedLeft - moveX;
        if (options.fixedTop) top = options.fixedTop - moveY;
        if (options.position != 'fixed') {
            this.element.setStyle({
                left: (left) + 'px',
                top: (top) + 'px'
            });
        }

        _dialogs.show(this.element,
            function() {
                if (freeze) {
                    Dialog.FreezeApplication(this.freezeElement, 'frozen');
                    if (this.options.onFreeze) this.options.onFreeze();
                }
            } .bind(this),
            function() {
                this.element.setStyle({
                    left: x + 'px',
                    top: y + 'px'
                });
                if (this.options.onEndShow) this.options.onEndShow();
            } .bind(this)
        );
        if (moveX != 0 || moveY != 0) {
            var effect = new Effect.Move(this.element, { 'x': moveX, 'y': moveY, 'duration': .25 });
            effect = null;
        }
        if (options.setFocus) $(options.setFocus).focus();

    },
    hide: function() {
        //_dialogs.closeAll();
        Element.setStyle(this.element, { left: null, top: null });
        Element.hide(this.element);
        //new Effect.SwitchOff(this.element);
        if (this.freeze) {
            Dialog.UnfreezeApplication(this.freezeElement);
        }
    }
};

