/**
 *
 *
 */

var _DesignLoader = Class.create({

    initialize: function(target_container_ref) {
        this.loading = false;
        this.editors_ar = new Array();
        this.entries = new Array();
        this.target = target_container_ref;
        this.withEvents = false;
    },

    enableEvents: function () {
        this.withEvents = true;
    },

    toString: function () {
        return("_DesignLoader [" + this.target.getAttribute("id") + "]");
    },
    
    loadDesign: function (design_id, onReady) {
        this.editors_ar = new Array();
        this.entries = new Array();

        this.loading = true;
        
        this.restoreById(design_id);

        this.onReady = onReady;
    //d("loadDesign ends " + design_id);
    },

    restoreById: function (design_id) {
        var self = this;
        new Ajax.Request('/ajax_store.php', {
            method: 'post',
            parameters: {
                action: 'restore_design_ID',
                "design_id": design_id
            },
            onSuccess: self.restore_processResult.bindAsEventListener(self),
            onFailure: function (transport) {
                alert("Restore failed=" + transport);
            },
            onException: function (transport, exc) {
                alert("Restore exception (id)=" + exc);
            }
        });
    },

    /**
     * @return an array[_Editors]
     */
    __updateEditorValues: function (editor_id, design_entry) {
        this.entries.push(design_entry);
    //d("after __updateEditorValues " + editor_id +"   entry=" + design_entry);
    },

    __returnResult: function () {
        var self = this;
        var mask_counter = 0;

        if (this.loading == false) {
            if (this.editor_type == "notebook") {
                var after_masks_are_loaded = function() {
                    mask_counter++;
                    if (mask_counter == 2) {
                        if (editor1) editor1.__updateEditorValues(self.entries[0]);
                        if (editor2) editor2.__updateEditorValues(self.entries[1]);
                        self.onReady(self.editors_ar);
                    }
                };
                var tmp_designer = new LabelDesigner("notebook");
                
                var editor1 = new _Editor("Aussen", 290, 222, 14.14, 10.39, this.withEvents, "/customer_images/Laptop/Deckel_Editor.gif",   "/customer_images/Laptop/Deckel_Editor_back.gif",   null, new _Viewport(0, 0, 290, 213)); //191, 200); // 43 x 40 cm   // 0.93  16.93 x 15.75;
                editor1.designer = tmp_designer;
                var editor2 = new _Editor("Innen",  290, 222, 13.86,  8.98, this.withEvents, "/customer_images/Laptop/Tastatur_Editor.gif", "/customer_images/Laptop/Tastatur_Editor_back.gif", null, new _Viewport(0, 0, 290, 213));  // 16 x 46,1 cm -->  6.30" x 18.15";
                editor2.designer = tmp_designer;

                this.editors_ar.push(editor1);
                this.editors_ar.push(editor2);

                editor1.setMaskLoadedListener(after_masks_are_loaded);
                editor1.__setup_masks();
                editor2.setMaskLoadedListener(after_masks_are_loaded);
                editor2.__setup_masks();
            } else {
                var after_masks_are_loaded_ws = function() {
                    mask_counter++;
                    if (mask_counter == 3) {
                        if (editor1_ws) editor1_ws.__updateEditorValues(self.entries[0]);
                        if (editor2_ws) editor2_ws.__updateEditorValues(self.entries[1]);
                        if (editor3_ws) editor3_ws.__updateEditorValues(self.entries[2]);

                        self.onReady(self.editors_ar);
                    }
                };

                var tmp_designer_ws = new LabelDesigner("workstation");
                var editor1_ws = new _Editor("Links",  190, 200, 22.28, 19.53, this.withEvents, "/customer_images/cosmos_transparent_links1.gif",  null, null, new _Viewport( 7, 23, 178, 172));
                editor1_ws.designer = tmp_designer_ws;
                var editor2_ws = new _Editor("Front",   82, 200,  6.30, 18.15, this.withEvents, "/customer_images/cosmos_transparent_vorn1_a.gif", null, null, new _Viewport(18, 27,  66, 170));
                editor2_ws.designer = tmp_designer_ws;
                var editor3_ws = new _Editor("Rechts", 190, 200, 22.28, 19.53, this.withEvents, "/customer_images/cosmos_transparent_rechts1.gif", null, null, new _Viewport(12, 23, 183, 172));
                editor3_ws.designer = tmp_designer_ws;

                this.editors_ar.push(editor1_ws);
                this.editors_ar.push(editor2_ws);
                this.editors_ar.push(editor3_ws);

                editor1_ws.setMaskLoadedListener(after_masks_are_loaded_ws);
                editor1_ws.__setup_masks();
                editor2_ws.setMaskLoadedListener(after_masks_are_loaded_ws);
                editor2_ws.__setup_masks();
                editor3_ws.setMaskLoadedListener(after_masks_are_loaded_ws);
                editor3_ws.__setup_masks();
            }
        }
    },

    /**
     *
     *
     */
    restore_processResult: function (transport) {
        var text = "";
        //d("response=" + transport.responseText);
        if (transport.responseText != "") {
            text = transport.responseText.evalJSON(true);
        } else {
            this.loading = false;
            return;
        }

        var left_info  = "";
        var front_info = "";
        var right_info = "";

        if (text.editor_type) {
            if (text.editor_type != "") this.editor_type = text.editor_type;
        }

        if ( typeof(text.left)  != 'undefined') left_info  = text.left.split(":");
        if ( typeof(text.front) != 'undefined') front_info = text.front.split(":");
        if ( typeof(text.right) != 'undefined') right_info = text.right.split(":");

        this.store_id = text.store_id * 1;
        //        d("design_store_id=" + this.store_id);
        //        d("editor_type=" + this.editor_type);
        var design_entry = null;

        if (left_info.length > 1) {
            design_entry = new __DesignEntry(left_info);
            this.__updateEditorValues(0, design_entry);
        }

        if (front_info.length > 1) {
            design_entry = new __DesignEntry(front_info);
            this.__updateEditorValues(1, design_entry);
        }

        if (right_info.length > 1) {
            design_entry = new __DesignEntry(right_info);
            this.__updateEditorValues(2, design_entry);
        }

        this.loading = false;
        this.__returnResult();
    }
});
