
// ----------------------------------------------------------------------------
// DEFINES
// ----------------------------------------------------------------------------

var cookie;
var title;

// ----------------------------------------------------------------------------
// FUNCTIONS
// ----------------------------------------------------------------------------

// Cookie生成関数
function createCookie( cookie_name, value, days )
{
	var date;
	var expires = "";
	
	if( days )
	{
		date = new Date();

		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );

		expires = "; expires=" + date.toGMTString();
	}
  
	document.cookie = cookie_name + "=" + value + expires + "; path=/";
}

// Cookie取得関数
function readCookie( cookie_name )
{
	var i, cookie_obj, cookie_array, str;

	str = cookie_name + "=";
	cookie_array = document.cookie.split( ';' );

	for( i = 0; i < cookie_array.length; i++ )
	{
		cookie_obj = cookie_array[ i ];
		
		while( cookie_obj.charAt( 0 ) == ' ' )
		{
			cookie_obj = cookie_obj.substring( 1, cookie_obj.length );
		}
		
		if( cookie_obj.indexOf( str ) == 0 )
		{
			return cookie_obj.substring( str.length, cookie_obj.length );
		}
	}

	return null;
}

function setActiveStyleSheet( title )
{
	var i, obj, link_obj;
	
	// リンクタグを全取得
	obj = document.getElementsByTagName( "link" );

	for( i = 0; obj[ i ]; i++ )
	{
		link_obj = obj[ i ];
	  
		if( link_obj.getAttribute( "rel" ).indexOf( "style" ) != -1 && link_obj.getAttribute( "title" ) )
		{
			link_obj.disabled = true;
	  
			if( link_obj.getAttribute( "title" ) == title )
			{
				link_obj.disabled = false;
			}
		}
	}
}

function getActiveStyleSheet()
{
	var i, obj, link_obj;

	obj = document.getElementsByTagName( "link" );

	for( i = 0; obj[ i ]; i++ )
	{
		link_obj = obj[ i ];
		
		if( link_obj.getAttribute( "rel" ).indexOf( "style" ) != -1 && 
			link_obj.getAttribute( "title" ) && !link_obj.disabled )
		{
			return link_obj.getAttribute( "title" );
		}
	}
	
	return null;
}

function getPreferredStyleSheet()
{
	var i, obj, link_obj;

	obj = document.getElementsByTagName( "link" );

	for( i = 0; obj[ i ]; i++ )
	{
		link_obj = obj[ i ];
		
	if( link_obj.getAttribute( "rel" ).indexOf( "style" ) != -1 && 
		link_obj.getAttribute( "rel" ).indexOf( "alt" ) == -1 && link_obj.getAttribute("title") )
	{
		return link_obj.getAttribute( "title" );
	}
  }
  return null;
}

function loadEvent()
{
	var cookie;
	var title;
  
	cookie = readCookie( "style" );
  
	if( cookie )
	{
		title = cookie;
	}
	else
	{
		title = getPreferredStyleSheet();	  
	}
  
	setActiveStyleSheet( title );
}

function unLoadEvent()
{
	var title;

	title = getActiveStyleSheet();
	
	createCookie( "style", title, 365 );
}

// ----------------------------------------------------------------------------
// PROCESSING
// ----------------------------------------------------------------------------

window.onload = loadEvent;
window.onunload = unLoadEvent;

cookie = readCookie( "style" );

if( cookie )
{
	title = cookie;
}
else
{
	title = getPreferredStyleSheet();
}

setActiveStyleSheet( title );
