﻿// Copyright(c) BABA Minoru - http://www.crystal-creation.com/

// heading list
( function()
{
	var listBox = document.getElementById( 'heading-list' );
	if( listBox )
	{
		var headers = document.getElementsByTagName( 'h2' );
		if( 0 < headers.length )
		{
			var list = document.createElement( 'ul' );
			for( var i = 0; i < headers.length; i++ )
			{
				var header = headers[ i ];
				if( header.parentNode.className == 'reference'
				 || header.parentNode.id == 'related-page' )
				{
					continue;
				}


				var anchorName = '';
				var anchorText = '';

				if( header.firstChild && header.firstChild.tagName == 'A' )
				{
					if( header.firstChild.id )
					{
						anchorName = header.firstChild.id;
					}
					else // example... <a href="">Foo</a>
					{
						anchorName = 'contents' + ( i + 1 );
						header.firstChild.id = anchorName;
					}

					anchorText = header.firstChild.innerHTML;
				}
				else
				{
					anchorName = 'contents' + ( i + 1 );
					anchorText = header.innerHTML;

					//
					var anchor = document.createElement( 'a' );
					anchor.id = anchorName;
					anchor.innerHTML = header.innerHTML;

					header.innerHTML = '';
					header.appendChild( anchor );
				}

				var link = document.createElement( 'a' );
				link.href = '#' + anchorName;
				link.innerHTML = anchorText;

				var listItem = document.createElement( 'li' );
				listItem.appendChild( link );

				list.appendChild( listItem );
			}

			var captionBox = document.createElement( 'div' );
			captionBox.appendChild( document.createTextNode( 'このページの内容' ) );

			listBox.appendChild( captionBox );
			listBox.appendChild( list );
		}
	}
} )();

// highlight current anchor
( function()
{
	var currentPath = document.location.pathname
	currentPath = currentPath.replace( /index\.(htm|html|php)$/, '' );

	HighlightExactMatch( 'g-side-navigation' );
	HighlightBroadMatch( 'g-navigation' );
	HighlightBroadMatch( 'g-footer' );

	
	function HighlightExactMatch(id )
	{
		var box = document.getElementById( id );
		if( box )
		{
			var links = box.getElementsByTagName( 'a' );
			for( var i = 0; i < links.length; i++ )
			{
				var pathname = links[ i ].pathname;
				if( pathname.charAt( 0 ) != '/' )
				{
					pathname = '/' + pathname; // for IE
				}

				if( currentPath == pathname )
				{
					links[ i ].style.backgroundColor = 'red';
					links[ i ].style.color = 'white';

					links[ i ].removeAttribute( 'href' );
				}
			}
		}
	}

	function HighlightBroadMatch( id )
	{
		var box = document.getElementById( id );
		if( box )
		{
			var links = box.getElementsByTagName( 'a' );
			for( var i = 0; i < links.length; i++ )
			{
				var pathname = links[ i ].pathname;
				if( pathname.charAt( 0 ) != '/' )
				{
					pathname = '/' + pathname; // for IE
				}

				if( currentPath.indexOf( pathname ) == 0 )
				{
					links[ i ].style.backgroundColor = 'red';
					links[ i ].style.color = 'white';
					
					if( currentPath == pathname )
					{
						links[ i ].removeAttribute( 'href' );
					}
				}
			}
		}
	}
} )();

// target pseudo-class
( function()
{
	if( /*@cc_on!@*/true )
	{
		// not IE
		return;
	}

	var className = ' target';
	if( document.location.hash )
	{
		var hash = document.location.hash.substring( 1 );
		var element = document.getElementById( hash );
		if( element )
		{
			element.className += className;
		}
	}

	for( var i = 0; i < document.links.length; i++ )
	{
		if( document.links[ i ].hash )
		{
			document.links[ i ].attachEvent( 'onclick',
				function( event )
				{
					// Remove
					if( document.location.hash )
					{
						var hash = document.location.hash.substring( 1 );
						var element = document.getElementById( hash );
						if( element )
						{
							element.className = element.className.replace( className, '' );
						}
					}

					// Add
					var hash = event.srcElement.hash.substring( 1 );
					var element = document.getElementById( hash );
					if( element )
					{
						element.className += className;
					}
				} );
		}
	}
} )();

// attach event to menu
( function()
{
	var categoryLink = document.getElementById( 'g-navigation' );
	if( categoryLink )
	{
		var lists = categoryLink.getElementsByTagName( 'li' );

		for( var i = 0; i < lists.length; i++ )
		{
			if( lists[ i ].className == 'menu' )
			{
				var menuBoxes = lists[ i ].getElementsByTagName( 'div' )
				if( menuBoxes && menuBoxes.length != 0 )
				{
					lists[ i ].onmouseover = function()
					{
						var menuBoxes = this.getElementsByTagName( 'div' )
						menuBoxes[ 0 ].style.visibility = 'visible';
					}

					lists[ i ].onmouseout = function()
					{
						var menuBoxes = this.getElementsByTagName( 'div' )
						menuBoxes[ 0 ].style.visibility = 'hidden';
					}


					menuBoxes[ 0 ].onmouseout = function()
					{
						this.style.visibility = 'hidden';
					}
				}
			}
		}
	}
} )();

// custom site search
if( google )
{
	google.load( 'search', '1', { language : 'ja' } );
	google.setOnLoadCallback( function()
		{
			var customSearchControl = new google.search.CustomSearchControl( 'partner-pub-9236318560658283:6pqg6n4h1u9' );
			customSearchControl.setResultSetSize( google.search.Search.FILTERED_CSE_RESULTSET );

			var options = new google.search.DrawOptions();
			options.setAutoComplete( true );
			options.enableSearchboxOnly( 'http://' + window.location.host + '/search/' );

			customSearchControl.draw( 'g-site-search-form', options );
		},
		true );
}

// text selection
( function()
{
	// textarea
	var textarea = document.body.getElementsByTagName( 'textarea' );
	if( textarea )
	{
		for( var i = 0; i < textarea.length; i++ )
		{
			textarea[ i ].onfocus = SelectText;
		}
	}

	// textbox
	var input = document.body.getElementsByTagName( 'input' );
	if( input )
	{
		for( var i = 0; i < input.length; i++ )
		{
			if( input[ i ].type == 'text' )
			{
				input[ i ].onfocus = SelectText;
			}
		}
	}


	function SelectText()
	{
		var element = this;
		window.setTimeout( function() { element.select(); }, 0 );
	}
} )();
