﻿var $luUtil = {
	/* ------------------------------------------------------------- */
	/* START $class */
	/* classを配列で返す */
	/* ------------------------------------------------------------- */
	getClass : function(classname,tagname,root){
		//classname : 検索するclassName
		//tagname　：　対象のタグ名　省略可
		//root　:　どのelementの中か　省略可
		if(!root) root = document;
		if(!tagname) tagname = '*';
		var all = root.getElementsByTagName(tagname);
		if(!classname) return all;
		
		var elements = [];
		for(var i=0;i<all.length;i++){
			var element = all[i];
			if(isMember(element,classname)) elements.push(element);
		}
		
		return elements;
		
		function isMember(element,classname){
			var classes = element.className;
			if(!classes) return false;
			if(classes == classname) return true;
			
			var whitespace = ' ';
			if(classes.indexOf(whitespace) == -1) return false;
			
			var c = classes.split(whitespace);
			for(var i = 0;i < c.length;i++){
				if(c[i] == classname) return true
			}
			return false;
		}
	},
	//getElementById
	id : function(idName){
		return document.getElementById(idName);
	},
	//getElementsByTagName
	tag : function(elm,tagName){
		return elm.getElementsByTagName(tagName);
	},
	/* ------------------------------------------------------------- */
	/* START EventListener */
	/* リスナーの追加と削除 */
	/* ------------------------------------------------------------- */
	addEventListener : function(elm,type,func,useCapture){
							if(!elm){return false}
							if(!useCapture){useCapture = false;}
							if(elm.addEventListener){
								elm.addEventListener(type,func,useCapture);
							}else if(elm.attachEvent){
								elm.attachEvent('on' + type,func);
							}else{return false;}
							return true;
						},
	removeEventListener : function(elm,type,func,useCapture){
							if(!elm){return false}
							if(!useCapture){useCapture = false;}
							if(elm.removeEventListener){
								elm.removeEventListener(type,func,useCapture);
							}else if(elm.detachEvent){
								elm.detachEvent('on' + type,func);
							}else{return false}
							return true;
						},
	/* ------------------------------------------------------------- */
	/* START target */
	/* イベント発生源の要素ノードオブジェクトを取得 */
	/* ------------------------------------------------------------- */
	target : function(evt){
				if(evt && evt.target){
					if(evt.target.nodeType == 3){
						return evt.target.parentNode;
					}else{
						return evt.target;
					}
				}else if(window.event && window.event.srcElement){
					return window.event.srcElement;
				}else{
					return null;
				}
			},
	/* ------------------------------------------------------------- */
	/* START preventDefault */
	/* デフォルトアクション禁止 */
	/* ------------------------------------------------------------- */
	preventDefault : function(evt){
						if(evt && evt.preventDefault){
							evt.preventDefault();
							evt.currentTarget['on' + evt.type] = function(){return false};
						}else if(window.event){
							window.event.returnValue = false;
						}
					},
	/* ------------------------------------------------------------- */
	/* START preventDefault */
	/* 伝播抑止 */
	/* ------------------------------------------------------------- */
	stopPropagation : function(evt){
						if(evt && evt.stopPropagation){
							evt.stopPropagation();
						}else if(window.event){
							window.event.cancelBubble = true;
						}
					},
	/* ------------------------------------------------*/
	//HTTP通信
	/* ------------------------------------------------*/
	http : {
		_factories : [
					function(){return new XMLHttpRequest();},
					function(){return new ActiveXObject("Msxml2.XMLHTTP");},
					function(){return new ActiveObject("Microsoft.XMLHTTP")}
				],
					
		_factory : null,
			
		newRequest : function(){
			if(this._factory != null) return this._factory();
			
			for(var i = 0;i < this._factories.length;i++){
				try{
					var factory = this._factories[i];
					var request = factory();
					if(request != null){
						this._factory = factory;
						return request;
					}
				}
				catch(e){
					continue;
				}
			}
			//エラー処理

			this._factory = function(){
				throw new Error("XMLHttpRequest not supported");
			}
			this._factory();
		},
		
		dataRequest : function(url,callback){
			var request = this.newRequest();
			request.onreadystatechange = function(){
				if(request.readyState==4 && request.status == 200){
					callback(request)
				}
			}
			request.open("GET",url);
			request.send(null);
		}
	}
};
/* ------------------------------------------------------------- */
/* START userAgent */
/* ユーザーエージェント */
/* ------------------------------------------------------------- */
$luUtil.ua = navigator.userAgent;
$luUtil.safari = ($luUtil.ua.indexOf("Safari") >= 0);
$luUtil.opera = ($luUtil.ua.indexOf("Opera") >= 0);
$luUtil.ie = ($luUtil.ua.indexOf("MSIE") >= 0);
$luUtil.ns = ($luUtil.ua.indexOf("Netscape") >= 0);
$luUtil.firefox = ($luUtil.ua.indexOf("Gecko") >= 0);
/* ------------------------------------------------*/
//フェードイン・アウト
/* ------------------------------------------------*/
$luUtil.Fade = function(id,alpha){
	//id:String ターゲットのID
	//alpha:String IN:stop値 / OUT:start値
	this.id = (typeof id == 'string')? $util.id(id) : id;
	this.alpha = alpha;	
	this.ieAlpha=(this.alpha != 1)? this.alpha.split('.')[1] + '0':'100';
	this.stopAlpha_opera = this.alpha + 0;
}
$luUtil.Fade.prototype.fadeIn = function(value,func){
	//value:String 透過の早さ
	//func：Function　alpha処理後に実行　[省略可]
	var instance = this;
	this.value = value;
	this.ieValue=(this.value != 1)? (this.value.length == 4)?this.value.charAt(3):this.value.split('.')[1] + '0':'100';
	this.func = func;
	this.id.style.opacity = 0;
	this.id.style.filter = 'alpha(style=0, opacity=0)';
	this.id.style.display = 'block';
	if($luUtil.ns){
			if(this.func){this.func.apply(this,[])}
		}else if($luUtil.ie){
			var counter = 0;
			var __closeAlpha = function(){
				instance.id.style.filter = 'alpha(style=0, opacity='+ counter +')';
				if(counter >= instance.ieAlpha){
					clearInterval(closeAlphaId);
					if(instance.func){instance.func.apply(instance,[])}
				}
				counter -= -instance.ieValue;
			}
			var closeAlphaId = setInterval(__closeAlpha,5);
		}else{
			//alert(instance.id + '\n' + instance.id.style.opacity + '\nstopAlpha_opera:' + instance.stopAlpha_opera);
			var __openAlpha = function(){
				instance.id.style.opacity -= -instance.value;
				//alert('instance.id.style.opacity：' + instance.id.style.opacity + '\n' + 'instance.value:' + instance.value)
				if(instance.id.style.opacity >= instance.alpha || instance.id.style.opacity >= instance.stopAlpha_opera){
					clearInterval(openAlphaId);
					if(instance.func){instance.func.apply(instance,[])}
				}
			}
			var openAlphaId = setInterval(__openAlpha,5);
		}
}
$luUtil.Fade.prototype.fadeOut = function(value,func){
	//value:String 透過の早さ
	//func：Function　alpha処理後に実行　[省略可]
	var instance = this;
	this.value = value;
	this.ieValue=(this.value != 1)? (this.value.length == 4)?this.value.charAt(3):this.value.split('.')[1] + '0':'100';
	this.func = func;
	instance.id.style.opacity = this.alpha;
	instance.id.style.filter = 'alpha(style=0, opacity=' + this.ieAlpha +')';
	if($luUtil.ns){
			this.id.style.display = 'none';
			if(this.func){this.func.apply(this,[])}
		}else if($luUtil.ie){
			var counter = instance.ieAlpha;
			var __closeAlpha = function(){
				instance.id.style.filter = 'alpha(style=0, opacity='+ counter +')';
				if(counter <= 0){
					clearInterval(closeAlphaId);
					if(instance.func){instance.func.apply(instance,[])}
				}
				counter -= instance.ieValue;
			}
			var closeAlphaId = setInterval(__closeAlpha,5);
		}else{
			//alert(id + '\n' + instance.id.style.opacity + '\nstopAlpha_opera:' + stopAlpha_opera);
			var __closeAlpha = function(){
				instance.id.style.opacity -= instance.value;
				if(instance.id.style.opacity <= 0){
					clearInterval(closeAlphaId);
					if(instance.func){instance.func.apply(instance,[])}
				}
			}
			var closeAlphaId = setInterval(__closeAlpha,5);
		}
}
/* ------------------------------------------------*/
//Cookieコンストラクタ
/* ------------------------------------------------*/
$luUtil.Cookie = function(name){
	this.$name = name;
	var allcookies = document.cookie;
	if(allcookies == '') return;
	
	var cookies = allcookies.split('; ');
	var cookie = null;
	for(var i = 0;i < cookies.length;i++){
		if(cookies[i].substring(0,name.length + 1) == (name + '=')){
			cookie = cookies[i];
			break;
		}
	}
	if(cookie == null) return;
	
	var cookieval = cookie.substring(name.length+1);
	
	var a = cookieval.split('&');
	for(var i = 0;i < a.length;i++){
		a[i] = a[i].split(':');
	}
	for(var i = 0;i < a.length;i++){
		this[a[i][0]] = decodeURIComponent(a[i][1]);
	}
}

$luUtil.Cookie.prototype.store = function(daysToLive,path,domain,secure){
	var cookieval = '';
	for(var prop in this){
		if((prop.charAt(0) == '$') || (typeof this[prop]) == 'function'){continue};
		if(cookieval != '') cookieval += '&';
		cookieval += prop + ':' + encodeURIComponent(this[prop]);
	}
	
	var cookie = this.$name + '=' +cookieval;
	if(daysToLive || daysToLive ==0){
		cookie += ';max-age=' + (daysToLive*24*60*60);
	}
	if(path) cookie += ';path=' + path;
	if(domain) cookie += ';domain=' + domain;
	if(secure) cookie += ';secure=' + secure;
	
	document.cookie = cookie;
}
$luUtil.Cookie.prototype.remove = function(path,domain,secure){
	for(var prop in this){
		if(prop.charAt(0) != '$' && typeof this[prop] != 'function')
		delete this[prop]
	}
	this.store(0,path,domain,secure)
}


$luUtil.loaded =true;