var locationIndex = document.location.toString().indexOf("/", 7);
var page_domain = document.location.toString().substring(0, locationIndex);
//var locationIndex = document.URL.toString().indexOf("/", 7);
//var page_domain = document.URL.toString().substring(0, locationIndex);

var gb = "http://gb.weather.gov.hk";
//var gb = "http://test.gb.weather.gov.hk";
var trad_domain = "http://www.weather.gov.hk";
//var trad_domain = "http://test.www.weather.gov.hk";
var trad;

if (document.URL.toString().search(/^http:\/\/gb\./) != -1)
//if (document.URL.toString().search(/^http:\/\/test\.gb\./) != -1)
	trad = trad_domain;
else
	trad = page_domain;

	
function CLFChiLink(clf_link) {
	if (document.URL.search(/^http:\/\/gb\./) != -1) {
//	if (document.URL.search(/^http:\/\/test\.gb\./) != -1) {
		location = trad_domain + clf_link;
	} else {
		location = clf_link;
	}
}

function popupCLFWin(type, lang) {
	var host = '';
	if (type == 'sim') {
		host = gb;
	}

	if (type == 'chi' && document.URL.search(/^http:\/\/gb\./) != -1) {
//	if (type == 'chi' && document.URL.search(/^http:\/\/test\.gb\./) != -1) {
		host = trad_domain;
	}
	
        msgWindow=window.open(host + "/message_"+lang+".htm", "msgWin", "top=350,left=330,width=400,height=100,scrollbars=no,menubar=no,toolbar=no,resizable=no");
        msgWindow.focus();
}

function genImg(width, t_link, s_link, t_title, s_title, img, trad_alt, sim_alt, type) {
	var address = document.URL.toString();
	var add_index;
	var title;
	if (document.URL.search(/^http:\/\/gb\./) != -1) {
//	if (document.URL.search(/^http:\/\/test\.gb\./) != -1) {
		title = t_title;
		if (type == 'chi' || type == 'sim') {
			if (t_link == '' || t_link == '/') {
				add_index = address.indexOf(gb) + gb.length;
				address = trad_domain + address.substring(add_index);
			} else if (t_link.search(/^javascript:/) != -1 ) {
				address = t_link;
			} else {
				address = trad_domain + t_link;
			}
		} else if (type == 'text') {
			address = t_link;
		} else if (type == 'eng') {
			if (t_link.search(/^javascript:/) != -1 ) {
				address = t_link;
			} else {
				address = trad_domain + t_link;
			}
		}
		document.write("<td width=\"" + width + "\" height=\"19\" background=\"/simimages/bkgdtexture.gif\" align=\"left\" valign=\"bottom\"><a href=\"" + address + "\" title=\"" + title + "\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('" + img + "','','/simimages/" + img + "_f2.gif',1)\"><img name=\"" + img + "\" src=\"/simimages/" + img + ".gif\" border=\"0\" alt=\"" + sim_alt + "\"></a></td>\n");
	} else {		
		title = s_title;
		if (type == 'chi' || type == 'sim') {
			if (s_link == '' || s_link == '/') {
				add_index = address.indexOf(page_domain) + page_domain.length;
				address = gb + address.substring(add_index);
			} else if (s_link.search(/^javascript:/) != -1 ) {
				address = s_link;
			} else {
				address = gb + s_link;
			}
		} else {
			title = t_title;
			address = t_link;
		}
		document.write("<td width=\"" + width + "\" height=\"19\" background=\"/chiimages/bkgdtexture.gif\" align=\"left\" valign=\"bottom\"><a href=\"" + address + "\" title=\"" + title + "\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('" + img + "','','/chiimages/" + img + "_f2.gif',1)\"><img name=\"" + img + "\" src=\"/chiimages/" + img + ".gif\" border=\"0\" alt=\"" + trad_alt + "\"></a></td>\n");
	}
}

function submit_search_form() {
//	window.document.search.submit();
	if (document.URL.search(/^http:\/\/gb\./) != -1) {
//	if (document.URL.search(/^http:\/\/test\.gb\./) != -1) {
           document.search_form.action="http://sc.info.gov.hk/TuniS/search.gov.hk/search.html";
	} else {
       	   document.search_form.action="http://search.gov.hk/search.html";
        }	        
	window.document.search_form.submit();
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function subMenu(name, linkb, title, cur_page, hasChild) {
	this.name = name;
	this.linkb = linkb;
	this.title = title;
	if (cur_page) {
		this.fontClass = 'chosen';
	} else {
		this.fontClass = 'side';
	}
	this.hasChild = hasChild;
}

function menu(menuNo, name, title, linkb, menuList, level, cur_page, hasParent) {
	this.menuNo = menuNo;
	this.name = name;
	this.title = title;
	this.menuList = menuList;
	this.margin = (level + 1) * 10;
	this.menuLink = linkb;
	if (cur_page) {
		this.fontClass = 'chosen';
	} else {
		this.fontClass = 'side';
	}
	this.hasParent = hasParent;
}

function openmenu(menuNo, dir, menuBgColor, barflag) {
	var menu = eval("menu_"+menuNo);
	var menuList = menu.menuList;
	var x = 0;
	var link = '';
        var checkmenuname = '';
        var submenuname = '';
	//var href_string = '?';
	var exist = false;
	var isDefaultPopup = false ;
	//var menuBgColor = "#0033CC" ;
	var submenuBgColor = "#005FEC" ;
	//var submenuBgColor = "#1192F9" ;

	var curLoc = getCurrentLocation( ) ;

	//if (location.search.toString().indexOf('menu=') - 1 != location.search.toString().indexOf('?')) {
	//	href_string = '&';
	//}

	if ( location.search.toString( ).indexOf('menu=') < 0 )
	{
		// if the submenu item is selected, 
		// openmenu will be called with the url
		// doesn't contain "menu="
		isDefaultPopup = true ;
	}

	// - Check if the current menu is main menu item. If yes,
	//   print out the menu link.
	// - There is no need to generate the menu link for sub menu, 
	//   since the link should be generated by parent's openmenu call.
	if (!menu.hasParent) {
		if ( isDefaultPopup )
		{
			link = curLoc ;
		}
		else
		{
			link = curLoc.substring(0, curLoc.indexOf('menu=') - 1);
		}

		if ( link.indexOf( "defaultpopup=" ) < 0 )
		{
			// check if the URL has other query
			if ( link.indexOf( '?' ) < 0 )
			{
				link = link + "?defaultpopup=0" ;
			}
			else
			{
				link = link + "&defaultpopup=0" ;
			}
		}

		if (menu.menuLink != '') {
			link = menu.menuLink;
		}

		document.write('<tr>\n<td align="left" valign="top" bgcolor="' + menuBgColor 
			+ '"><img src="' + dir + 'bar1.gif" border="0" width="188" height="1"></td>\n</tr>\n');
		document.write('<tr>\n<td align="left" valign="top" bgcolor="' + menuBgColor + '">\n');
		document.write('<p id="space"><a class="' + menu.fontClass + '" href="' + link 
			+ '" title="' + menu.title + '">' + menu.name + '</a>');
		document.write('</td>\n</tr>\n<tr>\n<td align="left" valign="top" bgcolor="' 
			+ menuBgColor + '"><img src="' + dir 
			+ 'bar1.gif" border="0" width="188" height="1"></td>\n</tr>\n');
	}

	// print out the menu link for it's sub menu.
	for (x = 0; x < menuList.length; x++) {
		if (menuList[x].hasChild) {
			if ( isDefaultPopup )
			{
				// This code fragment is to guarantee 
				// the link variable must contain the 
				// "menu=" string

				// check if the URL has other query
				if ( curLoc.indexOf( '?' ) < 0 )
				{
					link = curLoc + "?menu=" ;
				}
				else
				{
					link = curLoc + "&menu=" ;
				}
			}
			else
			{
				link = curLoc.substring(0, curLoc.indexOf('menu=') + 5);
			}

			exist = false;
			var subMenuList  = "" ;
			for (y = 0; y < pageList.length; y++) {
				if (pageList[y] == menuList[x].linkb) {
					// if the sub menu already pop up,
					// it's name should be excluded from the
					// link such that the sub menu will be closed
					// when user click the link.
					exist = true;
				} else {
					if ( subMenuList == "" )
					{
						subMenuList = pageList[y] ;
					}
					else
					{
						subMenuList = subMenuList + '&' + pageList[y] ;
					}
				}
			}
		
			link = link + subMenuList ;

			if (!exist) {
				link = link + '&' + menuList[x].linkb;
			} 

			// override the submenu popup if there is link
			// specified in the menu specification.
			var childMenu = eval("menu_"+menuList[x].linkb);
			if (childMenu.menuLink != '') {
				link = childMenu.menuLink;
			}

		} else {
			link = menuList[x].linkb;
		}

                checkmenuname = menuList[x].name.substring(0,4);
		document.write('<tr>\n<td align="left" valign="top" bgcolor="' + submenuBgColor + '">\n');
		document.write('<table width="100%" cellpadding="0" cellspacing="0" >\n<tr>\n');
                if (checkmenuname == '>>>>') {
                   submenuname = '&nbsp;&nbsp;>&nbsp;' + menuList[x].name.substring(4); 
		   document.write('<td align="right" valign="top" width="' + menu.margin + '%"><span class="submenu"></span></td>\n');
		   document.write('<td><a class="' + menuList[x].fontClass + '" href="' + link + '" title="' + menuList[x].title + '">' + submenuname + '</a></td>\n');
                } else {
		   document.write('<td align="right" valign="top" width="' + menu.margin + '%"><span class="submenu">>&nbsp;</span></td>\n');
		   document.write('<td><a class="' + menuList[x].fontClass + '" href="' + link + '" title="' + menuList[x].title + '">' + menuList[x].name + '</a></td>\n');
                }
		document.write('</tr>\n</table>\n</td>\n</tr>\n');
		document.write('<tr>\n<td align="left" valign="top" bgcolor="' + submenuBgColor + '"><img src="' + dir + 'bar1.gif" border="0" width="188" height="1"></td>\n</tr>\n');

		// open the sub menu if the sub menu is selected.
		if (menuList[x].hasChild) {
			y = 0;
			menuOn = false;
			for (y = 0; y < pageList.length; y++) {
				if(pageList[y] == menuList[x].linkb) {
					menuOn = true;
				}
			}
			if (menuOn) {
				openmenu(menuList[x].linkb, dir, menuBgColor, barflag);
			} else {
				closemenu(menuList[x].linkb, dir, menuBgColor, barflag);
			}
		}

		// display one more line at the end of menu
		if ( ( x == menuList.length - 1 )&& ( !menu.hasParent ) )
		{
                   if (barflag == 0) {
			document.write('<tr>\n<td align="left" valign="top" bgcolor="' + menuBgColor + '"><img src="' + dir + 'bar1.gif" border="0" width="188" height="1"></td>\n</tr>\n'); 
                   }
                   else
                   {
			document.write('<tr>\n<td align="left" valign="top" bgcolor="' + menuBgColor + '"><img src="' + dir + 'wbar1.gif" border="0" width="188" height="1"></td>\n</tr>\n'); 
                   }
		}

	}
}

function closemenu(menuNo, dir, color, barflag) {
	var href_string = '?';
	if (location.search.toString().indexOf('?') >= 0 && location.search.toString().indexOf('menu=') - 1 != location.search.toString().indexOf('?')) {
		href_string = '&';
	}
	var menu = eval("menu_"+menuNo);
	//var loc_link = location.toString();
	var loc_link = getCurrentLocation();

	if (!menu.hasParent) {
		document.write('<tr>\n<td align="left" valign="top" bgcolor="' + color + '">\n');
		if (location.search.toString().indexOf('menu=') >= 0) {
			loc_link = loc_link.substring(0, loc_link.indexOf('menu=') - 1);
		}
		if (menu.menuLink != '') {
			loc_link = menu.menuLink;
            		document.write('<p id="space"><a class="' + menu.fontClass + '" href="' + loc_link + '" title="' + menu.title + '">' + menu.name + '</a>');
		}
		else
		{
            		document.write('<p id="space"><a class="' + menu.fontClass + '" href="' + loc_link + href_string + 'menu=' + menu.menuNo + '" title="' + menu.title + '">' + menu.name + '</a>');
		}
                if (barflag == 0) {
		        document.write('</td>\n</tr>\n<tr>\n<td align="left" valign="top" bgcolor="' + color + '"><img src="' + dir + 'bar1.gif" border="0" width="188" height="1"></td>\n</tr>\n');
                }
                else
                {
		        document.write('</td>\n</tr>\n<tr>\n<td align="left" valign="top" bgcolor="' + color + '"><img src="' + dir + 'wbar1.gif" border="0" width="188" height="1"></td>\n</tr>\n');
                }
	}
}

// Get the current browser location but excludes the link destination
function getCurrentLocation( )
{
	var curLocation = location.toString( ) ;
	var hashIndex = curLocation.indexOf( '#' ) ;
	var searchIndex = curLocation.indexOf( '?' ) ;

	if ( hashIndex > 0 )
	{
		if ( searchIndex > hashIndex )
		{
			curLocation = curLocation.substring( 0, curLocation.indexOf('#') ) + location.search ;
		}
		else
		{
			curLocation = curLocation.substring( 0, curLocation.indexOf('#') ) ;
		}
	}

	return curLocation ;
}
