﻿/*	
	Updated @ 2009-11-03
	License: GNU General Public License v3
	Developer: Ehsun Behravesh
	email: ehsun7b@gmail.com
	Tested on: Firefox, IE 7, IE 8 
*/

var AjaxBox = Class.create({
    initialize: function (url, options) {
        try {
            this.url = url;
            this.eval = (options != undefined) && (options.eval != undefined) ? options.eval : false;
            this.darkLayerColor = (options != undefined) && (options.darkLayerColor != undefined) ? options.darkLayerColor : "black";
            this.darkLayerOpacity = (options != undefined) && (options.darkLayerOpacity != undefined) ? options.darkLayerOpacity : "0.5";
            this.style = (options != undefined) && (options.style != undefined) ? options.style : "background-color: #FFFFFF; padding: 10px; border: 1px solid black; left: 10px; top: 10px";
            this.autoShow = (options != undefined) && (options.autoShow != undefined) ? options.autoShow : false;
            this.static = (options != undefined) && (options.static != undefined) ? options.static : false;
            this.fetched = false;
            this.onFetch = (options != undefined) && (options.onFetch != undefined) ? options.onFetch : null;

            //  -----------------------------------------

            var viewSize = new function () {
                this.height = document.body.clientHeight;
                this.width = document.body.clientWidth;
            }

            var viewPort = document.viewport.getDimensions();

            this.darkLayer = new Element("div");
            this.darkLayer.setStyle({
                'width': Math.max(viewSize.width, viewPort.width) + 'px', 'height': Math.max(viewSize.height, viewPort.height) + 'px',
                'position': 'absolute', 'z-index': '99',
                'top': '0px', 'left': '0px', 'backgroundColor': this.darkLayerColor
            });
            this.darkLayer.setOpacity(this.darkLayerOpacity);

            var containerStyle = 'z-index: 999; position: absolute; ' + this.style;

            this.container = new Element("div", { 'style': containerStyle });

            //  -----------------------------------------

            if (!this.static)
                this.send();
            else {
                this.container.update(url);
                document.body.appendChild(this.darkLayer);
                document.body.appendChild(this.container);
                if (this.autoShow) {                    
                    this.show();
                }
            }
        } catch (e) {
            alert("error: " + e.message);
        }
    },

    send: function () {
        var request = new Ajax.Request(this.url);
        request.options.onSuccess = this.fetch.bind(this, request);
        request.options.onFailure = this.fetchFailure.bind(this, request);
        request.options.onException = this.fetchException.bind(this, request);
    },

    fetch: function (req) {
        try {
            this.container.update(req.transport.responseText.stripScripts());
            document.body.appendChild(this.darkLayer);
            document.body.appendChild(this.container);
            this.darkLayer.setStyle({ display: "none" });
            this.container.setStyle({ display: "none" });

            if (this.eval == true) {
                var scripts = req.transport.responseText.extractScripts();
                for (i = 0; i < scripts.length; ++i) {
                    var e = document.createElement("script");
                    e.setAttribute('type', 'text/javascript');
                    if (Prototype.Browser.IE) {
                        e.text = scripts[i];
                    } else {
                        e.appendChild(document.createTextNode(scripts[i]));
                    }
                    this.container.appendChild(e);
                }
            }

            this.fetched = true;

            if (Object.isFunction(this.onFetch))
                this.onFetch();

            if (this.autoShow)
                this.show();
        } catch (e) {
            alert("error: " + e.name + " - " + e.message);
        }
    },

    fetchFailure: function (req) {
        try {
            this.container.update(this.url + ' Not found!');
        } catch (e) {
            alert("error: " + e.name + " - " + e.message);
        }
    },

    fetchException: function (req) {
        try {
            this.container.update('Error in fetching URL!');
        } catch (e) {
            alert("error: " + e.name + " - " + e.message);
        }
    },

    show: function () {
        this.darkLayer.setStyle({ display: "" });
        this.container.setStyle({ display: "" });
    },

    remove: function () {
        this.container.remove();
        this.darkLayer.remove();
    }
});
