var loaderImg2 = '<img src="image/ajax-loader2.gif" width="100" height="100" style="margin:0 auto;">';
var ar_bm = { 
"hatena" : ["http://b.hatena.ne.jp/entry/image/large/","http://b.hatena.ne.jp/entry/"] ,
"yahoo" : ["http://num.bookmarks.yahoo.co.jp/image/small/","http://bookmarks.yahoo.co.jp/url?url="] ,
"ld" : ["http://image.clip.livedoor.com/counter/medium/","http://clip.livedoor.com/page/"] ,
"buz" : ["http://api.buzzurl.jp/api/counter/v1/image?url=","http://buzzurl.jp/entry/"] 
};
var jlist_blog_str = '<div style="border-top:1px dashed #aaa; padding:4px; "><p style="font-weight:bold;color:#f33;font-size:13px;">ブログの場合はなんと半額！</p><img src="http://seocheki.net/image/arrow.gif" style="width:10px;height:10px;margin-right:4px;"><a href="http://jentry.jlisting.jp/partner/seocheki_blog" target="_blank">ブログを登録する</a></div>';

var ar_server2 = [
"http://sokkuri.net/seocheki/",
"http://seocheki.awe.jp/",
"http://seo-tool.awe.jp/",
"http://seocheki.tank.jp/",
"http://seocheki.razor.jp/",
"http://seocheki.jeez.jp/",
"http://seocheki.bex.jp/",
"http://seocheki.geo.jp/",
"http://seocheki.xrea.cc/",
"http://seocheki.xrea.bz/",
"http://seocheki.xrea.jp/",
"http://seocheki.bulog.jp/",
"http://seocheki.happy.nu/"
];

//ar_server2 = ["sub/","sub/","sub/","sub/","sub/","sub/"];

var show = false;
var is_blog = false;

function kw_check(){

	var url = $("#url2").val();
	url = check_url(url); if(url==false) return;

	$("#kw-info").hide();
	$("#kw-frame").hide();
	$("#gmo-box").hide();
	$("#kw-loader").show();

	$.getJSON("get/get-parser.php?callback=?&url=" + encodeURIComponent(url), function(data){

		$("#gmo1kw,#gmo2kw,#gmo3kw").html(gmo_pbar);
		$("#kw-loader").hide();
		$("#kw-frame").show();
		$("#box-kw").html(data.table);
		if(data.error==true) return;
	
		//gmo-seo
		bar_len = 1;
		clearInterval(ti);
		ti = setInterval("bar()",800);
		$("#gmo-box").show();

		for(i=0;i<=2;i++){
			if(data.word[i]) {
				url = "http://seo-airlines.com/api/keyword/chk.php?apikey=2a44e281dca2633e66126b5f924ed7c6&url=" + encodeURIComponent(url) + "&keyword=" + encodeURIComponent(data.word[i]);
				seo_air_request(url,i+1,"kw")
			}
		}

	});

}

function site_check(){


    var i = ar_server2.length;
    while(i){
        var j = Math.floor(Math.random()*i);
		i--;
        var tmp = ar_server2[i];ar_server2[i] = ar_server2[j]; ar_server2[j] = tmp;
    }
	if(ar_server2[0]=="http://sokkuri.net/seocheki/") ar_server2[0]="http://seocheki.razor.jp/";

	if(show) clear_form();
	show=true; is_blog=false;
	
	var url = $("#url0").val();
	url = check_url(url); if(url==false) return;

	$("#td-url").html('<a href="' + url + '" target=\"_blank\">' + url + '</a>');
	$("#url1,#url2").val(url);

	$("#td-siteimg").html('<a href="' + url + '" target="_blank"><img src="http://capture.heartrails.com/200x200/cool?' + url + '" width="200" height="200" border="0"></a>');

	$("#td-pr,#td-alexa,#td-title,#td-desc,#td-host,#td-whois,#td-ycate,#td-jlist,#td-bpn,#td-emachi,#td-idir,#td-sas,#td-title,#td-desc,#td-kw,#td-h1,#td-outlink,#td-lm,#td-fs,#td-tt,#td-bm-hatena,#td-bm-yahoo,#td-bm-ld,#td-bm-buz,#td-bm-tw,#td-bm-fb").html(loaderImg);
	$("#td-gl,#td-gi").html(loaderImgG);
	$("#td-yl,#td-yi").html(loaderImgY);

	$("#css-check").attr("href","http://jigsaw.w3.org/css-validator/validator?uri=" + encodeURIComponent(url) +"&profile=css21&usermedium=all&warning=1&lang=ja");
	$("#html-check").attr("href","http://validator.w3.org/check?uri=" + encodeURIComponent(url) +"&charset=%28detect+automatically%29&doctype=Inline&group=0");
	$("#whois").attr("href","whois.php?w-url=" + url);
	$("#http-header").attr("href","http-header.php?url=" + url);
	$("#outlink").attr("href","outlink.php?url=" + url);
	$("#bl-check").attr("href","backlink.php?f=e&url=" + url);
	$("#bl-check2").attr("href","backlink.php?f=a&url=" + url);
	$("#archive").attr("href","http://web.archive.org/web/*/" + url);
	$("#aguse").attr("href","http://www.aguse.jp/?m=w&url=" + url);
	$("#gad").attr("href","https://www.google.com/adplanner/planning/site_profile#siteDetails?identifier=" + url + "&lp=true");

	$("#bl-moz").attr("href","http://www.opensiteexplorer.org/links?site=" +encodeURIComponent(url)+ "&=Search");
	$("#bl-maj").attr("href","https://www.majesticseo.com/reports/site-explorer?folder=&q=" +encodeURIComponent(url)+ "&IndexDataSource=H");
	$("#bl-ahr").attr("href","http://ahrefs.com/site-explorer/exact/" +encodeURIComponent(url.replace(/^https?:\/\//,"").replace(/\/$/,"").replace(/\//g,"%2F")));

	var eurl = encodeURIComponent(url);

	//サイトタイトルなど
	$.getJSON("get/get-siteinfo.php?url=" + eurl ,function(res){

			if(res.stat) {  location.href='./forbidden.php'; return; }

			is_blog = res.is_blog
			$("#td-title").html(res.title);
			$("#td-desc").html(res.desc);
			$("#td-kw").html(res.kw);
			$("#td-h1").html(res.h1);
			if(res.inlink) $("#td-outlink").html("外部リンク：" + res.outlink + "　内部リンク：" + res.inlink + '　<a href="outlink.php?url=' + url + '" target="_blank" style="font-size:12px;">全ての発リンクを見る</a>');
				else $("#td-outlink").html("&nbsp;");
			if(res.lastm) $("#td-lm").html(res.lastm + '　<a href="http-header.php?url=' + url + '" target="_blank" style="font-size:12px;">HTTPヘッダを見る</a>');
				else $("#td-lm").html("&nbsp;");
			$("#td-fs").html(res.fsize);
			$("#td-tt").html(res.ttime);
				
			ar_kw = res.kw.split(",");
			$("#word1").val(ar_kw[0]);
			
			var tw_str = '<a href="http://twitter.com/share" class="twitter-share-button" data-text="' +res.title+ '" data-url="' +url+ '" data-count="horizontal" data-lang="ja">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
			$("#td-bm-tw").html(tw_str);
			var fb_str = '<iframe src="http://www.facebook.com/plugins/like.php?href=' +encodeURIComponent(url)+ '&amp;layout=button_count&amp;show_faces=true&amp;width=110&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px; margin:auto;" allowTransparency="true"></iframe>';
			$("#td-bm-fb").html(fb_str);
			
			check_dir(url);
    });	

	//ペイジランク
	$.ajax({
		dataType: "jsonp",
		url: ar_server2[0] + "get-pralexa.php?m=pr&url=" + eurl,
		success: function(data) {
			$("#td-pr").html(data.val); 
			$("#fakepr").html('<a href="javascript:check_fakepr_e();">ページランク偽装チェック</a>');

			//ソーシャルサイト
			for(key in ar_bm){
				$("#td-bm-" + key).html('<a href="' + ar_bm[key][1] + url + '" target="_blank"><img src="' + ar_bm[key][0] + url + '" border="0"></a>');
			}

		},
		error: function(){ $("#td-pr").html("failed"); }
	});

	//アレクサ
	$.ajax({
		dataType: "jsonp",
		url: ar_server2[1] + "get-pralexa.php?m=alexa&url=" + eurl,
		success: function(data) { $("#td-alexa").html('<a href="http://www.alexa.com/siteinfo/' + url + '" target="_blank">' + data.val + '</a>'); }
	});

	//被リンク・インデックス　グーグル
	check_google(url,ar_server2[2],0);

/*
	//被リンク・インデックス　ヤッホー
	$.ajax({
		dataType: "jsonp",
		url: ar_server2[3] + "get-idxlink.php?se=y&url=" + eurl,
		success: function(data) {
			$("#td-yi").html('<a href="http://siteexplorer.search.yahoo.com/search?p=' + eurl + '" target="_blank">' + data.idx + '</a>');
			$("#td-yl").html('<a href="http://siteexplorer.search.yahoo.com/search?p=' + eurl + '&bwm=i" target="_blank">' + data.link + '</a>');
		}
	});
*/

/*
	//被リンクseomoz
	$.ajax({
		dataType: "jsonp",
		url: "sub/get-idxlink.php?se=moz&url=" +eurl,
		success: function(data) {
			$("#td-moz").html('<a href="http://www.opensiteexplorer.org/links?site=' + eurl + '&=Search" target="_blank">' + data.link + '</a>');
		}
	});
*/

	//ホスト
	$.ajax({
		dataType: "jsonp",
		url: "http://seocheki.bulog.jp/get-host.php?url=" +eurl,
		success: function(data) { 
			$("#td-host").html(data.host);
			$("#td-whois").html(data.whois);
		}
	});
	
		//show_ret_tag();

}

//ディレクトリ
function check_dir(url){
	$.ajax({
		dataType: "jsonp",
		url: "http://seo-tool.awe.jp/get-dir.php?url=" + encodeURIComponent(url),
//		url: "sub/get-dir.php?url=" + encodeURIComponent(url),
		success: function(data) { 
			$("#td-ycate").html(data.yahoo);
			$("#td-jlist").html(data.jlist);
			if(is_blog && data.jlist.match(/未登録/)) $("#td-jlist").append(jlist_blog_str); 
			$("#td-bpn").html(data.bpn);
			$("#td-emachi").html(data.emachi);
			$("#td-idir").html(data.idir);
			$("#td-sas").html(data.sas);
		}
	});

}


//被リンク・インデックス　グーグル
function check_google(url,server,cnt){
	$.ajax({
		dataType: "jsonp",
		url: server + "get-idxlink.php?se=yg&url=" + encodeURIComponent(url),
		success: function(data) { 
			if(data.idx.match(/failed/) && cnt<2) { check_google(url,ar_server2[Math.floor(Math.random()*ar_server2.length)],cnt+1); return; }

			var p = encodeURIComponent("site:"+url);
			$("#td-gi").html('<a href="http://www.google.co.jp/search?hl=ja&q=' + p + '&num=20" target="_blank">' + data.idx + '</a>');
			p = encodeURIComponent("link:" + url);
			$("#td-gl").html('<a href="http://www.google.co.jp/search?hl=ja&q=' + p + '&num=20" target="_blank">' + data.link + '</a>');
		}
	});
}



function clear_form(){

	for(i=1;i<=3;i++){
		$("#kw"+i).html("&nbsp;");
	
		$("#grank"+i).html("&nbsp;");
		$("#yrank"+i).html("&nbsp;");
		$("#word"+i).val("")
	}
	$("#box-kw").html("&nbsp;");
	$("#fakepr").html('&nbsp;');

}

function check_fakepr_e(){
	var url = $("#url0").val();
	url = check_url(url); if(url==false) return;
	$("#fakepr").html("調査中...");
	
	$.ajax({
		dataType: "jsonp",
		url: "get/check-fakepr-e.php?url=" + encodeURIComponent(url),
		success: function(data) { 
			$("#fakepr").html(data.res);
		}
	});
}

function check_fakepr(){
	var url = $("#url").val();
	url = check_url(url); if(url==false) return;
	$("#res-box").html(loaderImg2).css("textAlign","center");
	$.ajax({
		dataType: "jsonp",
		url: "http://seo-tool.awe.jp/check-fakepr.php?url=" + encodeURIComponent(url),
		success: function(data) { 
			$("#res-box").html(data.res).css("textAlign","left");
		}
	});
}

function show_jpartner(n){
	if(n==1){
		$("#jpartner").css("height","500px")
		$("#jpartner div").html("<img src='image/jpartner3.jpg' height='439'>");
	} else {
		$("#jpartner").css("height","400px")
		$("#jpartner div").html("<img src='image/bpn.png' height='338'>");
	}
    wx = $(window).scrollLeft() + Math.floor(($(window).width() - $('#jpartner').outerWidth()) / 2);
    wy = $(window).scrollTop() + Math.floor(($(window).height() - $('#jpartner').outerHeight()) / 2);
    $("#jpartner").css({ "top": wy, "left": wx }).fadeIn("fast");
}

function close_jpartner(){
    $("#jpartner").fadeOut("fast");
}

function check_url(url){
	url = url.replace(/^[ ]+|[ ]+$/g, "");
	if(url=="" || url=="about:blank") return false;
	if(!url.match(/^https?:\/\//)) url = "http://"+url;
	if(!url.match(/^https?:\/\/[-a-zA-Z0-9]+\.[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$/)){ alert("不正なURLです"); return false; }
	return url;
}

function html_ent(str){
	return str.replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;");
}


function show_bottom_ad(){
	$("#bottom-ad").html('<iframe src="testjs.html" width="728" height="90" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" allowtransparency="true"></iframe>');
}

function show_ret_tag(){
	$("#bottom-ad").html('<iframe src="retarget.html" width="0" height="0" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" allowtransparency="true"></iframe>');
}



