/////////////////////////////////////////////////////////
//
//	Copyright (c) 2002-2003 Softpress Systems Ltd.
//	All rights reserved.
//	http://www.softpress.com/
//
/////////////////////////////////////////////////////////

var kDMMoveTop = 6;
var kDMMoveLeft = 2;
var kDDMLayName;
var fwDDMLayCount;

var Opera = navigator.userAgent.indexOf("Opera") != -1;
var version = parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("Opera") + 5, navigator.userAgent.length));

if (!NS4)
{
	document.onload = FWDDMInit;
	window.onresize = FWDDMInit;
}

function FWDDMInit()
{
	for (var i = 1; i <= fwDDMLayCount; i++)
	{
		var layer = FindLayer(kDDMLayName + i);
		
		if (layer)
		{
			layer.left = 0;
			layer.top = 0;
		}
	}
}

function HideMenu(parentID)
{
	var index = fwActiveMenus.length - 1;
	
	while (index >= 0 && parentID != fwActiveMenus[index].submenuID)
	{
		HideLayer(fwActiveMenus[index].submenuID);
		ResetBGColor(fwActiveMenus[index].obj, fwActiveMenus[index].linkID, fwActiveMenus[index].color);
		fwActiveMenus.length--;
		index--;
	}
}

function SavePosition(obj, layerID, linkID, color, parentID)
{
	var layer = FindLayer(layerID);
	
	if (!layer)
		return;

	var obj_l = GetObjectLeft(obj);
	var obj_t = GetObjectTop(obj);
	var obj_w = GetObjectWidth(obj);
	var obj_h = GetObjectHeight(obj);
	
	var obj_r = obj_l + obj_w;
	var obj_b = obj_t + obj_h;

	var lay_l = parseInt(layer.left);
	var lay_t = parseInt(layer.top);
	var lay_w = GetLayerWidth(layerID);
	var lay_h = GetLayerHeight(layerID);
	
	var lay_r = lay_l + lay_w;
	var lay_b = lay_t + lay_h;
	
	// remember current menu
	
	var len = fwActiveMenus.length;
	
	fwActiveMenus[len] = new FWMenu();
	
	fwActiveMenus[len].obj = obj;
	fwActiveMenus[len].submenuID = layerID;
	fwActiveMenus[len].itemArea = new FWArea(obj_l, obj_t, obj_r, obj_b);
	fwActiveMenus[len].submenuArea = new FWArea(lay_l, lay_t, lay_r, lay_b);
	fwActiveMenus[len].linkID = linkID;
	fwActiveMenus[len].color = color;
	
	parentID = parentID ? parentID : "_top";
	fwActiveMenus[len].parentID = parentID;
}

function ShowMenu(obj, layerID, linkID, color, parentID, menuPos)
{
	if (Opera && version < 6)
		return;
	
	var len = fwActiveMenus.length - 1;
	
	if (len >= 0 && fwActiveMenus[len].submenuID == layerID)
		return;
	
	var layer = FindLayer(layerID);
	
	if (!layer || layer.visibility == "visible" || layer.visibility == "show")
		return;
	
	parentID = parentID ? parentID : "_top";

	HideMenu(parentID);

	var left = layer.left ? parseInt(layer.left) : 0;
	var top = layer.top ? parseInt(layer.top) : 0;
	
	if (left == 0 && top == 0)
	{
		if (menuPos == 0)
			SetPosBelow(obj, layer);
		else
			SetPosRight(obj, layer);
	}
			
	ShowLayer(layerID);
	SavePosition(obj, layerID, linkID, color, parentID);
}