//---------------------------------------------------------
//JS FILE REQUIRES YGWIN obj
//JS FILE REQUIRES DOMelement obj
//JS FILE REQUIRES function page_to_src
//JS FILE REQUIRES removeElementbyName
//---------------------------------------------------------

var YGDrag = {
    BIG_Z_INDEX : 10000,
    LAST_Z_INDEX : 10001,
    isDragging : false,
    itemindrag : null,
    mouseXinit : null,
    mouseYinit : null,
    lastdragged : null,

    makeDraggable : function(draggableitem,itemhandle) {
        itemhandle.draggableitem   = draggableitem;
        itemhandle.onmousedown     = YGDrag.onMouseDown;
        itemhandle.style["cursor"]="move";

        initPosition = YGDragUtils.findPos(draggableitem);
        draggableitem.initPositionX= initPosition.x;
        draggableitem.initPositionY= initPosition.y;
        draggableitem.style["position"]="absolute";
        draggableitem.style["left"]= initPosition.x+"px";
        draggableitem.style["top"]= initPosition.y+"px";
        draggableitem.width = parseInt(draggableitem.style["width"]);
        draggableitem.height= parseInt(draggableitem.style["height"]);
        draggableitem.originalZIndex  = draggableitem.style["zIndex"];
        draggableitem.originalOpacity = draggableitem.style["opacity"];
    },

    onMouseDown : function(event) {

        event = YGDragUtils.fixEvent(event);
        targ  = YGDragUtils.findTarget(event);

        while (!targ.draggableitem && targ!=null)
            targ = targ.parentNode ? targ.parentNode : targ.parentElement;
        if (targ==null) return false;
        
        if (YGDrag.lastdragged)
            YGDrag.lastdragged.style["zIndex"]  = YGDrag.lastdragged.originalZIndex;

        ditem = targ.draggableitem;
        ditem.originalLeft = parseInt(ditem.style["left"]);
        ditem.originalTop  = parseInt(ditem.style["top"]);

        document.onmousemove = YGDrag.onMouseMove;
        document.onmouseup = YGDrag.onMouseUp;
        YGDrag.itemindrag = ditem;
        YGDrag.isDragging = true;
        YGDrag.mouseXinit = event.clientX;
        YGDrag.mouseYinit = event.clientY;

        return false;
    },

    onMouseMove : function(event) {
        event = YGDragUtils.fixEvent(event);
        if (YGDrag.isDragging)
        {
            ditem = YGDrag.itemindrag;
            ditem.style["zIndex"]  = YGDrag.BIG_Z_INDEX;
            ditem.style["opacity"] = 0.75;
            YGDragUtils.repositionItem(event,ditem);
        }

        return false;
    },

    onMouseUp : function(event) {
        event = YGDragUtils.fixEvent(event);

        if (YGDrag.itemindrag)
        {
            ditem = YGDrag.itemindrag;
//            ditem.style["zIndex"] = ditem.originalZIndex;
            ditem.style["zIndex"] = YGDrag.LAST_Z_INDEX;
            ditem.style["opacity"]= ditem.originalOpacity;
            YGDrag.lastdragged = ditem;
        }


        YGDrag.itemindrag = null;
        YGDrag.isDragging = false;
        YGDrag.mouseXinit = null;
        YGDrag.mouseYinit = null;
        document.onmousemove = null;
        document.onmouseup = null;

        return false;
    }

};

var YGDragUtils = {

    fixEvent : function (e) {
        return (!e) ? window.event : e;
    },

    findPos : function (obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
        }
        return {"x":curleft, "y":curtop};
    },

    findTarget : function (e) {
        if (e.target) targ = e.target;
        else if (e.srcElement) targ = e.srcElement;
      if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;
        return targ;
    },

    repositionItem : function (event,ditem) {
        eventx    = event.clientX;
        eventy    = event.clientY;

        newleft   = (ditem.originalLeft + eventx - YGDrag.mouseXinit)*1;
        newtop    = (ditem.originalTop + eventy - YGDrag.mouseYinit)*1;

        ditem.style["left"] = newleft+"px";
        ditem.style["top"]  = newtop+"px";
    }

};

//-------------------------------------------------------------------------------------------------
var g_windowid =0;

/*
var g_img_blux =page_to_src('images/yg2_dialog_x_unselected.gif');
var g_img_redx =page_to_src('images/yg2_dialog_x_selected.gif');
var g_img_corn =page_to_src('images/yg2_dialog_top_corners.gif');
var g_img_tbar =page_to_src('images/yg2_dialog_top_bar.gif');
*/

var g_dialog_images = [ dialogimg('x_unselected'), dialogimg('x_selected'), dialogimg('top_corners'), dialogimg('top_bar') ];
function dialogimg (imgstr) { return page_to_src('images/yg2_dialog_'+imgstr+'.gif'); }
function dialog_preload_images()
{
   if (g_windowid==0)
   {   //preload images
       img = new Array();
       for(i=0; i<g_dialog_images.length; i++)
       {
          img[i] = new Image();
          img[i].src = g_dialog_images[i];
       }
   }
}
function create_dialog(title,body)
{
   dialog_preload_images();
   id= g_windowid++;
//   open_grey_screen(id);
   open_dialog_box(id,200,100,title,body);
   dialog = document.getElementById( 'dialog_div_id_'+id );
   handle = document.getElementById( 'handle_div_id_'+id );
   YGDrag.makeDraggable( dialog, handle );
}

/*
function create_okcanceldialog(title,body)
{
   dialog_preload_images();
   id= g_windowid++;
//   open_grey_screen(id);
   okcancel="<div align=right><input type=button value=OK onclick='close_dialog("+id+");'>&nbsp;<input type=button value=Cancel onclick='close_dialog("+id+");'></div>";
   open_dialog_box(id,200,100,title,body+okcancel);
   dialog = document.getElementById( 'dialog_div_id_'+id );
   handle = document.getElementById( 'handle_div_id_'+id );
   YGDrag.makeDraggable( dialog, handle );
}
*/
function open_grey_screen(id)
{
    dim = YGWin.getDocumentDimensions();
    style='display:block;width:100%;height:'+dim.height+'px;background-color:#000000;filter:alpha(opacity=50);opacity:0.5;-moz-opacity:0.5;position:absolute;top:0px;left:0px;z-index:1000';
    divid='grey_screen_div_'+id;
    obj = DOMelement("DIV");
    obj.setAttribute("id"   ,divid);
    obj.setAttribute("style",style);
    document.body.appendChild( obj.createElement() );
}
function open_dialog_box(id,wind_width,wind_height,title,body)
{
    var wind_width  = 300;
    var wind_height = 100;
    var c_window    = YGWin.getCenteredWindowCoords(wind_width,wind_height);
    var left_coord  = c_window.left;
    var top_coord   = c_window.top;

    dialog_id='dialog_div_id_'+id;

    styletext ="display:block;width:"+wind_width+"px;height:"+wind_height+"px;position:absolute;top:"+top_coord+"px;left:"+left_coord+"px;z-index:"+(1001+id);
    obj = DOMelement("DIV");
    obj.setAttribute("id",dialog_id);
    obj.setAttribute("style",styletext);
    obj.innerHTML=dialog_html(id,wind_height,title,body);
    document.body.appendChild( obj.createElement() );
}
function close_dialog(id)
{
    removeElementbyName('dialog_div_id_'+id);
}
function dialog_html(id,wind_height,title,text)
{
    handle_id='handle_div_id_'+id;
    h='';
    h+=  "<div style='font-family:Arial;'>";
    h+=      "<div style='background: url("+g_dialog_images[2]+") top left no-repeat; padding-left:5px;' id="+handle_id+">";
    h+=          "<div style='background: url("+g_dialog_images[2]+") top right no-repeat; padding-right:5px;'>";
    h+=              "<div style='background: url("+g_dialog_images[3]+"); height:24px; font-weight:bold; font-size:11pt; color:#ffffff;'>";
    h+=                      "<div style='padding-top:2px;'>";
    h+=                          "<div style='float:right;'>";
    h+=                              "<a href='javascript:close_dialog("+id+");'><img src='"+g_dialog_images[0]+"' onmouseover='this.src=\""+g_dialog_images[1]+"\";' onmouseout='this.src=\""+g_dialog_images[0]+"\";' width=21 height=21 border=0 style='cursor:pointer;'></a>";
    h+=                          "</div>";
    h+=                          title;
    h+=                      "</div>";
    h+=                  "</div>";
    h+=          "</div>";
    h+=      "</div>";
    h+=      "<div style='background:#f1f1f1; padding-left:5px; padding-right:5px; text-align:left; border-left:1px #3E6BC7 solid; border-right:1px #3E6BC7 solid; font-size:10pt;'>";
    h+=          "<div style='height:"+(wind_height-26)+"px'>";
    h+=              text;
    h+=          "</div>";
    h+=      "</div>";
    h+=      "<div style='background: url("+g_dialog_images[2]+") bottom left no-repeat; padding-left:5px;  font-size:1pt;'>";
    h+=          "<div style='background: url("+g_dialog_images[2]+") bottom right no-repeat; padding-right:5px;'>";
    h+=              "<div style='background: #f1f1f1; height:4px; border-bottom:1px #3E6BC7 solid;'></div>";
    h+=          "</div>";
    h+=      "</div>";
    h+=  "</div>";
    return h;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------





function create_okcanceldialog(title,body)
{
   dialog_preload_images();
   id= g_windowid++;
//   open_grey_screen(id);
   okcancel="<div align=right><input type=button value=OK onclick='close_dialog("+id+");'>&nbsp;<input type=button value=Cancel onclick='close_dialog("+id+");'></div>";
   open_dialog_box(id,200,100,title,body+okcancel);
   dialog = document.getElementById( 'dialog_div_id_'+id );
   handle = document.getElementById( 'handle_div_id_'+id );
   YGDrag.makeDraggable( dialog, handle );
}

