/***************************************
file Name: common.js
URI: 
Description: DOM Script Packege
Version: 1.0
Author: T.D.W
Author URI: 
***************************************/

var uri=location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/";		// 本番用




/***************************************
	外部リンク・ポップアップ
***************************************/
function opwin(){
	if($(this).attr("class").indexOf("gotoTop")<0&&
		$(this).attr("class").indexOf("thickbox")<0&&
		$(this).parent().parent().attr("id").indexOf("global_navi")<0&&
		$(this).attr("class").indexOf("history_back")<0){
		var url=$(this).attr("href");
		if(url.indexOf(location.hostname)<0 || 
			url.indexOf(location.hostname+"/wp-content/uploads")>0){
				var winopen = window.open(url);
				winopen.focus();
				return false;
		}
	}
}


/***************************************
	ページ上部へ
***************************************/
function goToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
	if (x > 0 || y > 0) {
		window.setTimeout(new Function("goToTop();"), 24);
	}
}

function serchGoToTop(){
	if (!document.getElementsByTagName){
		return;
	} 
	var links = document.getElementsByTagName("a");
	for( var i=0; i<links.length; i++){
		if(links[i].getAttribute("class") ){
			if(links[i].getAttribute("class") == "gotoTop"){
					links[i].onclick = function(){
					goToTop();
					return false;
				}
			}
		}
		else if(links[i].getAttribute("className")){
			if(links[i].getAttribute("className") == "gotoTop"){
				links[i].setAttribute("onclick", new Function("goToTop(); return false;"));	
			}
		}
	}
}



/***************************************
	thickbox初期化
***************************************/
function tbox(){
	$(".entry a[href$='.jpg']").add(".entry a[href$='.gif']").add(".entry a[href$='.png']").add(".entry a[href$='.jpeg']").addClass("thickbox").attr("rel","images");
}

/***************************************
	listToSelect
***************************************/
function listToSelect(id){
	var elm = [];
	var title = $(id+" h3 img").attr("alt");

	$(id+" ul li a").each(
		function(i){
		elm[i]=$(this);
	});
	
	var ct='<form>'+"\n"+
'	<select onChange="location.href=value;">'+"\n"+
'		<option value="#">'+title+'</option>'+"\n";

	$(elm).each(
		function(i){
			ct += '<option value="'+
					$(elm[i]).attr("href")+
					'">'+
					$(elm[i]).parent().text()+
					'</option>'+"\n";
		}
	);
	ct +='</form>'+"\n";

	$(id+" ul").replaceWith(ct);

}



/***************************************
	glovalNaviImg
***************************************/
function glovalNaviImg(){
	var headlist=new Array(6);
	headlist[0]={"src": "h_home_n.png",		"width": 71,	"height": 45,	"class": "page-item-30" };
	headlist[1]={"src": "h_vip_n.png",		"width": 288,	"height": 45,	"class": "page-item-7" };
	headlist[2]={"src": "h_course_n.png",	"width": 88,	"height": 45,	"class": "page-item-9" };
	headlist[3]={"src": "h_schedule_n.png",	"width": 123,	"height": 45,	"class": "page-item-22" };
	headlist[4]={"src": "h_event_n.png",	"width": 150,	"height": 45,	"class": "page-item-20" };
	headlist[5]={"src": "h_contact_n.png",	"width": 102,	"height": 45,	"class": "page-item-37" };

	// グローバルナビ
	var elm = [];
	$("#global_navi ul li a").each(
		function(i){
		elm[i]=$(this);
		var alt= $(elm[i]).text();
		var src=headlist[i].src;
		if($(elm[i]).parent().attr("class").indexOf("current_page_item")>=0 ||
			$(elm[i]).parent().attr("class").indexOf("current_page_parent")>=0){
			src=src.replace("_n.png", "_act.png");
		}
		$(elm[i]).empty();
//		$(elm[i]).append($("<img>").attr("src",location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/images/common/"+src).attr("width",headlist[i].width).attr("height",headlist[i].height).attr("alt",alt));
		$(elm[i]).append($("<img>").attr("src",uri+"images/common/"+src).attr("width",headlist[i].width).attr("height",headlist[i].height).attr("alt",alt));
	});

	// ロールオーバー
	menuHover("#global_navi ul li a img","_on");

}

/***************************************
	courseNaviImg
***************************************/
function courseNaviImg(){
	var headlist=new Array(7);
	headlist[0]={"src": "submn01_nm.png",	"width": 200,	"height": 42,	"class": "page-item-9" };
	headlist[1]={"src": "submn02_nm.png",	"width": 200,	"height": 40,	"class": "page-item-11" };
	headlist[2]={"src": "submn03_nm.png",	"width": 200,	"height": 42,	"class": "page-item-14" };
	headlist[3]={"src": "submn04_nm.png",	"width": 200,	"height": 40,	"class": "page-item-37" };
	headlist[4]={"src": "submn05_nm.png",	"width": 200,	"height": 40,	"class": "page-item-39" };
	headlist[5]={"src": "submn06_nm.png",	"width": 200,	"height": 42,	"class": "page-item-43" };
	headlist[6]={"src": "submn07_nm.png",	"width": 200,	"height": 40,	"class": "page-item-41" };
	

	// コースサイドバー
	var elm = [];
	$("ul#side_course_menu li a").each(
		function(i){
		elm[i]=$(this);
		var alt= $(elm[i]).text();
		var src=headlist[i].src;
		if($(elm[i]).parent().attr("class").indexOf("current_page_item")>=0){
			src=src.replace("_nm.png", "_on.png");
		}
		$(elm[i]).empty();
//		$(elm[i]).append($("<img>").attr("src",location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/images/course/"+src).attr("width",headlist[i].width).attr("height",headlist[i].height).attr("alt",alt));
		$(elm[i]).append($("<img>").attr("src",uri+"images/course/"+src).attr("width",headlist[i].width).attr("height",headlist[i].height).attr("alt",alt));
	});

	// ロールオーバー
	menuHover("ul#side_course_menu li a img","_on");
}


function menuHover(id,fix){
	// ロールオーバー
	$(id).hover(
		function(){
			if($(this).parent().parent().attr("class").indexOf("current_page_item")<0){
				$(this).attr("src",$(this).attr("src").replace("_n.png", fix+".png"));
			}
		},
		function(){
			if($(this).parent().parent().attr("class").indexOf("current_page_item")<0){
				$(this).attr("src",$(this).attr("src").replace(fix+".png", "_n.png"));
			}
		}
	);
}




/***************************************
	changeImg
	要素そのものを画像に置換え
***************************************/
function changeImg(id,src,width,height,alt){
	if(alt==""){
		alt=$(id).text();
		alt = alt.replace(/\r\n/g, "");		//IE
		alt = alt.replace(/\n/g, "");		//Firefox
	}
	$(id).wrap("<p>");
	$(id).parent().attr("id",id.replace("#","")+"_rep");
	$(id).parent().empty();
	$(id+"_rep").append($("<img>")
//						.attr("src",location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/images/"+src)
						.attr("src",uri+"images/"+src)
						.attr("width",width)
						.attr("height",height)
						.attr("alt",alt)
					);
}

/***************************************
	changeImglink
	要素以下を画像に置換え
***************************************/
function changeImgLink(id,src,width,height,alt){
	if(alt==""){
		alt=$(id).text();
		alt = alt.replace(/\r\n/g, "");		//IE
		alt = alt.replace(/\n/g, "");		//Firefox
	}
	$(id).empty();
	$(id).append($("<img>")
//				.attr("src",location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/images/"+src)
				.attr("src",uri+"images/"+src)
				.attr("width",width)
				.attr("height",height)
				.attr("alt",alt)
			);
}


/***************************************
	h2_decoration
***************************************/
function h2_decoration(){
	if($.browser.msie && $.browser.version =="6.0"){
	}else{
		$("h2").attr("class","storytitle");
		$("h2 a").append($("<span>"));
	}
}


/***************************************
	textLimit
***************************************/
function textLimit(target,len){
	var ls_search = Array(
						"&quot;",	//	"
						"&#34;",	//	"
						"&#x22;",	//	"
						"&lt;",		//	<
						"&#60;",	//	<
						"&#x3C;",	//	<
						"&gt;",		//	>
						"&#62;",	//	>
						"&#x3E;",	//	>
						"&amp;",	//	&
						"&#38;",	//	&
						"&#x26;");	//	&
						
	var ls_replace = Array(
						'"',		//	&quot;
						'"',		//	&quot;
						'"',		//	&quot;
						'<',		//	&lt;"
						'<',		//	&lt;"
						'<',		//	&lt;"
						'>',		//	&gt;"
						'>',		//	&gt;"
						'>',		//	&gt;"
						'&',		//	&amp;"
						'&',		//	&amp;"
						'&');		//	&amp;"
	
	var ls_research = Array(
						'&',		//	&amp;"
						'"',		//	&quot;
						'<',		//	&lt;"
						'>');		//	&gt;"

	var ls_rereplace = Array(
						"&amp;",	//	&
						"&quot",	//	"
						"&lt;",		//	<
						"&gt;");	//	>

	var new_txt,txt,link;

	$(target).each(
		function(i){
			new_txt=txt=$(this).text();
			link=$(this).attr("href");
			for(i=0;i<ls_search.length;i++){
				txt= txt.replace(ls_search[i], ls_replace[i]);
			}
			if( txt.length>len ){
				new_txt = txt.substring(0,len)+"...";
				for(i=0;i<ls_research.length;i++){
					new_txt = new_txt.replace(ls_research[i], ls_rereplace[i]);
				}
			}
			$(this).empty();
			$(this).append(new_txt);
	});
}
/***************************************
	マップ
***************************************/
function gloval_map(){
	var elm = [];
	var elm2 = [];

	$("#gloval_map div").each(
		function(i){
		elm[i]=$(this);
		elm[i].css("display","none");
	});
	$("#gloval_map div:eq(0)").css("display","block");

	$("#gloval_map").css("position","relative");

	var newCss = new Array(7);
	newCss[0]={"left": 608,	"top": 166, "background":"#000" };
	newCss[1]={"left": 623,	"top": 161, "background":"#000" };
	newCss[2]={"left": 590,	"top": 173, "background":"#000" };
	newCss[3]={"left": 100,	"top": 280, "background":"#000" };
	newCss[4]={"left": 100,	"top": 290, "background":"#000" };
	newCss[5]={"left": 100,	"top": 300, "background":"#000" };
	newCss[6]={"left": 100,	"top": 400, "background":"#000" };


	$("ul#map_btn li").each(
		function(i){
			elm2[i]=$(this);
			elm2[i].css("left",newCss[i].left)
					.css("top",newCss[i].top)
					.css("position","absolute")
					.css("width",10)
					.css("height",10)
					.css("background","#FF0000");
//					.css("backgroundUrl",location.protocol+"//"+location.hostname + "/wp-content/themes/vantan/images/global/map_b.gif")
//					.css("backgroundRepeat","none");
			elm2[i].attr("title",elm2[i].text());
			elm2[i].empty();
			
			elm2[i].hover(
				function(){
					$("#gloval_map div").css("display","none");
					elm[i].css("display","block");
				},
			function(){
			}
		);
	});
}














/***************************************
	acc
***************************************/
function acc(){
	var src="http://"+location.hostname+"/acc/acclog.cgi?"+
		"referrer="+document.referrer+
		"&width="+screen.width+
		"&height="+screen.height+
		"&color="+screen.colorDepth;
	$("#footer").append($("<img>").attr("src",src).attr("width",0).attr("height",0).attr("alt",""));
	
}

/***************************************
	Google Analytics
***************************************/
function googleAnalytics(){
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	$.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
		var pageTracker = _gat._getTracker("UA-6759182-1");
		pageTracker._initData();
		pageTracker._trackPageview();
	});
}

/***************************************
	addEvent
***************************************
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}
*/
/***************************************
	DOM初期化
***************************************/
$(function(){
	changeImgLink("h1 a","common/h1_title.png","315","63","");
	glovalNaviImg();
	courseNaviImg();
//	h2_decoration();
	tbox();
	listToSelect("#archives");
	textLimit("ul.navigation li a",8);
//	gloval_map();
	changeImg("#service_tbl","course/service/service_tbl.png","710","111","");
	changeImgLink("#page_next a","common/btn_next.png","39","9","");
	changeImgLink("#page_prev a","common/btn_prev.png","41","9","");


	changeImg("#aas_img2","course/aas/aas_img2.png","324","96","");
	changeImg("#aas_img3","course/aas/aas_img3.png","325","303","");

	changeImg("#aas_img4","course/aas/aas_img4.png","710","136","");
	changeImg("#aas_img5","course/aas/aas_img5.png","710","136","");
	changeImg("#aas_img6","course/aas/aas_img6.png","250","253","");

	changeImg("#prep_img2","course/prep/prep_img2.png","710","136","");
	changeImg("#prep_img3","course/prep/prep_img3.png","710","136","");

	changeImg("#sch_img1","schedule/sch_img1.png","710","176","");


	$("a").click(opwin);
	serchGoToTop();

//	acc();
	googleAnalytics();
});

