//MOOdalBox, Copyright (c) 2007 Razvan Brates, <http://e-magine.ro/moodalbox>, MIT Style License.
//eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('7 20="2W.. 2u 2v a 35 2w 2O 1P.<11 /><11 />"+"2x 36 2z.<11 /><11 />"+"<1v>2A 2j 2C O.</1v>";7 1G=18;7 1H=1y;7 1I=1y;7 21=12;7 1J=18;7 1Z=12;7 1K=18;7 1X=2c;7 1W=H;7 1M=H;7 1p={2a:h(d){6.d=2F.1w({22:1G,1k:1H,1Q:1I,1b:21,1q:1J,1D:1Z,1N:1K,1B:1X,1h:1W,1r:1M},d||{});6.1O=[];$A($$(\'a\')).1x(h(m){D(m.N&&m.L&&m.N.2H(\'^2U\',\'i\')){m.z=6.28.1C(m,6);6.1O.2J(m)}},6);6.1S=6.1T.2K(6);6.1f=6.19.y(6);6.E=n p(\'B\').J(\'v\',\'2L\').q(17.1j);6.b=n p(\'B\').J(\'v\',\'2M\').t({R:6.d.1k+\'j\',C:6.d.1Q+\'j\',W:\'-\'+(6.d.1k/2)+\'j\',u:\'14\'}).q(17.1j);6.e=n p(\'B\').J(\'v\',\'2P\').q(6.b);6.c=n p(\'B\').J(\'v\',\'2Q\').F(\'u\',\'14\').q(17.1j);6.1R=n p(\'a\').2S({v:\'2T\',L:\'#\'}).q(6.c);6.26=n p(\'B\').J(\'v\',\'2V\').q(6.c);n p(\'B\').F(\'2X\',\'2Y\').q(6.c);6.1U=n p(\'B\').J(\'v\',\'30\').1e(20);6.1R.z=6.E.z=6.O.y(6);7 l=6.l.y(6);6.k={E:6.E.23(\'S\',{16:12}).1E(),Z:6.b.25({16:6.d.22,10:l}),e:6.e.23(\'S\',{16:12,10:l}),c:6.c.25({16:18,10:l})};6.1L=32.34},28:h(V){I 6.M(V.L,V.1o,V.N)},M:h(1a,29,2b){6.L=1a;6.1o=29;6.N=2b;6.19();6.1g(2c);6.K=Q.1t()+(Q.1u()/15);6.b.t({K:6.K+\'j\',u:\'\'});6.k.E.G(0.8);I 6.1A(1a)},19:h(){6.E.t({K:Q.1t()+\'j\',C:Q.1u()+\'j\'})},1g:h(M){7 1c=$A($$(\'2e\'));1c.1w($$(1z.2f?\'2g\':\'2h\'));1c.1x(h(m){m.T.2k=M?\'2l\':\'\'});7 X=M?\'2n\':\'2o\';1z[X](\'2p\',6.1f)[X](\'Z\',6.1f);17[X](\'2q\',6.1S);6.o=0},1A:h(){D(6.o)I H;6.o=1;7 r=6.N.2s(/[0-9]+/g);6.d.1b=(r&&(r[0]>0))?r[0]:6.d.1D;6.d.1q=(r&&(r[1]>0))?r[1]:6.d.1N;6.c.t({S:\'0\',C:\'2y\',u:\'14\'});6.b.1l=\'27\';6.k.e.1E();7 l=6.l.y(6);7 13=6.13.y(6);7 2d={2B:\'2D\',2E:6.e,1h:6.d.1h,1r:6.d.1r,10:l,2G:13};6.1L=n 2I(6.L,2d).1P();I H},13:h(){6.e.1e(\'\');6.1U.2R().q(6.e);6.l();6.b.F(\'U\',\'1Y\');6.c.F(\'U\',\'1Y\');6.b.z=6.c.z=6.O.y(6)},l:h(){2Z(6.o++){P 1:6.b.1l=\'\';6.b.F(\'U\',\'24\');6.c.F(\'U\',\'24\');6.b.z=6.c.z=\'\';6.26.1e(6.1o);6.e.t({R:6.d.1b+"j",C:6.d.1q+"j"});D(6.b.1d!=6.e.1s){6.k.Z.G({C:[6.b.1d,6.e.1s]});Y}6.o++;P 2:D(6.b.1n!=6.e.1m){6.k.Z.G({R:[6.b.1n,6.e.1m],W:[-6.b.1n/2,-6.e.1m/2]});Y}6.o++;P 3:6.c.t({K:(6.K+6.b.1d)+\'j\',R:6.e.T.R,W:6.b.T.W,u:\'\'});6.k.e.G(0,1);Y;P 4:D(6.d.1B){6.k.c.G({S:[0,1],C:[0,6.c.1F]});Y}6.c.t({S:\'1\',C:6.c.1F+\'j\'});P 5:6.o=0}},1T:h(s){D((s.1V&&s.1i==\'w\')||(s.1V&&s.1i==\'x\')||(s.1i==\'31\')){6.O();s.33()}},O:h(){D(6.o<0)I;6.o=-1;2m(7 f 2r 6.k)6.k[f].2t();6.b.T.u=6.c.T.u=\'14\';6.b.1l=\'27\';6.k.E.2i(6.1g.1C(H,6)).G(0);I H}};Q.2N(1p.2a.y(1p));',62,193,'||||||this|var||||center|bottom|options|contents|||function||px|fx|nextEffect|el|new|step|Element|injectInside|aDim|event|setStyles|display|id|||bind|onclick||div|height|if|overlay|setStyle|custom|false|return|setProperty|top|href|open|rel|close|case|Window|width|opacity|style|cursor|link|marginLeft|fn|break|resize|onComplete|br|500|ajaxFailure|none||duration|document|400|position|sLinkHref|contentsWidth|elements|clientHeight|setHTML|eventPosition|setup|evalScripts|key|body|initialWidth|className|offsetWidth|clientWidth|title|MOOdalBox|contentsHeight|evalResponse|offsetHeight|getScrollTop|getHeight|em|extend|each|250|window|loadContents|animateCaption|pass|defContentsWidth|hide|scrollHeight|_RESIZE_DURATION|_INITIAL_WIDTH|_INITIAL_HEIGHT|_CONTENTS_HEIGHT|_DEF_CONTENTS_HEIGHT|ajaxRequest|_EVAL_RESPONSE|defContentsHeight|anchors|request|initialHeight|closelink|eventKeyDown|keyboardListener|error|control|_EVAL_SCRIPTS|_ANIMATE_CAPTION|pointer|_DEF_CONTENTS_WIDTH|_ERROR_MESSAGE|_CONTENTS_WIDTH|resizeDuration|effect|default|effects|caption|mb_loading|click|sLinkTitle|init|sLinkRel|true|ajaxOptions|object|ActiveXObject|select|embed|chain|anywhere|visibility|hidden|for|addEvent|removeEvent|scroll|keydown|in|match|clearTimer|there|was|with|Please|0px|again|Click|method|to|get|update|Object|onFailure|test|Ajax|push|bindWithEvent|mb_overlay|mb_center|onDomReady|your|mb_contents|mb_bottom|clone|setProperties|mb_close_link|moodalbox|mb_caption|Oops|clear|both|switch|mb_error|esc|Class|stop|empty|problem|try'.split('|'),0,{}))
/******************************************************************/
/*                        MOOdalBox 1.2.1                         */
/* A modal box (inline popup), used to display remote content     */
/* loaded using AJAX, written for the mootools framework          */
/*         by Razvan Brates, razvan [at] e-magine.ro              */
/******************************************************************/
/*               http://www.e-magine.ro/moodalbox                 */
/******************************************************************/
/*                                                                */
/* MIT style license:                                             */
/* http://en.wikipedia.org/wiki/MIT_License                       */
/*                                                                */
/* mootools found at:                                             */
/* http://mootools.net/                                           */
/*                                                                */
/* Original code based on "Slimbox", by Christophe Beyls:         */
/* http://www.digitalia.be/software/slimbox                       */
/******************************************************************/

// Constants defined here can be changed for easy config / translation
// (defined as vars, because of MSIE's lack of support for const)

var _ERROR_MESSAGE = "Oops.. there was a problem with your request.<br /><br />" +
                    "Please try again.<br /><br />" +
                    "<em>Click anywhere to close.</em>"; // the error message displayed when the request has a problem
var _RESIZE_DURATION         = 400;         // Duration of height and width resizing (ms)
var _INITIAL_WIDTH            = 250;        // Initial width of the box (px)
var _INITIAL_HEIGHT            = 250;        // Initial height of the box (px)
var _CONTENTS_WIDTH         = 500;        // Actual width of the box (px)
var _CONTENTS_HEIGHT        = 400;        // Actual height of the box (px)
var _DEF_CONTENTS_WIDTH        = 500;        // Default width of the box (px) - used for resetting when a different setting was used
var _DEF_CONTENTS_HEIGHT    = 400;        // Default height of the box (px) - used for resetting when a different setting was used
var _ANIMATE_CAPTION        = false;        // Enable/Disable caption animation
var _EVAL_SCRIPTS            = false;    // Option to evaluate scripts in the response text
var _EVAL_RESPONSE            = false;    // Option to evaluate the whole response text

// The MOOdalBox object in its beauty
var MOOdalBox = {
    
    // init the MOOdalBox
    init: function (options) {
        
        // init default options
        this.options = Object.extend({
            resizeDuration:     _RESIZE_DURATION,
            initialWidth:         _INITIAL_WIDTH,    
            initialHeight:         _INITIAL_HEIGHT,
            contentsWidth:         _CONTENTS_WIDTH,
            contentsHeight:     _CONTENTS_HEIGHT,
            defContentsWidth:     _DEF_CONTENTS_WIDTH,
            defContentsHeight:     _DEF_CONTENTS_HEIGHT,
            animateCaption:     _ANIMATE_CAPTION,
            evalScripts:         _EVAL_SCRIPTS,
            evalResponse:         _EVAL_RESPONSE
        }, options || {});
        
        // scan anchors for those opening a MOOdalBox
        this.anchors = [];
        $A($$('a')).each(function(el){
            // we use a regexp to check for links that 
            // have a rel attribute starting with "moodalbox"
            if(el.rel && el.href && el.rel.test('^moodalbox', 'i')) {
                el.onclick = this.click.pass(el, this);
                this.anchors.push(el);
            }
        }, this);
        
        // add event listeners
        this.eventKeyDown = this.keyboardListener.bindWithEvent(this);
        this.eventPosition = this.position.bind(this);
        
        // init the HTML elements
        // the overlay (clickable to close)
        this.overlay = new Element('div').setProperty('id', 'mb_overlay').injectInside(document.body);
        // the center element
        this.center = new Element('div').setProperty('id', 'mb_center').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
        // the actual page contents
        this.contents = new Element('div').setProperty('id', 'mb_contents').injectInside(this.center);

        // the bottom part (caption / close)
        this.bottom = new Element('div').setProperty('id', 'mb_bottom').setStyle('display', 'none').injectInside(document.body);
        this.closelink = new Element('a').setProperties({id: 'mb_close_link', href: '#'}).injectInside(this.bottom);
        this.caption = new Element('div').setProperty('id', 'mb_caption').injectInside(this.bottom);
        new Element('div').setStyle('clear', 'both').injectInside(this.bottom);
        
        this.error = new Element('div').setProperty('id', 'mb_error').setHTML(_ERROR_MESSAGE);
        
        // attach the close event to the close button / the overlay
        this.closelink.onclick  = this.close.bind(this);
        
        // init the effects
        var nextEffect = this.nextEffect.bind(this);
        this.fx = {
            overlay:     this.overlay.effect('opacity', { duration: 500 }).hide(),
            resize:     this.center.effects({ duration: this.options.resizeDuration, onComplete: nextEffect }),
            contents:     this.contents.effect('opacity', { duration: 500, onComplete: nextEffect }),
            bottom:     this.bottom.effects({ duration: 400, onComplete: nextEffect })
        };
        
        this.ajaxRequest = Class.empty;

    },
    
    click: function(link) {
        return this.open (link.href, link.title, link.rel);
    },

    open: function(sLinkHref, sLinkTitle, sLinkRel) {
        this.href = sLinkHref;
        this.title = sLinkTitle;
        this.rel = sLinkRel;
        this.position();
        this.setup(true);
        this.top = Window.getScrollTop() + (Window.getHeight() / 15);
        this.center.setStyles({top: this.top+'px', display: ''});
        this.fx.overlay.custom(0.8);
        return this.loadContents(sLinkHref);
    },

    position: function() {
        this.overlay.setStyles({top: Window.getScrollTop()+'px', height: Window.getHeight()+'px'});
    },

    setup: function(open) {
        var elements = $A($$('object'));
        elements.extend($$(window.ActiveXObject ? 'select' : 'embed'));
        elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
        var fn = open ? 'addEvent' : 'removeEvent';
        window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
        document[fn]('keydown', this.eventKeyDown);
        this.step = 0;
    },
    
    loadContents: function() {        
        if(this.step) return false;
        this.step = 1;
        
        // check to see if there are specified dimensions
        // if not, fall back to default values
        var aDim = this.rel.match(/[0-9]+/g);
        this.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defContentsWidth;
        this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defContentsHeight;
                        
        this.bottom.setStyles({opacity: '0', height: '0px', display: 'none'});
        this.center.className = 'mb_loading';
        
        this.fx.contents.hide();
        
        // AJAX call here
        var nextEffect = this.nextEffect.bind(this);
        var ajaxFailure = this.ajaxFailure.bind(this);
        var ajaxOptions = {
            method:         'get',
            update:         this.contents, 
            evalScripts:     this.options.evalScripts,
            evalResponse:     this.options.evalResponse,
            onComplete:     nextEffect, 
            onFailure:         ajaxFailure
            };
        this.ajaxRequest = new Ajax(this.href, ajaxOptions).request();
                
        return false;
    },
    
    ajaxFailure: function (){
        this.contents.setHTML('');
        this.error.clone().injectInside(this.contents);
        this.nextEffect();
        this.center.setStyle('cursor', 'pointer');
        this.bottom.setStyle('cursor', 'pointer');
        this.center.onclick = this.bottom.onclick = this.close.bind(this);        
    },
    
    nextEffect: function() {
        switch(this.step++) {
        case 1:
            // remove previous styling from the elements 
            // (e.g. styling applied in case of an error)
            this.center.className = '';
            this.center.setStyle('cursor', 'default');
            this.bottom.setStyle('cursor', 'default');
            this.center.onclick = this.bottom.onclick = '';
            this.caption.setHTML(this.title);
            
            this.contents.setStyles ({width: this.options.contentsWidth + "px", height: this.options.contentsHeight + "px"});

            if(this.center.clientHeight != this.contents.offsetHeight) {
                this.fx.resize.custom({height: [this.center.clientHeight, this.contents.offsetHeight]});
                break;
            }
            this.step++;
                    
        case 2:
            if(this.center.clientWidth != this.contents.offsetWidth) {
                this.fx.resize.custom({width: [this.center.clientWidth, this.contents.offsetWidth], marginLeft: [-this.center.clientWidth/2, -this.contents.offsetWidth/2]});
                break;
            }
            this.step++;
        
        case 3:
            this.bottom.setStyles({top: (this.top + this.center.clientHeight)+'px', width: this.contents.style.width, marginLeft: this.center.style.marginLeft, display: ''});
            this.fx.contents.custom(0,1);
            break;
        
        case 4:
            if(this.options.animateCaption) {
                this.fx.bottom.custom({opacity: [0, 1], height: [0, this.bottom.scrollHeight]});
                break;
            }
            this.bottom.setStyles({opacity: '1', height: this.bottom.scrollHeight+'px'});

        case 5:
            this.step = 0;
        }
    },
    
    
    keyboardListener: function(event) {
        // close the MOOdalBox when the user presses CTRL + W, CTRL + X, ESC
        if ((event.control && event.key == 'w') || (event.control && event.key == 'x') || (event.key == 'esc')) {
            this.close();
            event.stop();
        }        
    },
    
    close: function() {
        if(this.step < 0) return;
        this.step = -1;
        for(var f in this.fx) this.fx[f].clearTimer();
        this.center.style.display = this.bottom.style.display = 'none';
        this.center.className = 'mb_loading';
        this.fx.overlay.chain(this.setup.pass(false, this)).custom(0);
        return false;
    }
        
};

// startup
Window.onDomReady(MOOdalBox.init.bind(MOOdalBox));