﻿// File JScript
// Global variables 
var CurrentDownHeight = 0;
var CurrentTopHeight = 0;

var CurrentLeftWidth = 0;
var CurrentRightWidth = 0;

var CurrentPosition = 0;
var NewPosition = 0; 

var MouseDown = false;
var Horizontal = false;
var Vertical = false;

var DivA;
var DivB;

function Split_OnMouseDownVertical(mouse, conteinerA, conteinerB)
{ 
    DivA = conteinerA;
	DivB = conteinerB;
	
	InitSplit();
	Vertical = true;
	
	// Save the cursor position to its corresponding global variable 
	CurrentPosition = mouse.clientY;
	
	// Save the Current Div height to a local variable
	
	//var tempHeight = document.getElementById(DivA).style.height; 
	var tempHeight = document.getElementById(DivA).offsetHeight;

	//CurrentTopHeight = ExtractNumber(tempHeight);
	CurrentTopHeight = tempHeight;
	//tempHeight = document.getElementById(DivB).style.height; 
	tempHeight = document.getElementById(DivB).offsetHeight;

	//CurrentDownHeight = ExtractNumber(tempHeight);
	CurrentDownHeight = tempHeight;
} 

function Split_OnMouseDownHorizontal(mouse, conteinerA, conteinerB)
{ 
	DivA = conteinerA;
	DivB = conteinerB;
	
	InitSplit();
	Horizontal = true;
	
	CurrentPosition = mouse.clientX;
	
	//var tempWidth = document.getElementById(DivA).style.width; 
	var tempWidth = document.getElementById(DivA).offsetWidth;
	//CurrentLeftWidth  = ExtractNumber(tempWidth );
	CurrentLeftWidth = tempWidth;
	//tempWidth  = document.getElementById(DivB).style.width ; 
	tempWidth = document.getElementById(DivB).offsetWidth;
	//CurrentRightWidth  = ExtractNumber(tempWidth );
	CurrentRightWidth = tempWidth;
} 	
	

	
function ExtractNumber(Height)
{
    //La lettera 'p' sta per l'iniziale di pixel...
    heightArray = Height.split('p'); 
	return parseInt(heightArray[0]); 
}

function Split_OnMouseUp()
{   
    EndSplit();
}
	
function Split_OnMouseMove(e)
{   
    if (!e) var e = window.event;
    
    if(MouseDown)
    { 
	    if(Vertical)
        { 
	        Split_OnMouseMoveVertical(e);
        }   
        else if(Horizontal)
        {
            Split_OnMouseMoveHorizontal(e);
        }        
    }    
}   

function Split_OnMouseMoveVertical(e)
{
    NewPosition = e.clientY;
	    
	var movePerPixels = parseInt(NewPosition - CurrentPosition);
	var divTopLocation = parseInt(CurrentTopHeight + movePerPixels); 
	var divDownLocation = parseInt(CurrentDownHeight - movePerPixels); 
	 
	if(divDownLocation < 10)
	{
		document.getElementById(DivB).style.height = 10 + 'px'; 
	}   
	else if (divTopLocation < 10)
	{
		document.getElementById(DivA).style.height = 10 + 'px'; 
	}
	else
	{
		document.getElementById(DivA).style.height = divTopLocation + 'px'; 
		document.getElementById(DivB).style.height = divDownLocation  + 'px'; 
	}
}

function Split_OnMouseMoveHorizontal(e)
{
    // Set the new cursor location to NewPosition global variable 
    NewPosition = e.clientX;
	    
	// Calculate The mouse movement in pixels
	var movePerPixels = parseInt(NewPosition - CurrentPosition);
	     
	// Add the mouse movement to the first div height
	var divRightLocation = parseInt(CurrentRightWidth - movePerPixels); 
	var divLeftLocation = parseInt(CurrentLeftWidth + movePerPixels); 
	    
	if(divRightLocation < 10)
	{
		document.getElementById(DivB).style.width = 10 + 'px'; 				
	}
	else if(divLeftLocation < 10)
	{
		document.getElementById(DivA).style.width = 10 + 'px'; 
	}
	else
	{
		document.getElementById(DivA).style.width = divLeftLocation + 'px'; 
		document.getElementById(DivB).style.width = divRightLocation + 'px'; 
	}
}

//Funzione che attiva l'intercettazione del movimento del mouse...
function InitSplit()
{
     MouseDown = true;
     document.onmousemove = Split_OnMouseMove;
	 document.onmouseup = Split_OnMouseUp;
}
	
//Funzione che resetta l'intercettazione del movimento del mouse...
function EndSplit()
{
	MouseDown = false;
    Horizontal = false;
    Vertical = false;
   	document.onmousemove = null;
	document.onmouseup = null;
}


