﻿$(function(){
	$('#button-area a').click(function(){
	$('#main').ScrollTo(500, 'easeout');
	return false;
	});
})
	
function yalexa_search(next){
	if($("#search").css("color") == "#999" || $("#search").css("color") == "rgb(153, 153, 153)" || $("#search").val() == ""){ alert("キーワードを入力してください"); return; }
	$("#navi").remove();
	if(next == undefined){
		next = 1;
		$("#result").html('<div id="loading" align="center"><img src="load.gif" border="0" /><br /><small>loading...</small></div>');
	}
	else{
		$("#result").html($("#result").html()+'<div id="loading" align="center"><img src="load.gif" border="0" /><br /><small>loading...</small></div>');
	}

	$.get("search.php", {t: encodeURIComponent($("#search").val()), n: next}, result);
}

function result(view){
	$("#loading").remove();
	$("#result").html($("#result").html()+view);

	/* thickbox reload */
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;

}

function charChk(inputtext){
	if(inputtext == "キーワードを入力してください"){
		if($("#search").css("color") == "#999" || $("#search").css("color") == "rgb(153, 153, 153)"){
			$("#search").val(""); $("#search").css("color","#000");
		}
	}
	else if(inputtext == ""){ $("#search").css("color","#999"); $("#search").val("キーワードを入力してください"); }
}

function alexaCheck(box){

	var tcount;
	tcount = 0;

	$("input").each(function(){
		if(this.type == "checkbox"){
			if(this.checked == true){
				tcount++;
			}
		}
	});

	if(tcount > 5){
		document.getElementById(box).checked = false;
		alert("チェックは5個まで有効となっています。\n他のチェックを外し選択し直してください。");
	}


}

function alexaGraph(){

	var alexa = "http://www.alexa.com/data/details/traffic_details?y=r&z=1&h=300&w=610&range=6m&size=Medium&url=";
	var site = new Array();
	var add = "";
	var ua = navigator.userAgent;

	$("input").each(function(){
		if(this.type == "checkbox"){
			if(this.checked == true){
				site.push(this.id);
			}
		}
	});

	if(site.length == 0){
		alert("検索結果URL横のチェックボックスで、比較したいサイトを選んでください。");
		return;
	}
	else{
	var count = site.length;
	}
	for(i=0;i<count;i++){
		if(ua.indexOf("Firefox") >= 0){
			var j = document.getElementById(site[i]).parentNode.parentNode.childNodes;
			j = j[3].firstChild.href;
		} else {
			var j = document.getElementById(site[i]).parentNode.parentNode;
			j = j.firstChild.nextSibling.nextSibling.firstChild.href;
		}
		j = j.match(/:\/\/(.*?)\//);
		add += "&site"+i+"="+j[1];
	}

		if(ua.indexOf("Firefox") >= 0){
			var j = document.getElementById(site[0]).parentNode.parentNode.childNodes;
			j = j[3].firstChild.href;
		} else {
			var j = document.getElementById(site[0]).parentNode.parentNode;
			j = j.firstChild.nextSibling.nextSibling.firstChild.href;
		}
	jump = alexa+j+add;
	window.open(jump);

}

function changeGraph(add,flg,elem){

	alexa = "http://traffic.alexa.com/graph?u=&u=&r=3m&y="+flg+"&z=1&h=120&w=250&u="+add;

	var e = elem.parentNode.nextSibling;

	if(flg == "t"){ e.title = "トラフィック世界ランク"; elem.style.backgroundColor = '#ffa'; elem.nextSibling.style.backgroundColor = '#fff'; elem.previousSibling.style.backgroundColor = '#fff'; }
	else if(flg == "r"){ e.title = "サイト訪問者数"; elem.style.backgroundColor = '#ffa'; elem.nextSibling.style.backgroundColor = '#fff'; elem.nextSibling.nextSibling.style.backgroundColor = '#fff'; }
	else if(flg == "p"){ e.title = "平均ページビュー数"; elem.style.backgroundColor = '#ffa'; elem.previousSibling.style.backgroundColor = '#fff'; elem.previousSibling.previousSibling.style.backgroundColor = '#fff'; }

	e.src = alexa;

}

function analysis(add){

	WinX = (screen.width  - 700) / 2;
	WinY = (screen.height - 500) / 2;

	var myOption = 'location=yes,toolbar=yes,resizable=yes,scrollbars=yes,left='+WinX+',top='+WinY+',width=700,height=500';

	window.open('a/analysis.php?a='+encodeURIComponent(add), 'SubWin', myOption);

}

function exportData(){
	var data = "\""+document.getElementById("keyword_result").innerHTML+"\",";
	data = data+"\"Yahoo! JAPAN\",\""+document.getElementById("yahoo_result").innerHTML.replace(",", "")+"\"\n";
	data = data+",\"Google\",\""+document.getElementById("google_result").innerHTML.replace(",", "")+"\"\n";
	data = data+"\n";
	data = data+"\"Yahoo! JAPANの検索順位\",\"ページタイトル\",\"URL\",\"Yahoo! JAPAN 月間検索アクセス予測数\",\"Googleで当該順位だった場合の月間検索アクセス予測数\"\n";

	for(i = 0;i < $(".result-box").length;i++){
		data = data+"\""+(i+1)+"\",";
		data = data+"\""+document.getElementById("anchor"+(i+1)).firstChild.innerHTML+"\",";
		data = data+"\""+document.getElementById("anchor"+(i+1)).firstChild.href+"\",";
		data = data+"\""+document.getElementById("yahoo"+(i+1)).innerHTML.replace(",", "")+"\",";
		data = data+"\""+document.getElementById("google"+(i+1)).innerHTML.replace(",", "")+"\"\n";
	}

	var objBody = document.getElementsByTagName("body")[0];
	var frmObj = document.createElement("form");
	frmObj.method = "post";
	frmObj.name = "exdata";
	frmObj.action = "export.php";
	var input = document.createElement("input");
		input.setAttribute('type', "hidden");
		input.setAttribute('name', "data");
		input.setAttribute('value', data);
	frmObj.appendChild(input);
	objBody.appendChild(frmObj);
	frmObj.submit();
}