/**
 *
 *
 *
 *  @revision
 *      v1.002
 *  @modified
 *      July 7, 2008 - dranzd
 *          - added feature to drag dialog box
 *          - added gray background - disabled
 *      April 9, 2008
 *  @created
 *      January 1, 2008
 		July 11, 2008 - cs
 			- added function to show only one button i.e Close or Cancel button
			
		used with permission from rafael -val
 */

/*********** STATIC DIALOG OBJECT ***********/
var DIALOG = {
    instance: [],

    generateID: function(){
        var id = Math.floor( (10000-4999) * Math.random() ) + 5000;
        for( var k in DIALOG.instance ){
            if( k == id ){
                return Dialog.generateID()
            }
        }
        return id
    },

    addInstance: function(instance,id){
        if( ! id )
            var id = DIALOG.generateID()
        DIALOG.instance.push( { 'id':id, 'instance':instance } )
        return id
    },

    delInstance: function(id,instance){
        var new_instances = []
        var keys_to_delete = []
        for( var x=0; x < DIALOG.instance.length; ++x ){
            if( DIALOG.instance[x]['id'] == id ){
                delete DIALOG.instance[x];
            } else {
                new_instances.push(DIALOG.instance[x])
            }
        }
        DIALOG.instance = new_instances
    },

    getInstance: function(code){
        var instance = '';
        for( var x=0; x < DIALOG.instance.length; ++x ){
            if( DIALOG.instance[x]['id'] == code ){
                instance = DIALOG.instance[x]['instance'];
                break
            }
        }
        return typeof instance == 'object' ? instance : false
    }
}

/*********** DIALOG BOX ***********/
var Dialog = function(){ this.initialize.apply(this,arguments) }
Dialog.prototype = {
    _id: 0,
    _type: 1,
    _div_box: '',
    _div_title: '',
    _div_pointer: '',
    _div_dragger: '',
    _div_close: '',
    _div_content: '',
    _div_buttons: '',
    _button_extra: '',
    _button_okay: '',
    _button_cancel: '',
    _context_set: false,
    _content_set: false,
    _old_content: null,
    _button_observer: [],
    _options: {},
    _dragger: '',
    
    _div_forcefield: '',
    
    DIALOG_POP:1,
    DIALOG_CONTEXTUAL:2,
    DIALOG_FREESTYLE:3,

    initialize: function(){
        /*arguments: dialog type, prefered id, options in hash{}*/

        if(arguments[1]){
            var _instance = DIALOG.getInstance(arguments[1]);
            if(_instance){
                //Object.extend(this,_instance);
                return this;
            }
            var id = this._id = DIALOG.addInstance(this,arguments[1]);
        } else {
            var id = this._id = DIALOG.addInstance(this);
        }
				
				Object.extend(this._options, arguments[2] || {});

        var container = this._createContainer(id);
        this._div_box       = container;
        this._div_pointer   = container.down('div.dialog_pointer',0);
        this._div_close     = container.down('div.dialog_container span.dialog_close',0);
        this._div_title     = container.down('div.dialog_container h2.dialog_title span',0);
        this._div_content   = container.down('div.dialog_container div.dialog_content div.dialog_user_content',0);
        this._div_buttons   = container.down('div.dialog_container div.dialog_content div.dialog_buttons',0);
        this._button_extra   = container.down('div.dialog_container div.dialog_content div.dialog_buttons input[value="Extra"]',0);
        this._button_okay   = container.down('div.dialog_container div.dialog_content div.dialog_buttons input[value="Okay"]',0);
        this._button_cancel = container.down('div.dialog_container div.dialog_content div.dialog_buttons input[value="Cancel"]',0);
        document.body.appendChild(container);
        
        this._div_pointer.hide();
        if(typeof arguments[0] == 'number'){
            switch(arguments[0]){
                case this.DIALOG_POP:
                    this._type = this.DIALOG_POP;
                    this._moveDefault();
                    break;
                case this.DIALOG_CONTEXTUAL:
                    this._type = this.DIALOG_CONTEXTUAL;
                    this._div_pointer.show();
                    break;
                default: this._type = 1; break;
            }
        }

        this._div_box.setStyle({'zIndex':1001});
        this._div_close.observe('click',this._onClose.bindAsEventListener(this));
        
        if (this._options.isDraggable) {this.initDragger();}
        
        // to have a gray background
        //this.initForceField();

        return this;
    },
    
    initDragger: function(){
        if (this._dragger == '' || this._dragger == null) {
            return;
        }
        this._div_dragger = new Element('div').addClassName('dialog_dragger');
        this._div_box.insert({'top':this._div_dragger});
        this._dragger = new Dragger(this._div_dragger,{'toAnimate':this._div_box});
        this._dragger.start();
    },
    
    initForceField: function(){
        this._div_forcefield = new Element('div')
            .addClassName('dialog_forcefield')
            .hide()
            .setStyle({'width':'100%','height':'100%','position':'fixed','left':0,'top':0,'zIndex':1000,'backgroundColor':'#000'});
        this._div_forcefield.style.opacity = .75;
        this._div_box.insert({'before':this._div_forcefield});
    },

    showMessage: function(){
        var title = typeof arguments[0] == 'string' && arguments[0] != '' ? arguments[0] : 'Dialog Title';
        var content = typeof arguments[1] == 'string' && arguments[1] != '' ? arguments[1] : 'Dialog Content';
        var button_confirm = typeof arguments[2] == 'string' && arguments[2] != '' ? arguments[2] : 'Okay';

        if(arguments[0] != null)
            this._div_title.update(title)
        if(arguments[1] != null)
            this._div_content.update(content);

        this._button_extra.hide();
        this._button_cancel.hide();
        this._button_okay.value = button_confirm;

        if(! this._button_observer['okay'+this._id]){
            // :TODO: add checkpoint if button has not been observe by a handler.  prevent memory leak.
            var binderOkay = this._onConfirm.bindAsEventListener(this);
            this._button_okay.observe('click',binderOkay);
            this._button_observer['okay'+this._id] = true;
        }

        this._moveDefault();
        this.show();
        return this;
    },
    
    showMessage1: function(){
        var title = typeof arguments[0] == 'string' && arguments[0] != '' ? arguments[0] : 'Dialog Title';
        var content = typeof arguments[1] == 'string' && arguments[1] != '' ? arguments[1] : 'Dialog Content';
        var button_cancel = typeof arguments[3] == 'string' && arguments[3] != '' ? arguments[3] : 'Cancel';

        if(arguments[0] != null)
            this._div_title.update(title)
        if(arguments[1] != null)
            this._div_content.update(content);

        this._button_extra.hide();
        this._button_okay.hide();
        this._button_cancel.value = button_cancel;

                
        //if(this._button_observer['cancel'] != true){
            // :TODO: add checkpoint if button has not been observe by a handler.  prevent memory leak.
            var binderCancel = this._onCancel.bindAsEventListener(this);
            this._button_cancel.observe('click',binderCancel);
            this._button_observer['cancel'] = true;
        //}
        this._button_cancel.show();
        
        this._moveDefault();
        this.show();
        return this;
    },

    showChoice: function(){
        var title = typeof arguments[0] == 'string' && arguments[0] != '' ? arguments[0] : 'Dialog Title';
        //var content = typeof arguments[1] == 'string' && arguments[1] != '' ? arguments[1] : 'Dialog Content';
        var content = arguments[1];
        var button_confirm = typeof arguments[2] == 'string' && arguments[2] != '' ? arguments[2] : 'Okay';
        var button_cancel = typeof arguments[3] == 'string' && arguments[3] != '' ? arguments[3] : 'Cancel';

        if(arguments[0] != null)
            this._div_title.update(title);
        if(arguments[1] != null)
            this.setContent(content);
        if(arguments[2] != null)
            this._button_okay.value = button_confirm
        if(arguments[3] != null)
            this._button_cancel.value = button_cancel
		this._button_extra.hide();
            
        if(this._button_observer['okay'+this._id] != true){
            var binderOkay = this._onConfirm.bindAsEventListener(this);
            this._button_okay.observe('click',binderOkay);
            this._button_observer['okay'+this._id] = true;
        }
        this._button_okay.show();
        if(this._button_observer['cancel'+this._id] != true){
            // :TODO: add checkpoint if button has not been observe by a handler.  prevent memory leak.
            var binderCancel = this._onCancel.bindAsEventListener(this);
            this._button_cancel.observe('click',binderCancel);
            this._button_observer['cancel'+this._id] = true;
        }
        this._button_cancel.show();

        this.show();
        return this;
    },

    showChoices: function(){
        var title = typeof arguments[0] == 'string' && arguments[0] != '' ? arguments[0] : 'Dialog Title';
        //var content = typeof arguments[1] == 'string' && arguments[1] != '' ? arguments[1] : 'Dialog Content';
        var content = arguments[1];
        var button_extra = typeof arguments[2] == 'string' && arguments[2] != '' ? arguments[2] : 'Extra';
        var button_confirm = typeof arguments[3] == 'string' && arguments[3] != '' ? arguments[3] : 'Okay';
        var button_cancel = typeof arguments[4] == 'string' && arguments[4] != '' ? arguments[4] : 'Cancel';

        if(arguments[0] != null)
            this._div_title.update(title);
        if(arguments[1] != null)
            this.setContent(content);
        if(arguments[2] != null)
            this._button_extra.value = button_extra
        if(arguments[3] != null)
            this._button_okay.value = button_confirm
        if(arguments[4] != null)
            this._button_cancel.value = button_cancel

        //if(this._button_observer['extra'+this._id] != true){
            var binderExtra = this._onExtra.bindAsEventListener(this);
            this._button_extra.observe('click',binderExtra);
            this._button_observer['extra'] = true;
        //}
        this._button_extra.show();    
        if(this._button_observer['okay'+this._id] != true){
            var binderOkay = this._onConfirm.bindAsEventListener(this);
            this._button_okay.observe('click',binderOkay);
            this._button_observer['okay'+this._id] = true;
        }
        this._button_okay.show();
        //if(this._button_observer['cancel'] != true){
            // :TODO: add checkpoint if button has not been observe by a handler.  prevent memory leak.
            var binderCancel = this._onCancel.bindAsEventListener(this);
            this._button_cancel.observe('click',binderCancel);
            this._button_observer['cancel'] = true;
        //}
        this._button_cancel.show();

        this.show();
        return this;
    },

    onextra: null,
    
    onconfirm: null,

    oncancel: null,

    onclose: null,

    setContext: function(element){
        if(this._type != this.DIALOG_CONTEXTUAL) return this;
        if(this._context_set == false){
            this._context_set = true;
            if(element)
                this._div_box.clonePosition($(arguments[0]),{'setWidth':false,'setHeight':false,'offsetTop':element.getHeight(),'offsetLeft':element.getWidth()/2});
            else
                this._moveDefault();
        }
        return this;
    },

    setContent: function(html){
        //if(this._type != this.DIALOG_FREESTYLE) return this;
        this._content_set = true;
        this._div_content.update(html);
        return this;
    },

    setExtraButtonLabel: function(label){
        this._button_extra_previous = this._button_extra.value;
        this._button_extra.value = label;
    },
    
    setOkayButtonLabel: function(label){
        this._button_okay_previous = this._button_okay.value;
        this._button_okay.value = label;
    },

    setCancelButtonLabel: function(label){
        this._button_cancel_previous = this._button_cancel.value;
        this._button_cancel.value = label;
    },

    setPreviousExtraButtonLabel: function(){
        if( this._button_extra_previous ){
            var label = this._button_extra.value;
            this._button_extra.value = this._button_extra_previous;
            this._button_extra_previous = label;
        }
    },
    
    setPreviousOkayButtonLabel: function(){
        if( this._button_okay_previous ){
            var label = this._button_okay.value;
            this._button_okay.value = this._button_okay_previous;
            this._button_okay_previous = label;
        }
    },

    setPreviousCancelButtonLabel: function(){
        if( this._button_cancel_previous ){
            var label = this._button_cancel.value;
            this._button_cancel.value = this._button_cancel_previous;
            this._button_cancel_previous = label;
        }
    },

    remove: function(){
        this._destruct();
    },

    show: function(){
        if (this._div_forcefield != '') this._div_forcefield.show();
        this._div_box.show();
    },

    hide: function(){
        if (this._div_forcefield != ''){ 
					$j('.dialog_forcefield').fadeOut(200,function(){
							// this._div_forcefield.hide();							 
					});
				}
        this._div_box.hide();
    },

    destroy: function(){
        if (this._div_forcefield != '') this._div_forcefield.hide();
        this._destruct();
    },
    
    enableDragging: function(){
        if (typeof this._dragger == 'object') return;
        this.initDragger();
    },

    _onExtra: function(){
        if(this.onextra && typeof this.onextra == 'function'){
            freturn = this.onextra();
            if( freturn == true ) this.hide();
            return;
        }
        this.hide();
    },
    
    _onConfirm: function(){
        if(this.onconfirm && typeof this.onconfirm == 'function'){
            freturn = this.onconfirm();
            if( freturn == true ) this.hide();
            return;
        }
        this.hide();
    },

    _onCancel: function(){
        if(this.oncancel && typeof this.oncancel == 'function'){
            this.oncancel();
            return;
        }
        this.hide();
        this._destruct();
    },

    _onClose: function(){
        if(this.onclose && typeof this.onclose == 'function'){
            this.onclose();
        }
        this.destroy();
    },

    _destruct: function(){
        this._button_observer['okay'+this._id] = false;
        this._button_observer['cancel'+this._id] = false;
        DIALOG.delInstance(this._id,this);
				this._div_box.remove();
        //TODO::add stopObserving
    },

    _moveDefault: function(){
        //this._div_box.setStyle({'position':'fixed','top':'50px','marginLeft':'50%','left':'-'+(this._div_box.getWidth()/2)+'px'});
        var viewport = document.viewport.getDimensions();
        this._div_box.setStyle({'position':'absolute','top':'50px','left':((viewport.width / 2) - (this._div_box.getWidth()/2))+'px'});
    },

    _positionNone: function(){
        this._div_box.setStyle({'position':'','top':'0px','marginLeft':'0px','left':'0px'});
    },

    _createContainer: function(){
        var id = arguments[0];
        var dialog_container = "";
        dialog_container += "    <div class='dialog_forcefield'><span/></div>";
        dialog_container += "    <div class='dialog_pointer'><span/></div>";
        dialog_container += "    <table width='100%' style='border-collapse:collapse' cellspacing='0' cellpadding='0'><tr><td class='trans_border_top_left'></td><td class='trans_border'></td><td class='trans_border_top_right'></td></tr>";
        dialog_container += "    <tr><td class='trans_border'></td><td class='trans_border'>";
        dialog_container += "    <div class='dialog_container'>";
        dialog_container += "        <span class='dialog_close'><a>x</a></span>";
        dialog_container += "        <h2 class='dialog_title'><span>Generic Dialog Title</span></h2>";
        dialog_container += "        <div class='dialog_content' id='dialog_content'>";
        dialog_container += "            <div id='dialog_user_content' class='dialog_user_content'></div>";
        dialog_container += "            <div class='dialog_buttons clearfix' id='dialog_buttons'>";
        /*dialog_container += "                <input type='button' class='inputsubmit' value='Okay' />";
        dialog_container += "                <input type='button' class='inputsubmit' value='Cancel' />";*/
				dialog_container += "									<div id='left_extra' class='left'></div>";
        dialog_container += "                <input type='button' style='font-size:11px;' value='Extra' class=\"inputsubmit\" />";
        dialog_container += "                <input type='button' style='font-size:11px;' value='Okay' class=\"inputsubmit\" />";
        dialog_container += "                <input type='button' style='font-size:11px;' value='Cancel' class=\"inputsubmit\" />";
        dialog_container += "            </div>";
        dialog_container += "        </div>";
        dialog_container += "    </div>";
        dialog_container += "    </td><td class='trans_border'></td></tr>";
        dialog_container += "    <tr><td class='trans_border_bottom_left'></td><td class='trans_border'></td><td class='trans_border_bottom_right'></td></tr></table>";
        return new Element('div',{'id':'generic_dialog_'+id,'class':'generic_dialog generic_dialog_'+id,'style':'display:none'}).update(dialog_container);
    }
}

Dialog.button = function(){ this.initialize.apply(this,arguments); }
Dialog.button.prototype = {
    initialize: function(){
    
    },
    create: function(){
    }
}