/**
 *  _____ _____ _____ _____ _____ _____ _____ 
 * | __  |  _  |     |     |     |     |   | |
 * |    -|     |   --|   --|  |  |  |  | | | |  ∧＿_∧
 * |__|__|__|__|_____|_____|_____|_____|_|___| <´･ェ･`>
 *                                            
 * raccoon.js
 * @author Tenderfeel
 * @ver 1.0
 * @HOME http://tenderfeel.xsrv.jp/
 * 
 * @Need:mootools 1.2
 *
 * ---MIT License--------------------------------------------
 * Copyright (c) 2008 Tenderfeel all rights reserved.
 * ----------------------------------------------------------
 */

var DOMAIN = "http://seiwa-genji.jp/";

window.addEvent('domready', function(){
									 
	//new mooPingfix();
	//new wtnRollover({area:"#main"});
	new wtnLinkIcons({excludeStr:["file:///","http://web-zero.jp","http://seiwa-genji.jp/"],iconDir:DOMAIN+"images/filetype/",QuickBox:true,open:"blank"});
	
	if($("ancient-list"))
		new mooZebra($("ancient-list"))

	/* go to top */
	new GotoTop();
	
	/* Google Analytics*/
	
	
	//Tracker
	var container = $("container");
	var gtracker = new Element("script",{src:DOMAIN+'js/gtracker.js',"type":"text/javascript"});
	gtracker.inject(container,"after");
	
	//Analytics
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	var ga = new Element("script",{src:gaJsHost + "google-analytics.com/ga.js","type":"text/javascript"});
	ga.inject(container,"after");
	
});


/** QuickBox Plugin */
var external_js = new Asset.javascript(DOMAIN+'js/QuickBox.js');
var myCSS = new Asset.css(DOMAIN+"css/quickbox.css",{ "media":"screen"});



/** 
 * 
 * Google Ajax API 
 * 
 */
 
var gAPIkey = 'ABQIAAAAwrzVu2iAWtCM12EpP4ZXHBSrG2R-jI4ckwRvfWbEV34E2QpRKRQd_v38Co4Z8-C172llO4kctU1neA';
var gs,gm,gf;

/** Gsearch */
/*
var gsConfig = {
	"searchID":"012711302483671009736:jhsgvibdvnw",
	"formID":"searchForm",
	"resultID":"content",
	"titleBlock":"h1"
};
new Asset.javascript(DOMAIN+"js/gsearch.js");var gs=true;
*/


/** Gmap */

var gmConfig = {
	"pageName":"about.html",//表示するページ
	"gmLat":35.4047528865772,  //GLat
	"gmLng":137.10962176322937, //GLng
	"gmDivID":"gmap",    //地図を表示する要素のID
	"gmScope":16,        //拡大率
	"markerAdd":true,  //マーカーの表示
	"SMap_Controler":false, //小さいコントローラ
	"LMap_Controler":true, //大きいコントローラ
	"Type3_Controler":true, //地図タイプ切り替え（３種）
	"Type2_Controler":false, //地図タイプ切り替え（2種）
	"Scale_Controler":true, //スケール
	"OV_Controler":true, //右下の縮小地図
	"WheelZoom":false, //ホイールでのズーム操作
	"DClickZoom":true //ダブルクリックでのズーム操作
}


/** 情報ウィンドウのHTML */
var gmIWhtml = "<em>八十一隣春秋園<br />（春秋園木曽古文書歴史館）</em><br />岐阜県可児市久々利1644-1";


if(location.href.indexOf(gmConfig.pageName)!==-1){
	new Asset.javascript(DOMAIN+'js/gmap.js');var gm=true;
}


/** GFeed */
/*
var gfConfig = {
	"feedURI":"http://www.digg.com/rss/index.xml",
	"resultID":"feedControl",
	"max":2,
	"text":100,
	"feedBlock":"dl",
	"titleBox":"dt",
	"dateBox":"dd",
	"contentBox":"dd"
}
new Asset.javascript(DOMAIN+'js/gfeeds.js');var gf=true;
*/

new Asset.javascript('http://www.google.com/jsapi?callback=googleAPIloadings&key='+gAPIkey);

function googleAPIloadings(){
	if(gs){google.load('search', '1',{"callback" : searchLoaded});}
	if(gm){google.load("maps", "2",{"callback" : mapsLoaded,"language" : "ja"});}
	if(gf){google.load("feeds", "1",{"callback" : feedLoaded,"language" : "ja_JP","nocss" : true});}
}


/**
 * AnalogIE
 * @ver 1.0
 * IE6以下の場合にアナログと表示する
 */
var mooAnalogIE = new Class({
	
	Implements: [Options],

	options: {
		html:'<img src="images/analog.png" />',
		text:"まだアナログ環境をご利用ですか？今すぐデジタル環境へアップグレード",
		element:"#header"
	},
	initialize: function(options) {
		this.setOptions(options);
		this.html = this.options.html;
		this.text = this.options.text;
		this.element =  $$(this.options.element);
		if(this.element) this.start();
		else return false;
	},
	start:function(){
		if(Browser.Engine.trident && Browser.Engine.version < 5){
			new Element("a",{
				"href":"http://www.microsoft.com/japan/windows/products/winfamily/ie/function/default.mspx",
				"title":"Internet Explorer8のダウンロードサイトへ",
				"target":"_blank",
				"id":"analogIE",
				events:{
					"mouseover":function(){
						this.addClass("mover");
						this.set("html",this.text);
					},
					"mouseout":function(){
						this.removeClass("mout");
						this.set("html",this.html);
					}
				},
				html:this.html
			}).inject(this.element);
		}
	}
});

/**
 * GotoTop
 * @ver 1.0
 * ページのトップに戻るボタン
 */
var GotoTop = new Class({
	
	Implements: [Options],

	options: {
		id:'gototop',
		width:100
	},
	initialize: function(options) {
		this.setOptions(options);
		this.width = this.options.width;
		this.gototop =  $(this.options.id);
		new SmoothScroll({duration:500});
		if(this.gototop) this.start();
		else return false;
	},
	start:function(){
		var self=this;
		self.gototop.set('opacity','0').setStyle('display','block');
		window.addEvent('scroll',function(e) {
			if(Browser.Engine.trident4) {
				self.gototop.setStyles({
					'position': 'absolute',
					'bottom': window.getPosition().y + 10,
					'width': this.width
				});
			}
			self.gototop.fade((window.getScroll().y > 300) ? 'in' : 'out');
		});
	}
});

/**
 * wtnRollover
 * @ver 1.0
 * リンク下にある画像をロールオーバーor透過処理させる
 */

var wtnRollover = new Class({
	
	Implements: [Options],

	options: {
		current:true,
		area:"body",
		over:"_on",
		off:"_off"
	},
	initialize: function(options) {
		this.setOptions(options);
		this.overTxt = this.options.over;
		this.offTxt = this.options.off;
		this.imgs =  $$(this.options.area + " a img");
		if(this.imgs) this.Roll();
		return true;
	},
	Roll:function(){
		var self = this;
		self.imgs.each(function(img,index){
			var current;
			if($type(img)=="element"&& !img.hasClass("rollskip")){
				if(self.options.current==true){
					current = self.Current(img);
				}
				img.addEvents({
					"mouseover":function(){
						if(img.src.indexOf(self.offTxt) === -1)
							img.set("tween",{duration: 200}).tween("opacity",[1,0.5]);
						else
							img.src = img.src.replace(self.offTxt,self.overTxt);
						},
					 "mouseleave":function(){
					 	if(img.src.indexOf(self.overTxt) === -1||current==true)
							img.fade(1);
						else
							img.src = img.src.replace(self.overTxt,self.offTxt);
					 }
				});
			}
		});
		return;
	},
	Current:function(img){
		var URL = this.FilePass(document.location.href);
		var Href =  this.FilePass(img.getParent().href);
		if(Href === URL && Href){
			img.src = img.src.replace(this.offTxt,this.overTxt);
			return true;
		}
	},
	FilePass:function(str){
		var FilePass = str.substring(str.lastIndexOf("/")+1,str.length);
		return FilePass;
	}
});

/**
 * wtnLinkIcons
 * @ver 1.3
 * 
 * 別窓を開くアイコンとhrefの拡張子に応じたアイコンや属性を追加
 */

var wtnLinkIcons = new Class({					
	Implements: [Options],		
	options: {
		//search area
		areaID:"",
		//External Link OPTIONS
		ExternalLink:true,
		linkTitle:"リンクを新しいウインドウで開く",
		addExtIconClass:"exiAdd", //AddExternalIcon
		open:"blank",
		excludeStr:[],
		//Filetype Icon OPTIONS
		FiletypeIcon:true,
		ImagetypeIcon:false,//PNG JPG GIF
		media:["pdf","doc","zip","xls","mov","mp3","psd","swf","txt","wmv",".ai","exe","ppt","mpg","flv"],
		iconType:"gif",
		iconDir:"images/filetype/",
		iconW:16,
		iconH:16,
		//USE QuickBox OPTIONS
		QuickBox:false
		
	},
	initialize: function(options) {
		this.setOptions(options);
		this.exStr = this.options.excludeStr;
		this.addExI = this.options.addExtIconClass;
		this.media = this.options.media;
		this.iconD = this.options.iconDir;
		this.iconT = this.options.iconType;
		this.iconW = this.options.iconW;
		this.iconH = this.options.iconH;
		this.linkT = this.options.linkTitle;
		this.imgR = this.options.imgRel;
		this.FtI = this.options.FiletypeIcon;
		this.QuickBox = this.options.QuickBox;
		this.External = this.options.ExternalLink;
		this.gallery = false;
		this.gNum = 0;
		if(this.options.areaID =="")
			this.area = $(document.body);
		else
			this.area = $(this.options.areaID);
			
		this.area.getElements('a[href]').each(this.addIcon,this);
		return true;
	},
	
	addExternal:function(a){
		if(!this.External) return;
		if(this.options.open == "js"){
			var ANC = new Element("a",{"href":"#","title":this.linkT,"class":"newWin",events:{"click":function(){window.open(a.href);return false;}}});
		}else{
			var ANC = new Element("a",{"href":a.href,"title":this.linkT,"target":"_blank", "class":"newWin"});
		}
		ANC.set("html",'<img src="'+this.iconD +'external.'+this.iconT+'" width="'+this.iconW+'" height="'+this.iconH+'" alt="" />')
		ANC.inject(a,"after");
	},
	
	addArgs:function(qb,a){
		if(qb==1){//USE QuickBox
			var rel = a.getProperty("rel");
			var gal = ""; rel = (rel!=null)? " "+rel:"";
			//if((a.hasClass("start")&&!this.gallery)||this.gallery){ gal ="[img"+this.gNum+"]"; this.gallery = true; }
			//if(a.hasClass("end")){ gal ="[img"+this.gNum+"]"; this.gNum++; this.gallery = null; }
			a.setProperty("rel","quickbox"+gal+rel);
		}else{
			if(!this.External) return;
			if(this.options.open == "js"){a.addEvent("click",function(){window.open(this.href);return false;});}
			else{a.setProperty("target","_blank");}
		}
	},
	
	createIcon:function(tn,alt){
		return new Element("img",{
			"class":"icon",
			"src":this.iconD+tn+"."+this.iconT,
			"width":this.iconW,
			"height":this.iconH,
			"alt":alt
		});
	},
	
	addIcon:function(item,index){
		var self = this;
		var h = item.href;
		var t = h.replace(h.slice(0, -3), "").toLowerCase();
		var txt = item.get("text");
		var pct = item.getChildren("img");
		var icoskip = exiskip = expAdd = exiAdd = false;
		var prop = item.getProperties("target","onclick","Class");
		if(this.options.ImagetypeIcon==true){this.media.extend(["jpg","gif","png"]);}
		//Skip
		if(item.hasClass("icoSkip")||item.hasClass("jbiwnjtorzbrnezsjqya")){icoskip = true;}
		if(item.hasClass("exiSkip")||prop.target||(prop.onclick&&prop.onclick.indexOf("window.open")!==-1)||item.getParent().tagName=="OBJECT"||item.hasClass("jbiwnjtorzbrnezsjqya")){exiskip = true;}
		//Add
		if(item.hasClass("expAdd")){expAdd = true;}
		if(item.hasClass(this.addExI)){exiAdd =true;}
		//excludeString Check
		var exclude = this.exStr.some(function(str){ if(h.indexOf(str) !== -1) return true; })+$chk(h.match(/\.gif$|\.png$|\.jpg$|\.doc$|\.psd$|\.zip$|\.xls$|\.ai$|\.exe$|^mailto:|window\.print\(\)/));
		//Images Link
		if(t=="jpg"||t=="gif"||t=="png"){
			if(this.QuickBox===true){
				icoskip = true; self.addArgs(1,item);
			//Pict only & QuickBox OFF
			}else if (txt=="" && this.QuickBox===false){
				icoskip = true;self.addArgs(0,item);
			}else if(this.QuickBox===false){//
				self.addExternal(item);
			}
		}else if(!exiskip){
			//Add External
			if(!exiskip&&expAdd||expAdd){
				self.addArgs(2,item);
			}else{
				if(t=="txt"||t=="pdf"||t=="swf"||t=="mpg"||t=="mp3"||(h!="" && !exclude &&pct.length==0)||exiAdd){
					self.addExternal(item);
				}else if((txt!= ""&&pct.length!=0&& h!=""&& !exclude)||(txt==""&& pct.length!=0 &&h!=""&& !exclude)){
					self.addArgs(2,item);
				}
			}
		}
		//Add Filetype Icon
		if(!icoskip && this.FtI == true){
			if(prop.Class&&prop.Class.indexOf("icoAdd")!==-1){
				var n = prop.Class.slice(7);
				self.createIcon(n.toLowerCase(),n.toUpperCase() + " File").inject(item,"before");
			}
			//AddIconType Array Check
			if (this.media.contains(t)) {
				if(t.slice(0, 1)=="."){t=t.replace(".", "");}//2length Name
				self.createIcon(t,t.toUpperCase() + " File").inject(item,"before");
			}else if(item.protocol=="mailto:"){
				self.createIcon("email","Mail").inject(item,"before");
			}else if(item.protocol==="https:"){
				self.createIcon("https","SSL Page").inject(item,"before");
			}else if(h=="javascript:window.print()"){
				self.createIcon("print","Print").inject(item,"before");
			}
		}
	}
});
var mooZebra = new Class({
	
	Implements: [Options],

	options: {
		hilight:false,
		skip:2,
		start:2
	},
	initialize: function(element,options) {
		this.setOptions(options);
		if(element==null) this.element = $("stripe");
		else this.element = element;
		this.childs = null;
		this.zebra();
	},
	
	zebra:function(){
		this.childs = this.element.getChildren();
		
		this.childs.each(function(el){
			switch(el.get("tag")){
				case "tbody":
					this.table(el);
				break;
				
				case "li":
					this.list(this.childs);
				break;
			}
		}.bind(this));
	},
	bright:function(all){
		all.each(function(tr,row){
			tr.addEvents({
				mouseover:function(e){
					tr.addClass("bright");
				},
				mouseleave:function(e){
					tr.removeClass("bright");
				}
			});
		});
	},
	table:function(el){
		var all = el.getChildren();
		var trs = all.slice(this.options.start-1);
		if(this.options.hilight== true) this.bright(all);
		trs.each(function(tr,row){
			if(row % this.options.skip == 0){
				var tds = tr.getChildren();
				tr.addClass("zebra");
			}
		}.bind(this));
	},
	list:function(el){
		var lis = el.slice(this.options.start-1);
		if(this.options.hilight== true) this.bright(el);
		lis.each(function(li,row){
			if(row % this.options.skip == 0){
				li.addClass("zebra");
			}
		}.bind(this));
	}
});
