﻿/////////////////////////////////////////////////
//		GENERAL				//
/////////////////////////////////////////////////

function JCleanup ()
{
	JmvcCleanup ();
}
/////////////////////////////////////////////////
//			MVC				//
/////////////////////////////////////////////////

function JmvcInit (clear_update)
{
	top.aPage	= new Array();
	top.aView	= new Array ();
	
	top.clear_update = clear_update;
	clear_update ();
}

function JmvcCleanup ()
{
	top.aPage = null;
	top.aView = null;
}

function JmvcView (name)
{
	this.name		= name;
	this.page		= null;
	
	top.aView [top.aView.length] = this;
}

function JmvcUpdateAllViews ()
{
	var oView, oPage;
	
	// Scan the page list
	for (var i = 0; i < top.aView.length; i++)
	{
		// debug
		oView = top.aView[i];
		
		// debug
		if (!oView.page)
		{
			// alert ("JmvcUpdateAllViews : no page specified for view '" + oView.name + "'");
			continue;
		}
		
		// debug
		for (var j = 0; j < top.aPage.length; j++)
		{
			oPage = top.aPage[j];
			
			// debug
			// alert (oPage.name);
			
			if (oPage.name == oView.page)
				break;
		}
		
		// debug
		if (j == top.aPage.length)
		{
			alert ("JmvcUpdateAllViews : unknown page '" + oView.page + "'");
			continue;
		}
		
		// debug
		oPage.write (oView.name, oPage.name);
	}
	
	if (top.clear_update)
		clear_update ();
}

function JmvcIsPageVisible (page)
{
	var oView;
	
	for (var i = 0; i < top.aView.length; i++)
	{
		oView = top.aView[i];
		if (oView.page == page)
			break;
	}
	
	return ((i < top.aView.length) ? true : false);
}

function JmvcIsView (view)
{
	var oView;
	
	for (var i = 0; i < top.aView.length; i++)
	{
		oView = top.aView[i];
		if (oView.name == view)
			break;
	}
	
	return ((i < top.aView.length) ? true : false);
}

function JmvcSetPage (view, page)
{
	var oView;
	
	for (var i = 0; i < top.aView.length; i++)
	{
		// Ignore if bUpdate is not set
		oView = top.aView[i];
		
		if (oView.name == view)
			break;
	}
	
	if (i == top.aView.length)
	{
		alert ("JmvcSetPage : unknown view '" + view + "'");
		return;
	}
	
	oView.page = page;
}


function JmvcPage (name, write)
{
	this.name		= name;
	this.write		= write;
	
	this.bUpdate	= false;
	
	top.aPage [top.aPage.length] = this;
}

/////////////////////////////////////////////////
//			PANES			//
/////////////////////////////////////////////////

function JSetFrames (nFrames)
{
	// Added for Zazansc but not correct: clears toolbar!
	// top.aView.length = 0;
	
	var divClient = $('divClient');
	
	var text = "<div id='trackbarMove'></div>";
	
	for (var i = 0; i < nFrames; i++)
	{
		text += "<div class='frame'>"
				+ "<div class='frame_client'>"
					+ "<div class='header'></div>"
					+ "<div class='outer_content'>"
						+ "<div class='content'>"
						+ "</div>"
					+ "</div>"
				+ "</div>"
				+ (i > 0 ? "<div class='frame_trackbar' onmousedown='JfraBeginDrag(this.parentNode,event);'></div>" : "")
			+ "</div>";
	}
	
	divClient.innerHTML = text;
	
	var nHeight = 100/nFrames;
	var nTop = 0;
	var nBottom = nHeight * (nFrames - 1);
		
	for (var i = 0; i < nFrames; i++)
	{		
		var divFrame;
		
		divFrame = JGetFrame (i);
		if (top.bMSExplorer)
		{
			divFrame.style.top = 0;
			divFrame.style.height = nHeight + '%';
		}
		else
		{
			divFrame.style.top = nTop + '%';
			divFrame.style.bottom = nBottom + '%';
			nTop += nHeight;
			nBottom -= nHeight;
		}
		
		// No trackbar for the first frame
		if (i == 0)
		if (top.bMSExplorer)
			JGetFrameClient(divFrame).style.paddingTop = 0;
		else
			JGetFrameClient(divFrame).style.top = 0;
	}
}

function JGetFrameHeader (name)
{
	var eFrame = $(name);
	if (!eFrame)
		return null;
	var eListDivs = eFrame.getElementsByTagName ('div');
	for (var i = 0; i < eListDivs.length; i++)
	{
		if (eListDivs[i].className != 'header')
			continue;
		return eListDivs[i];
	}
	return null;
}

function JGetFrameOuterContent (name)
{
	var eFrame = $(name);
	if (!eFrame)
		return null;
	var eListDivs = eFrame.getElementsByTagName ('div');
	for (var i = 0; i < eListDivs.length; i++)
	{
		if (eListDivs[i].className != 'outer_content')
			continue;
		return eListDivs[i];
	}
	return null;
}

function JGetFrameContent (name)
{
	var eFrame = $(name);
	if (!eFrame)
		return null;
	var eListDivs = eFrame.getElementsByTagName ('div');
	for (var i = 0; i < eListDivs.length; i++)
	{
		if (eListDivs[i].className != 'content')
			continue;
		return eListDivs[i];
	}
	return null;
}

function JGetFrameClient (name)
{
	var eFrame = $(name);
	if (!eFrame)
		return null;
	var eListDivs = eFrame.getElementsByTagName ('div');
	for (var i = 0; i < eListDivs.length; i++)
	{
		if (eListDivs[i].className != 'frame_client')
			continue;
		return eListDivs[i];
	}
	return null;
}

function JGetFrame (iFrame)
{
	var eListDivs = document.getElementsByTagName ('div');
	for (i = 0, j = 0; i < eListDivs.length; i++) {
		if (eListDivs[i].className != 'frame')
			continue;
		if (j++ == iFrame)
			return eListDivs[i];
	}

	return null;
}

function JSetHeaderBackground (frame, background)
{
	JGetFrameHeader (frame).style.background = background;
}

function JSetHeaderHeight (frame, height)
{
	JGetFrameHeader(frame).style.height = height;
	
	if (top.bMSExplorer)
		JGetFrameOuterContent(frame).style.paddingTop = height;
	else
		JGetFrameOuterContent(frame).style.top = height;
}

function JfraBeginDrag (eDrag, event)
{
	var eFrameTop = null;
	var eFrameBottom = eDrag;
	var eClient = eDrag.parentNode;
	
	for (var i = 0, eFramePrev = null; i < eClient.childNodes.length; i++)
	{
		var eChild = eClient.childNodes[i];
		if (eChild.className != 'frame')
			continue;
		if (eChild == eDrag)
			break;
		eFrameTop = eChild;
	}
	
	var nClientHeight, nClientTop, nFrameTop_bottom, nFrameTop_heightPercent, nFrameBottom_heightPercent;
	
	if (top.bMSExplorer)
	{
		nClientHeight = eClient.offsetHeight - parseInt(eClient.currentStyle.paddingTop);
		nFrameTop_heightPercent = parseFloat (eFrameTop.currentStyle.height);
		nFrameBottom_heightPercent = parseFloat (eFrameBottom.currentStyle.height);
	}
	else
	{
		nClientHeight = parseInt(window.getComputedStyle(eClient, null).height);
		nClientTop = parseInt(window.getComputedStyle (eClient, null).top);
		
		var sFrameTop_bottom = window.getComputedStyle(eFrameTop, null).bottom;
		if (sFrameTop_bottom.indexOf ('%') >= 0)			// Safari 3.0
			nFrameTop_bottom = parseInt(sFrameTop_bottom) * nClientHeight /100;
		else										// Other browsers
			nFrameTop_bottom = parseInt (sFrameTop_bottom);
	}
	
	y0 = event.clientY;
	
	if (top.bMSExplorer)
	{
		document.attachEvent ("onmousemove", moveHandler);
		document.attachEvent ("onmouseup", upHandler);
		event.cancelBubble = true;
		event.returnValue = false;
		
		eDrag.setCapture ();
	}
	else
	{
		document.addEventListener ("mousemove", moveHandler, true);
		document.addEventListener ("mouseup", upHandler, true);
		event.stopPropagation();
		event.preventDefault();
	}
	
	function moveHandler (ev)
	{
		if (!ev)
			ev = window.event;
		
		if (top.bMSExplorer)
			ev.cancelBubble = true;
		else
			ev.stopPropagation();
		
		var dash = document.getElementById('trackbarMove');
		var y = checkMinimumHeight (ev.clientY - y0) + y0;
		if (!top.bMSExplorer)
			y = y - nClientTop;
		dash.style.top = y;
		dash.style.visibility = 'visible';
	}

	function upHandler (ev)
	{
		if (!ev)
			ev = window.event;
			
		if (top.bMSExplorer)
		{
			document.detachEvent ("onmouseup", upHandler);
			document.detachEvent ("onmousemove", moveHandler);
			ev.cancelBubble = true;
			
			eDrag.releaseCapture ();
		}
		else
		{
			document.removeEventListener ("mousemove", moveHandler, true);
			document.removeEventListener ("mouseup", upHandler, true);
			ev.stopPropagation();
		}
		
		document.getElementById('trackbarMove').style.visibility = 'hidden';
		
		resize (ev);
	}
	
	function checkMinimumHeight (y)
	{
		var nHeightMin = 50;
		if (y < nHeightMin - eFrameTop.offsetHeight)
			y = nHeightMin - eFrameTop.offsetHeight;
		if (y > eFrameBottom.offsetHeight - nHeightMin)
			y = eFrameBottom.offsetHeight - nHeightMin;
		
		return y;
	}
	
	function resize (ev)
	{
		var y = ev.clientY - y0;
		
		y = checkMinimumHeight (y);

		if (top.bMSExplorer)
		{
			var deltaPercent = 100 * y/nClientHeight;
			
			eFrameTop.style.height = (nFrameTop_heightPercent + deltaPercent) + '%';
			eFrameBottom.style.height = (nFrameBottom_heightPercent - deltaPercent) + '%';
		}
		else
		{
			var nBottom = nFrameTop_bottom - y;
			
			eFrameTop.style.bottom = 100*nBottom / nClientHeight + '%';
			eFrameBottom.style.top = (100 - 100*nBottom / nClientHeight) + '%';
		}
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function JToolbar ()
{
	this.iCell		= new Array ();
	this.xPos		= new Array ();
	this.bEnabled	= new Array ();
	this.bChecked	= new Array ();
	this.bOver		= new Array ();
	this.text		= new Array ();
	
	var table = document.createElement ("table");
	
	var divToolbar=$('divToolbarContent');
	divToolbar.appendChild (table);
	var row = table.insertRow (0);
	
	// debug
	table.style.width = '100%';
}

JToolbar.prototype.cleanup = function jTooCleanup ()
{
	this.iCell		= null;
	this.xPos		= null;
	this.bEnabled	= null;
	this.bChecked	= null;
	this.bOver		= null;
};

JToolbar.prototype.addButton =
function addButton (name, xpos, width, action)
{
	var eRow = $('divToolbarContent').getElementsByTagName ("tr")[0];
	
	var iCell = eRow.childNodes.length;
	
	this.iCell[name]	= iCell;
	this.xPos[name]		= xpos + 'px';
	this.bEnabled[name]	= true;
	this.bChecked[name]	= false;
	this.bOver[name]	= false;
	
	eRow.insertCell(iCell).appendChild(document.createTextNode (""));
	var eCell = eRow.childNodes[iCell];
	
	eCell.style.width = width + 'px';
	
	eCell.style.height = '25px';
	eCell.name = name;
	
	if (action)
		eCell.innerHTML =
			"<div style='width:" + width + "; height:22px; overflow:hidden; cursor:default;' "
			+ "onclick=\"" + action + "\""
			+ "onmouseover=\"OnMouseOver(this.parentNode);\""
			+ "onmouseout=\"OnMouseOut(this.parentNode);\">"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
			+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
			+ "</div>";
	
	// Comment: multiple &nbsp; necessary for MSIE
};

JToolbar.prototype.addText =
	function addText (text, textAlign, width)
	{
		var eRow = $('divToolbarContent').getElementsByTagName ("tr")[0];
		
		var iCell = eRow.childNodes.length;
		eRow.insertCell(iCell).appendChild(document.createTextNode (""));
		var eCell = eRow.childNodes[iCell];
		
		if (textAlign)
			eCell.style.textAlign = textAlign;
		if (width)
			eCell.style.width = width;
		
		if (text)
			eCell.innerHTML = text;
	};

function OnMouseOver (eCell)
{
	var name = eCell.name;

	if (!top.oToolbar.bEnabled[name])
		return;
	
	top.oToolbar.bOver [name] = true;
	
	this.oToolbar.paint (name);
}

function OnMouseOut (eCell)
{
	var name = eCell.name;
	
	if (!top.oToolbar.bEnabled[name])
		return;

	top.oToolbar.bOver [name] = false;
	this.oToolbar.paint (name);
}

JToolbar.prototype.addSeparator = function addSeparator (bPaint)
{
	var eRow = $('divToolbarContent').getElementsByTagName ("tr")[0];
	
	var iCell = eRow.childNodes.length;
	
	eRow.insertCell(iCell).appendChild(document.createTextNode (""));
	var eCell = eRow.childNodes[iCell];
	
	eCell.style.width = '10px';
	eCell.style.height = '22px';
	
	if (bPaint)
	{
		eCell.style.backgroundImage =  'url(pictures/tbButtons.png)';
		eCell.style.backgroundPosition = "0 -125px";
	}
};

JToolbar.prototype.enable = function jTbEnable (name, bEnable)
{
	// Ignore if undefined
	if (!top.oToolbar.iCell[name])
		return

	this.bEnabled[name] = bEnable;
	this.paint (name);
	
};

JToolbar.prototype.check = function jTbCheck (name, bChecked)
{
	// Ignore if undefined
	if (!top.oToolbar.iCell[name])
		return

	this.bChecked[name] = bChecked;
	this.paint (name);
};

JToolbar.prototype.paint = function jTbPaint (name)
{
	// Ignore if undefined
	if (!top.oToolbar.iCell[name])
		return;

	var eCell = $('divToolbarContent').getElementsByTagName('td')[this.iCell[name]];
	
	eCell.style.backgroundImage =  'url(pictures/tbButtons.png)';
	
	var y;
	
	if (this.bEnabled[name])
	{
		if (this.bChecked[name])
		{
			if (this.bOver[name])
				y = 50;
			else
				y = 25;
		}
		else
		{
			if (this.bOver[name])
				y = 25;
			else
				y = 0;
		}
	}
	else
		y = 75;
	
	eCell.style.backgroundPosition = '-' + this.xPos[name] + " -" + y + "px";
};
