// JavaScript Document

var rootHost = 'www.plan-japan.org/';
var rootPath = location.href.substr(0, location.href.indexOf(rootHost)) + rootHost;

var headlineWidth = 0;
var headlineSize = 590;
var headlineSpeed = 3;
var headlineInterval = 50;
var topFlashDuration = 3000;
var topFlashInterval = 10000;
var topFlashIndex = 0;
var topFlashZ = 1;

//-----------------------------------------------------------

var menu_a = [];
var menu_b = [];
var menu_s = [];

//-----------------------------------------------------------

$(document).ready(function() {
	$('img.hover').hover(function() {
		this._src = this.src;
	}, function() {
		this.src = this._src;
	});
	
	// menu a/b のマウスオーバー画像の初期化
	$('#menu_a//img,#menu_b//img').each(function() {
		this._src = this.src;
	});

  // menu a/b のマウスオーバー画像の切替処理
	$('#menu_a//img').hover(function() {
		if (!/_on\.gif$/.test(this.src)) {
			this.src = this.src.replace(/\.gif$/, '_on.gif');
		}
	}, function() {
		this.src = this._src;
	});

  // menu a/b のマウスオーバー画像の切替処理
	$('#menu_b//img').hover(function() {
		if (!/_over\.gif$/.test(this.src)) {
			this.src = this.src.replace(/\.gif$/, '_over.gif');
		}
	}, function() {
		this.src = this._src;
	});

	// menu b のプルダウンメニューの表示処理
	$('#menu_b//td').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	// menu b のプルダウンメニューの項目のマウスオーバー処理
	$('#menu_b//li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	// top_navi のイメージ画像のマウスオーバー処理
	$('#top_navi//a/img').hover(function() {
		this._src = this.src;
		if (!/_on\.gif$/.test(this.src)) {
			this.src = this.src.replace(/\.gif$/, '_over.gif');
		}
	}, function() {
		this.src = this._src;
	});

	// h3
	$('h3:not([@class])');
	
	// hr
	$('hr.blue').wrap('<div class="hr hr_blue"></div>');
	$('hr.dotted').wrap('<div class="hr hr_dotted"></div>');
	
	// sidemenu
	$('#menu_s//ul/li/a').each(function() {
		if(this.href == location.href) {
			$(this).addClass('selected');
		}
	});
	
	// sidemenu sub
	$('#menu_s//ul/li/ul/li/a').each(function() {
		if(this.href == location.href) {
			$(this).addClass('selected');
//			document.title = this.href;
		}
	});

	// 3階層目のページを表示しているときに2階層目のメニューを強調表示する処理　活動報告＞緊急支援報告、プラン・ライブラリ＞プラン紹介資料
	$('body').append('<img style="display: none;" id="rel_index" src="' + $('link[@rel="index"]').attr('href') + '"/>');
	var href = $('#rel_index').attr('src');
	//document.title = href;
	$('#sidemenu//ul/li/a').each(function() {
		if(-1 != this.href.indexOf(href) || this.href == href) {
			$(this).addClass('selected');
		}
	});


	// 4階層目のページを表示しているときに3階層目のメニューを強調表示する処理
	$('body').append('<img style="display: none;" id="rel_index" src="' + $('link[@rel="index"]').attr('href') + '"/>');
	var href = $('#rel_index').attr('src');
	//document.title = href;
	$('#sidemenu//ul/li/ul/li/a').each(function() {
		if(-1 != this.href.indexOf(href) || this.href == href) {
			$(this).addClass('selected');
		}
	});
	
	//.addClass('selected');
	
	// headline のスクロールアニメーション処理
	var j = $('#headline_inner');
	if (j) {
		headlineWidth = j.width();
		$('#headline_outer').addClass('headline_outer2');
		j.addClass('headline_inner2');
		j.css('left', headlineSize);
		setInterval(headlineAnimation, headlineInterval);
	}


	// top flash
	topFlashAnimation();
	
	// サイドメニューでの「チャイルドへの手紙」ボタンの表示・非表示処理
	if (/\/home\/supporter\/sp_/.test(location.href)) {
		//$('#sidemenu_button_tegami').show();
	}

	//
	$('a[@href^=#]').click(function() {
		var anchor = '#' + this.href.replace(/^.*?\#/, '');
		$(anchor).ScrollTo(500);
		return false;
	});
	
	// Aタグで .gif に対してリンクしているものは幅高指定のウィンドウで開く
	$('a[@href$=.gif]').click(function() {
		window.open(this.href, '_blank', 'width=1000,height=700,location=yes,resizable=yes');
		return false;
	});
});
	//
//-----------------------------------------------------------

function topFlashAnimation()
{
	if (0 == $('#top_flash').length) {
		return false;
	}
	
	var collection = $('#top_flash/div');
	
	if (collection.length <= topFlashIndex) {
		topFlashIndex = 0;
	}
	
	var element = collection.get(topFlashIndex++);
	$(element).css('z-index', topFlashZ++);
	$(element).fadeIn(topFlashDuration);
	setTimeout(topFlashAnimation, topFlashInterval);
}

//-----------------------------------------------------------

function headlineAnimation()
{
	var j = $('#headline_inner');
	var x = parseInt(j.css('left'));
	
	x-=headlineSpeed;
	
	if (x < -headlineWidth) {
		x = headlineSize;
	}
	
	j.css('left', x + 'px');
}

//-----------------------------------------------------------

function draw(html)
{
	html = html.replace(/href="\//ig, 'href="' + rootPath);
	html = html.replace(/src="\//ig, 'src="' + rootPath);
	
	document.write(html);
}


//----------------　文字サイズ変更SCRIPT　----------------------------------------------------------
function setActiveStyleSheet(title) {

	if(title == "small"){
		document.getElementById("size_big").src = "../../images/header/big_chg.gif";
		document.getElementById("size_normal").src = "../../images/header/normal_chg.gif";
		document.getElementById("size_small").src = "../../images/header/small_chg2.gif";
		$.cookie("style","small",{expires:30,path:'/'});
	}else if(title == "normal"){
		document.getElementById("size_big").src = "../../images/header/big_chg.gif";
		document.getElementById("size_normal").src = "../../images/header/normal_chg2.gif";
		document.getElementById("size_small").src = "../../images/header/small_chg.gif";
		$.cookie("style","normal",{expires:30,path:'/'});
	}else if(title == "big"){
		document.getElementById("size_big").src = "../../images/header/big_chg2.gif";
		document.getElementById("size_normal").src = "../../images/header/normal_chg.gif";
		document.getElementById("size_small").src = "../../images/header/small_chg.gif";
		$.cookie("style","big",{expires:30,path:'/'});
	}else{
		document.getElementById("size_big").src = "../../images/header/big_chg.gif";
		document.getElementById("size_normal").src = "../../images/header/normal_chg.gif";
		document.getElementById("size_small").src = "../../images/header/small_chg2.gif";
	}	


  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  if(title != null){
	  setActiveStyleSheet(title);
  }else{
	  setActiveStyleSheet('small');
  }
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

//var cookie = readCookie("style");
//var title = cookie ? cookie : getPreferredStyleSheet();
//setActiveStyleSheet(title);
//----------------　文字サイズ変更SCRIPT END　------------------------------------------------------
//--------------------------------------------------------------------------------------------------