﻿/*
* PopUp Light Box
*
* Copyright (c) 2009 Josh Bennett - Whittmanhart
*
* Depends:
*   mootools-1.2-core
*/

if (!$)
    alert('mooTools not found');

function obj_dump(obj) {
    var newWindow = window.open("", "_abc_" + (new Date()).getTime(),  '');
    var outStr = "";

    for (i in obj) {
    
        outStr += "<em>" + i + "</em> = " + obj[i] + "<br />";
    }
    
    newWindow.document.write(outStr);
    newWindow.document.close();

}

function openLocation(loc) {
    if (window.navigate) {
        window.setTimeout("(function(){ window.navigate('" + loc + "') } ) ()", 60);
        return;
    }
    window.location.href = loc;
}

(function() {

    var window = this;
    var prefx = "popup_"
    var regEx = /[a-z|A-Z]/g;
    var regExNoC = /,/g;

    window.com = {};
    window.com.roche = {};
    window.com.roche.widget = {}

    window.com.roche.widget.PopUp = function(opt) {

        var popUpObj,
            dialog,
            title,
            messageArea,
            btnArea,
            btnOkay,
            btnCancel,
            clearFix,
            self = this,
            options = {
                addToElement: opt.addToElement ? opt.addToElement : document.body,
                width: opt.width ? opt.width : "300px",
                height: opt.height ? opt.height : "auto",
                title: opt.title ? opt.title : "Title Here",
                message: opt.message ? opt.message : 'Message Here',
                okLink: opt.okLink ? opt.okLink : ""
            }

        var _init = function() {
            //Container
            popUpObj = new Element('div', {
                'class': 'popUpWindowShadowBox',
                'id': prefx + "popUpWindowShadowBox",
                'styles': {
                    'width': '100%',
                    'height': $(document.body).getStyle('height'),
                    'position': 'absolute',
                    'top': '0px',
                    'left': '0px'
                },
                'events': {
                    'click': function() {
                        window.$(document.body).scrollTo(0, 0);
                    }
                }
            });
            //Dialog Area
            dialog = new Element('div', {
                'class': 'popUpWindowDialogArea',
                'styles': {
                    'width': options.width,
                    'height': options.height,
                    'position': 'absolute',
                    'top': '50%',
                    'left': '50%'
                }
            });
            //Title Area
            title = new Element('div', {
                'class': 'popUpWindowTitle',
                'html': options.title,
                'styles': {

            }
        });
        //Message Area
        messageArea = new Element('div', {
            'class': 'popUpWindowMessageArea',
            'html': options.message
        });
        //Button Area
        btnArea = new Element('div', {
            'class': 'popUpWindowButtonArea'
        });
        //Okay Button
        btnOkay = new Element('a', {
            'class': 'popUpWindowOkay',
            'html': 'Ok',
            'href': 'javascript:void(0)',
            'events': {
                'click': function() {
                    btnArea.dispose();
                    messageArea.set('html', 'Loading ...');
                    openLocation(opt.okLink)
                }
            }
        });
        //Cancel Button
        btnCancel = new Element('a', {
            'class': 'popUpWindowCancel',
            'html': 'Cancel',
            'href': 'javascript:void(0)',
            'events': {
                'click': function() {
                    self.closeWindow();
                }
            }
        });
        //MooTools Injections
        title.inject(dialog, 'bottom');
        messageArea.inject(dialog, 'bottom');
        btnArea.inject(dialog, 'bottom');
        btnOkay.inject(btnArea, 'bottom');
        btnCancel.inject(btnArea, 'bottom');
        //Open The Light Box
        self.openWindow();
    }

    this.openWindow = function() {
        window.$(document.body).scrollTo(0, 0);
        popUpObj.set('opacity', '0.5');
        popUpObj.inject(options.addToElement, 'bottom');
        dialog.inject(options.addToElement, 'bottom');
        if (dialog.getStyle('height') == "auto")
            dialog.setStyle('height', '');
        dialog.setStyle('margin-left', -parseInt(dialog.getStyle('width'), 10) / 2 + "px");
        dialog.setStyle('margin-top', -parseInt(dialog.getStyle('height'), 10) / 2 + "px");
    }

    this.closeWindow = function() {
        popUpObj.dispose();
        dialog.dispose();
    }

    _init();
}
})()
