
/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
	キーバインドクラス 
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
function KeyBind()
{
	
}
KeyBind.prototype = {
	
	setKeyBind : function(flg){
		
		if($.isiPad) return;
		
		var target = $(window);
		if($.isIE) target = $("body");
			
		if(flg){
			
			
			//キーリスナー
			target.unbind("keydown").bind("keydown", function(e){
					
					//console.log(e.keyCode);
					
					//ローディングがいるなら中止
					if($("#loadingIconDetail").length) return;
					
					
					if(e.keyCode == 27) {
					//エスケープ
					
						if($("#archiveWinHolder #archiveWin").length > 0 &&  $("#archiveWinHolder").css("display") != "none"){
						
							if($T.A) $T.A.closeArchive();
						} else if($("#detailHolder").css("display") == "none") {
							if($T.A) $T.A.openArchive();
						} else {
							//イメージ詳細を閉じる
							$T.IMG.closeDetail();
						}
					} else if(e.keyCode == 37) {
					//左
						if($("#detailHolder").css("display") != "none") {
							$T.IMG.prevDetail();
						}
					
					} else if(e.keyCode == 39) {
					//右
						if($("#detailHolder").css("display") != "none") {
							$T.IMG.nextDetail();
						}
					}
					
					e.stopPropagation();
			});
				
		} else {
			target.unbind("keydown");
		}
		
	}
	
}

/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
	プロジェクトイメージクラス 
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
function ProjectIMG()
{
	
	var me = this;
	this.isJP = $T.DB.isJP(document.URL);
	
}
ProjectIMG.prototype = {
	
	isJP : false,
	categoryData : [],
	margin_th : 15,
	currentTHIndex : 0,
	currentProjectData : null,
	projectsHolder : null,
	listeners : [],
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		addEventListener
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	addEventListener : function(item){
		var findFlg = false;
		this.listeners.push(item);
		this.listeners = this.listeners.unique();
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		dispatch
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	dispatch : function(type){
		for(var i=0, len = this.listeners.length ; i < len; i++) {
			if(this.listeners[i][type]) this.listeners[i][type](this);
		}
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		initForProject
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	initForProject : function(){
		var me = this;
		
		
		this.initMenu();
		this.initCategoryData();
		
		var q = $T.U.getQueryObj();
		
		
		
		var category = this.categoryData[0].name;
		if(q.category){
			category = decodeURI(q.category);
		} else if($.cookie('currentCategory')){
			//クッキー取得
			category = $.cookie('currentCategory');
		}
		
		console.log("categoryは　"+category);
		
		//指定があればコールバック
		var callback = null;
		
		if(q.projectID) {
			var projectID = q.projectID;
			callback = function(){
				
				me.projectsHolder.hide();
				
				var data = $T.DB.getDataFromProjectID(projectID);
				var pnum = 0;
				if(q.pnum) pnum = q.pnum;
				me.showDetail(data, pnum);
			
			}
		};
			
		this.changeCategory(category, callback);
		
		//for IE7
		if($.verIE == "IE8gokan"){
			
			
			
		}
		$("#projectmenu").wrap("<div id='projectmenuHolder'></div>");

	},
	
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		setProjectsHolder
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	setProjectsHolder : function(holder){
		this.projectsHolder = holder
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		initMenu
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	initMenu : function(){
		var me = this;
				
		var node = "";
		node += "<ul class='clearfix'>";
		var ary = jsDB.categoryAry;
		for(var i=0; i < ary.length; i++) {
			var cate = ary[i].name;
			node += "<li category='"+cate+"'><a href='javascript:void(0);'>"+cate+"</a> </li>";
			
			if(i < ary.length-1)node += "<li class='pmenu_split'><img src='../images/projects_split.gif' width='1' height='12' alt='' title=''></li>";
			//カテゴリデータ配列も初期化
			this.categoryData.push({ name : cate, ary : [] });
			
		}
		node += "</ul>";
		$("#projectmenu").empty().append(node);
				
				
		
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		makeTH
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	makeTH : function(ary, quick){
		
		if(!ary)return;
		
		var t = new Date();
		var me = this;
		var node = "";
		
		var gyo = 0;
		var retsu = 0;
		var retsuMax = 6;
		var urls = [];
		for(var i=0; i < ary.length; i++) {
			var d = ary[i];
			var url = "../images/projectthumb/prd_"+d.projectID+".jpg?rnd="+t.getTime();
			urls.push(url);
			node += "<div id='th_"+(i)+"' class='projectTH' num='"+i+"' gyo='"+gyo+"' retsu='"+retsu+"'><a href='javascript:void(0);'><img width='120' height='120' alt='"+d.projectID+"' title='"+d.projectID+"'></a></div>";
			
			retsu++;
			if(retsu == retsuMax){
				retsu = 0;
				gyo++;
			}
		}
		
		me.projectsHolder.empty().append(node);
		
		//ロード処理
		$("#projectsHolder .projectTH").each(function(){
			
			//ロード実行
			var th = $(this);
			var img = th.find("img");
			var num = th.attr("num");
			var url = urls[num];
			//エラー処理
			img.bind("error", function(e){
				console.log("ロードエラ e  "+url);
				
				var node = "<p class='projectTH_noimage'>NO IMAGE</p>";
				th.empty().append(node);
			});
			
			//ロード実行
			$(this).find("img").attr("src", url)
			
			//位置決定
			var mynum = th.attr("num");
			var gyo = th.attr("gyo");
			var retsu = th.attr("retsu");
			var x = retsu*(120+me.margin_th);
			var y = gyo*(120+me.margin_th);
			$.data(th.get(0), "mydata", { x:x, y:y, pdata:ary[mynum] });
			
			//ボタンアクション
			th.find("a").bind("click",function(e){
				e.stopPropagation();
				
				var mydata = $.data(th.get(0), "mydata");
				me.showDetail(mydata.pdata, 0);
				return false;
				
			}).hover(function(e){
			//オーバ
				e.stopPropagation();
				
				if($.isiPad) return;
				var me2 = $(this);
				var img = me2.find("img");
				
				img.stop().animate({ opacity:0.7}, {duration: 100, easing: 'easeOutCubic', complete:null});
				return false;
				
			},function(e){
			//アウト
				e.stopPropagation();
				
				if($.isiPad) return;
				var me2 = $(this);
				var img = me2.find("img");
				img.stop().animate({ opacity:1.0 }, {duration: 200, easing: 'easeInOutCubic', complete:null});
				return false;
			});
			
			//出現
			if(quick){
				th.css("top", y).css("left", x).css("visibility", "visible");
			} else {
				var startY = y+Math.random()*100;
				var d = 0+(50*mynum);
				var duration = 1000+Math.random()*100;
				if($.isiPad) {
					startY = y;
					//d = 100*mynum;
					//duration = 1000;
				}
				//th.css("top", startY).css("opacity", 0).css("visibility", "visible").css("left", x);
				th.css("top", y).css("opacity", 0).css("visibility", "visible").css("left", x);
				th.delay(d).animate({ top : y, left :x, opacity : 1}, {duration: duration, easing: 'easeInOutCubic', complete:function(){
					
					var me2 = $(this);
					me2.find("img").css("position", "relative").css("top", 0).css("left", 0);
	
				}});
			}
		});
				
	
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		showDetail　詳細表示トリガ
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	showDetail : function(detailData, index, callback){
		var me = this;
		
		//dispatch
		me.dispatch("onShowDetail");
	
		if(me.projectsHolder.css("display") == "none"){
			
			if($.verIE == "IE8" || $.isiPad){
				$("#detailHolder").hide();
				loadD();
			} else {
				$("#detailHolder").fadeOut("fast", function(){
					loadD();
				});
			}
				
		} else {
			
			if($.verIE == "IE8" || $.isiPad){
				me.projectsHolder.hide();
				loadD();
			} else {
				me.projectsHolder.fadeOut("fast", function(){
					loadD();
				});
			}
		}

		
		
		
		//loadD
		function loadD(){
			
			
			//ローディング
			me.showLoading(true, function(){
				me.loadDetail(detailData, index, callback);
			});
		}
		
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		詳細ビューの番号ボタンを取得
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	getNumBtnDetail : function(index, withLink){
		var node = "<li id='detailMenu_"+(index+1)+"' num='"+index+"'><a href='javascript:void(0);'>"+(index+1)+"</a></li>";
		return node;
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		showLoading
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	showLoading : function(flg, callback){
		
		if(flg){
			$("#loadingIconDetail").remove();
			$("#maincont").append("<div id='loadingIconDetail'><img src='../images/loadinfo.gif'></div>");
			var top = 170;
			if($.isiPad) top = 130;
			
			$("#loadingIconDetail").css("top", top).fadeIn("fast", function(){
				if(callback) callback();
			});
		} else {
			$("#loadingIconDetail").fadeOut("slow", function(){
				$(this).remove();
				if(callback) callback();
			});
		}
	},
		
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		loadDetail
		index　プロジェクト内何個目のデータを表示するのか
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	loadDetail : function(detailData, index, callback){
		
		var me = this;
		currentTHIndex = index;
		currentProjectData = detailData;
		
		if(!detailData){
			console.log("detailDataがないよ");
			return;
		} else if(!detailData.urls){
			console.log("detailData.urlsがないよ");
			return;
		}
		var obj = detailData.urls[index];
		
		var t = new Date();
		var url = "../images/archives/"+obj.url+"?rnd="+t.getTime();
		var type = obj.type.toLowerCase();
		/*
			type c：ぶちぬき、キャプションは画像の下
			typeそれ以外：基準ラインより右がキャプション、左が画像、画像は上下センター
		
		*/

		//位置計算に必要です
		$("#detailHolder").css("display", "block").css("visibility", "hidden");
		
		//キャプションエリア
		var node = "<div id='detailCAP'>";
			node += "<div id='detailName' class='text11'>";
				node += detailData.year+"<br>"+detailData.name;
			node += "</div>";
			
			node += "<ul id='detailMenu' class='clearfix'>";
			var ary = detailData.urls;
			for(var i=0; i < ary.length; i++) {
				//node += "<li id='detailMenu_"+(i+1)+"' num='"+i+"'><a href='javascript:void(0);'>"+(i+1)+"</a></li>";
				node += me.getNumBtnDetail(i, true);
			}
			node += "</ul>";
		node += "</div>";
		$("#detailICAPHolder").empty().append(node);
		
		//メインイメージ
		var img = $("#imgDetail").css("visibility", "hidden");
		
		//開発
		var detailIMG = $("#detailIMG").css("display", "block");
		
		$("#detailIMGHolder").show();
		
		
		
		//ロード成功処理
		img.unbind("load").bind("load", function(e){
			
			console.log("ロード完了 "+e.target.width);
			
			pos(e.target);
			//
			//$.dump(sizeData);
			
			
		});
		
		
		//ロードエラー処理
		img.unbind("error").bind("error", function(e){
			console.log("エラーじゃん");
			var holder = $("#detailIMGHolder");
			var node = "<div id='detailIMGErrorHolder'><div id='detailIMGErrorMsg'>ERROR</div></div>";
			holder.find("img").remove().end().append(node);
			var CAPHolder = $("#detailICAPHolder");
			
			var msgObj = $("#detailIMGErrorMsg");
			var pos = msgObj.position();
			console.log("ma-ssssisn "+msgObj.css("margin-top"));
			var marginTop = parseInt(msgObj.css("marginTop").replace("px", ""));
			
			
			CAPHolder.css("top",  marginTop+ msgObj.outerHeight()).css("left", pos.left + msgObj.outerWidth() - CAPHolder.width());
			
			//出現
			me.showLoading(false, function(){
				showFunc();
			});
			
		});
		
		
		
		//ロード実行
		
		img.attr("src", url);
		
		console.log("src "+img.attr("src")+"   img "+img);
		
		//番号ボタンカレント処理
		$("#detailMenu li").each(function(){
			var me2 = $(this);
			var num = parseInt(me2.attr("num"),10);
			if(num == index){
				var htmlstr = me2.find("a").html();
				me2.empty().append(htmlstr);
				me2.attr("id", "detailMenu_"+(num+1)+"c").addClass("detailMenuC");
			} else {
				//me2.removeClass("detailMenu_current").empty().append("<a href='javascript:void(0);'>"+(num+1)+"</a>");
				//me2.attr("id", "detailMenu_"+(num+1));
				
				me2.after(me.getNumBtnDetail(num));
				var newBtn = me2.next("li");
				me2.remove();
				
				
				//番号ボタン
				newBtn.find("a").unbind("click").bind("click", function(e){
					e.stopPropagation();
					
					var me3 = $(this);
					var li = me3.parent();
					var num = li.attr("num");
					console.log(num);
					me.showDetail(detailData, num);
					return false;
				});
			}
		});

		
		/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
			pos　ロード後、ポジション調整
		ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
		function pos(target){
			
			var oriImg = target;
			var img2 = $(target);
			//イメージ位置
			var x = 0;
			var y = 0;
			var baseX = 588;
			var capX = baseX + 18;
			var minHeight = 452;
			var sizeData = $T.U.getImg_true_size(target);
			var w = sizeData.width
			var h = sizeData.height;
			
			//サイズをセット
			img2.width('width', w).height('height', h);
			
			var CAPHolder = $("#detailICAPHolder");
			if(type == "c"){
				
				detailIMG.css("top", 0).css("left", Math.round($("#detailIMGHolder").width()/2 - w/2));
				
				var mtop = 10;//キャプションと写真までの距離
				if($.isiPad) mtop = 4;
				var y = detailIMG.position().top + h + mtop;
				CAPHolder.css("top", y).css("left", capX);
				
			} else {
			
				//タテはセンター
				if(h < minHeight){
					console.log("タテはセンター  "+h);
					y = Math.round((minHeight - h)/2);
					
				}
				
				
				detailIMG.css("top", y).css("left", baseX - w);
				var y = detailIMG.position().top + h - CAPHolder.height();
				CAPHolder.css("top", y).css("left", capX);
			}
			
			
			img2.onload = "";//IEメモリーリーク対策
  			img2 = void 0; // free obj （IEメモリーリーク対策）
			
			//出現
			me.showLoading(false, function(){
				
  			
				showFunc();
			});
			
		}
		
		/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
			showFunc
		ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
		function showFunc(){
			console.log("showFunc");
			
			//2011.0901追加
			//$("#detailIMGHolder").show();
			
			var detailHolder = $("#detailHolder");
			var img = $("#imgDetail").css("visibility", "visible");
			
			if($.verIE == "IE8" || $.isiPad){
			//if($.verIE == "IE8"){
				//var detailIMG = $("#detailIMG");
				//detailIMG.css("visibility", "hidden");
				
				detailHolder.css("visibility", "visible").show();
				if(callback) callback();
				showBackBtn();

			} else {
				
				//出現
				detailHolder.css("visibility", "visible").hide().fadeIn("normal",function(){
					
					if(callback) callback();
					
					showBackBtn();

				});
			
			}
			
			/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
				showBackBtn
			ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
			function showBackBtn(){
				
				//バックボタン
					$("#btn_back").fadeIn("fast").find("a").unbind("click").bind("click", function(e){
						e.stopPropagation();
						me.closeDetail();
						return false;
					});
					
			}
			
			/*
			$("#detailHolder").css("visibility", "visible").css("display", "none").fadeIn("slow", function(){
				
				if(callback) callback();
				
				//バックボタン
				$("#btn_back").fadeIn("fast").find("a").unbind("click").bind("click", function(){
					me.closeDetail();
				});

			});
			*/
			
			
			
		}
		
		

	},
	
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		prevDetail
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	prevDetail : function(){
		
		var index = currentTHIndex -1;
		var urls = currentProjectData.urls;
		if(index < 0) return;
		this.showDetail(currentProjectData, index);
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		nextDetail
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	nextDetail : function(){
		
		var index = currentTHIndex +1;
		var urls = currentProjectData.urls;
		if(index > urls.length -1 ) return;
		this.showDetail(currentProjectData, index);
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		closeDetail
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	closeDetail : function(callback, quick){
		
		var me = this;
		
		//見えてないので終了
		if($("#detailHolder").css("display") == "none") {
			if(callback) callback();
			return;
		}
		//アニメ中なので終了
		if($('#detailHolder:animated').length) return;
		
		
		
		//dispatch
		me.dispatch("onCloseDetail");
		
		
		$("#btn_back").hide();
		
		var detailHolder = $("#detailHolder");
		//var projectsHolder = me.projectsHolder;
		if(quick){
			detailHolder.hide();
			me.projectsHolder.show();
			if(callback) callback();
		} else {
			
			if($.verIE == "IE8" || $.isiPad){
				
				me.projectsHolder.show();
				detailHolder.hide();
				if(callback) callback();
				
				
				
				
				
			} else {
				detailHolder.fadeOut("fast", function() {
					me.projectsHolder.fadeIn("fast");
					
					console.log("クローズ完了　"+$("#detailHolder").css("display"));
					if(callback) callback();
				});
			}
		}
		
		
	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		changeCategory
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	changeCategory : function(category, callback){
		
		if(!category){
			console.log("changeCategory  カテゴリーなし");
			return;
		}
		console.log("チェンジ　"+category);
		var me = this;
		
		//クッキーに保存
		$.cookie('currentCategory', category); //cookieをセット  
		
		
		
		//メニュー更新
		
		$("#projectmenu li[category]").each(function() {
			var li = $(this);
			var mycategory = li.attr("category");
			
			if(mycategory == category){
				
				li.addClass("currentp").empty().append(mycategory);
				
			} else {
				if(li.hasClass("currentp")){
					li.removeClass("currentp").empty().append("<a href='javascript:void(0);'>"+mycategory+"</a>");
				}
			}
		});
		
		//ボタンアクションを設定
		$("#projectmenu li a").unbind("click").bind("click",function(e){
			e.stopPropagation();
			var category = $(this).parent().attr("category");
			me.changeCategory(category);
			return false;
		});
		
		
		var data = me.getCategoryData(category);
		var ary = data.ary;
		if(callback){
			
			
			me.projectsHolder.empty().css("opacity", 1);
			me.closeDetail(null, true);
			me.makeTH(ary, true);
			callback();
			return;
		}
		
		if($.verIE == "IE8" || $.isiPad){
			me.projectsHolder.empty().css("opacity", 1);
			//イメージ詳細を閉じる
			me.closeDetail(function(){
		
				me.makeTH(ary, false);
			
			});
				
		} else {
			me.projectsHolder.animate({ opacity : 0}, {duration: 300, easing: 'easeInOutCubic', complete:function(){
				
				$(this).empty().css("opacity", 1);
				
				
				//イメージ詳細を閉じる
				me.closeDetail(function(){
			
					me.makeTH(ary, false);
				
				});
			}});
		}

	},
	
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		initCategoryData
		データをカテゴリ毎に分けて格納
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	initCategoryData : function(){
		
		var me = this;
		//categoryData
		
		for(var i=0; i < jsDB.itemAry.length; i++) {
			var data = jsDB.itemAry[i];
			
			
			
			if(data.urls){
				var category = data.category;
				for(var z=0; z < me.categoryData.length; z++) {
					
					if(me.categoryData[z].name == category && data.urls.length > 0) {
					
						me.categoryData[z].ary.push(data);
						
						break;
					}
				}
			}
		}
		
		/*
		console.log("でーた");
		for(var z=0; z < me.categoryData.length; z++) {
			console.log("カテゴリ "+me.categoryData[z].name);
			var ary = me.categoryData[z].ary;
			for(var i=0; i < ary.length; i++) {
				console.log(ary[i].id+"   "+ary[i].name);
			}
		}
		*/
		
		
		
	}, 
		
	/* ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
		getCategoryData
	ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー */
	getCategoryData : function(category){
		
		console.log("getCategoryDataだよ　"+category);
		for(var i=0; i < this.categoryData.length; i++) {
			
			console.log("compare "+this.categoryData[i].name+"/"+category);
			
			if(this.categoryData[i].name == category) {
				console.log("getCategoryData　あった");
				return this.categoryData[i];
			}
		}
		return null;
	}
	
}
