// JavaScript Document
var menuList = new Array();
var menuPosition = new Array();
var menuImageList = new Array();
var menuStaticData = new Array();
var callbackPosition = null;
function getMenuPosition(id)
{
	var menu = getMenuObject(id);
	return menuPosition[menu.position];
}
function addMenuStaticData(id,html)
{
	var temp = new Array();
	temp['html']=html;
	menuStaticData[id]=temp;
}
function addMenuImage(id,filename)
{
	var temp = new Array();
	temp['image']=filename;
	menuImageList[id]=temp;
}
function loadDefaultMenu(pid)
{
	for(var i=0;i<menuList.length;i++)
	{
		if(menuList[i].defaultMenu=="true" && menuList[i].pid==pid)
		{
			drawMenu(menuList[i].id);
		}
	}
}
function countSubMenu(id)
{
	var count = 0;
	for(var i=0;i<menuList.length;i++)
	{
		if(menuList[i].pid==id)
		{
			count++;
		}
	}
	return count;
}
function addPosition(id,htmlObj,openClass,closeClass,nextLavel,oriantation,postLoadFunction,menuImage,sepType,sepName)
{
	var temp = new Array();
	temp['htmlObject']=htmlObj;
	temp['openClass']=openClass;
	temp['closeClass']=closeClass;
	temp['nextLavel']=nextLavel;
	temp['oriantation']=oriantation;
	temp['postLoadFunction']=postLoadFunction;
	temp['menuImage']=menuImage;
	temp['sepType']=sepType;
	temp['sepName']=sepName;
	temp['id']=id;
	menuPosition[id]=temp;
}
function setVisibility(id,v)
{
	var obj =getMenuObject(id);
	obj.visible=v;
}
function addMenuItem(id,name,pid,position,defaultMenu,fun,dataCall)
{
	var temp = new Array();
	temp['id']=id;
	temp['name']=name;
	temp['pid']=pid;
	temp['href']=fun;
	temp['position']=position;
	temp['defaultMenu']=defaultMenu;
	temp['dataCall']=dataCall;
	temp['visible']=true;
	menuList[menuList.length]=temp;
	addMenuImage(id,'');
}
function getMenuObject(id)
{
	for(var i=0;i<menuList.length;i++)
	{
		if(menuList[i].id==id)
		{
			return menuList[i];
		}
	}
	return null;
}
function initMenuStat(pid)
{
	var nObj = getMenuObject(pid);
	if(nObj!= null)
	{
		pid=nObj.pid;
	}
	for(var i=0;i<menuList.length;i++)
	{
		if(menuList[i].pid==pid)
		{
			var pos = menuList[i].position;
			var obj = document.getElementById(pos+"_"+menuList[i].id);
			if(obj!=null)
			{
				obj.className=menuPosition[pos].closeClass;
			}
		}
	}
}
var openDataContener=false;
function loadData(id)
{
	/*
	if(openDataContener!=false)
	{
		var obj = document.getElementById(openDataContener);
		document.getElementById('displayDiv').removeChild(obj);
		document.getElementById('dataCOntener').appendChild(obj);
	}
	var objId = 'data_'+id;
	var obj = document.getElementById(objId);
	if(obj!=null)
	{
		document.getElementById('dataCOntener').removeChild(obj);
		document.getElementById('displayDiv').appendChild(obj);
		openDataContener = objId;
	}
	*/
	if(id!="root")
	{
		var menu = getMenuObject(id);
		if( menu == null || menu.dataCall==true )
		{
			blockUI();
			$.post(applicationPath+"service.php","id="+id,
			function(resp)
			{
				unBlockUi();
				var temp = document.getElementById('subMenuDiv');
				document.getElementById('displayDiv').innerHTML="";
				
				if(temp!=null)
				{
					document.getElementById('displayDiv').appendChild(temp);
					document.getElementById('displayDiv').innerHTML = document.getElementById('displayDiv').innerHTML+resp;
				}
				else
				{
					document.getElementById('displayDiv').innerHTML = resp;
				}
				postLoadData(id);
			});
		}
	}
	
}

function drawMenu(pid)
{
	initMenuStat(pid);	
	var nObj;
	if(pid!='root')
	{
		nObj = getMenuObject(pid);
		var nextLavel = menuPosition[nObj.position].nextLavel;
		var subCount = countSubMenu(pid);
		if(nextLavel && subCount > 0 && menuPosition[nextLavel].htmlObject != '')
		{
			document.getElementById(menuPosition[nextLavel].htmlObject).innerHTML="";
		}
	}
	var tempPos = null;
	if(menuStaticData[pid]==null)
	{
		var totalMenuInPid = 0;
		for(var i=0;i<menuList.length;i++)
		{
			if(menuList[i].pid==pid)
			{
				totalMenuInPid++;
			}
		}
		var curMenu = 0;
		for(var i=0;i<menuList.length;i++)
		{
			if(menuList[i].pid==pid)
			{
				curMenu++;
				var pos = menuList[i].position;
				tempPos= pos;
				if(menuPosition[pos].htmlObject!='')
				{
					if(menuList[i].visible==true)
					{
						var obj = document.createElement('A');
						obj.innerHTML = menuList[i].name;
						obj.id=pos+"_"+menuList[i].id;
						if(menuList[i].href=="")
						{
							obj.href="javascript:onMenuClick('"+menuList[i].id+"')";
						}
						else
						{
							var protocol = menuList[i].href.split(':');
							obj.href=menuList[i].href;
						}
						if(menuList[i].defaultMenu=='true')
						{
							obj.className=menuPosition[pos].openClass;
						}
						else
						{
							obj.className=menuPosition[pos].closeClass;
						}
						if(menuPosition[pos].oriantation=="V")
						{
							var orObj = document.createElement('dt');
							orObj.appendChild(obj);
							obj=orObj;	
						}			
						
						document.getElementById(menuPosition[pos].htmlObject).appendChild(obj);
						if(menuPosition[pos].sepType!="")
						{
							if(curMenu<totalMenuInPid)
							{
								if(menuPosition[pos].sepType=="char")
								{
									var sepObj = document.createElement("span");
									sepObj.innerHTML=menuPosition[pos].sepName;
									sepObj.className=menuPosition[pos].id+"_seprator";
									document.getElementById(menuPosition[pos].htmlObject).appendChild(sepObj);
								}
								if(menuPosition[pos].sepType=="image")
								{
									var sepObj = document.createElement("img");
									sepObj.src=menuPosition[pos].sepName;
									sepObj.className=menuPosition[pos].id+"_seprator";
									document.getElementById(menuPosition[pos].htmlObject).appendChild(sepObj);
								}
							}
						}
					}
				}
			}
			
		}
	}
	else
	{
		var menuObj = getMenuObject(pid);
		pos = menuObj.position;
		var nextLavel = menuPosition[menuObj.position].nextLavel;
		tempPos = nextLavel;
		document.getElementById(menuPosition[nextLavel].htmlObject).innerHTML = menuStaticData[pid].html;  
	}
	if(tempPos != null)
	{
		callbackPosition = menuPosition[tempPos];		
		if(menuPosition[tempPos].postLoadFunction!=null)
		{
			menuPosition[tempPos].postLoadFunction(nObj);	
		}
		if(menuPosition[tempPos].menuImage==true)
		{
			if(menuImageList[nObj.id].image!='')
			{
				var menuImage = document.createElement("img");		
				menuImage.id = "menuImage";
				menuImage.src="global/page_4/images/"+menuImageList[nObj.id].image;
				document.getElementById("iconImage").innerHTML="";
				document.getElementById("iconImage").appendChild(menuImage);
				//document.getElementById('menuImage').src="global/page_4/images/"+menuImageList[nObj.id].image;
			}
			else
			{
				var menuImage =  document.getElementById("menuImage");
				if(menuImage!=null)
				{
					document.getElementById("iconImage").removeChild(document.getElementById("menuImage"));
				}
			}
		}		
	}
	loadData(pid);	

	if(pid!='root')
	{
		
		var nObj = getMenuObject(pid);
		if(nObj.visible==true)
		{
			var pos = nObj.position;
			document.getElementById(pos+"_"+nObj.id).className=menuPosition[pos].openClass;
			document.getElementById(pos+"_"+nObj.id).blur();
		}
		//alert(menuPosition[tempPos].htmlObject);
		
	}
	loadDefaultMenu(pid);	

}
function onMenuClick(id)
{
	callBackOnClick(id);
	drawMenu(id);
}

