OhayoObj				= function () {};
OhayoObj.bannerCache	= '';

var FONT_M				= "medium";
var FONT_L				= "large";
var COOKIE_FONT			= "ohayo_font_size";
var COOKIE_EXPIRE		= 30 * 24 * 60 * 60 * 1000;
var PRINT_WINDOW_NAME	= "ohayo_print_window";
var NAV_NUM				= 7;
var navNodes			= new Array();
var navBoxNodes			= new Array();
var menuTimer;
var menuH;
var MENU_TIME_LAG		= 1200;


var openMenuId;
var openedMenuId;
var navIds				= [ 'products', 'fun', 'campaign', 'support', 'recruit', 'policy', 'company' ];
						
var fontSize			= FONT_M;
var fontSizeNode;

var searchOptTimer;
var searchOptNode;
var searchOptH = 0;
var searchOptBtnNode;
var searchOptBoxNode;
var searchOptAreaNode;

var printNode;

var isReadyBoo			= 0;

function initMain()
{
	getComptedStyle();
	
//	replaceMenuNavs();			// <- 待てないからソース内に移動
	
	putSideBanners();
	
	replaceAnchor();
	
	replaceOhayoAnchor();
	
	modInputTextForm();
	
	handleFontSize();
	
	handlePrint();
	
	replaceSearchOptionButton();
	
	isReadyBoo = 1;
	
//	replaceRecruitNav();		// for Renewal
}

function isReady()
{
	return isReadyBoo;
}

/**************************************************
 *
 *	入力フォームの処理
 *
**************************************************/
function modInputTextForm()
{
	var iNodes = document.getElementsByTagName( 'input' );
	var tNodes = document.getElementsByTagName( 'textarea' );
	
	if ( iNodes )
	{
		for( var i=0, l=iNodes.length; i<l; i++ )
		{
			var node = iNodes[i];
			
			//
			// 検索フォームならば
			//
			if ( node.name == "q" )
			{
				addFunctionToElement( node, "onfocus", function(){ if ( this.value == '検索したい語句を入力' ){ this.value = ''; }} );
				addFunctionToElement( node, "onblur", function(){ if ( this.value == '' ){ this.value = '検索したい語句を入力'; }} );
			}
			
			//
			// フォーカス後の色変更
			//
			if( node.type == "text" || node.type == "password" )
			{
				node.style.border = '1px solid #a7a6aa';
				addFunctionToElement( node, "onfocus", function(){ this.oldbg = this.style.backgroundColor; this.oldbdr = this.style.borderColor; this.style.backgroundColor = '#f8fcff'; this.style.borderColor = '#7db8de'; } );
				addFunctionToElement( node, "onblur", function(){ if ( this.oldbg ){ this.style.backgroundColor = this.oldbg; }else{ this.style.backgroundColor = '#fff'; }; if ( this.oldbdr ){ this.style.borderColor = this.oldbdr; }} );
			}
		}
	}
	
	if ( tNodes )
	{
		for( var i=0, l=tNodes.length; i<l; i++ )
		{
			var node = tNodes[i];
			
			node.style.border = '1px solid #a7a6aa';
				addFunctionToElement( node, "onfocus", function(){ this.oldbg = this.style.backgroundColor; this.oldbdr = this.style.borderColor; this.style.backgroundColor = '#f8fcff'; this.style.borderColor = '#7db8de'; } );
				addFunctionToElement( node, "onblur", function(){ if ( this.oldbg ){ this.style.backgroundColor = this.oldbg; }else{ this.style.backgroundColor = '#fff'; }; if ( this.oldbdr ){ this.style.borderColor = this.oldbdr; }} );
		}
	}
}

/**************************************************
 *
 *	その他のアンカーの処理
 *
**************************************************/
function replaceOhayoAnchor()
{
	var aNodes = document.getElementsByTagName( 'a' );
	var node;
	
	for( var i=0, l=aNodes.length; i<l; i++ )
	{
		node = aNodes[i];
		
		if ( node )
		{
			//
			// 一覧へ戻る
			//
			if( node.id == "return2list" )
			{
				var anchor = URL.replace( /[^\/]*$/, '' );
				
				//
				// i) 直URL
				//
				if( !REFERRER && !window.name )
				{
					node.href = anchor;
				}
				//
				// ii) 別ドメインから飛んできたか、別ウィンドウ表示の場合はアンカーを消す
				//
				else if(( REFERRER.match( HOST_REG ) && RegExp.$1 != HOST_NAME ) || window.name )
				{
					node.parentNode.removeChild( node );
				}
				//
				// iii) 一覧画面、検索画面、別ページに戻る
				//
				else
				{
					node.href = REFERRER;
					
					if ( anchor != REFERRER )
					{
						var modRef = REFERRER.replace( /\?.*$/, '' );
						
						//
						// 一覧に戻る
						//
						if( modRef.match(/(?:index\.html|\/)$/) && !modRef.match(/(?:ohayo|co\.jp)\/(?:index\.html$|$)/) )
						{
							node.className = '';
						}
						//
						// 前ページに戻る（2010.05.07）
						//
						else
						{
							node.className = 'return2';
						}
					}
					
				//	node.onclick = function()
				//	{
				//		history.back();
				//		return false;
				//	}
				}
			}
			//
			// History.back()
			//
			else if ( node.className == 'bach' )
			{
				node.href = "#";
				node.onclick = function()
				{
					history.back();
					return false;
				}
			}
			
		}
	}
}

/**************************************************
 *
 *	メニューの処理
 *
**************************************************/
function replaceMenuNavs()
{
	for ( var i = 0; i < NAV_NUM; i++ )
	{
		var navNode			= document.getElementById( 'nav' + digit( i + 1 ) );
		
		if ( !navNode )
		{
			return;
		}
		
		var navUlNodes		= navNode.getElementsByTagName( 'ul' );
		
		//
		// 子メニューがある場合
		//
		if ( navUlNodes[0] )
		{
			navNodes[i]		= navNode;
			navBoxNodes[i]	= navUlNodes[0];
			
			navBoxNodes[i].id = navNode.id + "box";
			
			navNode.onmouseover = function()
			{
				openMenuId = this.id;
				checkOpenMenu();
				return false;
			}
			
			navNode.onmouseout = function()
			{
				openMenuId = null;
				setTimeout( function(){ checkOpenMenu() }, MENU_TIME_LAG );	// 遅延処理
				return false;
			}
		}
		//
		// 子メニューがない場合
		//
		else
		{
			navNode.onmouseover = function()
			{
				checkOpenMenu();	// 開いている子メニューを消すため
				return false;
			}
		}
	}
}

function checkOpenMenu()
{
	for ( var i = 0; i < NAV_NUM; i++ )
	{
		if ( navNodes[i] )
		{
			if ( openMenuId == navNodes[i].id )
			{
				showArea ( navNodes[i].id + "box" );
				showMenuAni( navBoxNodes[i] );
				
				navNodes[i].firstChild.style.backgroundPosition = "0 -50px";
			}
			else
			{
				hideArea ( navNodes[i].id + "box" );
				
				var isActive = false;
				
				if ( navIds[i] == document.body.id )
				{
					navNodes[i].firstChild.style.backgroundPosition = "0 -100px";
				}
				else
				{
					navNodes[i].firstChild.style.backgroundPosition = "0 0";
				}
			}
		}
	}
	
	setTimeout( function(){ checkCloseMenu() }, MENU_TIME_LAG );
}

function checkCloseMenu()
{
	if ( !openMenuId )
	{
		openedMenuId = null;
	}
}

function showMenuAni( node )
{
	if ( openedMenuId != node.id )
	{
		clearTimeout( menuTimer );
		node.style.clip = "rect( 0 " + node.offsetWidth + "px 0 0 )";
		menuH = 0;
		showMenuAniHandler( node );
		openedMenuId = node.id;
	}
}

function showMenuAniHandler( node )
{
	if( menuTimer ) clearTimeout( menuTimer );
	
	menuH += ( node.offsetHeight - menuH ) * 0.4;
	
	if((((node.offsetHeight - menuH) > 0) ? (node.offsetHeight - menuH) : (menuH - node.offsetHeight)) < 2)
	{
		menuH = node.offsetHeight;
		clearTimeout( menuTimer );
	}
	else
	{
		menuTimer = setTimeout( function(){ showMenuAniHandler( node ) }, 10);
	}
	
	node.style.clip = "rect(0 " + node.offsetWidth + "px " + menuH + "px 0)";
}

/**************************************************
 *
 *	FontSizeの処理
 *
**************************************************/
function handleFontSize()
{
	fontSizeNode		= document.getElementById( 'fontsize' );
	
	if( fontSizeNode )
	{
		//
		// ボタンの表示 (デフォルトで消えているので)
		//
		fontSizeNode.parentNode.style.display = "block";
		
		addFunctionToElement( fontSizeNode, "onclick", function(){ changeFontSize(); applyFontSize(); return false; } );
		applyFontSize();
	}
}

function getFontSize()
{
	var myFontSize = getCookie( COOKIE_FONT );
	
	if ( !myFontSize )
	{
		myFontSize = FONT_M;
	}
	return myFontSize;
}

function changeFontSize()
{
	fontSize = getFontSize();
	
	if ( fontSize == FONT_M )
	{
		fontSize = FONT_L;
	}
	else
	{
		fontSize = FONT_M;
	}
	
	setCookie( COOKIE_FONT, fontSize, COOKIE_EXPIRE, '/' );
}

function applyFontSize()
{
	fontSize = getFontSize();
	
	if ( fontSize == FONT_M )
	{
		document.body.style.fontSize				= '80%';
		fontSizeNode.style.backgroundPosition		= '0 0';
		addFunctionToElement( fontSizeNode, "onmouseover", function(){ this.style.backgroundPosition = '0 -18px'; return false; } );
		addFunctionToElement( fontSizeNode, "onmouseout", function(){ this.style.backgroundPosition = '0 0'; return false; } );
	}
	else
	{
		document.body.style.fontSize = '100%';
		fontSizeNode.style.backgroundPosition		= '0 -36px';
		addFunctionToElement( fontSizeNode, "onmouseover", function(){ this.style.backgroundPosition = '0 -54px'; return false; } );
		addFunctionToElement( fontSizeNode, "onmouseout", function(){ this.style.backgroundPosition = '0 -36px'; return false; } );
	}
}

/**************************************************
 *
 *	Printの処理
 *
**************************************************/
function handlePrint()
{
	printNode = document.getElementById( 'print' );
	
	if( printNode )
	{
		//
		// ボタンの表示 (デフォルトで消えているので)
		//
		printNode.parentNode.style.display = "block";
		
		//
		// 「印刷する」場合
		//
		if ( window.name == PRINT_WINDOW_NAME )
		{
			//
			// 印刷画面用CSS
			//
			var styleNodes = document.getElementsByTagName('link');
			var cssNode = document.createElement( 'link' );
			
			cssNode.setAttribute( 'rel',	'stylesheet' );
			cssNode.setAttribute( 'type',	'text/css' );
			cssNode.setAttribute( 'href',	ROOT_PATH + 'css/printer.css' );
			cssNode.setAttribute( 'media',	'all' );
			
			insertAfter( cssNode, styleNodes[ styleNodes.length -1 ] );
			
			//
			// ヘッダ
			//
			var mainNode		= document.getElementById( 'main' );
			var headerNode		= getPrintHeaderNode();
			var logoNode		= getLogoNode();
			
			//
			// 全文検索
			//
			if ( !mainNode )
			{
				mainNode		= document.getElementById( 'resultarea' );
			}
			
			mainNode.parentNode.insertBefore( headerNode, mainNode );
			mainNode.parentNode.insertBefore( logoNode, mainNode );
		}
		//
		// 「印刷用表示」の場合
		//
		else
		{
			addFunctionToElement( printNode, "onclick", function(){ var w = window.open( document.URL, PRINT_WINDOW_NAME, 'width=820,height=600,menubar=yes,toolbar=no,scrollbars=yes,status=yes'); w.focus(); return false; } );
		}
	}
}

function getPrintHeaderNode()
{
	var div = document.createElement('div');
	div.innerHTML = '<p id="close"><a href="javascript:void(window.close());">ウインドウを閉じる</a></p><p id="printout"><a href="javascript:void(window.print());">このページを印刷する</a></p>';
	div.id = 'printBox';
	
	return div;
}

function getLogoNode()
{
	var p = document.createElement('p');
	p.innerHTML = 'オハヨー乳業株式会社';
	p.id = 'logop';
	
	return p;
}

function insertAfter( newChild, refChild )
{
	var parent = refChild.parentNode;
	
	if ( parent.lastChild == refChild )
	{
		return parent.appendChild( newChild );
	}
	else
	{
		return parent.insertBefore( newChild, refChild.nextSibling );
	}
}

/**************************************************
 *
 *	バナーを配置する ※要jquery.js
 *
**************************************************/
function putSideBanners()
{
	var sideNode = document.getElementById( 'side' );
	
	if ( sideNode )
	{
		var jsonUrl;
		
		var data = OhayoObj.bannerCache;
		
		if ( data )
		{
			return _putSideBanners( data );
		}
		
		jsonUrl		= ROOT_PATH + 'js/banner.json';
    	
		if ( window.jQuery )
		{
			var onLoadedBannerHtml = function ( data )
			{
	            if ( !data )
	            {
					return;
				}
				
				OhayoObj.bannerCache = data;
				_putSideBanners( data );
	        }
	        
	        jQuery.getJSON( jsonUrl, onLoadedBannerHtml );
		}
	}
}

function _putSideBanners( htmlData )
{
	var htmls = htmlData[ 'bannerHtml' ];
	var sideNode = document.getElementById( 'side' );
	var divNode = document.createElement( 'div' );
	var html = '';
	
	for ( var i = 0, l = htmls.length; i < l; i++ )
	{
		html += htmls[i];
	}
	
	//
	// 相対パスの置換
	//
	html = html.replace( /(href|src)=("|')\//gi, "$1=$2" + ROOT_PATH );
	divNode.innerHTML = html;
	replaceAnchor( divNode );
	sideNode.appendChild( divNode );
}

/**************************************************
 *
 *	検索窓の表示・非表示
 *
**************************************************/
function replaceSearchOptionButton()
{
	searchOptBtnNode	= document.getElementById( 'searchopt' );
	
	if ( !searchOptBtnNode )
	{
		return;
	}
	
	searchOptBoxNode	= document.getElementById( 'optbox' );
	searchOptAreaNode	= searchOptBoxNode.parentNode;
	
	searchOptAreaNode.style.position = "relative";
	
	searchOptBoxNode.style.position = "absolute";
	searchOptBoxNode.overflow = "hidden";
	searchOptBoxNode.style.clip = "rect(0 " + searchOptBoxNode.offsetWidth + "px 0 0)";
	
	searchOptBtnNode.onclick = function()
	{
		if ( this.className == 'plus' )
		{
			this.className = 'minus';
			this.innerHTML = '検索オプションを閉じる';
			showArea( 'optbox' );
			showSearchOption();
		}
		else
		{
			this.className = 'plus';
			this.innerHTML = '検索オプションを開く';
		//	hideArea( 'optbox' );
			hideSearchOption();
		}
		return false;
	}
}

function showSearchOption()
{
	clearTimeout( searchOptTimer );
	searchOptionHandler( searchOptBoxNode.offsetHeight );
}

function hideSearchOption()
{
	clearTimeout( searchOptTimer );
	searchOptionHandler( 0 );
}

function searchOptionHandler( toH )
{
	if( searchOptTimer ) clearTimeout( searchOptTimer );
	
	searchOptH += (toH - searchOptH) * 0.4;
	
	if((((toH - searchOptH) > 0) ? (toH - searchOptH) : (searchOptH - toH)) < 2)
	{
		searchOptH = toH;
		clearTimeout( searchOptTimer );
	}
	else
	{
		searchOptTimer = setTimeout( function(){ searchOptionHandler( toH ) }, 10);
	}
	
	searchOptBoxNode.style.clip = "rect(0 " + searchOptBoxNode.offsetWidth + "px " + searchOptH + "px 0)";
	searchOptAreaNode.style.height = searchOptH + "px";
}

/**************************************************
 *
 *	採用情報を別窓で開かせる
 *
**************************************************/
function replaceRecruitNav()
{
	var navNode = document.getElementById( 'nav05' );
	
	if ( navNode )
	{
		var anchorNode = navNode.childNodes[0];
		anchorNode.className = "ext";
		replaceAnchor( navNode );
	}
}


addOnloadEvent( initMain );

