$(document).ready(function(){
	$("#headerContainer").load("/cn/header.html");
	$("#footerContainer").load("/cn/footer.html");
	
	// 3言語リンク設定
	var re;
	var jp;
	var cn;
	var en;
	
	re = new RegExp(location.protocol + "//" + location.host, "i");
	cn = document.URL.replace(re, "");
	
	re = new RegExp("^" + cn + "+$", "i");
	if ("/cn/".match(re)) {
		jp = cn.replace("/cn/", "/index.html");
	} else {
		jp = cn.replace("/cn", "");
	}
	en = cn.replace("/cn", "/en");
	
	$("div.changeText a:eq(3)").attr("href", jp);
	$("div.changeText a:eq(4)").attr("href", en);

	// ローカルナビ
	localNaviInit();
	
	// 右クリック禁止
	$("img").bind("contextmenu",function(event){
	return false;
	});
});

// ローカルナビ
var scrollTimer;
function localNaviInit() {
	var localNavi = $('.mainContents > .subNaviContainer');
	if ( localNavi.length > 0 ) {
		localNavi.css('position', 'relative');
		var positionTop = localNavi.position().top;
		var maxPosition;
		maxPosition = (localNavi.height() > $('.mainContents > .contents').height()) ? 0 : $('.mainContents > .contents').height() - localNavi.height();
		$(window).scroll(function(){
			clearTimeout(scrollTimer);
			var targetPosition =  (document.body.scrollTop  || document.documentElement.scrollTop) - positionTop;
			if ( targetPosition > maxPosition ) targetPosition = maxPosition;
			if ( targetPosition < 0 ) targetPosition = 0;
			scrollTimer = setTimeout(function(){localNavi.animate({top:targetPosition+'px'}, 'fast', 'swing');}, 100);
		});
	}
}

(function($)
{
	$(document).ready(function() {
		$('.styleswitch').click(function()
		{
			switchStylestyle(this.getAttribute("rel"));
			return false;
		});
		var c = readCookie('style');
		if (c) switchStylestyle(c);
	});

	function switchStylestyle(styleName)
	{
		$('link[@rel*=style][title]').each(function(i) 
		{
			imgObj = $('.changeText .size a[rel='+this.getAttribute('title')+'] img');
			imgObj.attr('src','/cn/common/images/bt_size_'+this.getAttribute('title')+'.gif');
			this.disabled = true;
			if (this.getAttribute('title') == styleName){
				this.disabled = false;
				imgObj.attr('src','/cn/common/images/bt_size_'+this.getAttribute('title')+'_c.gif');
			}
		});
		createCookie('style', styleName, 365);
	}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
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 var 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;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
// /cookie functions

// new window
function newWin(url, target, wW, wH, attr) {
	var toolbar = attr.charAt(0);
	var location = attr.charAt(1);
	var directories = attr.charAt(2);
	var status = attr.charAt(3);
	var menubar = attr.charAt(4);
	var resizable = attr.charAt(5);
	var scrollbars = (attr.charAt(6) == '1') ? 'yes':'0';
	var win_attr = 'toolbar='+toolbar+',location='+location+',directories='+directories+',status='+status+',menubar='+menubar+',resizable='+resizable+',scrollbars='+scrollbars+',width='+wW+',height='+wH;
	newWindow=open(url, target, win_attr);
}
