/** ドキュメント読み込み後の挙動を定めます。*/
$(document).ready(function()
{
	// ヘッダーメニューの選択状態を定めます。
	setHeaderMenuSelected();

	// アーティスト検索ボタンがクリックされた時の挙動を定めます。
	$("#searchArtistsButton").click(function(event)
	{
		if ($("#textfield").val() != '')
			window.location.href = passedURL + '/searches/artists/' + m13eURLEncode($("#textfield").val());
	});

	// 曲名検索ボタンがクリックされた時の挙動を定めます。
	$("#searchTracksButton").click(function(event){
		if ($("#textfield").val() != '')
			window.location.href = passedURL + '/searches/tracks/' + m13eURLEncode($("#textfield").val());
	});

	// アルバム検索ボタンがクリックされた時の挙動を定めます。
	$("#searchAlbumsButton").click(function(event){
		if ($("#textfield").val() != '')
			window.location.href = passedURL + '/searches/albums/' + m13eURLEncode($("#textfield").val());
	});

	// キーワード検索ボタンがクリックされた時の挙動を定めます。
	$("#searchKeywordsButton").click(function(event){
		if ($("#textfield").val() != '')
			window.location.href = passedURL + '/searches/keywords/' + m13eURLEncode($("#textfield").val());
	});
});

/** ヘッダーメニューの選択状態を定めます。 */
function setHeaderMenuSelected()
{
	if (controller == 'news')
		$("#news a img").addClass("selected");
		
	if (controller == 'artists')
		$("#artist a img").addClass("selected");
		
	if (controller == 'specials')
		$("#special a img").addClass("selected");
		
	if (controller == 'movies')
		$("#movie a img").addClass("selected");
}

/** 与えられた文字列を URL エンコードします。*/
function m13eURLEncode(s)
{
	return encodeURIComponent(s);
}

/** イベントの伝播を終了します。*/
function stopEvent(event)
{
	// イベントの伝播を終了します。
	event.cancelBubble = true;
	if (event.stopPropagation)
		event.stopPropagation();

	return false;
}

/**
 * 任意のブロック要素を基準として、与えられたブロック要素の位置を揃えます。
 * jQuery 1.4.3 が必要です。
 * 
 * @param base
 * 	基準とするブロック要素です。
 * @param targetArray
 * 	位置を揃えたいブロック要素を配列で与えます。
 * @param direction
 * 	表示を揃える位置を指定します。指定できるのは下記の通りです。
 * 	t  - 上
 * 	b  - 下
 * 	l  - 左
 * 	r  - 右
 * 	lt, tl - 左上
 * 	lb, bl - 左下
 * 	rt, tr - 右上
 * 	rb, rb - 右下
 * @param offset
 * 	揃え位置からのオフセット量を指定します。これは下記のようなオブジェクトである必要があります。
 * 	{h: [number], v: [number]}
 */
function alignBlocks(base, targetArray, direction, offset)
{
	if (!base || !targetArray || !direction)
		return;

	var baseOffset = base.offset(),
		baseWidth = base.width(),
		baseHeight = base.height(),
		baseTop = baseOffset.top,
		baseLeft = baseOffset.left,
		baseBottom = baseTop + baseHeight;
		baseRight = baseLeft + baseWidth;

	var index, length = targetArray.length, fixedTop, fixedLeft;

	for (index = 0; index < length; index++)
	{
		if (targetArray[index])
		{
			var target = targetArray[index],
				targetOffset = target.offset(),
				targetWidth = target.width(),
				targetHeight = target.height(),
				targetTop = targetOffset.top,
				targetLeft = targetOffset.left,
				targetBottom = targetTop + targetHeight;
				targetRight = targetLeft + targetWidth;

			switch(direction)
			{
				case 't':
					fixedTop = baseTop + offset.v;
					target.css({top: fixedTop});
					break;
				case 'r':
					fixedLeft = targetLeft + (baseRight - targetRight) + offset.h;
					target.css({left: fixedLeft});
					break;
				case 'b':
					fixedTop = targetTop + (baseBottom - targetBottom) + offset.v;
					target.css({top: fixedTop});
					break;
				case 'l':
					fixedLeft = baseLeft + offset.h;
					target.css({left: fixedLeft});
					break;
				case 'lt':
				case 'tl':
					fixedTop = baseTop + offset.v;
					fixedLeft = baseLeft + offset.h;
					target.css({top: fixedTop, left: fixedLeft});
					break;
				case 'lb':
				case 'bl':
					fixedTop = targetTop + (baseBottom - targetBottom) + offset.v;
					fixedLeft = baseLeft + offset.h;
					target.css({top: fixedTop, left: fixedLeft});
					break;
				case 'rt':
				case 'tr':
					fixedTop = baseTop + offset.v;
					fixedLeft = targetLeft + (baseRight - targetRight) + offset.h;
					target.css({top: fixedTop, left: fixedLeft});
					break;
				case 'rb':
				case 'br':
					fixedTop = targetTop + (baseBottom - targetBottom) + offset.v;
					fixedLeft = targetLeft + (baseRight - targetRight);
					target.css({top: fixedTop, left: fixedLeft});
					break;
			}
		}
	}
}

