/**
* CSShiarchMenu v0.6
*	written by: me[AT]daantje[DOT]nl
*	last update: Sat Nov 26 13:08:45 CET 2005
*
*	Documentation:
*		Build this small script cause all the gpl-ed scripts I found where too big
*		and had too many options I never going to use, or the config for the menu
*		was not easy to set with a PHP routine.
*
*	License:
*		LGPL. (http://www.gnu.org/copyleft/lesser.txt)
*
*	Dontate:
*		Please, when you like my script, click a view banners on
*		http://www.dantje.nl Or use my pay-pal donation button.
*/

//declare
var submenu = new Array();
var tmr = new Array();
var adj = new Array();
var last_zIndex = 10000;
var lastOverId = '';


// EXAMPLE CONFIG
var alignSubmenu 	= 'bottom';		//bottom or right side of the main button.
var useLastItemCSS	= true;			//generate last menu item too
									//if false, it will behave as a normal item
									//and the css menuItemLast class will not be used
var adjustFirst		= false;		//overlap the first submenu too?
var adjustTop		= 0;			//move the submenu's some pixels. Positive number (10) for down, negative (-10) for up.
var adjustLeft		= 0;			//move the submenu's some pixels. Positive number (10) for right, negative (-10) for left.

//SAMPLE CONFIG

//submenu['menu19'] = new Array();													//layer1
//submenu['menu19'][0] = menuItem('my own website','http://www.daantje.nl','_top');
//submenu['menu19'][1] = menuItem('other sites');
	//submenu['menu19_1'] = new Array();											//layer2
	//submenu['menu19_1'][0] = menuItem('google','http://www.ipd.gov.hk','_self');
	//submenu['menu19_1'][1] = menuItem('/.','../cargo.htm','_self');
	//['menu19_1'][2] = menuItem('Open in _BLANK:');
		//submenu['menu19_1_2'] = new Array();										//layer3
		//submenu['menu19_1_2'][0] = menuItem('/.','../cargo/cargo.htm','_self');
		//['menu19_1_2'][1] = menuItem('test again','http://www.slashdot.org','_blank');
//submenu['menu1'][2] = menuItem('This should be the last one');

// END OF EXAMPLE CONFIG

//Home
submenu['menu1'] = new Array();

//Whats News
submenu['menu2'] = new Array();
submenu['menu2'][0] = menuItem('News','/eng/whats_new/news.htm','_self');
submenu['menu2'][1] = menuItem('Press Releases','/eng/whats_new/press_releases.htm','_self');
submenu['menu2'][2] = menuItem('Upcoming Events','/eng/whats_new/up_coming.htm','_self');

//About us
submenu['menu3'] = new Array();
submenu['menu3'][0] = menuItem('Mission Statement','/eng/about_us/vision_and_mission.htm','_self');
submenu['menu3'][1] = menuItem('Organization','/eng/about_us/organisation.htm','_self');
submenu['menu3'][2] = menuItem('Outsourcing Exercise','/eng/about_us/outsourcing.htm','_self');
submenu['menu3'][3] = menuItem('Examination of Estimates of Expenditure','/eng/about_us/reply/CITB_E.pdf','_blank');
submenu['menu3'][4] = menuItem('Performance Pledge','/eng/about_us/performance_pledge.htm','_self');
submenu['menu3'][5] = menuItem('IP Statistics','/eng/intellectual_property/ip_statistics.htm','_self');
submenu['menu3'][6] = menuItem('Employment Opportunities','/eng/about_us/employment.htm','_self');
submenu['menu3'][7] = menuItem('Contact Us','/eng/about_us/contact_us.htm','_self');
submenu['menu3'][8] = menuItem('Other Information','/eng/about_us/otherinfo.htm','_self');

//Publications and Press Releases
submenu['menu4'] = new Array();
submenu['menu4'][0] = menuItem('Publications','/eng/pub_press/publications.htm','_self');
submenu['menu4'][1] = menuItem('Consultation Papers','/eng/pub_press/consultation_papers.htm','_self');
submenu['menu4'][2] = menuItem('IPD Circulars','/eng/pub_press/ipd_circulars.htm','_self');
submenu['menu4'][3] = menuItem('Press Releases','/eng/pub_press/press_releases.htm','_self');
submenu['menu4'][4] = menuItem('Speeches and Presentations','/eng/pub_press/speeches.htm','_self');
submenu['menu4'][5] = menuItem('Study Aids','/eng/intellectual_property/study_aids.htm','_self');

//Code on Access to information
submenu['menu19'] = new Array();
submenu['menu19'][0] = menuItem('Code on Access to Information','/eng/access_to_information/code_on_access_to_information.htm','_self');
submenu['menu19'][1] = menuItem('Requests for information','/eng/access_to_information/requests_for_information.htm','_self');
submenu['menu19'][2] = menuItem('List of information (either published or otherwise ) made available to the public, either free of charge or at a cost','/eng/access_to_information/list_of_information_published.htm','_self');
submenu['menu19'][3] = menuItem('List of records by category which<br>the public may have access to','/eng/access_to_information/list_of_records.htm','_self');

//What is Intellectual Property?
submenu['menu6'] = new Array();
submenu['menu6'][0] = menuItem('IP Overview','/eng/intellectual_property.htm#01','_self');
submenu['menu6'][1] = menuItem('Trademarks','/eng/intellectual_property.htm#02','_self');
submenu['menu6'][2] = menuItem('Patents','/eng/intellectual_property.htm#03','_self');
submenu['menu6'][3] = menuItem('Designs','/eng/intellectual_property.htm#04','_self');
submenu['menu6'][4] = menuItem('Copyright','/eng/intellectual_property.htm#05','_self');
submenu['menu6'][5] = menuItem('Trade Secrets (Undisclosed <br>Commercial Information)','/eng/intellectual_property.htm#06','_self');
submenu['menu6'][6] = menuItem('Protection of Layout-Design (Topography) of Integrated Circuits','/eng/intellectual_property.htm#07','_self');
submenu['menu6'][7] = menuItem('Plant Varieties Protection','/eng/intellectual_property.htm#08','_self');
submenu['menu6'][8] = menuItem('Company Name Registration: Not an Indication of Intellectual Property Rights','/eng/intellectual_property.htm#09','_self');


//Applying for Registration
submenu['menu7'] = new Array();
submenu['menu7'][0] = menuItem('Trademarks','/eng/applicants/trademarks.htm','_self');
submenu['menu7'][1] = menuItem('Patents','/eng/applicants/patents.htm','_self');
submenu['menu7'][2] = menuItem('Designs','/eng/applicants/designs.htm','_self');

//Forms and Fees
submenu['menu8'] = new Array();
submenu['menu8'][0] = menuItem('Trademarks Forms and Fees Cap.559','/eng/forms_fees/trademarks_559.htm','_self');
submenu['menu8'][1] = menuItem('Trademarks Forms and Fees Cap.43','/eng/forms_fees/trademarks_43.htm','_self');
submenu['menu8'][2] = menuItem('Patents Forms and Fees','/eng/forms_fees/patents.htm','_self');
submenu['menu8'][3] = menuItem('Designs Forms and Fees','/eng/forms_fees/design.htm','_self');
submenu['menu8'][4] = menuItem('Copyright Licensing Bodies <br>Forms and Fees','/eng/forms_fees/copyright.htm','_self');

//FAQ
submenu['menu11'] = new Array();
submenu['menu11'][0] = menuItem('Trademarks','/eng/faq/trademarks.htm','_self');
submenu['menu11'][1] = menuItem('Patents','/eng/faq/patents.htm','_self');
submenu['menu11'][2] = menuItem('Designs','/eng/faq/designs.htm','_self');
submenu['menu11'][3] = menuItem('Copyright','/eng/faq/copyright.htm','_self');
submenu['menu11'][4] = menuItem('Trade Secrets <br>(Undisclosed Commercial Information) ','/eng/intellectual_property/trade_secrets.htm','_self');

//IP Practitioners
submenu['menu12'] = new Array();
submenu['menu12'][0] = menuItem('News','/eng/ip_practitioners/whats_new.htm','_self');
submenu['menu12'][1] = menuItem('Trademarks','/eng/ip_practitioners/trademarks.htm','_self');
submenu['menu12'][2] = menuItem('Patents','/eng/ip_practitioners/patents.htm','_self');
submenu['menu12'][3] = menuItem('Designs','/eng/ip_practitioners/designs.htm','_self');
submenu['menu12'][4] = menuItem('Copyright','/eng/ip_practitioners/copyright.htm','_self');
submenu['menu12'][5] = menuItem('Trade Secrets <br>(Undisclosed Commercial Information)','/eng/intellectual_property/trade_secrets.htm','_self');
submenu['menu12'][6] = menuItem('Consultation Papers','/eng/ip_practitioners/consultation.htm','_self');
submenu['menu12'][7] = menuItem('Opening up of patent and trademark agency services in the Mainland','/eng/whats_new/news/Opening_up_patent_and_trade_mark_agency_services_in_the_Mainland.htm','_self');
submenu['menu12'][8] = menuItem('Application for Guangdong Provincial Famous Trademarks','/eng/ip_cooperation_corner/famous_tm.htm','_self');
submenu['menu12'][9] = menuItem('International Agreements to which <br>Hong Kong, China is a party','/eng/ip_practitioners/international_agreements.htm','_self');

//Guangdong/Hong Kong IP Cooperation Corner
submenu['menu14'] = new Array();
submenu['menu14'][0] = menuItem('Introduction','/eng/ip_cooperation_corner/introduction.htm','_self');
submenu['menu14'][1] = menuItem('Meetings of "Guangdong/Hong Kong <br>Expert Group on the Protection of <br>Intellectual Property Rights"','/eng/ip_cooperation_corner/meeting.htm','_self');
submenu['menu14'][2] = menuItem('Upcoming Events','/eng/ip_cooperation_corner/upcoming_events.htm','_self');
submenu['menu14'][3] = menuItem('Cooperation Items','/eng/ip_cooperation_corner/cooperation_items.htm','_self');
submenu['menu14'][4] = menuItem('Enforcement Cooperation','/eng/ip_cooperation_corner/enforcement_cooperation.htm','_self');
submenu['menu14'][5] = menuItem('Links','/eng/ip_cooperation_corner/Link.htm','_self');

//Promotion and Education
submenu['menu15'] = new Array();
submenu['menu15'][0] = menuItem('Upcoming Events','/eng/promotion_edu/upcoming_events.htm','_self');
submenu['menu15'][1] = menuItem('Seminars and Exhibitions','/eng/promotion_edu/seminars.htm','_self');
submenu['menu15'][2] = menuItem('Public Lectures','/eng/promotion_edu/public_lecture.htm','_self');
submenu['menu15'][3] = menuItem('Surveys','/eng/promotion_edu/survey.htm','_self');
submenu['menu15'][4] = menuItem('"I Pledge" Campaign','/eng/promotion_edu/i_pledge.htm','_self');
submenu['menu15'][5] = menuItem('"No Fakes Pledge" Scheme','/eng/promotion_edu/no_fakes.htm','_self');
submenu['menu15'][6] = menuItem('Public Education Activities','/eng/promotion_edu/educational_corner.htm','_self');
submenu['menu15'][7] = menuItem('Promotional and Educational Videos','/eng/promotion_edu/promotional_educational_videos.htm','_self');
submenu['menu15'][8] = menuItem('Promotional Materials and Learning Aids','/eng/promotion_edu/materials.htm','_self');

//Tender Notices
//submenu['menu16'] = new Array();
//submenu['menu16'][0] = menuItem('Tender Notices','/eng/tender/tender_notices.htm','_self');
//submenu['menu16'][1] = menuItem('Tenderers Briefing (26.5.2011)','/eng/tender/Tenderers_Briefing_20110526.pdf','_self');
//submenu['menu16'][2] = menuItem('Tenderers Enquiries','/eng/tender/Tenderers_Enquiries.pdf','_self');

//build or unhide submenu div...
function buildSubmenu(obj,align){
	lastOverId = obj.id;

	//overrule default alignment of this submenu
	if(!align)
		align = alignSubmenu;

	//get common part of div id
	menuPath = obj.id.split('_');

	//unset mousout of parent menus and make sure they are visible...
	x = "div";
	for(i=0;i<menuPath.length;i++){
		x+= '_' + menuPath[i];
		if(document.getElementById(x)){
			if(tmr[x])
				window.clearTimeout(tmr[x]);
			document.getElementById(x).style.visibility = 'visible';
		}
	}

	//check if we have a submenu of the obj...
	if(submenu[obj.id]){
		//calc position of mouseover
		d = obj;
		if(d){
			L_pos = d.offsetLeft + d.offsetWidth;
			T_pos = d.offsetTop;
			while(d.offsetParent){
				d = d.offsetParent;
				L_pos+= d.offsetLeft;
				T_pos+= d.offsetTop;
			}
		}

		//patch first submenu to go right below the main buttons...
		if(obj.className.indexOf('menuItem') < 0 && align == 'bottom'){
			L_pos-= obj.offsetWidth;
			T_pos+= obj.offsetHeight;
		}

		//move the submenu (overlap?)
		if((adjustTop || adjustLeft) && (adjustFirst || (!adjustFirst && obj.className.indexOf('menuItem') >= 0))){
			L_pos+= adjustLeft;
			T_pos+= adjustTop;
		}

		//Only parse when position is higher than 0,0... Else we dont have the right position!
		if(L_pos > 0 && T_pos >0){
			//check if allready build...
			c = document.getElementById('div_' + obj.id);
			if(c){
				//unhide...
				c.style.visibility = 'visible';
				c.style.zIndex = last_zIndex++;

				//hard replace
				c.style.top = (T_pos - 1) + "px";
				c.style.left = (L_pos - 1) + "px";
			}else{
				//build new div
				subObj = document.createElement('div');
				subObj.id = 'div_' + obj.id;
				subObj.className = 'submenu';
				subObj.style.position = 'absolute';
				subObj.style.zIndex = last_zIndex++;
				subObj.style.top = (T_pos - 1) + "px";
				subObj.style.left = (L_pos - 1) + "px";

				//write div to the body...
				document.getElementsByTagName('body')[0].appendChild(subObj);

				//build html for submenu
				content = "";
				m = submenu[obj.id];
				for(i=0;i<m.length;i++){

					//determin target for onclick...
					//	(sorry, I should rewrite this routine...)
					if(m[i][1]){
						if(m[i][1].substring(0,11) == 'javascript:'){
							act = m[i][1].substring(11);
						}else{
							if(!m[i][2] || m[i][2] == '_self')
								act = "self.location.href='" + m[i][1] + "';";
							else if(m[i][2] == '_top')
								act = "top.location.href='" + m[i][1] + "';";
							else if(m[i][2] == '_parent')
								act = "parent.location.href='" + m[i][1] + "';";
							else if(m[i][2] == '_blank')
								act = "window.open('" + m[i][1] + "');";
							else
								act = "window.frames['"+m[i][2]+"'].location.href='" + m[i][1] + "';";
						}
					}

					//make item
					content+= "<div onmouseout=\"hideSubmenu(this)\" onmouseover=\"buildSubmenu(this)\" "+ (m[i][1] ? "onclick=\""+ act + "\" " : "") +"class=menuItem" + (i==0 ? 'First' : (i==(m.length -1) && useLastItemCSS ? 'Last' : '')) + " id=\"" + obj.id + "_" + i +"\">" + m[i][0] + "</div>";
					adj[i] = obj.id + "_" + i;
				}

				//insert new menu
				subObj.innerHTML = content;
			}
		}
	}
}

//hide a submebu div
function hideSubmenu(obj){
	//get common part of div id
	closePath = obj.id.split('_');

	//hide path
	x = "div";
	for(i=0;i<closePath.length;i++){
		x+= '_' + closePath[i];
		if(document.getElementById(x))
			tmr[x] = window.setTimeout("document.getElementById('"+x+"').style.visibility = 'hidden';",50);
		//The timeout above is needed for MSIE browsers... Or else the menu's will disapear on EVERY mousout!!!
		//Please get a normal browser like Firefox, Mozilla or Opera!!
	}
}

//add an menu item to the config array (called in the config lines)
function menuItem(txt,url,tar){
	return new Array(txt,url,tar);
}

//change div's on mouse over and out...
document.onmouseover = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirst' || obj.className == 'menuItem' || obj.className == 'menuItemLast')
		obj.className+='Over';
}
document.onmouseout = function(e){
	obj = document.all ? event.srcElement : e.target;
	if(obj.className == 'menuItemFirstOver' || obj.className == 'menuItemOver' || obj.className == 'menuItemLastOver')
		obj.className = obj.className.substring(0,(obj.className.length - 4));
}

//*** Patch for firefox bug with focus on mouseover...
//		This function should be called onMouseOver of every iFrame that's under the menu structure.
function iFramePatch(){
	if(!document.all && lastOverId)
		hideSubmenu(document.getElementById(lastOverId));
}


