﻿ var dragbox;   
 var deltaX, deltaY;   
   
 function getX(node) { return parseInt(node.style.left); }
 function getY(node) { return parseInt(node.style.top); }
 function setX(node,x) {    node.style.left = x+"px";   }     
 function setY(node,y) {    node.style.top = y+"px";   }
 //-----------------------
 function dragPress(evt) 
 {
    try
    {  
     if(isMax)
        return;
    }catch(E){}
     evt = new Evt(evt);
     //dragbox = evt.source;  
     dragbox = gel('dragbox');
     //setOpacity(dragbox,.7);    
     deltaX = evt.x - getX(dragbox);    
     deltaY = evt.y - getY(dragbox);    
     Evt.addEventListener(document,"mousemove",dragMove,false);    
     Evt.addEventListener(document,"mouseup",dragRelease,false);   
 }      
 function dragMove(evt)
 {
     evt = new Evt(evt); 
     o=gel('dragbox');
     setX(dragbox,evt.x - deltaX);    
     setY(dragbox,evt.y - deltaY);    
     evt.consume(); 
 }      
 function dragRelease(evt) 
 {
     evt = new Evt(evt);    
     setOpacity('dragbox',1);    
     Evt.removeEventListener(document,"mousemove",dragMove,false);    
     Evt.removeEventListener(document,"mouseup",dragRelease,false);    
 }
 //---------------  
 function closeDialog()
{
    isMin=false;
    isMax=false;
    gel('max2Img').style.display='none';
    gel('max1Img').style.display='';
    gel('min2Img').style.display='none';
    gel('min1Img').style.display='';
    gel("BodyCover").style.display='none';
    gel("dragbox").style.display='none';
    gel("frmBody").src='empty.aspx';
    removeEvent( window, 'scroll', checkScroll);
}
//---------------
function CreateDialog(PageURL,Title,Width,Height)
{
    dialog=gel('dragbox').style;
    dialog.width = (Width ? Width : '600') + 'px';
	dialog.height = (Height ? Height : '300') + 'px';
    centerWin(dialog.width, dialog.height);
    dialog.display='';
    gel('BodyCover').style.display='';
    gel('sp_Title').innerHTML='بارگزاری';
    frm=gel('frmBody');
    frm.style.visibility='visible';
    frm.src=PageURL;
    addEvent( window, 'scroll', checkScroll);
    //frm.contentWindow.document.body.onload=function(){gel("sp_Title").innerHTML=Title};
    setTimeout(function(){
                   gel('sp_Title').innerHTML=Title;
                   gel('iconIMG').src='../ImageGallery/SystemFiles/Images/Dialog/icon_normal.gif';
               },3000);
}
//---------------
function CreateTextMessage(Text, Title, Width, Height)
{
    dialog=gel('dragbox').style;
    dialog.width='600px';
	dialog.height='300px';
    //centerWin();
    dialog.display='';
    gel('helpBody').innerHTML=Text;
    gel('BodyCover').style.display='';
    gel('sp_Title').innerHTML=Title;
}
//-----------------------------
function changeDialogImage(type,flag)
{
    oIMG=gel(type+'Img');
    url='../ImageGallery/SystemFiles/Images/Dialog/Btns/btn_'+type+'_'+flag+'.gif';
    oIMG.src=url;
}
//-----------------------------
var DragBox_lastW = '600px';
var DragBox_lastH = '300px';
function maxDialog(flag)
{
    if(flag)
    {
        oDiv=gel('dragbox');
        DragBox_lastW = oDiv.style.width;
        DragBox_lastH = oDiv.style.height;
        if(!isMin)
        {
            gel('max1Img').style.display='none';
            gel('max2Img').style.display='';
            
            oDiv.style.left=document.body.scrollLeft+1;
            oDiv.style.top=document.body.scrollTop;
            oDiv.style.width=document.body.clientWidth;
            oDiv.style.height=document.body.clientHeight;
            isMax=true;
        }
    }
    else
    {
        gel('max2Img').style.display='none';
        gel('max1Img').style.display='';
        centerWin(DragBox_lastW, DragBox_lastH);
        isMax=false;
    }
}
//-----------------------------
function minDialog(flag)
{
    if(!isMax)
    {
        if(flag)
        {
            gel('min1Img').style.display='none';
            gel('min2Img').style.display='';
            gel('body').style.display='none';
            gel('dragbox').style.height='1px';
            isMin=true;
        }
        else 
        {
            gel('min2Img').style.display='none';
            gel('min1Img').style.display='';
            gel('body').style.display='';
            gel('dragbox').style.height='300px';
            isMin=false;
        }
    }
}
//-----------------------------
function centerWin(Width, Height)
{
	dialog = gel('dragbox').style;
	dialog.width = (Width ? Width : '600px');
	dialog.height = (Height ? Height : '300px');
	var xPos=Math.round(parseInt(document.body.scrollLeft) + (parseInt(document.body.clientWidth) / 2 - parseInt(dialog.width) / 2));
	var yPos=Math.round(parseInt(document.body.scrollTop) + (parseInt(document.body.clientHeight) / 2 - parseInt(dialog.height) / 2));	
	//if(isMin)
	{
		//xPos=0;
		//yPos=document.body.scrollTop;
	}
	dialog.left=xPos;
	dialog.top=yPos;
}
//-----------------------------
function checkScroll()
{
    d=gel('dragbox').style;
    if(isMax)
    {
        d.left=document.body.scrollLeft+1;
        d.top=document.body.scrollTop;
    }
    else
    {
        var xPos=Math.round(parseInt(document.body.scrollLeft) + (parseInt(document.body.clientWidth) / 2 - parseInt(dialog.width) / 2));
	    var yPos=Math.round(parseInt(document.body.scrollTop) + (parseInt(document.body.clientHeight) / 2 - parseInt(dialog.height) / 2));	
	    d.left=xPos;
	    d.top=yPos;
    }
}
