/*
Universal JavaScript Library ver.0.4
written by y.iibuchi@gmail.com



*/
// ------------------- main timeline ---------------------
if (self != top)top.location.href = self.location.href;
// http://archiva.jp/web/html-css/ie6_background_flickr.html
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}
//http://d.hatena.ne.jp/amachang/20071010/1192012056
//http://postoro.blogspot.com/2011/02/1-ie-javascript-ie9.html
/**
 *  Acceleration of IE(8-) (error by IE9)
 *  @see http://d.hatena.ne.jp/amachang/20071010/1192012056
 */
/*@cc_on
if (@_jscript_version < 9) {
    var _d = document;
    eval("var document = _d");
}
@*/
if (window.attachEvent){
	window.attachEvent('onload', jqueryexec);
	window.attachEvent('onload', initAhref);
} else {
	window.addEventListener('load', initAhref, false);
	window.addEventListener('load', jqueryexec, false);
}

//$(document).ready(jqueryexec);

// ------------------- function ---------------------

function jqueryexec(){
/* 
	$('a.top_nav,a.nav_parent').parent().hover(function(){
		$(this).get(0).className+=' sfhover';
		},function(){
		$(this).get(0).className=$(this).get(0).className.replace(new RegExp(' sfhover\\b'), '');
	});*/
	$('table.evencolor tr:nth-child(even)').addClass('odd');
//alert("OK");
}

function initAhref(){
/*******************************************************************
initAhref() ver.2.4 scripted by y.iibuchi@gmail.com

This Searchs all "a" elements and "area" elements,
and setting class name in a best suited.
Omitted at "link" elements and classed "noappend" elements.

ex: window.onload = initAhref;

related functions: appendClassName,trimHref,onclickHashedLink
*******************************************************************/
	var aElms = new Array();
	for(var i=0;i<document.getElementsByTagName("a").length;i++){
		aElms.push(document.getElementsByTagName("a")[i]);
	}
	for(var i=0;i<document.getElementsByTagName("area").length;i++){
		aElms.push(document.getElementsByTagName("area")[i]);
	}
	var myhost = new RegExp("^https*://"+location.hostname,"i");
	var locpath = trimHref(location.href.replace(myhost,""));
	for(var i=0;i<aElms.length;i++){ // loop at number of 'a' elements.
		if(aElms[i].className.indexOf("noappend")==-1){
			// branching at "href" parameter matching from here.
			var hr = trimHref(aElms[i].href.replace(myhost,""));
			if(hr==locpath){ // link at myself
				if(aElms[i].href.indexOf("#")!=-1){ // anchor
					appendClassName(aElms[i],"anchor");
					aElms[i].onclick = onclickHashedLink;
				} else {
					appendClassName(aElms[i],"current");
				}
			} else if(hr.match(/^mailto:/i)){
				// at WinIE, replace "mailto:", textNode is same strings.
				// http://blog.kaihatsubu.com/archives/001451.html
				var aVal = aElms[i].innerHTML; // provision at WinIE
				// provision at multi-byte language
				var aHrefEnc = encodeURIComponent(aElms[i].href);
				var atmarks = [
					new RegExp("[_\(\-]at[_\)\-]", "i"),
					new RegExp("%EF%BC%88at%EF%BC%89", "i"),
					new RegExp("%25EF%25BC%2588at%25EF%25BC%2589", "i"),
					new RegExp("[_\(\-]atmark[_\)\-]", "i"),
					new RegExp("%EF%BC%88atmark%EF%BC%89", "i"),
					new RegExp("%25EF%25BC%2588atmark%25EF%25BC%2589", "i"),
					new RegExp("%EF%BC%A0"),
					new RegExp("%25EF%25BC%25A0")
				];
				for(var j=0;j<atmarks.length;j++){
					aHrefEnc = aHrefEnc.replace(atmarks[j], "@");
				}
				aElms[i].href = decodeURIComponent(aHrefEnc);
				aElms[i].innerHTML = aVal; // provision at WinIE
				appendClassName(aElms[i],"mail");
			} else if(hr.match(/^tel:/i)){
				// Replace HTML for PC from mobile "tel:" links.
				if(document.all){ // Win IE(Shift_JIS+CR/LF)
					var myhref = [
						"mailto:Input%20your%20mobile%20mail%20address",
						"?subject=%82%A8%96%E2%82%A2%8D%87%82%ED%82%B9%94%D4%8D%86",
						"&body=%82%B1%82%CC%94%D4%8D%86%82%F0%83%81%81%5B%83%8B%82%C5%8Cg%91%D1%82%C9%93%5D%91%97%82%B7%82%E9%82%C6%81A%82%A8%8E%E8%8Cy%82%C9%82%A8%93d%98b%82%AA%82%A9%82%AF%82%E7%82%EA%82%DC%82%B7%81B%0D%0A",
						aElms[i].href
					].join("");
				} else { // Outside Win IE(UTF-8+LF)
					var myhref = [
						"mailto:Input%20your%20mobile%20mail%20address",
						"?subject=%E3%81%8A%E5%95%8F%E3%81%84%E5%90%88%E3%82%8F%E3%81%9B%E7%95%AA%E5%8F%B7",
						"&body=%E3%81%93%E3%81%AE%E7%95%AA%E5%8F%B7%E3%82%92%E3%83%A1%E3%83%BC%E3%83%AB%E3%81%A7%E6%90%BA%E5%B8%AF%E3%81%AB%E8%BB%A2%E9%80%81%E3%81%99%E3%82%8B%E3%81%A8%E3%80%81%E3%81%8A%E6%89%8B%E8%BB%BD%E3%81%AB%E3%81%8A%E9%9B%BB%E8%A9%B1%E3%81%8C%E3%81%8B%E3%81%91%E3%82%89%E3%82%8C%E3%81%BE%E3%81%99%E3%80%82%0A",
						aElms[i].href
					].join("");
				}
				aElms[i].href=myhref;
				appendClassName(aElms[i],"tel");
			} else if(hr.match(/^javascript:/i)){ // extension
				appendClassName(aElms[i],"javascript");
			} else if(hr.match(/\.pdf$/i)){
				appendClassName(aElms[i],"pdf");
			} else if(hr.match(/\.doc$|\.docx$/i)){
				appendClassName(aElms[i],"word");
			} else if(hr.match(/\.xls$|\.xlsx$/i)){
				appendClassName(aElms[i],"excel");
			} else if(hr.match(/\.jpg$|\.gif$|\.png$/i)){
				appendClassName(aElms[i],"image");
			} else if(hr.match(/\.wmv$|\.mp4$|\.avi$/i)){
				appendClassName(aElms[i],"movie");
			} else if(hr.indexOf("http")==0){ // external server
				if(
					(aElms[i].innerHTML.match(/^\s*<img[^>]+>\s*$/i))
				&&
					(aElms[i].className.indexOf('popup')==-1)
				&&
					(aElms[i].className.indexOf('blank')==-1)
				){
					appendClassName(aElms[i],"blank_noicon");
				} else if(
					(aElms[i].className.indexOf('popup')==-1)
				&&
					(aElms[i].className.indexOf('blank_noicon')==-1)
				){
					appendClassName(aElms[i],"blank");
				}
			}
			// branching at class name matchings from here.
			var classes = aElms[i].className.split(" ");
			//aElms[i].title += "\nclassName="+classes.join("/");
			for (var j=0;j<classes.length;j++){
				if(classes[j]=="popup"){
					aElms[i].onclick=function(){
						window.open(this.href,"popup","width=704, height=800, location=no, menubar=no, scrollbars=yes, resizable=yes");
						return false;
					};
				} else if(classes[j]=="blank_noicon"){
					// at IE9, this.target not work well.
/*					aElms[i].target = "_blank";*/

					aElms[i].onclick=function(){
						window.open(this.href,"_blank");
						return false;
					};
//					aElms[i].title = "blank_noicon";
				} else if(classes[j]=="blank"){
					// at IE9, this.target not work well.
/*					aElms[i].target = "_blank";*/

					aElms[i].onclick=function(){
						window.open(this.href,"_blank");
						return false;
					};
//					aElms[i].title = "blank";
				} else if(classes[j]=="pdf"){
/*					// at IE9, this.target not work well.
					aElms[i].target = "_blank";
*/
					aElms[i].onclick=function(){
						window.open(this.href,"_blank");
						return false;
					};
//					aElms[i].title = "pdf";
				}
			}
		}
	}
	function appendClassName(elm,str){
		var clses = elm.className.split(" ");
		for(var i=0;i<clses.length;i++){
			if(clses[i]==str){
				var flag = true;
			}
		}
		elm.className = (!flag)?elm.className+" "+str:elm.className;
	}
	
	function trimHref(str){ // delete search and hash from href.
		if(str.indexOf("?")!=-1){
			str = str.substring(0,str.indexOf("?"));
		} else if(str.indexOf("#")!=-1){
			str = str.substring(0,str.indexOf("#"));
		}
		if(str.charAt(str.length-1)=="/~macbook/"){
			str+="index.html";
		}
		return str;
	}
	
	function onclickHashedLink(){ // Replace anchor jump to onclick jump.
		var myHash = this.href.substring(this.href.indexOf("#")+1);
		if(myHash=="top"){
			var tgtY = 0;
		} else {
			var tgtY = document.getElementById(myHash).offsetTop;
		}
		//alert("IE:"+d.offsetTop+"; FF:"+d.offsetTop);
		document.documentElement.scrollTop = tgtY;//ff
		return false;
	}
}


function URL_J() {
	var option_value = document.getElementById("scopeselector").value;
	var search_value = document.getElementById("search").value;
	if (option_value == "virus") {
		document.header_search.action = ("http://www.fortiguard.com/encyclopedia/search/" + search_value);
	} else if (option_value == "jp") {
		document.header_search.action = ("/cgi-bin/namazu.cgi?query="+search_value);
	}
}

function initDL(acc, tra) {
/*
jQuery initializer written by ono-j@morilabo.com
arguments:
	acc, target elements of accordion at jQuery CSS selectors.
	tra, target elements of tracking at jQuery CSS selectors.
use pages:
	/resource_center/index.html
	/leads/downloadpage.php
*/

	// accordion
	$(function() {
	$(acc + " h3").click(function() {
		// close
		$(acc + " div").not($(this).next()).slideUp();
		// open the next element
		$(this).next().animate({height: "toggle", opacity:"toggle" }, 500);
	}).next().hide();
	});

	// Call tracking code on click.
	// http://code.google.com/intl/ja/apis/analytics/docs/tracking/asyncMigrationExamples.html */
	jQuery(function($) {
	$(tra).click(function() {
		var fn = $(this).attr("href");
		if (fn.match(/download\.php\?filename=(.+)/)) {
			//alert('/memberfiles/' + RegExp.$1);
			_gaq.push(['_trackPageview', '/memberfiles/' + RegExp.$1]);
		} else if (fn.match(/\/doc\//)) {
			//alert(fn);
			_gaq.push(['_trackPageview', fn]);
		}
		});
	});
}

function threatlevelWrite() {
/*
Top Page / FortiGuard Threat Level Write
written by ono-j@morilabo.com 2011/12/16

arguments:
	none
use pages:
	/index.html
notice:
	/includes/threatlevel.txt is reverse proxy.
	(http://www.fortiguard.com/includes/threatlevel.txt?seed=)
	Special thanks to develop team
*/
	var level_txt = new Array("", "脆弱性", "ウイルス/<br />スパイウェア", "スパム");
	var write_txt = "";
	$.ajax({
	    url: '/includes/threatlevel.txt',
	    type: 'GET',
		cache: false,
	    dataType: 'text',
	    timeout: 1000,
	    error: function() {
			for (i = 1; i < 4; i++) {
				write_txt += '<div class="threatmeter"><div>' + level_txt[i] +
				' <span style="float:right;">-- no data --</span></div></div>\n';
			}
			$("#threatlevel").html(write_txt);
	    },
	    success: function(data) {
			var level_arr = data.split("&");
			for (i = 1; i < 4; i++) {
				var tl = level_arr[i].match(/.+=(\d)/); // RegExp.$1 -> level
				write_txt += '<div class="threatmeter"><div>' + level_txt[i] + '</div> <img alt="' + level_txt[i] + ' Threat Level ' + RegExp.$1 + '" src="/theme_fn/threatlevel' + RegExp.$1 + '.gif" /></div>\n';
			}
			$("#threatlevel").html(write_txt);
	    }
	});
}


