/////////////////////////////////////////////////////////////////////////////
// Manage windows and frames in Main Page. (IE and Mozilla compatible) 
//
// Tested on: 
//   1. MS Internet Explorer. Version: 6.0.2600.0000.xpclnt_qfe.021108-2107
// 	 2. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041217
// 	 3. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) 
//
// Copyright (c) 2004-2005
// Author David Davtyan
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
var m_isMSIE; // is Internet Explorer
var m_isNetscape; // There is a defference between Mozilla and Netscape (mouseUp on frame scrollbars)

var m_Assistant;
var m_frameText;
var m_frameExp;
var m_frameTree;
var m_Splitter;
var m_btnShowHideExp;
var m_btnAutoScroll;

var m_MouseAction = 'none';
var m_bAutoScrolling = false;
var m_oldX;
var m_oldY;

var m_AnimationStep = 0;
var m_CurrentPercent;

var m_treeScrollY; // Used only for Netscape

function InitWndManager()
{ 
	m_isMSIE = navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1; 
	m_isNetscape = navigator.userAgent.indexOf('Netscape') != -1; 
	
	m_Assistant = document.getElementById("Assistant");
	m_frameText = document.getElementById("frameText");
	m_frameExp = document.getElementById("frameExplanation");
	m_frameTree = document.getElementById("frameTree");
	m_Splitter = document.getElementById("Splitter");
	m_btnShowHideExp = document.getElementById("ShowHideExp");
	m_btnAutoScroll = document.getElementById("AutoScroll");
	
	if(m_btnAutoScroll && window.frames.length>=2)
	{
		var frame = window.frames['1'];
		if(frame.document.body.scrollHeight > 350)
			m_btnAutoScroll.style.visibility='visible';
	}
	
	document.onmousedown = WndManageMouseDown; 
	document.onmousemove = WndManageMouseMove;
	document.onmouseup = WndManageMouseUp; 
}

function WndManageMouseDown(e)
{
	var Obj = m_isMSIE ? event.srcElement : e.target; 
	switch(Obj.id)
	{
		case 'Splitter': 	return SplitterMouseDown(e);
		case 'Assistant': 	return AssistantMouseDown(e);
	}
	
	return true;
}

function WndManageMouseMove(e)
{
	switch(m_MouseAction)
	{
		case 'moveSplitter': 	return SplitterMouseMove(e);
		case 'moveAssistant': 	return AssistantMouseMove(e);
	}
		
	return true;
}

function WndManageMouseUp(e)
{
	switch(m_MouseAction)
	{
		case 'moveSplitter': 	return SplitterMouseUp(e);
		case 'moveAssistant': 	return AssistantMouseUp(e);
	}
		
	return true;
}

function SplitterMouseDown(e)
{
	m_oldX = m_isMSIE ? event.clientX : e.clientX;
	m_oldY = m_isMSIE ? event.clientY : e.clientY;
	m_MouseAction = 'moveSplitter';
	
	if(m_isMSIE) // IE
		m_Splitter.setCapture(true);
	else // Mozilla
	{
		for(i=0; i<window.frames.length; i++) 
		{
			window.frames[i].onmousemove = WndManageMouseMove;
			window.frames[i].onmouseup = WndManageMouseUp;
		}
		
		// only for netscape navigator (hide tree scroll bars and save scrollPos)
		if(m_isNetscape && window.frames.length > 0) 
		{
			//m_treeScrollY = window.frames[0].document.body.scrollTop;
			//window.frames[0].frameElement.scrolling = 'no';
			m_frameTree.style.visibility = 'hidden';
		}
	}

	return false;
}

function SplitterMouseMove(e)
{
	X = m_isMSIE ? event.clientX : e.clientX; 
	Y = m_isMSIE ? event.clientY : e.clientY;

	var nWidth=0;
	if(m_isMSIE) // IE
		nWidth = m_frameTree.style.pixelWidth;
	else  // Mozilla
		nWidth = parseInt(m_frameTree.style.width);
		
	nWidth += X-m_oldX;
	if(nWidth < 30)
		nWidth = 1;
	else if(nWidth > 400)
		nWidth = 400; 
	else
	{
		m_oldX = X;
		m_oldY = Y;
	}
	
	if(m_isMSIE) // IE
		m_frameTree.style.pixelWidth = nWidth;
	else  // Mozilla
		m_frameTree.style.width = nWidth;
	
	return false;
}

function SplitterMouseUp(e)
{
	m_MouseAction = 'none';
	if(m_isMSIE) // only for IE
		m_Splitter.releaseCapture();
	else 
	{
		for(i=0; i<window.frames.length; i++) 
		{
			window.frames[i].onmousemove = '';
			window.frames[i].onmouseup = '';
		}
		
		if(m_isNetscape && window.frames.length > 0)
		{
			//window.frames[0].frameElement.scrolling = 'yes';
			//window.frames[0].scrollBy(0, m_treeScrollY);
			m_frameTree.style.visibility = 'visible';
		}
	}
	
	m_Splitter.style.background = '#fffdd4';
	return false;
}

function AssistantMouseDown(e)
{
	m_oldX = m_isMSIE ? event.clientX : e.clientX;
	m_oldY = m_isMSIE ? event.clientY : e.clientY;
	m_MouseAction = 'moveAssistant';
	
	if(m_isMSIE) // IE
		m_Assistant.setCapture(true);
	else // Mozilla
	{
		for(i=0; i<window.frames.length; i++) 
		{
			window.frames[i].onmousemove = WndManageMouseMove;
			window.frames[i].onmouseup = WndManageMouseUp;
		}
	}
	
	return false;
}

function AssistantMouseMove(e)
{
	X = m_isMSIE ? event.clientX : e.clientX; 
	Y = m_isMSIE ? event.clientY : e.clientY;
	
	xDiff = X-m_oldX;
	yDiff = Y-m_oldY;
	
	m_Assistant.style.left = parseInt(m_Assistant.style.left) + xDiff;
	m_Assistant.style.top = parseInt(m_Assistant.style.top) + yDiff;
	
	m_oldX = X;
	m_oldY = Y;
	return false;
}

function AssistantMouseUp(e)
{
	m_MouseAction = 'none';
	if(m_isMSIE) // only for IE
		m_Assistant.releaseCapture();
	else 
	{
		for(i=0; i<window.frames.length; i++) 
		{
			window.frames[i].onmousemove = '';
			window.frames[i].onmouseup = '';
		}
	}
	return false;
}

function GetTreeWidth()
{
	if(!m_frameTree)
		return 205;
		
	return parseInt(m_frameTree.style.width);
}

function GetExpVisible()
{
	if(!m_frameExp || m_frameExp.style.visibility == 'visible')
		return 'yes';
		
	return 'no';
}

function ShowHideExplanation()
{
	var bShow = m_frameExp.style.visibility == 'visible' ? false : true;
	if(bShow)
	{
		m_CurrentPercent = 95;
		m_StepAnimation = -4;
		m_btnShowHideExp.src = 'img/toright.bmp';
	}
	else
	{
		m_CurrentPercent = 63;
		m_StepAnimation = 4;
		m_frameExp.style.visibility = 'hidden';
		m_btnShowHideExp.src = 'img/toleft.bmp';
	}
	
	window.setTimeout(StepExpAnimation, 10);
}

function StepExpAnimation()
{
	m_CurrentPercent += m_StepAnimation;
	m_frameText.style.width = m_CurrentPercent + "%";
	
	if( (m_StepAnimation>0 && m_CurrentPercent<95) || (m_StepAnimation<0 &&m_CurrentPercent>63))
		window.setTimeout(StepExpAnimation, 30);
	else if(m_StepAnimation<0)
		m_frameExp.style.visibility = 'visible';
}

function DoAutoScroll()
{
	var frame = window.frames[1];
	if(!frame)
		return;
	
	if(!m_bAutoScrolling)
	{
		//if(m_isMSIE) // for Internet Explorer
		//	m_btnAutoScroll.setCapture();
		m_btnAutoScroll.src = 'img/autoscrl2.bmp';
		m_bAutoScrolling = true;
		window.setTimeout(AutoScrollStep, 1); 
	}
	else
	{
		//if(m_isMSIE) // for Internet Explorer
		//	m_btnAutoScroll.releaseCapture();
		m_btnAutoScroll.src = 'img/autoscrl.bmp';
		m_bAutoScrolling = false;
	}
}

function AutoScrollStep()
{
	var frame = window.frames[1];
	if(!frame || !m_bAutoScrolling)
		return;
		
	old = frame.document.body.scrollTop;
	frame.scrollBy(0,1);
	
	if(old == frame.document.body.scrollTop)
	{
		//if(m_isMSIE) // for Internet Explorer
		//	m_btnAutoScroll.releaseCapture();

		m_btnAutoScroll.src = 'img/autoscrl.bmp';
		m_btnAutoScroll.style.cursor= "pointer";
		m_bAutoScrolling = false;
		return;
	}
	
	window.setTimeout(AutoScrollStep, 50);
}