﻿DialogManager = function(){
    this.oContainer = null;
    this.oTitle = null;
    this.oMessageBody = null;
    this.startOverAfterClose = true;
    this.oCloseBtn = new Image();
    this.oCloseBtn.src = 'images/honda/dialog/btnDialogClose.gif';
    
    this.setMessageBody = function(sBodyMsg){
        this.checkStatus();
        this.oMessageBody.innerHTML = sBodyMsg;
    };
    
    this.setCloseBtnSrc = function(sImgSrc){
        this.oCloseBtn.src = sImgSrc;
    };
    
    this.setTitle = function(sTitle){
        this.checkStatus();
        this.oTitle.innerHTML = sTitle;
    };
    this.showClient = function(dialogParams,point, startOver){
        if(point != null)
        {
            var framePoint = awi_findPos(awi_$(gObjects.IFrameID));
            point = [(point[0] + (framePoint[0]-8)), ((framePoint[1]-24)+ (point[1]))]
        }            
        try{
            this.startOverAfterClose = startOver;
            this.checkStatus();
            this.setTitle(dialogParams[0]);
            this.setMessageBody(dialogParams[1]);
            this.move(point);
            this.oContainer.style.display = 'block';
            top.awi_Veil.show(true);
        }
        catch(e)
        {
            alert(e);
        }         
    };
    
    this.show = function(dialogParams,point, startOver,fromServer){ 
            this.showClient(dialogParams,point, startOver);
    };
    
    this.hide = function(){       
        this.oContainer.style.display = 'none';
        this.move([-1000,-1000]);
        top.awi_Veil.show(false);
        top.RequestObject.DialogClosed = true;
        if (this.startOverAfterClose)
        {
            window.location.href=window.location.href;
        }
    };
    
    this.checkStatus = function(){
        if(this.oContainer == null)
        {
            this.init();
        }
    };
    
    this.init = function(){
        this.oContainer = document.createElement('div');
        this.oContainer.dialogManger = this;
        this.oContainer.className = 'dialogbox';
        
        
        var table = document.createElement('table');
        table.setAttribute('cellSpacing', '0');
        table.setAttribute('cellPadding', '0');
        
        var tBody = document.createElement('tbody'); 
        
        var tRow = document.createElement('tr');
        var tlCell = document.createElement('td');
        tlCell.innerHTML = '&nbsp;';
        var trCell = document.createElement('td');
        trCell.innerHTML = '&nbsp;';
        var cCell = document.createElement('td');
        var bCell = document.createElement('td');
        bCell.className = 'dialog_title_cell';
        
//        var closeText = document.createElement('div');
        if(gObjects.Theme != 'Acura')
        {
            this.oCloseBtn.width = '60';
            this.oCloseBtn.heigth = '18';
        }
//        else
//        {
//            closeText.innerHTML = '&nbsp;';
//        }
//        closeText.className = 'close_text';         
        var closeCont = document.createElement('div');
//        closeCont.className = 'dialog_close_group';
//        closeCont.appendChild(closeText);
        this.oCloseBtn.onclick = DialogManager.close;        
        closeCont.appendChild(this.oCloseBtn);
        bCell.appendChild(closeCont);
        bCell.style.textAlign = 'right';
               
        this.oTitle = document.createElement('div');
        cCell.appendChild(this.oTitle);
        tlCell.className = 'dialog_left_corner';
        cCell.className = 'dialog_title_cell';
        this.oTitle.className = 'dialog_title_text';
        trCell.className = 'dialog_right_corner'; 
        
        tRow.appendChild(tlCell);
        tRow.appendChild(cCell);
        tRow.appendChild(bCell);
        tRow.appendChild(trCell);
        
        tBody.appendChild(tRow);
        
        var bRow = document.createElement('tr');
        var blCell = document.createElement('td');
        
        var bbCell = document.createElement('td');
        var brCell = document.createElement('td');
        
        blCell.className = 'dialog_bottom_left_corner';    
        blCell.innerHTML = '&nbsp;';   
        brCell.innerHTML = '&nbsp;';
        bbCell.className = 'dialog_message_cell';
        bbCell.colSpan = 2;
        brCell.className = 'dialog_bottom_right_corner';
        
        this.oMessageBody = document.createElement('div');
        
        this.oMessageBody.className = 'dialog_message_body';
        bbCell.appendChild(this.oMessageBody);
        bRow.appendChild(blCell);
        bRow.appendChild(bbCell);
        bRow.appendChild(brCell);
        tBody.appendChild(bRow);
        
        table.appendChild(tBody);        
        this.oContainer.appendChild(table); 
        document.body.appendChild(this.oContainer);
    };
    
    this.move = function(point){
        var left;
        var top;
        if(point == null)
        {
            left = _getWindowWidth();
            left = (Math.floor(left/2)) - 200;
            top = _getWindowHeight();
            top = Math.floor(top *.33);
        }
        else
        {
            left = point[0];
            top = point[1];
        }
        this.oContainer.style.top =  top + 'px';
        this.oContainer.style.left =  left + 'px';
    };
    
    function _getWindowWidth() {
	    return awi_winW();
	}

    function _getWindowHeight() {
	    return awi_winH();
	}
}
DialogManager.close = function(e) {
   
    if (!e) e = window.event;
    var node = e.target ? e.target : e.srcElement;
    var count = 0;
    while ((node != null) && (count < 20)) 
    {
	    if (node.dialogManger) 
	    {
		    node.dialogManger.hide();
		    return false;
	    }
	    node = node.parentNode;
	    count++;
    }
    return false;
}
