
currentMenu = false;
_ListMenu = function(){};

_ListMenu.prototype.init = function (){
	this.forceHide = true;	
	this.clickMenuFirst = true;
	this.members = new Array();
	this.isIE = (document.all)?true:false; 
	this.isFF = (!document.all)?true:false;
	this.closeMenuCallers = "|BODY|FORM|HTML|SELECT|INPUT|TEXAREA|BUTTON|DIV|"; //TD|TABLE|A|
	this.maxHeight = 0;
	this.maxWidth = 60;
	this.minWidth = 60;
	this.openBoxEvent='click';
	//navigator.userAgent
}

_ListMenu.prototype.setMinWidth = function (w,n){
	this.members[n].minWidth = w;
}

_ListMenu.prototype.addMember = function (m){
	this.member = {};
	this.setObject(m);
	this.getChildren();
	this.members.push(this.member);
	this.addMemberEvent();
}

_ListMenu.prototype.setObject = function (m){
	this.member.object = $(m);
	this.member.menuN = this.member.object.id.split('_')[1];
	this.member.id = m;
}

_ListMenu.prototype.getChildren = function (){
	this.member.header = $('menuSelect_'+this.member.menuN);
	
	this.member.header.objText = tag(this.member.header,'A')[0];
	this.member.header.objValue = tag(this.member.header,'INPUT')[0];
	
	this.member.header.text = this.member.header.objText.innerHTML;
	this.member.header.value = this.member.header.objValue.value;
	
	this.member.arrow = $('menuArrow_'+this.member.menuN);
	this.member.arrow.objText = tag(this.member.arrow,'A')[0];
	this.member.arrow.text = this.member.header.objText.innerHTML;
	
	this.member.body = $('menuBody_'+this.member.menuN);
	this.member.body.listMembers = tag(this.member.body,'LI');
	
	this.member.body.listMembers.member = new Array();
	
	//simplify object name
	this.listMembers = new Array();
	
	if(this.member.body.listMembers.length>20)
	{
		this.member.body.className =  'menuBodyLong';
	}
	
	for(x=0;x<this.member.body.listMembers.length;x++)
	{
		memberPos = this.member.body.listMembers[x];
			
		memberPos.onclick = function callSelectMenu(){
			selectMenu(this);
		}
			
		this.member.body.listMembers.member[x] = {};
		this.member.body.listMembers.member[x].objValue = tag(memberPos,'B')[0];
		this.member.body.listMembers.member[x].value = this.member.body.listMembers.member[x].objValue.innerHTML;
		
		this.member.body.listMembers.member[x].objText = tag(memberPos,'A')[0];
		this.member.body.listMembers.member[x].text = this.member.body.listMembers.member[x].objText.innerHTML;	
		
		this.listMembers[x] = this.member.body.listMembers.member[x];	
	}
}

_ListMenu.prototype.addMemberEvent = function(){
									
	addEvent(this.member.header, this.openBoxEvent, moveMenu);
	addEvent(this.member.arrow, this.openBoxEvent, moveMenu);	
	
}


_ListMenu.prototype.setCurrentMenu = function(o){
	this.currentMenu = this.members[this.getObjectN(o)];
}

_ListMenu.prototype.getCurrentMenu = function(){
	return this.currentMenu;	
}
	
_ListMenu.prototype.getObjectN = function(o){
	objPosN = o.id.split('_');
	return o.id.split('_')[objPosN.length-1];
}	

_ListMenu.prototype.setHeaderValue = function(){
	currentMenu.header.objText.innerHTML = currentMenu.body.listMembers.member[objN].text;
	currentMenu.header.objValue.value = currentMenu.body.listMembers.member[objN].value;
	currentMenu.header.text = currentMenu.body.listMembers.member[objN].text;
	currentMenu.header.value = currentMenu.body.listMembers.member[objN].value;	
}	

moveMenu = function(e){

	currentMenu = false;
	closeAllMenus();
	objEvent = getObjectEvent(this);

	_listMenus.setCurrentMenu(objEvent);
	currentMenu = _listMenus.getCurrentMenu();
	currentMenu.object.focus();
	
	if(currentMenu.body.style.display==''||currentMenu.body.style.display=='none')
	{
		openMenu();
	}
	else
	{
		//closeMenu();
	
	}
	
	//Fix for LI width on FF ... using for IE as well
	//if(!_listMenus.isIE)
	//{			
		getAndSetMinWidth('menuBody_'+objEvent.id.split('_')[1]);	
	//}	
	if(_listMenus.maxHeight>0)
	{	
		getAndSetTotalHeight('menuBody_'+objEvent.id.split('_')[1]);			
	}	
}

setCurrentMenu = function(o){
	
	currentMenu = false;
	closeAllMenus();
	objEvent = getObjectEvent(o);
	_listMenus.setCurrentMenu(objEvent);
	currentMenu = _listMenus.getCurrentMenu();
}

// Reset the height based on the LIs offsetHeight
getAndSetTotalHeight = function(ddMenu){
	
	heightMenu = $(ddMenu).getElementsByTagName('LI');			
	totalHeight = 0;	
			
	for(d=0;d<heightMenu.length;d++)
	{
		totalHeight+=heightMenu[d].offsetHeight;	
	}
	
	if(totalHeight<_listMenus.maxHeight)
	{				
		$(ddMenu).className = 'menuBody';
	}
	else	
	{
		$(ddMenu).className = 'menuBodyLong';
	}	
}

// fix for FireFox LI width ... I am using for IE as well
getAndSetMinWidth = function(ddMenu){
	
	widthMenus = $(ddMenu).getElementsByTagName('LI');	
	minWidth = new Array();		
		
	for(d=0;d<widthMenus.length;d++)
	{
		minWidth.push(widthMenus[d].clientWidth);
	}
	
	sortAndFindMin = minWidth.sort(sortNumber);													
	minWidth = sortAndFindMin[sortAndFindMin.length-1]	;
	 
	if(_listMenus.minWidth > minWidth)
	{
		minWidth = _listMenus.minWidth;
	}
	
	if(currentMenu.minWidth){
		if(currentMenu.minWidth > minWidth)
		{
			minWidth = currentMenu.minWidth;
		}			
	}
	
	
	for(d=0;d<widthMenus.length;d++)
	{
		if(typeof(window['useMinWidth']) != "undefined")
		{
			if(useMinWidth == 'Y')
			{
				widthMenus[d].style.width = minWidth+'px';
			}
		}
		else
		{
			widthMenus[d].style.width = minWidth+'px';
		}


	}
	
	//this added to expand the UL to the minWidth
	ULMenu = $(ddMenu);	
	ULMenu.style.width = minWidth+'px';
	
	
}

function sortNumber(a, b)
{
	return a - b
}
	

selectMenu = function(o){
	tt = (o)?o:this;
    o = tt;	
    objEvent = getObjectEvent(o);
	objN = _listMenus.getObjectN(objEvent);
	_listMenus.setHeaderValue();
	if(currentMenu.body.onClickEvent)
	{		
		
		if(typeof(window['useDelayedFunction']) != "undefined")
		{
			if(useDelayedFunction == 'Y')
			{
				t=setTimeout("eval(currentMenu.body.onClickEvent+'('+currentMenu.body.onClickEventAttb+')')", 900);
				t=setTimeout("_listMenus.setHeaderValue()",1000);
			}
			else
			{
				eval(currentMenu.body.onClickEvent+'('+currentMenu.body.onClickEventAttb+')');
			}
		}
		else
		{
			eval(currentMenu.body.onClickEvent+'('+currentMenu.body.onClickEventAttb+')');
		}
		
	}		
	closeMenu();
}


_ListMenu.prototype.assignEvent = function(ev,n){
	
		functionName = ev.split('(')[0];
		attb = 	ev.split('(')[1].split(')')[0];	
		
		this.members[n].body.onClickEvent = functionName;
		this.members[n].body.onClickEventAttb = attb;	
}

getCountries = function(call,affect){
	_affect = affect;
	_resultKeys = new Array('COUNTRY_ID','NAME');

	DWREngine._execute(_cfGenericFunctionsLocation, null,'getCountries','eng','aldoDev',dropDownSetRelation);
}

dropDownSetRelation = function(result){
	
	affectPar = (_ListMenu.isIE)?$(_affect).parentElement:$(_affect).parentNode;	
	
	affectobj =	$('menuBody_' + affectPar.id.split('_')[1]);
	
	objChildren = (_ListMenu.isIE)?affectobj.children:affectobj.childNodes;	
	
	actualMember = 	_listMenus.members[affectPar.id.split('_')[1]].body;
	
	if((typeof(isQuickView)!= "undefined") && isQuickView == "show" && navigator.appName != "Microsoft Internet Explorer") {
		deleteChildren(affectobj,0);
	} else {
		deleteChildren(affectobj,1);
	}

	for(x=3;x<result.length+3;x++)
	{
			
		liObj = document.createElement('LI');
		liObj.id =  $(_affect).id + '_'+(x);
					
		bObj = document.createElement('B');	
		bObj.innerHTML = result[x-3][_resultKeys[0]];
		
		aObj = document.createElement('A');	

		_textDisplay = '';	
		for(y=1;y<_resultKeys.length;y++)
		{
			_textDisplay += ' '+result[x-3][_resultKeys[y]];
		}							
			
		aObj.innerHTML = _textDisplay;
		aObj.href = 'javascript:void(0);';
				
		liObj.appendChild(bObj);						
		liObj.appendChild(aObj);
		affectobj.appendChild(liObj);	
		
		liObj.onclick = function callSelectMenu(){
			selectMenu(this);
		}
		
		actualMember.listMembers[x] = '';	
		actualMember.listMembers[x] = liObj;
			
		actualMember.listMembers.member[x] = {};
		
		actualMember.listMembers.member[x].objValue = bObj;
		actualMember.listMembers.member[x].value = result[x-3][_resultKeys[0]];
		
		actualMember.listMembers.member[x].objText = aObj;
		actualMember.listMembers.member[x].text = _textDisplay;
		
		actualMember.listMembers[x] = actualMember.listMembers.member[x];	
				
	}	

	intObj = $('menuSelect_'+affectPar.id.split('_')[1]);
	initObjValue = tag(intObj,'INPUT')[0];
	initObjtext = tag(intObj,'A')[0];
					
	initValue = tag(actualMember.listMembers[0],'B')[0].innerHTML;	
	initText = tag(actualMember.listMembers[0],'A')[0].innerHTML;	
		
	initObjValue.value = initValue;
	initObjtext.innerHTML = initText;			
}			

deleteChildren = function(obj,n){
	
	objChildren = (_ListMenu.isIE)?obj.children:obj.childNodes;
	nVal = (_listMenus.isIE)?n-1:n;	
		
	for(x=objChildren.length-1;x>nVal;x--)
	{
		obj.removeChild(objChildren[x]);
	}				
}

deleteChildrenByTagName = function(obj,tg,n){
	
	objChildren = tag(obj,tg);
	nVal = (_listMenus.isIE)?n-1:n;		
	for(x=objChildren.length-1;x>nVal;x--)
	{
		obj.removeChild(objChildren[x]);
	}				
}

closeMenu = function(){
	if(currentMenu){
		currentMenu.body.style.display = 'none';
	}
}

openMenu = function(){if(currentMenu){currentMenu.body.style.display = 'list-item';}}			

closeAllMenus = function(){
	for(x=0;x<_listMenus.members.length;x++)
	{
		_listMenus.members[x].body.style.display = 'none';
	}								
}
	

_doSearch = function(e){
	
	//simplify name
	if(currentMenu)
	{		
		kCodeN = (_listMenus.isIE)?event.keyCode:e.which;
		
		if(canSearch(kCodeN))
		{	
			kCode = (document.all)?String.fromCharCode(event.keyCode):String.fromCharCode(e.which);
			searchOn = currentMenu.body.listMembers.member;
			for(x=1;x<searchOn.length;x++)
			{
				if(searchOn[x].text.substring(0,1).toUpperCase()==kCode.toUpperCase())
				{
					objN = x;
					//searchOn[x].objText.style.backgroundColor = '#333;'
					_listMenus.setHeaderValue();		
					break;		
				}
			}
		}		
	}													
}

canSearch = function(k){
	if(k>=32||k<=96)
	{
		return true;
	}
	else{return false;}
}

getObjectEvent = function(e){
	
	if(_listMenus.isIE&&e)
	{
		if(e.id)
		{
			return 	e;
		}
		else
		{
			return 	(_listMenus.isIE)?event.srcElement.parentNode:e;
		}	
	}
	else
	{
		return 	(_listMenus.isIE)?event.srcElement.parentNode:e;
	}
}

$ = function (m){
	return document.getElementById(m);
}

tag = function (o,m){
	return o.getElementsByTagName(m);
}


debug = function(oo){

	d = '';	
	for(i in oo)
	{
		d+= i + ' - ' + oo[i] + '\r';			
	}
	
	if(!$('dodo'))
	{
		textArearea = document.createElement('TEXTAREA');
		textArearea.style.width = '500px';
		textArearea.style.height = '500px';
		textArearea.id = 'dodo';
		document.body.appendChild(textArearea);	
	}
	$('dodo').value = d;				
}

dump =  function(oo){
	if(!$('dodo'))
	{
		textArearea = document.createElement('TEXTAREA');
		textArearea.style.width = '500px';
		textArearea.style.height = '500px';
		textArearea.id = 'dodo';
		document.body.appendChild(textArearea);	
	}
	$('dodo').value = oo;
}

addEvent = function(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
	   var r = obj.attachEvent("on"+evType, fn); 
	   return r; 
	} 
	else 
	{ 
	   return false; 
	} 
}
	
checkTag = function(e)
{
	
	if(_listMenus.isIE)
	{
		_tagName = event.srcElement.tagName;
	}
	else
	{
		_tagName = e.target.nodeName;
	}

	if(_listMenus.closeMenuCallers.indexOf('|'+_tagName+'|')>=0)
	{
		closeMenu();	
	}
}	

//addEvent(document, 'keyup', _doSearch);
addEvent(document, 'click', checkTag);










// For Js dropDown menu built

_ListMenu.prototype.initJsDropDown = function (){
	this.mainClass = '';
	this.menuName = '';
	this.dropDownSize = 'colapse'; //colapse or showAll
	this.bodyClassName = 'menuBody';
	this.currentObjectN = this.getLastN()+1;
}

_ListMenu.prototype.getLastN = function (){
	n = (!this.member)?-1:this.member.menuN;
	return parseInt(n);
}

_ListMenu.prototype.setClassName = function (){
	this.mainClass = 'menuWrap '+this.mainClass;
}

_ListMenu.prototype.setDropDownSize = function (w){
	this.dropDownSize = w;
	this.bodyClassName = (w == 'colapse')?'menuBody':'menuBodyLong';
}

_ListMenu.prototype.setMenuStructure = function (){
	
	this.setClassName();
	
	this.mainDiv = document.createElement('DIV');
	this.mainDiv.id = 'menuWrap_'+this.currentObjectN;
	this.mainDiv.className = this.mainClass;
	
	this.menuSelect = document.createElement('DIV');
	this.menuSelect.id = 'menuSelect_'+this.currentObjectN; 
	this.menuSelect.className = 'menuSelect';
	
	//Menu Select Children
	this.menuHeader =  document.createElement('A');
	this.menuHeader.id = 'menuHeader_'+this.currentObjectN;
	this.menuHeader.href = 'javascript:void(0);';
	
	this.menuValueObject =  document.createElement('INPUT');
	this.menuValueObject.id = this.menuName;
	this.menuValueObject.name = this.menuName;
	this.menuValueObject.type = 'checkbox';
	this.menuValueObject.checked = true;
	// --- Menu Select Children
	
	this.menuSelect.appendChild(this.menuHeader);
	this.menuSelect.appendChild(this.menuValueObject);
	
	this.menuArrow =  document.createElement('DIV');
	this.menuArrow.id = 'menuArrow_'+this.currentObjectN;
	this.menuArrow.className = 'menuArrow';
	
	//Menu Arrow Child
	this.menuArrowA =  document.createElement('A');
	this.menuArrowA.innerHTML = '+';
	// --- Menu Arrow Child
	
	this.menuArrow.appendChild(this.menuArrowA);
	
	this.br1 =   document.createElement('BR');
	this.br2 =   document.createElement('BR');
	
	this.menuBody =  document.createElement('UL');
	this.menuBody.id = 'menuBody_'+this.currentObjectN; 
	this.menuBody.className = this.bodyClassName;
	
	this.mainDiv.appendChild(this.menuSelect);
	this.mainDiv.appendChild(this.menuArrow);
	this.mainDiv.appendChild(this.br1);
	this.mainDiv.appendChild(this.br2);
	this.mainDiv.appendChild(this.menuBody);
	
}

_ListMenu.prototype.setMenuObjects = function (){
	
	if(arguments.length==2)
	{
		values = arguments[0];
		texts =  arguments[1];
		
		for(x=0;x<values.length;x++)
		{
			this.li = document.createElement('LI');
			this.li.id = this.menuName+'_'+x;
			
			this.b =  document.createElement('B');
			this.b.innerHTML = values[x];
			
			this.a =  document.createElement('A'); 
			this.a.href = 'javascript:void(0);';
			this.a.innerHTML = texts[x];
			
			this.li.appendChild(this.b);
			this.li.appendChild(this.a);
			this.menuBody.appendChild(this.li);
		}
	}
	else
	{
		alert('You must pass value and text in 2 arrays.')	
	}
	
	return this.mainDiv;
}


_ListMenu.prototype.appendMenu = function (o,ch){
	o.appendChild(ch);
	_listMenus.addMember('menuWrap_'+this.currentObjectN);
}

