/**
 * メイン処理。
 */
$(function(){
	
	// 文字サイズを変更して、メニューも表示する。
	setFontSize($.cookie('font-size'));
	createFontSizeMenu();
	
	// サブメニュー下部の追加情報を表示する。
	//createSub();
	
	// スムーススクロールを設定する。
	//$('a[href*="#"]').smoothAnchor();
	
	// 画像ホバーのハイライトを設定する。
	setHighLight();
	
	// 画像の遅延読み込みを設定する。
	/*
	if (!$.os.name.match(/^ip/)) {
		$('img[class!="nolazy"]').lazyload({placeholder:getRelativePath() + 'common/img/spacer.gif', effect:'fadeIn'});
	}
	*/
	
	// IE6専用の処理を実行する。
	forIE6();
});

/**
 * サブメニュー下部の追加情報を表示する。
 */
function createSub()
{
	if ($('div#contents_outer').attr('class') != 'home'){
		var relPath = getRelativePath();
		$.get(relPath + 'sub.tpl', function(data){
			// ドキュメントルートへの相対パスを置換する。
			data = data.replace(/!ROOT!/gi, relPath);
			$('div#sub_menu').after($(data));
			setHighLight('div.sub');
		});
	}
}

/**
 * IE6専用の設定。
 */
function forIE6()
{
	if ($.browser.msie && (parseFloat($.browser.version) <= 6.0)) {
		// キャッシュを有効にする。
		try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}
		// 透過PNGを有効にする。
		DD_belatedPNG.fix('.png');
	}
}

/**
 * 現在のページのドキュメントルートからの相対パスを取得する。
 */
function getRelativePath()
{
	var relPath = '';
	// URLがローカル(file://)の場合に機能しない。
	// for (var i = 0; i < location.pathname.split('/').length - 3; i++) relPath += '../';
	// ローカルルールでショートカットアイコンのパスから取得する。
	relPath = $('link[rel="shortcut icon"]').attr('href').replace('favicon.ico','');
	return relPath;
}

/**
 * highlightクラスが設定された要素内のimgに、マウスオーバー時のハイライトを設定する。
 */
function setHighLight(context)
{
	context = context || '';
	target = context + ' .highlight img';
	$(target.trim()).each(function(){
		var el = $(this).parent();
		$(el).css('background-color','#fff').hover(
			function(){ $(el).find('img').fadeTo('fast', 0.75); },
			function(){ $(el).find('img').fadeTo('fast', 1.0); }
		);
	});
}

/**
 * bodyタグ(ページ全体を意味する)の文字サイズを設定し、Cookieに保存する。
 *
 * @param size String: CSSのfont-sizeに指定する文字列。
 */
function setFontSize(size)
{
	size = size || '1.2em';
	$('body').css('font-size', size);
	$.cookie('font-size', size, {expires:365,path:'/'});
	return false;
};

/**
 * ヘッダーにフォントサイズのメニューを追加する。
 */
function createFontSizeMenu()
{
	var ul = $('<ul class="header_font_size"></ul>');
	ul.append($('<li id="font_size"><span class="reader">フォントサイズ</span></li>'));
	var a = $('<a href="#TOP"><span class="reader"></span></a>').css('cursor','pointer');
	ul.append($('<li class="font_size_zoom btn"></li>').append(a.clone().find('span').text('拡大').end().clickpress(function(){ return setFontSize('1.5em'); })));
	ul.append($('<li class="font_size_normal btn"></li>').append(a.clone().find('span').text('標準').end().clickpress(function(){ return setFontSize('1.2em'); })));
	$('#header').append(ul);
}

