/*********************************************************************

  グローバル変数定義

*********************************************************************/

var sendFlag = false;
var editFlag = false;

/*********************************************************************

  入力内容チェック

*********************************************************************/

function checkForm(form)
{
	
	if (form.subj && !form.subj.value) {
		alert('The title is not input');
		return false;
	}
	if (form.text && !form.text.value) {
		alert('The text is not input');
		return false;
	}

	if (sendFlag == true) {
		alert('Duplicate publication is a prohibition');
		return false;
	} else {
		sendFlag = true;
	}

	return true;
}

/*********************************************************************

  ファイル挿入

*********************************************************************/

function insertFile(form)
{
	var text = form.text;
	var file = form.file;

	if (file) {
		file.onchange = function()
		{
			if (file.value) {
				text.focus();

				if (document.selection) {
					document.selection.createRange().text = file.value;
				} else if (text.selectionEnd - text.selectionStart == 0) {
					var before = text.value.substring(0, text.selectionStart);
					var after  = text.value.substring(text.selectionEnd, text.value.length);

					text.value = before + file.value + after;
				} else {
					text.value += file.value;
				}

				text.focus();
			}

			form.file.selectedIndex = 0;
		};
	}

	return;
}

/*********************************************************************

  Cookie取得

*********************************************************************/

function getCookie(form)
{
	if (!document.cookie) {
		return;
	}

	var cookies = document.cookie.split('; ');
	var cookie  = new Array();

	for (var i = 0; i < cookies.length; i++){
		var pair = cookies[i].split('=');
		cookie[pair[0]] = unescape(decodeURI(pair[1]));
	}

	if (form.name && cookie['comment[name]']) {
		form.name.value = cookie['comment[name]'];
	}
	if (form.mail && cookie['comment[mail]']) {
		form.mail.value = cookie['comment[mail]'];
	}
	if (form.url && cookie['comment[url]']) {
		form.url.value = cookie['comment[url]'];
	}
	if (form.name && cookie['comment[name]']) {
		form.save.checked = true;
	}

	return;
}

/*********************************************************************

  処理開始

*********************************************************************/

window.onload = function()
{
	var nodeArticleForm = document.getElementById('article_form');
	var nodeCommentForm = document.getElementById('comment_form');
	var nodeLoginForm   = document.getElementById('login_form');

	if (nodeArticleForm) {
		//入力内容チェック
		nodeArticleForm.onsubmit = function()
		{
			return checkForm(nodeArticleForm);
		};
		nodeArticleForm.text.onkeyup = function()
		{
			if (!editFlag) {
				editFlag = true;
			}
			return;
		};

		//ファイル挿入
		insertFile(nodeArticleForm);
	}

	if (nodeCommentForm) {
		//Cookie取得
		getCookie(nodeCommentForm);
	}

	if (nodeLoginForm) {
		//フォーカス設定
		nodeLoginForm.pwd.focus();
	}

	return;
};
window.onbeforeunload = function()
{
	if (!sendFlag && editFlag) {
		return 'ページを移動した場合、編集中のテキストは破棄されます。';
	}

	return;
};

$(function(){

/*********************************************************************

  フローティングメニューの処理

*********************************************************************/

$('#sidebar').scrollFollow({
	speed: 500,
	offset: 30
});

/*********************************************************************

  フェードの処理

*********************************************************************/

$(".fade").load(function(){
	$(this).fadeTo(0, 0.0);
});

$(".fade").hover(
				 
function(){
	$(this).fadeTo(150, 1.0);
},
function(){
	$(this).fadeTo(150, 0.0);
}

);

});

/*********************************************************************

  初期化処理

*********************************************************************/

$(document).ready( function () {
							 
	$("p").css("font-size",$.cookie('fsize'));
	$("p").css("line-height",$.cookie('linehight'));
	
	ml=$("#main_left").height();
	mr=$("#main_right").height();
	if(ml>mr)$("#main_right").css("height", ml);
	if(mr>ml)$("#main_left").css("height", mr);

});

/*********************************************************************

  クッキー読み込み

*********************************************************************/

function font(size,lineh){
$("p").css("font-size",size);
$("p").css("line-height",lineh);
$.cookie("fsize",size,{expires:30,path:'/'});
$.cookie("linehight",lineh,{expires:30,path:'/'});
}

/*********************************************************************

  メニューリンク先の表示

*********************************************************************/

$(function () {
	$('.bubbleInfo').each(function () {
	   var distance = 20;
	   var time = 250;
	   var hideDelay = 250;
	   var hideDelayTimer = null;
	   var beingShown = false;
	   var shown = false;
	   var trigger = $('.trigger', this);
	   var info = $('.popup', this).css('opacity', 0);
	   $([trigger.get(0), info.get(0)]).mouseover(function () {
		   if (hideDelayTimer) clearTimeout(hideDelayTimer);
		   if (beingShown || shown) {
			   // don't trigger the animation again
			   return;
		   } else {
			   // reset position of info box
			   beingShown = true;
			   info.css({
				   top: 0,
				   left: 0,
				   display: 'block'
			   }).animate({
				   top: '-=' + distance + 'px',
				   opacity: 0.8
			   }, time, 'swing', function() {
				   beingShown = false;
				   shown = true;
			   });
		   }
		   return false;
	   }).mouseout(function () {
		   if (hideDelayTimer) clearTimeout(hideDelayTimer);
		   hideDelayTimer = setTimeout(function () {
			   hideDelayTimer = null;
			   info.animate({
				   top: '-=' + distance + 'px',
				   opacity: 0
			   }, time, 'swing', function () {
				   shown = false;
				   info.css('display', 'none');
			   });
		   }, hideDelay);
		   return false;
	   });
	});
});
