/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
 
Af.HtmlDialog = Class.create();

Af.HtmlDialog.prototype = {
    initialize: function(title, handler, content) {
	    this._initializeDialog(title, handler, content);
    },
   
    _initializeDialog: function(title, handler, content) {
		this.title = title;
	    this.handler = handler;
		this.content = content;
        // The title area drag target
		this.titleElement = null;
        // Where the actual title text is
		this.titleSpan = null;
		this.cb = null;
		this.visible = false;
		this.width = -1;
		this.firstTime = true;
		this.left = -1;
		this.top = -1;
		this.obj = null;
	    this.isCreateMode = false;
	    this.contentContainer = null;	
	    this.container = null;	
	    this.isDialog = true;
	    this.childDialog = null;
	    if (this.handler != null && this.handler.isDialog && this.handler != this) {
            this.handler.childDialog = this;
        }
        this.armedButton = null;
    },
   
    showHTMLTemplate: function (url, container, sourceElementId) {
        this.container = container;
        this.sourceElementId = sourceElementId;
        if (this.element == null) {
			this.createElement();
        }
        if (this.tl == null) {
            this.tl = new Af.TemplateLoader(url, null, sourceElementId, this.dialogArea);
            this.tl.listener = this;
            this.tl.loadTemplate(url);
        } else {
            this.makeVisible();
        }
	},
	
	newObject: function(className, namingAttr, namingAttrValue, dl) {
      
        if (namingAttr == null) {
            naminAttr = 'name';
        }
        if (namingAttrValue == null) {
            namingAttrValue = '';
        } else {
            namingAttrValue = dtCache.getUniqueValue(dl, namingAttr, namingAttrValue);
        }
      
        var obj = new Object();
        obj[namingAttr] = namingAttrValue;
        if (dl != null) {
            obj.__parent = dl.__parent;
            obj.__className = dl.__className;
            obj.__index = dl.length;
            setUniqueId(dl, obj);
            dl.push(obj);
        } else {
            obj.__className = className;
            obj.__id = className + ".0";
            obj.__fullId = className + ".0";
        }
      
        this.isCreateMode = true;
     
        dtCache.beginTx();
        dtCache.addCreateChange(obj, dl);
        this.postCreate(obj);
      
        dtCache.endTx();

        this.setObj(obj);
        return obj;
    },
    
    postCreate: function(obj) {
    
    },
	
	setObj: function(obj, mode) {
        this.obj = obj;
        this.isCreateMode = mode;
	},
	
	templateLoaded: function() {
        if (this.handler != null && this.handler.templateLoaded != null) {
            this.handler.templateLoaded();
        }
	},
	
	makeVisible: function() {
	    this.visible = true;
	    var p;
		if (typeof this.container == "string") {
            p = document.getElementById(this.container);
		} else {
            p = this.container;
		}
		
	    this.hideSelectBoxes();
		p.appendChild(this.element);
		
		if (this.firstTime) {
            this.firstTime = false;
            var t;
            if (this.left < 0) {
                t = p.offsetLeft + (p.offsetWidth - this.element.offsetWidth) / 2;
            } else {
                t = p.offsetLeft + this.left;
            }
            if (t > 0) {
                this.element.style.left = t + "px";
            }
            if (this.top < 0) {			  
                t = p.offsetTop + (p.offsetHeight - this.element.offsetHeight) / 2;				
            } else {
                t = p.offsetTop + this.top;
            }
            if (t > 0) {
                //  this.element.style.top = t + "px";
            }
            draggable = new Rico.Draggable(this.sourceElementId, this.element);
            draggable.allowedTarget = this.titleElement;
            dndMgr.registerDraggable(draggable);
		}
	},
	 
    close: function(ev) {
	    if (this.handler != null && this.closeDialog) {
            this.handler.closeDialog(this);
		} else {
            this.hide();
		}
    },
	 
    hide: function() {
	    if (this.childDialog != null) {
            this.childDialog.hide();
            this.childDialog = null;
		}
		if (this.handler != null && this.handler.isDialog) {
            this.handler.childDialog = null;
        }
	    this.visible = false;
	   
	    if (this.element != null && this.container != null) {
	        var p;
			if (typeof this.container == "string") {
                p = document.getElementById(this.container);
			} else {
                p = this.container;
			}
			try{
				p.removeChild(this.element);
				this.showSelectBoxes();
			}catch(err){				
			}			
		}
		
    },
	 
    setLocation: function(x, y) {
        this.firstTime = false;
        this.element.style.left = x + "px";
        this.element.style.top = y + "px";
    },
	 
	
    createElement: function() {
        var div = document.createElement('div');
        if (is_ie) {
            div.style.overflow = "visible";
        } else {
            div.style.overflow = "auto";
        }
        if (this.width != -1) {
            div.style.width = this.width;
        }
        div.className = 'Dialog';
        var table = document.createElement('table');
        table.cellSpacing = '0px';
        table.cellPadding = '0px';
        div.appendChild(table);
		
        var tbody = document.createElement('tbody');
        table.appendChild(tbody);
		
        var td;
        var tr;
		
        tr = document.createElement('tr');
        tbody.appendChild(tr);
        td = document.createElement('td');
        td.className = 'DialogTitle';
        tr.appendChild(td);
        this.titleElement = td;
        var span = document.createElement('span');
        span.className = "DialogTitleSpan";
        this.titleSpan = span;
        this.setTitle(this.title);
        td.appendChild(span);
	
        this.cb = document.createElement('a');
        this.cb.className = 'DialogTitleButton';
        var img = document.createElement('img');
        img.className = 'DialogCloseButtonImage';
        img.src = '/images/close.gif';
        this.cb.appendChild(img);
		
        td.appendChild(this.cb);
        this.cb.onclick = this.close.bindAsEventListener(this);
		
        tr = document.createElement('tr');
        tbody.appendChild(tr);
        td = document.createElement('td');

        tr.appendChild(td);
        this.contentContainer = td;
        if (this.content != null) {
            if (typeof this.content == "string") {
                td.innerHTML = this.content;
            } else {
                td.appendChild(this.content);
            }
        }
		
        this.dialogArea = td;
		
        this.element = div;
        this.element.onkeydown = this.keyDown.bindAsEventListener(this);
		
    },

    /**
     * Set the title text of the dialog
     * 
     * @param title the text to set
     *
     * 
     */
    setTitle: function(title) {
        this.title = title;
        this.titleSpan.innerHTML = title;
    },
	 
    setContent: function(content) {
	    this.content = content;
	    if (this.content != null && this.contentContainer != null) {
		    if (typeof this.content == "string") {
                this.contentContainer.innerHTML = this.content;
		    } else {
                this.contentContainer.appendChild(this.content);
			}
		}
    },
	
    centerDialog: function(width, height) {  
			
        if (this.element == null) {
            this.createElement();
        }	
        var fullHeight = this.getViewportHeight();

        var fullWidth = this.getViewportWidth();

 

        var scLeft,scTop;

        if (self.pageYOffset) {

            scLeft = self.pageXOffset;

            scTop = self.pageYOffset;

        } else if (document.documentElement && document.documentElement.scrollTop) {

            scLeft = document.documentElement.scrollLeft;

            scTop = document.documentElement.scrollTop;

        } else if (document.body) {

            scLeft = document.body.scrollLeft;

            scTop = document.body.scrollTop;

        } 

            

        var topMargin = scTop + ((fullHeight - height) / 2);

        if (topMargin < 0) { topMargin = 0; }
			
        if(this.element != null)
        this.element.style.top = topMargin + "px";
        if(this.element != null)
        this.element.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";

    },

    getViewportHeight: function() {

        if (window.innerHeight!=window.undefined) return window.innerHeight;

        if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;

        if (document.body) return document.body.clientHeight; 

        return window.undefined; 

    },

    getViewportWidth: function() {

        if (window.innerWidth!=window.undefined) return window.innerWidth; 

        if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 

        if (document.body) return document.body.clientWidth; 

        return window.undefined; 

    },
      
    hideSelectBoxes: function() {
        // parent will manage it.
        if (this.handler != null && this.handler.isDialog) {
            //debugA("returning");
            return;
        }
        if (is_ie && !is_ie7) {
            var elements = document.getElementsByTagName("SELECT");
            if (elements) {
                for(var i = 0; i < elements.length; i++) {
                    var e = elements[i];
                    e.style.visibility = "hidden";
                }
            }
            var ec = new Af.ElementCollection(this.element);
            var iter = ec.getIterator();
            while (iter.hasNext()) {
                var e = iter.next();
                if (e.tagName == "SELECT") {
					e.style.visibility = "visible";
                }
            }
        }
    },
	  
    showSelectBoxes: function() {
        // parent will manage it.
        if (this.handler != null && this.handler.isDialog) {
            //debugA("returning");
            return;
        }
        if (is_ie && !is_ie7) {
            var elements = document.getElementsByTagName("SELECT");
            if (elements) {
                for(var i = 0; i < elements.length; i++){
                    var e = elements[i];
                    elements[i].style.visibility = "visible";
                }
            }
        }
    },
	  
    keyDown: function(e) {
        var eventTarget = e.target ? e.target : e.srcElement;
	      
        var c = e.keyCode ? e.keyCode : e.which;
        if (c == 13) {
            setTimeout(this.loseFocus.bind(this, eventTarget), 1);
        }
        return true;
    },
	   
    loseFocus: function(t) {
        if (is_ie) {
            try {				
				// t.blur();
            }
            catch(e) {};
        }
        setTimeout(this.doClick.bind(this, t), 1);
    },
	   
    doClick: function(t) {			   
        if (this.armedButton != null && this.armedButton.onclick) {	
            if(t != null && (t.value != "Submit" && t.value != "Cancel" && t.value != "Done" && t.value != "Save" && t.value != "Ok" && t.value != "Continue")){				 			   
				if(t.type != "button" && t.type != "submit" && t.type != "textarea" ){										
                    this.armedButton.onclick();
				}
            }
        }
    }
	   
}
