/**
@package : My JavaScript Objects{mjsobjects}
@name : Javascript Dynamic Dialog Object(JDDialog){HTML}
@version : 1.0 (beta) RC 1
@author : G.kamalakar
@copyright : All rights reserved by G.kamalakar
@created-date : 30-3-2006
*/
var baction = {Minimize: '', Maximize: 'maxMe', Close: 'Closeme'};
aDjwname = new Array();
var theamb = 'white';
var isdrag=false,tollbar_click=false;

if(!document.MJDCore){eval("document.write('<script type=\"text/javascript\" src=\"Core.js\"></script>');");}

var DEFA_WIN = ['Minimize','Maximize','Close'];
var TOOL_WIN = ['Close'];

//Object for creating the dialog
function JDDialog(){
    this.varibles = new Array();
    this.Dlgname = '';
    this.idDestroyed = false;
    var left = -1000, top = 0, height = 0, width = 0;
   
    this.Build_Dialog = function(title, stitle, arg1, arg2, body, dtype){
    eval(JDCore.GlobalTheme+".prototype = new coreTheam();\n var theme = new "+JDCore.GlobalTheme+"(); theme.setPath();");
        height =(JDCore.Browser()) != 'msie' ? arg1+theme.Dialog.Borders.Dimension[0] : arg1;
        width = arg2+(theme.Dialog.Borders.Dimension[2]*2);aDjwname[(aDjwname.length)] = title+"Dialog";
        this.Setaxis();
        this.Dlgname = title;
        var new_dilg = document.createElement('div');
        new_dilg.dheight = height+'px';  new_dilg.dwidth = width+'px';  new_dilg.dleft = left+'px';  new_dilg.dtop = top+'px';  new_dilg.Dlgname = title;  new_dilg.id = title+"Dialog";
        new_dilg.style.position ="absolute";new_dilg.style.visibility = "hidden";new_dilg.style.width = (width)+"px";new_dilg.style.height = (height)+"px";new_dilg.style.top = (top)+"px";new_dilg.style.left = "-1000px";
        new_dilg.innerHTML = "<table width=\"100%\" style=\"height:100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"Dialog"+title+"\">\n<tbody>\n\t<tr>\n"
        + "<td width=\""+theme.Dialog.Borders.Dimension[3]+"\"><img width=\"100%\" src=\""+theme.Dialog.Borders.Top[0]+"\" height=\""+theme.Dialog.Borders.Dimension[0]+"\"/></td>"
        + "<td valign=\"middle\" style=\"background-image:url("+theme.Dialog.Borders.Top[1]+")\">\n"
        + this.bic_table(title,stitle,dtype)+"\n"
        + "</td>\n\t</td>"
        + "<td width=\""+theme.Dialog.Borders.Dimension[1]+"\"><img src=\""+theme.Dialog.Borders.Top[2]+"\" width=\"100%\" height=\""+theme.Dialog.Borders.Dimension[0]+"\"/></td>"
        + "  <tr>\n"
        + "    <td style=\"background-image:url("+theme.Dialog.Borders.Left+")\">&nbsp;</td>\n"
        + "    <td height=\"100%\" width=\"100%\" style=\"background-color: "+theme.Dialog.backgroundColor+";\"><div id=\"body_"+title+"\" name=\"body_"+title+"\">"+body+"</div></td>\n"
        + "    <td style=\"background-image:url("+theme.Dialog.Borders.Right+")\">&nbsp;</td>\n"
        + "  </tr>\n"
        + "  <tr>\n"
        + "    <td><img src=\""+theme.Dialog.Borders.Bottom[0]+"\"/></td>\n"
        + "    <td><img src=\""+theme.Dialog.Borders.Bottom[1]+"\" width=\"100%\" height=\""+theme.Dialog.Borders.Dimension[2]+"\" /></td>\n"
        + "    <td><img src=\""+theme.Dialog.Borders.Bottom[2]+"\"/></td>\n"
        + "  </tr>\n"
        + "\n</tbody></table>\n</div>\n";
    document.body.appendChild(new_dilg);
    if(JDCore.Browser() == "msie"){
        document.getElementById(title+"Dialog").setAttribute("onmousedown",this.selectDialog);
        document.getElementById(title+"Dialog").setAttribute("onmouseup",new Function("isdrag=false;tollbar_click=false;"));
    } else {
        document.getElementById(title+"Dialog").addEventListener("mousedown",this.selectDialog,false);
        document.getElementById(title+"Dialog").addEventListener("mouseup",new Function("isdrag=false;tollbar_click=false;"),false);
        }
    }
    
    this.Show = function(){
    if(!this.idDestroyed){
        var udlg = this.Dlgname+'Dialog';setzindex(udlg); document.getElementById(udlg).style.visibility = "visible";
        document.getElementById(udlg).style.left = document.getElementById(udlg).dleft; document.getElementById(udlg).style.top = document.getElementById(udlg).dtop;
        }
    }
    
    this.Setaxis = function(){
        if(!this.idDestroyed){
        	if(arguments.length <= 0){
            if(JDCore.Browser() == 'msie'){
                top =  Math.floor((document.documentElement.offsetHeight - height)/2);
                left =  Math.floor((document.documentElement.offsetWidth - width)/2);
            } else {
                top = Math.floor((window.innerHeight - height)/2);
                left = Math.floor((window.innerWidth - width)/2);
            }
          } else {
          	top = arguments[0];
          	left = arguments[1];
          }
        }
    }
    
    this.Destroy = function(){
        var chi = document.getElementById('Dialog'+this.Dlgname);
        var parent = chi.parentNode; parent.removeChild(chi); this.idDestroyed = true;
    }
    
    this.bic_table = function(vid,vdtle,Dtyp){
    eval(JDCore.GlobalTheme+".prototype = new coreTheam();\n var theme = new "+JDCore.GlobalTheme+"(); theme.setPath();");
    var hpanal_w = theme.Dialog.Buttons.Dimension[1];
        var tad= "<table width=\"100%\" border=\"0\" id=\""+vid+"tollbar\" cellpadding=\"0\" cellspacing=\"0\" summary=\"BorderIcons\">\n<tbody>\n\t<tr>\n";
        tad += "\t\t<td><div style=\"height:"+theme.Dialog.Borders.Dimension[0]+"px;\" onmousedown=\"tollbar_click=true;\" align=\"center\" id=\""+vid+"td\" name=\""+vid+"td\"><div style=\"padding: "+theme.Dialog.Title.Distances+";\">"+vdtle+"</div>";
        if(JDCore.Browser() != 'msie')tad += "<div style=\"position: relative; z-index: 1;bottom: "+parseInt((theme.Dialog.Borders.Dimension[0]/2)*1.5)+"px;height:"+theme.Dialog.Borders.Dimension[0]+"px;\"></div>";
        tad +="</div></td>\n";
        for(var i = 0; i < Dtyp.length; i++){
            tad += "\t\t<td width=\""+theme.Dialog.Buttons.Dimension[1]+"\" style=\"padding: "+theme.Dialog.Buttons.Distances+";\"><img style=\"background:url("+eval('theme.Dialog.Buttons.'+Dtyp[i])+")  0px 50%; width:"+hpanal_w+"px;\" alt=\""+Dtyp[i]+"\" id=\""+Dtyp[i]+"\" title=\""+Dtyp[i]+"\" src=\""+dSkin_Dir+"/spacer.gif\"\n"
            + "\t\tonmouseover=\"this.style.backgroundPosition = '"+(-hpanal_w) +"px 50%';\"\n"
            + "\t\tonclick=\"this.style.backgroundPosition = '"+(-hpanal_w*2) +"px 50%';"+baction[Dtyp[i]]+"('"+vid+"Dialog');\"\n"
            + "\t\tonmouseout=\"this.style.backgroundPosition = '0px 50%';\">\n\t</td>\n";
        }
        tad += "\t</tr>\n</tbody>\n</table>\n";
        return tad;
    }
    
    this.selectDialog = function(){
      var x,y,dxa,dya;var dobj; var handle = (!arguments[0])? event : arguments[0];
      var fobj = (!arguments[0])? handle.srcElement : handle.target;
      while (fobj.id.indexOf("Dialog") == -1 && fobj.style.position != "absolute"){
        fobj = (!arguments[0])? fobj.parentElement : fobj.parentNode;}
        setzindex(fobj.id);
    if(tollbar_click)
      if (fobj.id.indexOf("Dialog") != -1){
        isdrag = true; dobj = fobj;
        dxa = parseInt(dobj.style.left+0); dya = parseInt(dobj.style.top+0);
        x = handle.clientX; y = handle.clientY;
        document.onmousemove = function (){ if(isdrag){var handle = (!arguments[0])? event: arguments[0];
            dobj.style.left = (dxa + handle.clientX - x)+'px';
            dobj.style.top = (dya + handle.clientY - y)+'px';
            return false;} };
        return false;
       }
    }
}

function Closeme(){
var mystyle = document.getElementById(arguments[0]);
    mystyle.style.visibility = "hidden"; mystyle.dleft = mystyle.style.left;
    mystyle.dleft = mystyle.style.top; mystyle.dtop = mystyle.style.top; mystyle.style.left = '-1000px';
}

function maxMe(){
var mystyle = document.getElementById(arguments[0]);
    if(mystyle.style.width != "99%" && mystyle.style.height != "99%"){
    	mystyle.dtop = mystyle.style.top; mystyle.dleft = mystyle.style.left;
        mystyle.style.width = "99%"; mystyle.style.height = "99%"; mystyle.style.top = '0px'; mystyle.style.left = '0px';
    } else {
        mystyle.style.width = mystyle.dwidth; mystyle.style.height = mystyle.dheight; mystyle.style.top = mystyle.dtop; mystyle.style.left = mystyle.dleft;
    }
}

function setzindex(dname){
    for(var i = 0;i < aDjwname.length;i++){
        if(aDjwname[i] == dname){
            document.getElementById(aDjwname[i]).style.zIndex = aDjwname.length;
        } else {
            document.getElementById(aDjwname[i]).style.zIndex = aDjwname.length-i;
        }
    }
}