/**
 * 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://tono-seibu.org/";
 
 
window.addEvent("domready",function(){		 
	new wtnFontSize();
	new mooPingfix();
	new wtnRollover({area:"#fontsize"});
	new wtnRollover({area:"#main"});
	new wtnLinkIcons({media:["pdf","doc","zip","xls","mp3","psd","swf","txt","wmv","ppt","flv"],excludeStr:[DOMAIN,"tono-seibu.org"],iconDir:"http://tono-seibu.org/images/filetype/",iconType:"png",QuickBox:true,open:"blank"});
	
	new SmoothScroll({duration:500});
	
	/* go to top */
	var gototop = $('gototop');
	gototop.set('opacity','0').setStyle('display','block');
	window.addEvent('scroll',function(e) {
		if(Browser.Engine.trident4) {
			gototop.setStyles({
				'position': 'absolute',
				'bottom': window.getPosition().y + 10,
				'width': 100
			});
		}
		gototop.fade((window.getScroll().y > 300) ? 'in' : 'out')
	});
});


/** QuickBox Plugin(有効にする場合は以下２行のコメントを外してください）*/
//new Asset.javascript(DOMAIN+'js/QuickBox.js');
//new Asset.css(DOMAIN+"css/quickbox.css",{ "media":"screen"});

/** 
 * Google Ajax API 
 * 
 * 使用したい機能のコメントを外し、取得したAPIキーを設定してください
 *
 */
 
var gAPIkey = "ABQIAAAAwrzVu2iAWtCM12EpP4ZXHBRizo_C6ECxJ28VOA1WTppzsVID9BRn8M9k7w_DREPM9oZiBeQDWMwFxQ";
var gs,gm,gf;


/** Gsearch */

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

new Asset.javascript('http://www.google.com/jsapi?callback=googleAPIloadings&key='+gAPIkey,{charset:"utf-8"});
function googleAPIloadings(){
	if(gs==true){google.load('search', '1',{"callback" : searchLoaded});}
}



/**
 * wtnRollover
 * @ver 1.0
 */

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

	options: {
		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){
			if($type(img)=="element"&& !img.hasClass("rollskip")){
				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 )
							img.fade(1);
						else
							img.src = img.src.replace(self.overTxt,self.offTxt);
					 }
				});
			}
		});
		return;
	}
});


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:"png",
		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")){icoskip = true;}
		if(item.hasClass("exiSkip")||prop.target||(prop.onClick&&prop.onclick.indexOf("window.open")!==-1)){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{
			//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");
			}
		}
	}
});

/**
 * mooPingFix
 * @ver 1.0
 */

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

	options: {
		areaID:null
	},
	initialize: function(options) {
		if(Browser.Engine.trident && Browser.Engine.version < 5){
			this.setOptions(options);
			if(this.options.areaID !== null) this.area = $(this.options.areaID);
			else this.area = $(document.body);
			this.anchers = null;
			this.getAncs();
			if(this.anchers) this.createFix();
		}
		return true;
	},
	getAncs:function(){
		this.anchers = this.area.getElements("img").filter(function(el,index){
			if(el.getProperty("src").test("fix.png")){return el;}else{return null;}
		});
	},
	createFix:function(){
		
		this.anchers.each(function(el,index){
			var parent = el.getParent();
			var Props = el.getProperties('src','alt','width','height');
			var text = Props.alt ?  Props.alt : "&nbsp;";
			var span = new Element("span",{"html":text,styles:{"display":"inline-block","text-indent":"-9999","width":Props.width+"px","height":Props.height+"px","filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+Props.src+"', sizingMethod='scale');"}});
			var eClass = el.getProperty("class");
			if(eClass != null) span.setProperty("class",eClass);
			if(parent.tagName == "A") span.setStyle("cursor","pointer");
			span.inject(el,"after");
			el.dispose();
		});
	}
});

/**
 * wtnFontSize
 * @ver 1.0
 */
var wtnFontSize = new Class({
	
	Implements: [Options],

	options: {
		Small:11,
		SmallIE:"x-small",
		Midium:13,
		MidiumIE:"small",
		Large:16,
		LargeIE:"large"
	},
	initialize: function(panel,options) {
		this.setOptions(options);
		if(panel==null) this.panel = $("fontsize");
		else this.panel = panel;
		
		this.BtnS = this.panel.getElement("a#small");
		this.BtnM = this.panel.getElement("a#midium");
		this.BtnL = this.panel.getElement("a#large");
		
		this.cookie = null;
		this.Selected(Cookie.read("mooFontSize"));
		this.Action();
		this.domain = location.href.match(/^[httpsfile]+:\/{2,3}([0-9a-z\.\-:]+?):?[0-9]*?\//i)[1];
		return true;
	},
	Selected:function(val){
		switch(val){
			case "small":
				this.Smalls();
				break;
			case "midium":
				this.Midiums();
				break;
			case "large":
				this.Larges();
				break;
			default:
				return null;
		}
		return null;
	},
	Action:function(){
		var self = this;
		self.BtnS.addEvent("click",function(){self.Smalls();});
		self.BtnM.addEvent("click",function(){self.Midiums();});
		self.BtnL.addEvent("click",function(){self.Larges();});
	},
	btnClass:function(){
		
	},
	setCookie:function(select){
		Cookie.write('mooFontSize',select, {domain:this.domain, duration: 30});
	},
	Smalls: function(){
		if(Browser.Engine.trident)
			document.body.style.fontSize = this.options.SmallIE;
		else
			$$("body").setProperty("style","font-size:"+this.options.Small+"px;");
		this.setCookie("small");
	},
	Midiums: function(){
		if(Browser.Engine.trident)
			document.body.style.fontSize = this.options.MidiumIE;
		else
			$$("body").setProperty("style","font-size:"+this.options.Midium+"px");
		this.setCookie("midium");
	},
	Larges: function(){
		if(Browser.Engine.trident)
			document.body.style.fontSize = this.options.LargeIE;
		else
			$$("body").setProperty("style","font-size:"+this.options.Large+"px");
		this.setCookie("large");
	}
});
