var flg_browser;
var linkObj;

window.onBeforeUnload = browser_check();

function browser_check(){
	var browser = navigator.userAgent.toLowerCase();	
		
	if(browser.indexOf("msie") != -1) flg_browser = 'ie';
	OnloadAddEvent();
}

function OnloadAddEvent(){
	if(flg_browser == 'ie')	window.attachEvent('onload',linkLoad);
	else window.addEventListener('load',linkLoad,false);
}

function linkLoad(){
	var fileName;
	var uri = location.href;
	fileName = 'common/js/linkList.xml';
	if(uri.match('/philosophy/') || uri.match('/company/') || uri.match('/business/') || uri.match('/irinfo/') || uri.match('/csr/') || uri.match('/pages/') || uri.match('/irtest/')){
		fileName = '../' + fileName;
	}
	
	linkObj = createHttpRequest();	
	linkObj.open('get',fileName,true);
	linkObj.onreadystatechange = function(){
		if(linkObj.readyState == 4){
			linkSet();
			headSet();
			footSet();
		}
	}
	
	linkObj.send('');
}

function createHttpRequest(){
	if(window.ActiveXObject){
		try	{
			return new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e1){
			try	{
				return new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e2){
				return null;
			}
		}
		
	}
	else if(window.XMLHttpRequest) return new XMLHttpRequest();
	else return null;
}

function linkSet(){
	var linkHTML = '<ul id="menu">';
	var submenuHTML = '';
	var Item = linkObj.responseXML.getElementsByTagName('category');
	var id;
	var menucount = 0;
	var submenucount = 0;
	var menuheight = 26;
	var uri = location.href;
	var menu_class;
	var blank;
	var num = 0;
	
	var uri_array = uri.split("/");
	var script_name;
	
	if(uri.match('moon') || uri.match('renewal') || uri.match('english')){
		script_name = '/'+uri_array[uri_array.length-3]+'/'+uri_array[uri_array.length-2]+'/'+uri_array[uri_array.length-1];
	}else{
		script_name = '/'+uri_array[uri_array.length-2]+'/'+uri_array[uri_array.length-1];
	}
	
//	document.write(script_name);
	
	if(uri.match('/philosophy/')){
		id = 'philosophy';
	}else if(uri.match('/company/')){
		id = 'company';
	}else if(uri.match('/business/')){
		id = 'outline';
	}else if(uri.match('/irinfo/') || uri.match('/irtest/')){
		id = 'ir';
	}else if(uri.match('/csr/')){
		id = 'csr';
	}
	if(uri == 'http://www.assetmanagers.co.jp/' || uri == 'http://www.assetmanagers.co.jp/' || uri == 'http://www.assetmanagers.co.jp/index.php'){
		id = 'top';
	}
	
	for(i=0; i < Item.length; i++){
		detailLink = Item[i].getElementsByTagName('detail');
		
		//irnews 2009-12-22
		if(Item[i].getAttribute("id") == 'irnews'){
			if(Item[i].getAttribute("blank")){
				blank = 'target="_blank"';
			}else{
				blank = '';
			}
			
			if(id !='top' && id != 'ir'){
				continue;
			}else{
				if(id != 'ir'){
					linkHTML = linkHTML + '<li class="'+ Item[i].getAttribute("id") +'" id="'+ Item[i].getAttribute("id") +'"><a href="'+ Item[i].getAttribute("href") +'" title="'+Item[i].getAttribute("alt")+'"'+blank+'><img src="'+Item[i].getAttribute("img")+'.gif" alt="'+Item[i].getAttribute("alt")+'" height="30" width="165"></a></li>';
				}else{
					linkHTML = linkHTML + '<li class="'+ Item[i].getAttribute("id") +'" id="'+ Item[i].getAttribute("id") +'"><a href="'+ Item[i].getAttribute("href") +'" title="'+Item[i].getAttribute("alt")+'"'+blank+'><img src="'+Item[i].getAttribute("img")+'.gif" alt="'+Item[i].getAttribute("alt")+'" height="30" width="165"></a></li>';
				}
				continue;
			}
		}
		
		// 現在地めぬー
		if(id == Item[i].getAttribute("id")){
			linkHTML = linkHTML + '<li class="'+ Item[i].getAttribute("id") +'_on" id="'+ Item[i].getAttribute("id") +'"><a href="'+ Item[i].getAttribute("href") +'" title="'+Item[i].getAttribute("alt")+'"><img src="'+Item[i].getAttribute("img")+'g.gif" alt="'+Item[i].getAttribute("alt")+'" height="25" width="165"></a></li>';
			for(j=0; detailLink.item(j); j++){
				if(detailLink.item(j).getAttribute("blank")){
					blank = 'target="_blank"';
				}else{
					blank = '';
				}
				
				//現在地表示 2009-12-22
				if(script_name == detailLink.item(j).getAttribute("href")){
					linkHTML = linkHTML + '<li id="'+ detailLink.item(j).getAttribute("id") +'_on"><a href="'+ detailLink.item(j).getAttribute("href") +'" title="'+detailLink.item(j).getAttribute("alt")+'"'+blank+'><img src="'+detailLink.item(j).getAttribute("img")+'_on.gif" alt="'+detailLink.item(j).getAttribute("alt")+'" height="25" width="165"></a></li>';
				}else{
					linkHTML = linkHTML + '<li id="'+ detailLink.item(j).getAttribute("id") +'"><a href="'+ detailLink.item(j).getAttribute("href") +'" title="'+detailLink.item(j).getAttribute("alt")+'"'+blank+'><img src="'+detailLink.item(j).getAttribute("img")+'.gif" alt="'+detailLink.item(j).getAttribute("alt")+'" height="25" width="165"></a></li>';
				}
				submenucount++;
			}
		// 現在地以外めぬー
		}else{
			// さぶめぬーの有無
			if(detailLink.length){
				linkHTML = linkHTML + '<li class="'+ Item[i].getAttribute("id") +'" id="'+ Item[i].getAttribute("id") +'" onMouseOver="show(\'menu_'+ Item[i].getAttribute("id") +'\');" onMouseOut="hide(\'menu_'+ Item[i].getAttribute("id") +'\');"><a href="'+ Item[i].getAttribute("href") +'" title="'+Item[i].getAttribute("alt")+'"><img src="'+Item[i].getAttribute("img")+'.gif" alt="'+Item[i].getAttribute("alt")+'" height="25" width="165"></a></li>';
				var margin = menuheight * (submenucount + menucount);
				submenuHTML = submenuHTML + '<div style="margin-top:'+margin+'px" id="menu_'+ Item[i].getAttribute("id") +'" onMouseOver="show(\'menu_'+ Item[i].getAttribute("id") +'\');submenu_on(\''+ Item[i].getAttribute("id") +'\');" onMouseOut="hide(\'menu_'+ Item[i].getAttribute("id") +'\');submenu_off(\''+ Item[i].getAttribute("id") +'\');"><ul id="sub_'+ Item[i].getAttribute("id") +'">';
				
				num=0;
				for(j=0; detailLink.item(j); j++) if(!detailLink.item(j).getAttribute("att")) num += 1;
				for(j=0; j<num; j++){
					if(detailLink.item(j).getAttribute("blank")){
						blank = 'target="_blank"';
					}else{
						blank = '';
					}

					if(num > j+1){
						menu_class = 'normal';
					}else{
						menu_class = 'last';
					}
					if(!detailLink.item(j).getAttribute("att")){
						submenuHTML = submenuHTML + '<li class="'+ menu_class +'" id="'+ detailLink.item(j).getAttribute("id") +'"><a href="'+ detailLink.item(j).getAttribute("href") +'" title="'+ detailLink.item(j).getAttribute("alt") +'" '+blank+'>'+ detailLink.item(j).getAttribute("alt") +'</a></li>';
					}
				}
				
				submenuHTML = submenuHTML + '</ul></div>';
			}else{
				linkHTML = linkHTML + '<li class="'+ Item[i].getAttribute("id") +'" id="'+ Item[i].getAttribute("id") +'"><a href="'+ Item[i].getAttribute("href") +'" title="'+Item[i].getAttribute("alt")+'"><img src="'+Item[i].getAttribute("img")+'.gif" alt="'+Item[i].getAttribute("alt")+'" height="25" width="165"></a></li>';
			}
		}
		menucount++;
	}
	linkHTML = linkHTML + '</ul>';
	document.getElementById('leftmenu').innerHTML = linkHTML;
	document.getElementById('submenu').innerHTML = submenuHTML;
}

function submenu_on(id){
	if(flg_browser == 'ie'){
		document.getElementById(id).setAttribute('className',id+'_on');
	}else{
		document.getElementById(id).setAttribute('class',id+'_on');
	}
}

function submenu_off(id){
	if(flg_browser == 'ie'){
		document.getElementById(id).setAttribute('className',id);
	}else{
		document.getElementById(id).setAttribute('class',id);
	}
}


function headSet(){
	var headHTML = '<p id="english"><a href="/english/" title="English" name="top"><span><img src="/images/common_head_b_english02.gif" alt="English" height="20" width="57"></span></a></p><!--<p style="float:left;width:1px;background-color:#f39704;"><img src="images/spacer.gif" alt="" height="19" width="1"></p><p id="chinese"><a href="" title="Chinese"><span>Chinese</span></a></p>-->';
	document.getElementById('foreign').innerHTML = headHTML;
}

function footSet(){
	var footHTML;
	
	if(document.getElementById('foot')){
		footHTML  = '<div id="b_pagetop"><a href="#top">ページトップへ</a></div>';
		footHTML += '<p><a href="/pages/terms.html">利用方針</a>｜<a href="/pages/policy.html">個人情報保護方針</a>｜<a href="http://www.aspir.co.jp/koukoku/2337/2337.html" target="_blank">電子公告</a>｜<a href="/pages/careers.html">採用情報</a></p><p style="color:#333333">&copy;2010 Asset Managers Holdings Co., Ltd. All rights reserved.</p>';

	}else if(document.getElementById('foot2')){
		footHTML = '<p><a href="/pages/terms.html">利用方針</a>｜<a href="/pages/policy.html">個人情報保護方針</a>｜<a href="http://www.aspir.co.jp/koukoku/2337/2337.html" target="_blank">電子公告</a>｜<a href="/pages/careers.html">採用情報</a></p><p style="color:#333333">&copy;2010 Asset Managers Holdings Co., Ltd. All rights reserved.</p>';
	}
	if(document.getElementById('foot')){
		document.getElementById('foot').innerHTML = footHTML;
	}else if(document.getElementById('foot2')){
		document.getElementById('foot2').innerHTML = footHTML;
	}

}
