﻿//漢字美

	//☆faviconを自動表示するスクリプト
	
	var izFavicon = {
		url: "",
		name: "favicon.ico",
		isExist: function(sImageUrl){
			//画像用存在確認関数
			var img = new Image();
			img.src = sImageUrl;
			return img.height > 0; // 読み込みに失敗すれば 0 になる。
		},
		searchFavicon: function(relativePath){
			//拡張子.icoのファイルをさかのぼって探す
			//☆再帰呼び出し
			
			var temp = relativePath + this.name;
			if(izFavicon.isExist(temp)){
				this.url = temp;
				return true;
			}
			else{
				var sepalator = izFavicon.inStrCount(window.location.href,"/");
				var up_count = izFavicon.inStrCount(relativePath,"../");
				var lastCount = sepalator - up_count;
				if(lastCount > 0){
					return izFavicon.searchFavicon("../" + relativePath);
				}
				return false;
			}
		},
		inStrCount: function(target,search){
			var temp = target;
			var retCount = 0;
	
			var end_pos = temp.lastIndexOf(search,temp.length);
			for(; end_pos > 0; end_pos = temp.lastIndexOf(search,temp.length)){
				retCount++;
				temp = temp.substring(0,end_pos);
			}
			return retCount;
		},
		append: function(){
			if(this.url){
				//faviconを読み込むタグを追加する
				var favicon = document.createElement("link");
				favicon.id = "favicon";
				favicon.rel ="shortcut icon";
				favicon.type = "image/x-icon";
				favicon.href = this.url;
				document.getElementsByTagName("head")[0].appendChild(favicon); 
				
				//キャッシュを読み込みなおす。
				var temp = new Image();
				temp.src = this.url;
			}
		}
	};
	
	if(izFavicon.searchFavicon("")){izFavicon.append();}
	
