﻿// JScript File
//document.onmousedown = down;
document.onmouseup = Dockable_MouseUp;
document.onmousemove = Dockable_MouseMove;
// Mozzila
if (window.addEventListener)
{
    window.addEventListener("DOMContentLoaded", CenterHtmlEditor, false)
}
// IE
if (window.attachEvent)
    window.attachEvent("onload", CenterHtmlEditor);

 var mouseDown = 0;
 var offsetX = 0;
 var offsetY = 0;
 
 function Dockable_MouseDown(e)
 {
    if (!e) var e = window.event;
    
    mouseDown = 1;
    
    try
    {
    
    document.getElementById('DockableContent').style.display = "none";

    offsetX = isNaN(e.offsetX) ? e.layerX : e.offsetX;
    offsetY = isNaN(e.offsetY) ? e.layerY : e.offsetY;
    }
    catch (err)
    {
    alert(err)
    }
    
 }
 
 function Dockable_MouseMove(e)
 {
    if (!e) var e = window.event;
    
    if (mouseDown == 1)
    {
       var DockableControl = document.getElementById('DockableControl').style;
        
       DockableControl.left = (e.clientX - offsetX + document.documentElement.scrollLeft).toString(10) + 'px';
       if (e.clientY - offsetY + document.documentElement.scrollTop < 0)
            DockableControl.top = '0px'
       else
            DockableControl.top = (e.clientY - offsetY + document.documentElement.scrollTop).toString(10) + 'px';
    }
 }
 
 function Dockable_MouseUp(e)
 {
    if (!e) var e = window.event;
    
    mouseDown = 0;
    if (!document.getElementById('DockableContent')) return;
    
    document.getElementById('DockableContent').style.display = "";
 }

function CenterHtmlEditor()
{

   var i = 0, x = 0, y = 0;
   var DockableControl = null;
   try 
   {
    DockableControl = document.getElementById('DockableControl');
}
   catch (err)
   {
    alert(err);
   }
   if (!DockableControl) return;

   x = ( (getPageSize()[2] - DockableControl.style.width.replace("px", "")) / 2 );
   y = ( (getPageSize()[3] - DockableControl.style.height.replace("px", "")) / 2 );
   if ( x < 0 ) x = 0;
   if ( y < 0 ) y = 0;

    DockableControl.style.left = x + 'px';    
    DockableControl.style.top = y + 'px';
    
    //disableSelection(document.getElementById('DockableMoveArea'))
    
   var ModalPanelControl = document.getElementById('modalPanelControl');
   if (ModalPanelControl != null)
   {
        // alert(getPageSize()[0] + ' : ' + getPageSize()[1] + ' : ' + getPageSize()[2] + ' : ' + getPageSize()[3] + ' : ')
        ModalPanelControl.style.width = getPageSize()[0] + 'px';
        ModalPanelControl.style.height = getPageSize()[1] + 'px';
   }
}

function disableSelection(target)
{
    if (typeof target.onselectstart!="undefined") //IE route
	    target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	    target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
	    target.onmousedown=function(){return false}
    target.style.cursor = "default"
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//disableSelection(document.body) //Disable text selection on entire body
