//config
var conf={
	dir:''
	,cookFdomain:''
};

//rollover
function bindHover(){
	var keyName='rollover';
	var normalSfx='_off.';
	var overedSfx='_on.';
	var imgcache;
	$('img[src*="'+normalSfx+'"], input[src*="'+normalSfx+'"]').each(function(){
		if($.data(this,keyName))return;
		var obj={};
		obj.orig_src=this.src;
		obj.over_src=this.src.replace(normalSfx,overedSfx);
		imgcache=new Image();
		imgcache.src=obj.over_src;
		$.data(this,keyName,obj);
		$(this)
			.bind('mouseover',function(){
				$(this).attr('src',$.data(this,keyName).over_src);
			})
			.bind('mouseout',function(){
				$(this).attr('src',$.data(this,keyName).orig_src);
			});
	});
}

//browser ver
var g_browser=(function(){
	var o={};
	var ua=navigator.userAgent;
	var apn=navigator.appName;
	var apv=navigator.appVersion;
	o.isWin9X = (apv.search(/windows 98/i)>-1);
	o.isIE = (apn.search(/internet explorer/i)>-1);
	o.isOpera = (ua.search(/opera/i)>-1);
	if(o.isOpera)o.isIE=false;
	o.isSafari = (apv.search(/safari/i)>-1);
	o.AndroidMobile = ((ua.search(/android/i)>-1) && (ua.search(/mobile/i)>-1));
	o.AndroidTablet = ((ua.search(/android/i)>-1) && !(ua.search(/mobile/i)>-1));
	o.iPhone = (ua.search(/iphone/i)>-1);
	o.iPod = (ua.search(/ipod/i)>-1);
	o.iPad = (ua.search(/ipad/i)>-1);
	o.SmartPhone = (ua.search(/webkit/i)>-1&&ua.search(/mobile/i)>-1);
	if(o.iPad)o.SmartPhone=false;
	o.iev=-1;
	if(apn=='Microsoft Internet Explorer'){
		var re=new RegExp('MSIE ([0-9]{1,}[\.0-9]{0,})');
		if(re.exec(ua)!=null)o.iev=parseFloat(RegExp.$1);
	}
	return o;
})();

//date I/F
var DateIF={
	yobi:'日 月 火 水 木 金 土'.split(' '),
	yobiE:'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' '),
	tukiE:'January February March April May June July August September October November December'.split(' '),
	parse:function(str){
		var ret=false;
		if(!str)return false;
		if(str.match(/(¥d{4})(¥d{2})(¥d{2})(¥d{2})(¥d{2})/)){
			ret = new Date( parseInt(RegExp.$1,10), parseInt(RegExp.$2,10)-1, parseInt(RegExp.$3,10), parseInt(RegExp.$4,10), parseInt(RegExp.$5,10) );
		}
		else if(str.match(/(¥d{4})(¥d{2})(¥d{2})/)){
			ret = new Date( parseInt(RegExp.$1,10), parseInt(RegExp.$2,10)-1, parseInt(RegExp.$3,10) );
		}
		else if(str.match( /(¥d{4})¥D(¥d{1,2})¥D(¥d{1,2})¥D(¥d{1,2})¥D(¥d{1,2})¥D(¥d{1,2})/ )){
			ret = new Date( parseInt(RegExp.$1,10), parseInt(RegExp.$2,10)-1, parseInt(RegExp.$3,10), parseInt(RegExp.$4,10), parseInt(RegExp.$5,10), parseInt(RegExp.$6,10) );
		}
		else if(str.match( /(¥d{4})¥D(¥d{1,2})¥D(¥d{1,2})¥D(¥d{1,2})¥D(¥d{1,2})/ )){
			ret = new Date( parseInt(RegExp.$1,10), parseInt(RegExp.$2,10)-1, parseInt(RegExp.$3,10), parseInt(RegExp.$4,10), parseInt(RegExp.$5,10) );
		}
		else if(str.match( /(¥d{4})¥D(¥d{1,2})¥D(¥d{1,2})/ )){
			ret = new Date( parseInt(RegExp.$1,10), parseInt(RegExp.$2,10)-1, parseInt(RegExp.$3,10) );
		}
		return ret;
	},
	format:function(fmt, d){
		var t={};
		t.Y = fmt.match(/Y+/);
		if(t.Y)fmt = fmt.replace( /Y+/g, d.getFullYear().toString().slice(4-t.Y[0].length) );
		t.M = fmt.match(/M+/);
		if(t.M)fmt = fmt.replace( /M+/g, this.zeroPad(d.getMonth()+1, t.M[0].length) );
		t.D = fmt.match(/D+/);
		if(t.D)fmt = fmt.replace( /D+/g, this.zeroPad(d.getDate(), t.D[0].length) );
		fmt = fmt.replace( /a+/g, this.yobi[d.getDay()] );
		var hh=d.getHours();
		var hf=((hh-12)>=0)?1:0;
		fmt = fmt.replace( /A+/g, Array('AM','PM')[hf] );
		fmt = fmt.replace( /G+/g, Array('午前','午後')[hf] );
		t.h = fmt.match(/h+/);
		if(t.h)fmt = fmt.replace( /h+/g, this.zeroPad(hh, t.h[0].length) );
		t.n = fmt.match(/n+/);
		if(t.n)fmt = fmt.replace( /n+/g, this.zeroPad( Array(hh,hh-12)[hf], t.n[0].length) );
		t.m = fmt.match(/m+/);
		if(t.m)fmt = fmt.replace( /m+/g, this.zeroPad(d.getMinutes(), t.m[0].length) );
		t.s = fmt.match(/s+/);
		if(t.s)fmt = fmt.replace( /s+/g, this.zeroPad(d.getSeconds(), t.s[0].length) );

		fmt = fmt.replace( /K+/g, this.tukiE[d.getMonth()] );
		fmt = fmt.replace( /k+/g, this.tukiE[d.getMonth()].substring(0,3) );
		fmt = fmt.replace( /X+/g, this.yobiE[d.getDay()] );
		fmt = fmt.replace( /x+/g, this.yobiE[d.getDay()].substring(0,3) );
		return fmt;
	},
	reformat:function(fmt,dstr){
		var dt=this.parse(dstr);
		if(dt){
			return this.format(fmt, dt);
		}
		else return false;
	},
	getFirstDay:function(d){
		return this.parse( this.format('YYYY/MM/01',d) );
	},
	getLastDay:function(d){
		var s=this.format('M',d);
		for(var i=29;i<33;i++){
			if(s!=this.format('M',this.parse(this.format('YYYY/MM/'+i,d)))){
				return i-1;
			}
		}
	},
	zeroPad:function(s,l){
		s=s.toString();
		while(s.length<l){
			s='0'+s;
		}
		return s;
	},
	addDate:function(d, add){
		var ad={Y:0,M:0,D:0,h:0,m:0,s:0};
		for(var i in add){
			ad[i]=add[i];
		}
		return (new Date(d.getFullYear()+ad.Y, d.getMonth()+ad.M, d.getDate()+ad.D, d.getHours()+ad.h, d.getMinutes()+ad.m, d.getSeconds()+ad.s));
	},
	setDate:function(d, set){
		var se={Y:d.getFullYear(),M:d.getMonth(),D:d.getDate(),h:d.getHours(),m:d.getMinutes(),s:d.getSeconds()};
		for(var i in set){
			if(i=='M'){se[i]=set[i]+1;}
			else se[i]=set[i];
		}
		return new Date(se.Y, se.M, se.D, se.h, se.d, se.s);
	},
	timeDelete:function(d){
		return this.parse(this.format('YYYY/MM/DD',d));
	},
	diffDate:function(d1,d2){
		return (this.timeDelete(d1)-this.timeDelete(d2))/86400000;
	}
};

//cookie
var cookF={
	 setup:function(opt){
		this.expires=opt.expires||undefined;
		this.domain=opt.domain||undefined;
		this.path=opt.path||undefined;
		this.secure=opt.secure||undefined;
	}
	,overRide:function(opt){
		var o={};
		o.expires=opt.expires||(this.expires||undefined);
		o.domain=opt.domain||(this.domain||undefined);
		o.path=opt.path||(this.path||undefined);
		o.secure=opt.secure||(this.secure||undefined);
		return o;
	}
	,set:function(key, val, opt){
		var t='';
		t+= key+'=';
		t+= encodeURIComponent(val)+';';
		var o=this.overRide(opt);
		if(o.expires){t+='expires='+this.getGMT(o.expires)+';';}
		if(o.domain){t+='domain='+o.domain+';';}
		if(o.path){t+='path='+o.path+';';}else{t+='path=/;';}
		if(o.secure){t+='secure';}
		document.cookie=t;
	}
	,get:function(key){
		var t=document.cookie.split(';');
		var u;
		for(var i=0;i<t.length;i++){
			u=t[i].split('=');
			if(this.trim(u[0])==key){
				return decodeURIComponent(this.trim(u[1]));
			}
		}
		return '';
	}
	,del:function(key){
		var o=this.overRide({expires:-365});
		this.set(key, '', o);
	}
	,getGMT:function(d){
		if(d.constructor==Date){
			return d.toGMTString();
		}
		var exp;
		exp=new Date();
		exp.setTime(exp.getTime()+1000*60*60*24*d);
		return exp.toGMTString();
	}
	,trim:function(s){
		return s.replace(/^ +| +$/g, '');
	}
	,check:function(){
		//return navigator.cookieEnabled;
		var chk='checkstring';
		var ck='cookiecheck';
		this.set(ck,chk,{});
		if( this.get(ck) === chk ){
			this.del(ck);
			return true;
		}
		return false;
	}
};
cookF.setup({
	domain: conf.cookFdomain
});

//smoothScroll
function smoothScroll(_spd){
	if(!_spd)_spd=400;
	$('a[href*=#]').live('click',function(){
		var $target=$( this.hash );
		$target=$target.length&&$target||$('[name='+this.hash.slice(1)+']');
		if($target.length){
			var targetOffset=$target.offset().top;
			$('html,body').animate({
				scrollTop: targetOffset
			},_spd);
		}
		return false;
	});
}

//shuffle
Array.prototype.shuffle = function() {
	var i = this.length;
	while(i){
		var j = Math.floor(Math.random()*i);
		var t = this[--i];
		this[i] = this[j];
		this[j] = t;
	}
	return this;
}
