/* 
[Create Name Space]
PG === Photo Gallery  
*/

var PG = function(){};

/* [Create Common] */

PG.Common = function(){};

PG.Common.prototype ={
	//ブラウザ
	getUA : function(){
		if(navigator.userAgent.indexOf("MSIE") !== -1){
			return "MSIE"	
		}
		else if(navigator.userAgent.indexOf("Firefox") !== -1){
			return "Firefox"
		}
		else if(navigator.userAgent.indexOf("Safari") !== -1){
			return "Safari"
		}
		else if(navigator.userAgent.indexOf("Opera") !== -1){
			return "Opera"
		}
		else if(navigator.userAgent.indexOf("Netscape") !== -1){ 
			return "Netscape"
		}
		
	},
	//全要素取得
	getAllElms : function(){
		var allElms = document.body.getElementsByTagName("*");
		return allElms;
	},
	getElms : function(obj){
		var Elms = obj.getElementsByTagName("*");
		return Elms;
	},
	//DOM構築後に実行
	getElmLoad : function(id,fn){

			loadTimer = setInterval(function(){	
				var IDElm = document.getElementById(id);
				
				if(IDElm !== null){
					clearInterval(loadTimer);
					fn();
				}

			},1);
	},
	//イベント実行
	addEvents : function(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	},
	//イベント発生元取得
	getSource : function(e){
		if(e.target){
			return e.target;
		}else if(window.event){
			return window.event.srcElement;
		}

	},
	//イベントの行き先
	getTo : function(e){
		if(e.relatedTarget){
			return e.relatedTarget;
		}else if(window.event){
			return window.event.toElement;
		}

	},
	//イベントの移動元
	getFrom : function(e){
		if(e.relatedTarget){
			return e.relatedTarget;
		}else if(window.event){
			return window.event.fromElement;
		}

	},
	//デフォルトイベントをキャンセル
	preventDefault : function(e){
	  if(e.preventDefault){
	    e.preventDefault();
	  }
	  else if(window.event){
	    window.event.returnValue = false;
	  }
	},
	//完全一致をチェック
	checkString : function(string,match){
		var myReg = new RegExp("^" + match + "$");
		if(string.match(myReg) !== null){
			return true;
		}
	},
	//全要素中からクラスを取得
	getSelectClass : function(classname){
		var objects = [];
		var allTags = this.getAllElms();

		for (var i = 0; i < allTags.length; i ++) {
			var names = allTags[i].className;
			if ((names !== undefined) && (names !== "")) {
				var nameSplits = names.split(" ");
				for (var j in nameSplits) {
					if(this.checkString(nameSplits[j],classname)){
						objects.push(allTags[i]);
					}
				}
			}
		}

		if(objects.length === 0){
			return false;
		}else{
			return objects;
		}
	},
	//子要素の中からクラスを取得
	getChildClass : function(obj,classname){
		var objects = [];
		var objts = this.getElms(obj);

		for  (var i = 0; i < objts.length; i ++) {
			var names = objts[i].className;
			if ((names !== undefined) && (names !== "")) {
				var nameSplits = names.split(" ");
				for (var j in nameSplits) {
					if(this.checkString(nameSplits[j],classname)){
						objects.push(objts[i]);
					}
				}
			}
		}
		
		if(objects.length === 0){
			return false;
		}else{
			return objects;
		}

	},
	//classNameから指定のクラスを取得
	getClassName : function(obj,classname){
		var check = false;

		var names = obj.className.split(" ");

		for (var i in names) {
			if(this.checkString(names[i],classname)){
				 check = true;
			}
		}
		return check;
	},
	//子要素を取得
	getChildNodes: function(obj){

		var objects = [];
		for (var i = 0; i < obj.childNodes.length; i++) {
			if (obj.childNodes[i].nodeType != 3) {
					objects.push(obj.childNodes[i]);
				}
		}

		if(objects.length === 0){
			return false;
		}else{
			return objects;
		}

	},
	//rel属性値の値を取得
	getRelation : function(obj){
		var objects1 = [];
		var objects2 = [];
		for (var i = 0; i < obj.length; i++) {
			var rels = obj[i].getAttribute("rel");
			if(rels !== null){
				var relsDetail = rels.split(" ");
				objects1.push(relsDetail);
			}
		}
		objects2.push(objects1);
		return objects1;
	},
	//指定したオプションの値を取得
	getOptions : function(obj,options){
		for (var i = 0; i < obj.length; i++) {
			var matchObj1 = obj[i].replace(/:(.)+/,"");
			var matchObj2 = obj[i].replace(/(.)+:/,"");
			if( matchObj1 == options){
			return matchObj2;
			}
		}
	},
	//クラスを追加
	setClass : function (element,value){
		var items = element.getAttribute("class");

		if(!element.className){
			element.className = value;
		}else{
			newClassName = element.className;
			newClassName += " ";
			newClassName += value;
			element.className = newClassName;
		}
	},
	//追加したクラスを削除
	removeClass : function(obj,classname){
		var classes = obj.className;
		
		var UA = this.getUA();
				
		if(classes.match(classname) !== null){
			
			if(classes.match(" "+classname) !== null){
				reClass = classes.replace(" "+classname,"");

			}else if(classes.match(classname) !== null){
				reClass = classes.replace(classname,"");
			}
	
			if (reClass === "") {		
				if(UA !== "MSIE"){
					obj.removeAttribute("class");
				}else if(UA === "MSIE"){
					obj.removeAttribute("className");
				}
			}
			else {
				obj.className = reClass;
			}
		}
	},
	//要素に指定されたスタイルを取得
	getStyle : function(element){
		if(element.currentStyle){
			style = element.currentStyle;
		}else if(document.defaultView.getComputedStyle(element, '')){
			style = document.defaultView.getComputedStyle(element, '');
		}
		return style;
	},
	//要素の高さを取得
	getStyleHeight : function(element){
		if(element.currentStyle){
			style = element.currentStyle;
		}else if(document.defaultView.getComputedStyle(element, '')){
			style = document.defaultView.getComputedStyle(element, '');
		}

		var elmStyles = [style.paddingTop,style.paddingBottom,style.borderTopWidth,style.borderBottomWidth];

		var elementHeight = element.offsetHeight;

		for (var i in elmStyles) {
			if(!parseInt(isNaN( parseInt( elmStyles[i] )))){
				elmStyles[i] = elmStyles[i].toString().replace(/px/,"");
				elementHeight -=  Number(elmStyles[i]);

			}

		}
		return elementHeight;

	},
	//配列をシャッフル
	arrayShuffle : function(list){
	  var i = list.length;
	  while (--i) {
	    var j = Math.floor(Math.random() * (i + 1));
	    if (i == j) {continue;}
	    var k = list[i];
	    list[i] = list[j];
	    list[j] = k;
	  }
	  return list;
	},
	//透明度を変更
	changeAlpha : function(obj,pts){
		
		var UA = this.getUA();
		
		if (UA === "MSIE") {
			obj.style.filter = 'alpha(opacity=' + (pts * 10) + ')';
		}else if(UA === "firefox"){
			obj.style.MozOpacity = pts / 10;
		}else{
			obj.style.opacity = pts / 10;
			
		}

		return true;
	},
	//xmlhttp生成
	getXHR : function() {
	  try {
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		return xmlhttp;
	  } catch (e) {
	    try {
	      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e2) {
	      xmlhttp = false;
	    }
	  }
	  if (!xmlhttp && typeof XMLHttpRequest!=='undefined') {
	    xmlhttp = new XMLHttpRequest();
		return xmlhttp;
	  }
	}
};

/*=======================================================

	☆汎用機能一覧

	getAllElms()
	addEvents(elm,listener,fn)
	checkString(string,match)
	getSelectClass(classname)
	getChildClass(obj,classname)
	getChildNodes(obj)
	getRelation(obj)
	getOptions(obj,options)
	setClass(element,value)
	removeClass(obj,classname)
	getStyle(element)
	arrayShuffle(list)
	incrementAlpha(obj,speed)
	orderDisplay(obj,speed)
	changeAlpha(obj,pts)
	getXHR()

=======================================================*/

/* 
 *
 * < Create Common Instance > 
 * 
 */

PG.COM = new PG.Common();

/* [Create Display Switch] */

PG.ClassChange = function(elm,clsname){
	this.elm = elm;
	this.clsname = clsname;
};

PG.ClassChange.prototype = {
	chg_onmouse : function(){
		var clsname = this.clsname;
		var obj = this.elm;
		
		var movement = PG.COM.getChildNodes(obj)[0];
		
		if(PG.COM.getClassName(movement,"balloon-container-01")){
			var amount = "plus";
		}else if(PG.COM.getClassName(movement,"balloon-container-02")){
			var amount = "minus";
		}
		
		var moveStyle = PG.COM.getStyle(movement);
		
		var objAll = PG.COM.getElms(obj);
		
		var count = 0;
		
		PG.COM.addEvents(obj,"mouseover",
			function(e){
				
				if(obj.getAttribute("style") !== null){
					obj.removeAttribute("style");
				}

				
				var currentObj = this;
				
				var chk = false;

				for (var i=0; i<objAll.length; i++) {
					if(PG.COM.getFrom(e) === objAll[i] || PG.COM.getFrom(e) === obj){
						chk = true;
					}
					if (PG.COM.getFrom(e) === null) {
						chk = true;
					}
				}

				if(chk === false){
					
				PG.COM.setClass(obj,clsname);
				
				var nowPos = moveStyle.left.replace(/px/,"");
				
				movement.checkTimer = setInterval(function(){				
					
					if(amount === "plus"){
						count++;
						movement.style.left = Number(nowPos) + count + "px";
					}else if(amount === "minus"){
						count--;
						movement.style.left = Number(nowPos) + count + "px";
					}
															
					if(count == 10 || count == -10){
					clearInterval(movement.checkTimer);
					count = 0;
					}
		
				}, 10);
				
				}
			}
		);
		PG.COM.addEvents(obj,"mouseout",
			function(e){

				var currentObj = this;
				
				var chk = false;
				
				for (var i=0; i<objAll.length; i++) {
					if(PG.COM.getTo(e) === objAll[i] || PG.COM.getTo(e) === obj){
						chk = true;
					}
					
				}
				
				var nowPos = moveStyle.left.replace(/px/,"");

				if(chk === false){
				if(amount === "plus"){
					movement.style.left = 5 + "px";
				}else if(amount === "minus"){
					movement.style.left = -75 + "px";
				}
				PG.COM.removeClass(obj,clsname);
				clearInterval(movement.checkTimer);
				}			
			}
		);
	},
	chg_onclick : function(elm,clickobj){
		var clsname = this.clsname;
		var obj = this.elm;
		
		var chgElm = elm;
		var btn= clickobj;
		
		var clickChk = false;
		
		var chgAlpha = new PG.AnimeAlpha(chgElm,"10");
	
		PG.COM.addEvents(obj,"click",
			function(e){
				
				if (!clickChk) {
					
					clearInterval(obj.checkTimer);

					var num = 2;
					
					PG.COM.changeAlpha(chgElm,0);
					PG.COM.setClass(chgElm, clsname);

					obj.checkTimer = setInterval(function(){
						
						PG.COM.changeAlpha(chgElm,num);
						
						num = num + 2;											
						
						if(num > 10){
						
						clickChk = true;

						clearInterval(obj.checkTimer);
							
						}
			
					}, 10);
					
					PG.COM.preventDefault(e);
					
				}else if(clickChk){
					
					clearInterval(obj.checkTimer);
					
					var num = 10;
					
					checkTimer = setInterval(function(){
						
						PG.COM.changeAlpha(chgElm,num);
						num = num - 2;										
						if(num < 0){

						PG.COM.removeClass(chgElm, clsname);
						clickChk = false;
						clearInterval(checkTimer);
							
						}
			
					}, 10);
					
					PG.COM.preventDefault(e);
				}	
			}
		);
		
		PG.COM.addEvents(btn,"click",
			function(e){
				if (clickChk) {
					
					clearInterval(btn.checkTimer);
					
					var num = 10;
					
					btn.checkTimer = setInterval(function(){
		
						PG.COM.changeAlpha(chgElm,num);
						num = num-2;										
						if(num < 0){

						PG.COM.removeClass(chgElm, clsname);
						clickChk = false;
						clearInterval(btn.checkTimer);
							
						}
					}, 10);
					
					PG.COM.preventDefault(e);

				}else if (!clickChk){
					PG.COM.preventDefault(e);
				}
			}
		);
	}
}

/* [Create Image Alpha] */

PG.ImageAlpaha = function(elm,point){
	this.elm = elm;
	this.point = point;
};


PG.ImageAlpaha.prototype ={

	chg_onmouse : function(){
		var elm = this.elm;
		var point = this.point;
		
		PG.COM.addEvents(elm,"mouseover",
			function(){
				PG.COM.changeAlpha(elm,point);
			}
		);
		
		PG.COM.addEvents(elm,"mouseout",
			function(){
				PG.COM.changeAlpha(elm,"10");
			}
		);
	}
	
}

/* [Create Animation Alpha] */

PG.AnimeAlpha = function(elm,speed){
	this.elm = elm;
	this.speed = speed;
};

PG.AnimeAlpha.prototype ={
	increaseAlpha : function(num,obj){	

		
		return function(){
			return  ++num;
		}

	}
	
}

PG.Animation = function(elm,speed){
	this.elm = elm;
};

PG.Animation.prototype ={
	animationTop: function(speed,count){
		
		var elm = this.elm;
		
		var animeTimer = setInterval(function(){
		
			elm.style.top = count + "px";

			count--;
			
			if (count < 0) {
				elm.removeAttribute("style");
				clearInterval(animeTimer);
			}
			
		}, speed);

	},
	animationAlphaPlus: function(speed,count,UA){
		
		var elm = this.elm;
		var alphaArray = ['alpha(opacity=10)','alpha(opacity=20)','alpha(opacity=30)','alpha(opacity=40)','alpha(opacity=50)','alpha(opacity=60)','alpha(opacity=70)','alpha(opacity=80)','alpha(opacity=90)','alpha(opacity=100)'];	
		
		if (UA === "MSIE") {
			var animeTimer = setInterval(function(){
				elm.style.filter = alphaArray[count];
				count++;
				if (count > 10) {
					clearInterval(animeTimer);
				}
				
			}, speed);

		}else if(UA === "firefox"){
			var animeTimer = setInterval(function(){
				elm.style.MozOpacity = count / 10;
				count++;
				
				if (count > 10) {
					clearInterval(animeTimer);
				}
				
			}, speed);
		}else{
			var animeTimer = setInterval(function(){
				elm.style.opacity = count / 10;
				count++;
				
				if (count > 10) {
					clearInterval(animeTimer);
				}
				
			}, speed);
		}
		
	}
}

/* ============================================================== */

PG.thumbLoading = function(obj){

	var thumb_list = document.getElementById("list-photo-thumb");
	
	var list_child = PG.COM.getChildNodes(thumb_list);
	var list_childLength = list_child.length;

	for (var i=0; i<list_childLength; i++) {
		PG.COM.setClass(list_child[i],"off");
	}
}

/* ============================================================== */

PG.photoThumb = function(ID,clsname){
	var thumb_list = document.getElementById(ID);
	if(!thumb_list){return false;}
	var list_child = PG.COM.getChildNodes(thumb_list);
	var list_childLength = list_child.length;
	
	var shuffleList = PG.COM.arrayShuffle(list_child);
	
	var UA = PG.COM.getUA();

	function thumbDisplay(obj,speed){
		setTimeout(function(){
		obj.style.top = "10px";
		
		

/*
		if (UA === "MSIE") {
			obj.style.filter = 'alpha(opacity=50)';
		}else if(UA === "firefox"){
			obj.style.MozOpacity = 5 / 10;
		}else{
			obj.style.opacity = 5 / 10;
		}
*/

		
		PG.COM.removeClass(obj,"off");
		var anime = new PG.Animation(obj);
		anime.animationTop(10,10);
		//anime.animationAlphaPlus(10,5,UA);
		
		},speed)
	}
	
	for (var i=0; i<list_childLength; i++) {
		thumbDisplay(shuffleList[i],50 * [i]);
		PG.Balloon = new PG.ClassChange(list_child[i],clsname);
		PG.Balloon.chg_onmouse();
	}
}

PG.sitemenu = function(btID,menuID,closeID,clsname){
	var menu_bt = document.getElementById(btID);
	if(!menu_bt){return false;}
	var menu_container = document.getElementById(menuID);
	var close_bt = document.getElementById(closeID).getElementsByTagName("img")[0];

	PG.SiteMenu = new PG.ClassChange(menu_bt,clsname);
	PG.SiteMenu.chg_onclick(menu_container,close_bt);
}

PG.COM.addEvents(window,"load",
	function(){
		PG.photoThumb("list-photo-thumb","on");
		PG.sitemenu("bt-menu","menu-container","bt-close","on");
	}
);

/* ============================================================== */
