//$Id: ZDBDialogUtil.js,v 1.1 2008/11/25 16:25:50 sathyav Exp $

var ZDBDialogUtil = new function()
{
  this.i = 0;
  this.zCounter = 2000;
  this.blinkCounter = 0;
  this.intervalID = null;
  this.dialogs = new Array();
  this.openedDialog = 0;
  this.showDialog = function(div, params)
  {
    		
    for(prop in params)
    {
      params[prop.toUpperCase()] = params[prop];
    }
    var dlg = div;
    if(params["ADDFRAME"])
    {
      dlg = this.createDialog(); 
      dlg.dlgBodyDIV.innerHTML="";
      if(div!=null)
      {
        dlg.dlgBodyDIV.appendChild(div);
        dlg.bodyContent = div;
      }
    }
		
		if(dlg.bodyContent==null)
		{
		ZDBUtil.setNamedChildEls(dlg,dlg,"elname"); // I need the dlgBodyContent even it is your own dlg...CURSOR ISSUE.
		}
		
    ZDBDialogUtil.openedDialog++;
    ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1]=dlg;
    ZDBDialogUtil.zCounter =ZDBDialogUtil.zCounter + 3;
		
    if(params["TITLE"])
    {
			dlg.dlgTitle.innerHTML = params["TITLE"];
    }

    if(params["TITLECLASS"])
    {
      dlg.dlgIcon.className = params["TITLECLASS"];
      dlg.setIconClass = function(className)
      {
        dlg.dlgIcon.className = className;
      }
    }
	
    dlg.params = params;		
		
    dlg.style.position="absolute";
    dlg.style.visibility = "hidden";
    dlg.style.display="block";
		
    document.body.appendChild(dlg);
    ZDBDialogUtil.positionCenter(dlg);
    if(params["ADDFRAME"] || params["SHADOW"])
    {
      ZDBUtil.addShadow(dlg);
    }
    ZDBUtil.addIframe(dlg);
    ZDBDialogUtil.focusDialog = dlg;
    dlg.style.zIndex=ZDBDialogUtil.zCounter;
    if(params["ZINDEX"])
    {
      dlg.style.zIndex = params["ZINDEX"];
    }
		
    if(params["MODAL"])
    {
      this.freezeBackground();
    }
    dlg.style.visibility = "visible";
		if(dlg.bodyContent!=null)
    {
      setTimeout ( "ZDBDialogUtil.setOverflow()", 100 );
    }
		this.setOverflow = function()
		{	
      dlg.bodyContent.style.overflow="hidden"; //hack for cursor issues in firefox
			ZDBUtil.setNamedChildEls(dlg.bodyContent, dlg.bodyContent);
			if(dlg.bodyContent.defaultFocus!=null)
			{
				dlg.bodyContent.defaultFocus.select();
			}
    }
    document.onkeydown = ZDBDialogUtil.whichEvent;
  }
	
  this.setFocusDialog = function(el)
  {
    var dlg = (el.getAttribute("isDialog") == "true")? el:ZDBUtil.getParentWithAttr(el, "isDialog");
		
    if(dlg == ZDBDialogUtil.focusDialog)
    {
      return;	
    }
    ZDBDialogUtil.focusDialog  = dlg;
  }

  this.positionCenter = function(el)
  {
		
    var dlg = el;
	var adjustElTop = (BROWSER.IE)?0:document.body.scrollTop;
    var posX = (dlg.params["LEFT"]!=null)? dlg.params["LEFT"] : ((document.body.clientWidth/2)-((dlg.offsetWidth)*0.40));
    var posY = (dlg.params["TOP"]!=null)? dlg.params["TOP"] : ((document.body.clientHeight/2)-((dlg.offsetHeight)*1.00)) + adjustElTop; 
    dlg.style.left=posX+"px";
    dlg.style.top=posY+"px";
  }
  this.freezeBackground = function()
  {
    if(ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer == null)
    {
      var freezeLayer = document.createElement("DIV");
      freezeLayer.className = "freezeLayer";
      freezeLayer.style.zIndex = parseInt(ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].style.zIndex)-1;
      ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer = freezeLayer;
    }
    document.body.appendChild(ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer);
    ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer.onclick = ZDBDialogUtil.alertModalDlfEffect;
  }
	
  this.alertModalDlfEffect = function(ev)
  {
    var dlg = ZDBDialogUtil.focusDialog;
    ZDBDialogUtil.blinkCounter=0;
    ZDBDialogUtil.intervalID = setTimeout(ZDBDialogUtil.blinkEffect, 100);
  }
  
  this.blinkEffect = function(ev)
  {
    var dlg = ZDBDialogUtil.focusDialog;
    if(ZDBDialogUtil.blinkCounter<3)
    {
      if((ZDBDialogUtil.blinkCounter%2)!=0)
      {
        dlg.className="normalTitleStyle";
      }
      else
      {
        dlg.className="modalBlinkEffect1";
      }
      ZDBDialogUtil.blinkCounter++;
      setTimeout(ZDBDialogUtil.blinkEffect,100);
    }
    else
    {
      dlg.className="normalTitleStyle";
    }
  }
	
  this.createDialog = function()
  {
    
      var dlg = document.getElementById("ZDBDialogTemplate").cloneNode(true);
	  dlg.id="";
    ZDBUtil.setNamedChildEls(dlg,dlg,"elname");		
    return dlg;
  }
	
  this.whichEvent = function(ev)
  {
    var keyCode = (BROWSER.IE)?window.event.keyCode:ev.which;
	switch(keyCode)
	{
		case 27: {
					if(!ZDBDialogUtil.focusDialog.params["NOESC"])
					{
						 ZDBDialogUtil.closeDialog();
     					 ZDBUtil.stopEventBubble(ev);
					}	
					break;
				 }
		
		case 13: {
					if(ZDBDialogUtil.focusDialog.params["ONENTER"])
					{
						ZDBDialogUtil.focusDialog.params["ONENTER"]();
					}
					break;
				 }
		default:{
					return true;
				}
	}
    
   
  }
  
  this.dragDialog = function(el,ev)
  {
    ZDBDialogUtil.setFocusDialog(el);
    var whichDialog =  ZDBDialogUtil.focusDialog;				 
    whichDialog.style.cursor="move";
    if(BROWSER.IE)
    {
      this.diffLeft = window.event.clientX + document.body.scrollLeft - parseInt(findPosX(whichDialog));
      this.diffTop = window.event.clientY + document.body.scrollTop - parseInt(findPosY(whichDialog));
    } 
    else if (BROWSER.NETSCAPE || BROWSER.OPERA) 
    {
      this.diffLeft = ev.pageX - parseInt(findPosX(whichDialog));
      this.diffTop = ev.pageY - parseInt(findPosY(whichDialog));
    }
    ZDBUtil.stopEventBubble(ev);
    document.onmousemove = ZDBDialogUtil.moveDialog;
    document.onmouseup = ZDBDialogUtil.releaseDialog;
  }
	
  this.moveDialog = function(ev)
  {
    var whichDialog = ZDBDialogUtil.focusDialog;
    if (BROWSER.IE) 
    {
      var left = window.event.clientX + document.body.scrollLeft - ZDBDialogUtil.diffLeft;
      var top = window.event.clientY + document.body.scrollTop - ZDBDialogUtil.diffTop;
      left = (left >= 0) ? left : 0;
      top = (top >= 0) ? top : 0;
      whichDialog.style.left = left+"px";
	  whichDialog.style.top = top+"px";     
    } 
    else if (BROWSER.NETSCAPE || BROWSER.OPERA) 
    {
      whichDialog.style.left = ((ev.pageX - ZDBDialogUtil.diffLeft > 0) ? ev.pageX - ZDBDialogUtil.diffLeft : 0) + "px";
      whichDialog.style.top = ((ev.pageY - ZDBDialogUtil.diffTop > 0) ? ev.pageY - ZDBDialogUtil.diffTop : 0) + "px";
    }
	
	if(whichDialog.iframe!=null)
	  	{
			whichDialog.iframe.style.left = whichDialog.style.left;
			whichDialog.iframe.style.top = whichDialog.style.top;
		}
		
    ZDBUtil.stopEventBubble(ev);
  }

  this.releaseDialog = function(ev)
  {
    var whichDialog = ZDBDialogUtil.focusDialog;
    whichDialog.style.cursor = "default";
    document.onmousemove = null;
    document.onmouseup = null;
    ZDBUtil.stopEventBubble(ev);		
  }

	
  this.closeDialog = function(closeBtn, ev, arg)
  {
    var whichDialog = null;
    if(!closeBtn)
    {
      whichDialog = ZDBDialogUtil.focusDialog;
    }
    else
    {
      whichDialog = ZDBUtil.getParentWithAttr(closeBtn, "isDialog");
    }
    this.closeThisDialog(whichDialog, arg);
    if(ev != null)
    {
      ZDBUtil.stopEventBubble(ev);
    }

  }

  this.closeThisDialog = function(whichDialog,arg)
  {
    if((whichDialog.params["ONCLOSE"])&&(!whichDialog.params["ONCLOSE"](whichDialog,arg)))
    {
      return true;
    }
		
    if(whichDialog.bodyContent!=null)
    {
      whichDialog.bodyContent.style.overflow="";
    }
		
		
    if(whichDialog.params["DONTREMOVE"] && whichDialog.params["ADDFRAME"])
    {
      document.getElementById("ZDBDialogHiddenDiv").appendChild(whichDialog.bodyContent);
    }
    else if(whichDialog.params["DONTREMOVE"])
    {
      document.getElementById("ZDBDialogHiddenDiv").appendChild(whichDialog);
    }
		
    try
    {
      if(ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer)
      {
			
        document.body.removeChild(ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1].freezeLayer);
      }
    }
    catch(e)
    {
    }
	
	ZDBUtil.removeIframe(whichDialog);
    whichDialog.parentNode.removeChild(whichDialog);
    
    ZDBDialogUtil.openedDialog--;
    if(ZDBDialogUtil.openedDialog !=0)
    {
      ZDBDialogUtil.focusDialog = ZDBDialogUtil.dialogs[ZDBDialogUtil.openedDialog-1];
    }
    else
    {
      document.onkeydown= null;
    }
  }							   
}


var BROWSER = new function()
{
    this.NETSCAPE = 0; 
    this.FIREFOX = 0;
    this.OPERA = 0;
    this.MyOwn = 0;
    this.IE = 0;

    if(document.all)
    {
        this.IE = 1; 
    }
    else if(document.getElementById)
    {
        this.NETSCAPE = 1; 
        this.FIREFOX = 1;
        this.OPERA = 1;
        this.MyOwn = 1;
    }
}
