/*
* top.js
*
**************************************/

var w = new Welcome();
var s = new Shop();
var a = new Access();
var g = new Gallery();

$(document).ready(function(){
	w.init();
	s.init();
	a.init();
	g.init();
	
	window.onresize = function(){
		w.resetPanel();
		s.resetPanel();
		a.resetPanel();
		g.resetPanel();
	}
	
// Panel
	fleXenv.initByClass("shopInfoBody");
});

function getPanel(category, data){
	var currentDate = new Date();
	var postDate = new Date(data.date).getTime();
	var newItem = currentDate.getTime() - postDate < 604800000 ? 1 : 0;
	var startDate = new Date(data.start);
	var endDate = new Date(data.end);
	var future = startDate > currentDate ? 1 : 0;
	var past = currentDate > endDate ? 1 : 0;
	var endDateNext = new Date();
	endDateNext.setTime(endDate.getTime() + 86400000);
	
	var gCalMonth = startDate.getMonth() < 9 ? "0" + (startDate.getMonth() + 1) : startDate.getMonth() + 1;
	var gCalDate = startDate.getDate() < 10 ? "0" + startDate.getDate() : startDate.getDate();
	var gCalStart = String(startDate.getFullYear()) + String(gCalMonth) + String(gCalDate);
	gCalMonth = endDateNext.getMonth() < 9 ? "0" + (endDateNext.getMonth() + 1) : endDateNext.getMonth() + 1;
	gCalDate = endDateNext.getDate() < 10 ? "0" + endDateNext.getDate() : endDateNext.getDate();
	var gCalEnd = String(endDateNext.getFullYear()) + String(gCalMonth) + String(gCalDate);
	var gCal = 'http://www.google.com/calendar/event?action=TEMPLATE&text=' + encodeURIComponent(data.title) + '&dates=' + gCalStart + '/' + gCalEnd + '&details=' + encodeURIComponent(data.memo) + '&location=' + encodeURIComponent(data.place) + '&trp=false&sprop=' + encodeURIComponent('http://gyre-omotesando.com/') + '&sprop=name:GYRE';
	var yCal = 'http://calendar.yahoo.co.jp/?V=60&&TITLE=' + encodeURIComponent(data.title) + '&ST=' + gCalStart + 'T0000&DUR=0000&DESC=' + encodeURIComponent(data.memo) + '&URL=' + encodeURIComponent('http://gyre-omotesando.com/') + '&IN_LOC=' + encodeURIComponent(data.place) + '&ENC=UTF-8';
	
	content = '<div id="' + category + data.id + '" class="topPanelA01">\
				 <div class="frame">\
					<div class="content">\
					 <div class="loadingPanel"><img src="/shared/img/loading_02.gif" width="20" height="20" alt="" /></div>\
					 <div class="panelImage">\
								<div class="image"><img src="' + data.image + '" width="500" height="300" alt="' + data.title + '" /></div>';
	if(category == "g"){
		if(future){
			content = content + '<div class="icon2"><img width="65" height="63" alt="Future" src="/img/icon_future_01.png"></div>';
		} else if(past){
			content = content + '<div class="icon2"><img width="65" height="63" alt="Past" src="/img/icon_past_01.png"></div>';
		} else {
			content = content + '<div class="icon2"><img width="65" height="63" alt="Current" src="/img/icon_current_01.png"></div>';
		}
	}
	content = content + '<div class="moreButton"><img class="hover" src="/img/btn_more_01.png" width="66" height="26" alt="More" /></div>\
					 </div>\
					 <div class="panelBody">\
								<div class="panelText">\
										 <h3 class="default">' + data.title + '</h3>\
										 <p>' + data.body + '</p>\
								</div>\
								<div class="panelButton">\
										 <div class="shareTitle"><img src="/img/txt_share_01.gif" width="26" height="7" alt="共有する" /></div>\
										 <div class="button"><a href="http://twitter.com/share?url=http://gyre-omotesando.com/?p=' + data.id + '&text=' + encodeURIComponent(data.title + ' #gyre') +'&via=gyre_omotesando&lang=ja" target="twitter"><img class="hover" src="/img/btn_twitter_01.gif" width="20" height="20" alt="Twitterに投稿" /></a></div>\
										 <div class="button"><a href="facebook_like.html?id=' + data.id + '&title=' + encodeURIComponent(data.title) + '" target="_blank"><img class="hover" src="/img/btn_facebook_01.gif" width="20" height="20" alt="Facebookに投稿" /></a></div>\
										 <div class="buttonLast"><a href="http://mixi.jp/share.pl?u=' + encodeURI('http://gyre-omotesando.com/?p=' + data.id) + '&k=f13d69c264c30e84393dc2b3a845b211d11f4693" target="_blank"><img class="hover" src="/img/btn_mixi_01.gif" width="20" height="20" alt="mixiに投稿" /></a></div>\
										 <div class="calendarTitle"><img src="/img/txt_calendar_01.gif" width="48" height="7" alt="カレンダーに追加" /></div>\
										 <div class="button2"><a href="' + gCal + '" target="_blank"><img class="hover" src="/img/btn_google_01.gif" width="60" height="20" alt="Googleカレンダー" /></a></div>\
										 <div class="button2Last"><a href="' + yCal + '" target="_blank"><img class="hover" src="/img/btn_yahoo_01.gif" width="60" height="20" alt="Yahooカレンダー" /></a></div>\
										 <div class="closeButton"><img class="hover" src="/img/btn_close_01.gif" width="20" height="20" alt="閉じる" /></div>\
								</div>\
					 </div>\
					<!--/content--></div>\
				 <!--/frame--></div>';
	if(category == "w"){
		if(newItem){
			content = content + '<div class="icon"><img src="/img/icon_new_01.png" width="46" height="46" alt="New" /></div>';
		}
	}
				 
	content = content + '<!--/topPanelA01--></div>';

	return content;	
}

function getTweetPanel(data){
	var content = '<div id="t001" class="topPanelA02">\
		 <div class="iconBlockA01">\
					<div class="thumb"><a href="http://twitter.com/gyre_omotesando" target="twitter"><img src="' + data.user.profile_image_url + '" width="30" height="30" alt="" /></a></div>\
					<div class="name"><a href="http://twitter.com/gyre_omotesando" target="twitter">' + data.user.name + '</a></div>\
					<div class="logo"><img src="/img/logo_twitter_02.gif" width="20" height="14" alt="Twitter" /></div>\
		 </div>\
		 <p class="default">' + data.text + '</p>\
		 <p class="date">' + tweetTime(data.created_at) + '</p>\
		<!--/topPanelA02--></div>';
	
	return content;	
}

function CheckLoad(id, img){
	var id = "#" + id + " div.loadingPanel";
	var imgObj = new Image();
	imgObj.src = img;
	check();
	
	function	check(){
		if(imgObj.complete){
			$(id).fadeOut("normal");
		} else {
			setTimeout(check, 250);
		}
	}
	
	this.check = function(){
		check();
	}
}

/* Welcome *************************************************************************/
function Welcome(){
	var panelData = "";
	var tweetData = "";
	var num = 1;
	var basePos = 0;
	var elemBasePos = 0;
	
	this.init = function(){
		$.getJSON('/?cat=3', {'d': new Date().getTime()}, function(data){
			panelData = data;
			setPanel();
		});
		$.getJSON('http://api.twitter.com/1/statuses/user_timeline/gyre_omotesando.json?callback=?', {'d': new Date().getTime()}, function(data){
			tweetData = data;
		});
		
		if(iPhone || iPad){
			$("div#welcome div.prevPanelButton").show();
			$("div#welcome div.nextPanelButton").show();
		} else {
			$("div#welcome").hover(function(){
				if(IE){
					$("div#welcome div.prevPanelButton").show();
					$("div#welcome div.nextPanelButton").show();
				} else {
					$("div#welcome div.prevPanelButton").fadeIn("normal");
					$("div#welcome div.nextPanelButton").fadeIn("normal");
				}
			},
			function(){
				if(IE){
					$("div#welcome div.prevPanelButton").hide();
					$("div#welcome div.nextPanelButton").hide();
				} else {
					$("div#welcome div.prevPanelButton").fadeOut("normal");
					$("div#welcome div.nextPanelButton").fadeOut("normal");
				}
			});
		}
		$("div#welcome div.prevPanelButton img").click(this.showWelcomePrev);
		$("div#welcome div.nextPanelButton img").click(this.showWelcomeNext);
		$("div#welcomeController div.prev img").click(this.showWelcomePrev);
		$("div#welcomeController div.next img").click(this.showWelcomeNext);
	}
	
	function setPanel(){
		if(tweetData == ""){
			setTimeout(setPanel, 250);
		} else {
			var content;
			var tweetContent;
			var imgs = new Array();
			if(panelData.length > 2){
				content = getPanel("w", panelData[panelData.length - 1]);
				$("div#welcome").append(content);
				tweetContent = getTweetPanel(tweetData[panelData.length - 1]);
				$("div#welcome").append(tweetContent);
				for(i=0; i<panelData.length-1; i++){
					content = getPanel("w", panelData[i]);
					$("div#welcome").append(content);
					tweetContent = getTweetPanel(tweetData[i]);
					$("div#welcome").append(tweetContent);
				}
			} else {
				for(i=0; i<panelData.length; i++){
					content = getPanel("w", panelData[i]);
					$("div#welcome").append(content);
					tweetContent = getTweetPanel(tweetData[i]);
					$("div#welcome").append(tweetContent);
				}
			}
			
			for(i=0; i<panelData.length; i++){
				imgs[i] = new CheckLoad("w" + panelData[i].id, panelData[i].image);
			}
			
			num = panelData.length;
			resetPanel();
			fleXenv.initByClass("panelText");
			
			$("div#welcome div.image img").live("click", function(){
				$(this).parent("div").parent("div").parent("div.content").dequeue().animate({"top": "-300px"}, 800, "easeOutExpo");
			});
			$("div#welcome div.moreButton img").live("click", function(){
				$(this).parent("div").parent("div").parent("div.content").dequeue().animate({"top": "-300px"}, 800, "easeOutExpo");
			});
			$("div#welcome div.closeButton img").live("click", function(){
				$(this).parent("div").parent("div").parent("div").parent("div.content").dequeue().animate({"top": 0}, 800, "easeOutExpo");
			});
			
			if(location.href.indexOf("/?id=w") != -1){
				var specifyId = "#" + location.href.substring(location.href.indexOf("/?id=w") + 5);
				
				if($(specifyId).length == 1){
					$.scrollTo("#welcome", {duration: 700, easing: "easeOutExpo", offset: {top: -20}});
					var specifyPos = 0;
					$("#welcome > .topPanelA01").each(function(e){
						if("#" + $(this).attr("id") == specifyId){
							specifyPos = e;
							console.log("e:" + e);
							console.log("specifyPos:" + specifyPos);
						}
					});
					if(specifyPos == 0){
						w.showWelcomePrev();
					}
					if(specifyPos > 1){
						var j = 1;
						while(j<specifyPos){
							console.log(j);
							w.showWelcomeNext();
							j++;
						}
					}
				}
			}
		}
	}
	
	function resetPanel(){
		basePos = document.documentElement.clientWidth < 980 ? Math.round(980 / 2) - 475 : Math.round(document.documentElement.clientWidth / 2) - 475;
		elemBasePos = basePos + 145 - 660;
		$("#welcomeController").css("left", basePos + "px");
		for(i=0; i<num; i++){
			if(i==0){
				$("div#welcome .topPanelA01:eq(" + i + ")").css("left", (elemBasePos + 660 * i - 140) + "px");
				$("div#welcome .topPanelA02:eq(" + i + ")").css("left", (elemBasePos + 660 * i - 140 + 500) + "px");
			} else {
				$("div#welcome .topPanelA01:eq(" + i + ")").css("left", (elemBasePos + 660 * i) + "px");
				$("div#welcome .topPanelA02:eq(" + i + ")").css("left", (elemBasePos + 660 * i + 500) + "px");
			}
		}
	}
	
	this.resetPanel = function(){
		resetPanel();
	};
	
	this.showWelcomePrev = function(){
		$("div#welcome .topPanelA01").dequeue();
		$("div#welcome .topPanelA02").dequeue();
		for(i=0; i<num; i++){
			$("div#welcome .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i + 1)) + "px"}, 800, "easeOutExpo");
			$("div#welcome .topPanelA02:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i + 1) + 500) + "px"}, 800, "easeOutExpo");
		}

		var elem = $("div#welcome .topPanelA01:eq(" + (num - 1) + ")");
		$("div#welcome .topPanelA01:eq(" + (num - 1) + ")").remove();
		$("div#welcome").prepend(elem);
		$("div#welcome .topPanelA01:eq(0)").dequeue().css("left", (elemBasePos + 660 * (-1) - 140) + "px");
		$("div#welcome .topPanelA01:eq(0)").animate({"left": (elemBasePos - 140) + "px"}, 800, "easeOutExpo");
		var elem2 = $(".topPanelA02:eq(" + (num - 1) + ")");
		$("div#welcome .topPanelA02:eq(" + (num - 1) + ")").remove();
		$("div#welcome").prepend(elem2);
		$("div#welcome .topPanelA02:eq(0)").dequeue().css("left", (elemBasePos + 660 * (-1) - 140 + 500) + "px");
		$("div#welcome .topPanelA02:eq(0)").animate({"left": (elemBasePos - 140 + 500) + "px"}, 800, "easeOutExpo");
	}
	
	this.showWelcomeNext = function(){
		$("div#welcome .topPanelA01").dequeue();
		$("div#welcome .topPanelA02").dequeue();
		for(i=0; i<num; i++){
			if(i != 0){
				if(i == 1){
					$("div#welcome .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i - 1) - 140) + "px"}, 800, "easeOutExpo");
					$("div#welcome .topPanelA02:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i - 1) - 140 + 500) + "px"}, 800, "easeOutExpo");
				} else {
					$("div#welcome .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i - 1)) + "px"}, 800, "easeOutExpo");
					$("div#welcome .topPanelA02:eq(" + i + ")").animate({"left": (elemBasePos + 660 * (i - 1) + 500) + "px"}, 800, "easeOutExpo");
				}
			}
		}

		var elem = $("div#welcome .topPanelA01:eq(0)");
		$("div#welcome .topPanelA01:eq(0)").remove();
		$("div#welcome").append(elem);
		$("div#welcome .topPanelA01:eq(" + (num - 1) + ")").dequeue().css("left", (elemBasePos + 660 * (num)) + "px");
		$("div#welcome .topPanelA01:eq(" + (num - 1) + ")").animate({"left": (elemBasePos + 660 * (num - 1)) + "px"}, 800, "easeOutExpo");
		var elem2 = $(".topPanelA02:eq(0)");
		$("div#welcome .topPanelA02:eq(0)").remove();
		$("div#welcome").append(elem2);
		$("div#welcome .topPanelA02:eq(" + (num - 1) + ")").dequeue().css("left", (elemBasePos + 660 * (num) + 500) + "px");
		$("div#welcome .topPanelA02:eq(" + (num - 1) + ")").animate({"left": (elemBasePos + 660 * (num - 1) + 500) + "px"}, 800, "easeOutExpo");
	}
	
}

/* Shops & Restaurants *************************************************************************/
function Shop(){
	var status = 1;
	var panelNum;
	var floorNum = 6
	var basePos;
	var panelBasePos;
	var detailBasePos;

	this.init = function(){
		panelNum = $("div.shopPanelBlockA01").length;
		this.resetPanel();

		if(iPhone || iPad){
			$("div#shop div.prevPanelButton").show();
			$("div#shop div.nextPanelButton").show();
		} else {
			$("div#shop").hover(function(){
				if(IE){
					$("div#shop div.prevPanelButton").show();
					$("div#shop div.nextPanelButton").show();
				} else {
					$("div#shop div.prevPanelButton").fadeIn("normal");
					$("div#shop div.nextPanelButton").fadeIn("normal");
				}
			},
			function(){
				if(IE){
					$("div#shop div.prevPanelButton").hide();
					$("div#shop div.nextPanelButton").hide();
				} else {
					$("div#shop div.prevPanelButton").fadeOut("normal");
					$("div#shop div.nextPanelButton").fadeOut("normal");
				}
			});
		}
		$("div#shop div.prevPanelButton img").click(this.showShopPrev);
		$("div#shop div.nextPanelButton img").click(this.showShopNext);
		$("div#shopController div.prev img").click(this.showShopPrev);
		$("div#shopController div.next img").click(this.showShopNext);
		$("div.floorInfo ul.linkList li img").live("click", function(){
			var target = document.getElementById($(this).attr("id").replace("link","")).offsetTop;
			var floorId = "shopInfoBody" + $(this).attr("id").substring(4,5);
			document.getElementById(floorId).fleXcroll.setScrollPos(0,target,false);
		});
		$("div.shopPanelBlockA01 img").live("click", this.showShopDetail);
		$("div#shopDetailBlock div.closeButton img").live("click", this.hideShopDetail);
	}
	
	this.resetPanel = function() {
		basePos = document.documentElement.clientWidth < 980 ? Math.round(980 / 2) - 415 : Math.round(document.documentElement.clientWidth / 2) - 415;
		panelBasePos = basePos + 145 - 220;
		detailBasePos = basePos + 145 - 620;
//		$("#shop").css("background-position", (basePos - 655) + "px top");

		$("#shopController").css("left", basePos + "px");
		for(i=0; i<panelNum; i++){
			if(i == 0){
				$(".shopPanelBlockA01:eq(" + i + ")").css("left", (panelBasePos + 220 * i - 140) + "px");
			} else {
				$(".shopPanelBlockA01:eq(" + i + ")").css("left", (panelBasePos + 220 * i) + "px");
			}
		}
		for(i=0; i<floorNum; i++){
			if(i == 0){
				$(".floorBlock:eq(" + i + ")").css("left", (detailBasePos + 620 * i - 140) + "px");
			} else {
				$(".floorBlock:eq(" + i + ")").css("left", (detailBasePos + 620 * i) + "px");
			}
		}
	}

	this.showShopPrev = function(){
		if(status == 1){
			$(".shopPanelBlockA01").dequeue();
			for(i=0; i<panelNum; i++){
				$(".shopPanelBlockA01:eq(" + i + ")").animate({"left": (panelBasePos + 220 * (i + 1)) + "px"}, 800, "easeOutExpo");
			}
	
			var elem = $(".shopPanelBlockA01:eq(" + (panelNum - 1) + ")");
			$(".shopPanelBlockA01:eq(" + (panelNum - 1) + ")").remove();
			$("div#shopThumbBlock").prepend(elem);
			$(".shopPanelBlockA01:eq(0)").dequeue().css("left", (panelBasePos + 220 * (-1) - 140) + "px");
			$(".shopPanelBlockA01:eq(0)").animate({"left": (panelBasePos - 140) + "px"}, 800, "easeOutExpo");
		} else {
			$(".floorBlock").dequeue();
			for(i=0; i<floorNum; i++){
				$(".floorBlock:eq(" + i + ")").animate({"left": (detailBasePos + 620 * (i + 1)) + "px"}, 800, "easeOutExpo");
			}
	
			var elem = $(".floorBlock:eq(" + (floorNum - 1) + ")");
			$(".floorBlock:eq(" + (floorNum - 1) + ")").remove();
			$("div#shopDetailBlock").prepend(elem);
			$(".floorBlock:eq(0)").dequeue().css("left", (detailBasePos + 620 * (-1) - 140) + "px");
			$(".floorBlock:eq(0)").animate({"left": (detailBasePos - 140) + "px"}, 800, "easeOutExpo");
		}
	}
	
	this.showShopNext = function(){
		if(status == 1){
			$(".shopPanelBlockA01").dequeue();
			for(i=0; i<panelNum; i++){
				if(i == 1){
					$(".shopPanelBlockA01:eq(" + i + ")").animate({"left": (panelBasePos + 220 * (i - 1) - 140) + "px"}, 800, "easeOutExpo");
				} else {
					$(".shopPanelBlockA01:eq(" + i + ")").animate({"left": (panelBasePos + 220 * (i - 1)) + "px"}, 800, "easeOutExpo");
				}
			}
	
			var elem = $(".shopPanelBlockA01:eq(0)");
			$(".shopPanelBlockA01:eq(0)").remove();
			$("div#shopThumbBlock").append(elem);
			$(".shopPanelBlockA01:eq(" + (panelNum - 1) + ")").dequeue().css("left", (panelBasePos + 220 * (panelNum)) + "px");
			$(".shopPanelBlockA01:eq(" + (panelNum - 1) + ")").animate({"left": (panelBasePos + 220 * (panelNum - 1)) + "px"}, 800, "easeOutExpo");
		} else {
			$(".floorBlock").dequeue();
			for(i=0; i<floorNum; i++){
				if(i == 1){
					$(".floorBlock:eq(" + i + ")").animate({"left": (detailBasePos + 620 * (i - 1) - 140) + "px"}, 800, "easeOutExpo");
				} else {
					$(".floorBlock:eq(" + i + ")").animate({"left": (detailBasePos + 620 * (i - 1)) + "px"}, 800, "easeOutExpo");
				}
			}
	
			var elem = $(".floorBlock:eq(0)");
			$(".floorBlock:eq(0)").remove();
			$("div#shopDetailBlock").append(elem);
			$(".floorBlock:eq(" + (floorNum - 1) + ")").dequeue().css("left", (detailBasePos + 620 * (floorNum)) + "px");
			$(".floorBlock:eq(" + (floorNum - 1) + ")").animate({"left": (detailBasePos + 620 * (floorNum - 1)) + "px"}, 800, "easeOutExpo");
		}
	}
	
	this.showShopDetail = function(){
		status = 2;
		var floorId = parseInt($(this).attr("id").substring(4,5));
		for(i=0; i<floorNum; i++){
			var elem = $(("div#floor" + (floorId + i)));
			$(("div#floor" + (floorId + i))).remove();
			$("#shopDetailBlock").append(elem);
			$(("div#floor" + (floorId + i))).css("left", (detailBasePos + 620 * (i + 1)) + "px");
		}
		if(floorId > 1){
			for(i=0; i<floorId - 1; i++){
				var elem = $(("div#floor" + i));
				$(("div#floor" + i)).remove();
				$("#shopDetailBlock").append(elem);
				$(("div#floor" + i)).css("left", (detailBasePos + 620 * (floorNum - floorId + 1 + i)) + "px");
			}
		}
		if(floorId > 0){
			var elem = $(("div#floor" + (floorId - 1)));
			$(("div#floor" + (floorId - 1))).remove();
			$("#shopDetailBlock").prepend(elem);
			$(("div#floor" + (floorId - 1))).css("left", detailBasePos + "px");
		} else {
			var elem = $(("div#floor" + (floorNum - 1)));
			$(("div#floor" + (floorNum - 1))).remove();
			$("#shopDetailBlock").prepend(elem);
			$(("div#floor" + (floorNum - 1))).css("left", detailBasePos + "px");
		}

		var shopId = parseInt($(this).attr("id").substring(5,6));
		var floorElem = "#floor" + floorId + " div.shopInfoBody";
		var target = floorId + "-" + shopId;
		var targetPos = document.getElementById(target).offsetTop;
		floorId = "shopInfoBody" + floorId;
		document.getElementById(floorId).fleXcroll.setScrollPos(0,targetPos,false);
		$("div#shopThumbBlock").animate({"top": "-450px"}, 800, "easeOutExpo");
		$("div#shopDetailBlock").animate({"top": "0px"}, 800, "easeOutExpo");
	}
	
	this.hideShopDetail = function(){
		status = 1;
		$("div#shopThumbBlock").animate({"top": "0px"}, 800, "easeOutExpo");
		$("div#shopDetailBlock").animate({"top": "450px"}, 800, "easeOutExpo");
	}
}

/* Access *************************************************************************/
function Access() {
	var num = 4;
	var basePos;
	var elemBasePos;

	this.init = function(){
		this.resetPanel();

		if(iPhone || iPad){
			$("div#access div.prevPanelButton").show();
			$("div#access div.nextPanelButton").show();
		} else {
			$("div#access").hover(function(){
				if(IE){
					$("div#access div.prevPanelButton").show();
					$("div#access div.nextPanelButton").show();
				} else {
					$("div#access div.prevPanelButton").fadeIn("normal");
					$("div#access div.nextPanelButton").fadeIn("normal");
				}
			},
			function(){
				if(IE){
					$("div#access div.prevPanelButton").hide();
					$("div#access div.nextPanelButton").hide();
				} else {
					$("div#access div.prevPanelButton").fadeOut("normal");
					$("div#access div.nextPanelButton").fadeOut("normal");
				}
			});
		}
		$("div#access div.prevPanelButton img").click(this.showAccessPrev);
		$("div#access div.nextPanelButton img").click(this.showAccessNext);
		$("div#accessController div.prev img").click(this.showAccessPrev);
		$("div#accessController div.next img").click(this.showAccessNext);
	}
	
	this.resetPanel = function(){
		basePos = document.documentElement.clientWidth < 980 ? Math.round(980 / 2) - 355 : Math.round(document.documentElement.clientWidth / 2) - 355;
		elemBasePos = basePos + 145 - 500;

		$("#accessController").css("left", basePos + "px");
		for(i=0; i<num; i++){
			if(i == 0){
				$("#access > div:not(.fixed):eq(" + i + ")").css("left", (elemBasePos + 500 * i - 140) + "px");
			} else {
				$("#access > div:not(.fixed):eq(" + i + ")").css("left", (elemBasePos + 500 * i) + "px");
			}
		}
	}
	
	this.showAccessPrev = function(){
		$("#access > div:not(.fixed)").dequeue();
		for(i=0; i<num; i++){
			$("#access > div:not(.fixed):eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i + 1)) + "px"}, 800, "easeOutExpo");
		}

		var elem = $("#access > div:not(.fixed):eq(" + (num - 1) + ")");
		$("#access > div:not(.fixed):eq(" + (num - 1) + ")").remove();
		$("div#access").prepend(elem);
		$("#access > div:not(.fixed):eq(0)").dequeue().css("left", (elemBasePos + 500 * (-1) - 140) + "px");
		$("#access > div:not(.fixed):eq(0)").animate({"left": (elemBasePos - 140) + "px"}, 800, "easeOutExpo");
	}
	
	this.showAccessNext = function(){
		$("#access > div:not(.fixed)").dequeue();
		for(i=0; i<num; i++){
			if(i == 1){
				$("#access > div:not(.fixed):eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i - 1) - 140) + "px"}, 800, "easeOutExpo");
			} else {
				$("#access > div:not(.fixed):eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i - 1)) + "px"}, 800, "easeOutExpo");
			}
		}

		var elem = $("#access > div:not(.fixed):eq(0)");
		$("#access > div:not(.fixed):eq(0)").remove();
		$("div#access").append(elem);
		$("#access > div:not(.fixed):eq(" + (num - 1) + ")").dequeue().css("left", (elemBasePos + 500 * (num)) + "px");
		$("#access > div:not(.fixed):eq(" + (num - 1) + ")").animate({"left": (elemBasePos + 500 * (num - 1)) + "px"}, 800, "easeOutExpo");
	}
}

/* Gallery *************************************************************************/
function Gallery(){
	var panelData = "";
	var num;
	var basePos;
	var elemBasePos;
	
	this.init = function(){
		$.getJSON('/?cat=4', {'d': new Date().getTime()}, function(data){
			panelData = data;
			setPanel();
		});
		
		if(iPhone || iPad){
			$("div#gallery div.prevPanelButton").show();
			$("div#gallery div.nextPanelButton").show();
		} else {
			$("div#gallery").hover(function(){
				if(IE){
					$("div#gallery div.prevPanelButton").show();
					$("div#gallery div.nextPanelButton").show();
				} else {
					$("div#gallery div.prevPanelButton").fadeIn("normal");
					$("div#gallery div.nextPanelButton").fadeIn("normal");
				}
			},
			function(){
				if(IE){
					$("div#gallery div.prevPanelButton").hide();
					$("div#gallery div.nextPanelButton").hide();
				} else {
					$("div#gallery div.prevPanelButton").fadeOut("normal");
					$("div#gallery div.nextPanelButton").fadeOut("normal");
				}
			});
		}
		$("div#gallery div.prevPanelButton img").click(this.showGalleryPrev);
		$("div#gallery div.nextPanelButton img").click(this.showGalleryNext);
		$("div#galleryController div.prev img").click(this.showGalleryPrev);
		$("div#galleryController div.next img").click(this.showGalleryNext);
	}
	
	function setPanel(){
		var content;
		var tweetContent;
		var imgs = new Array();
		if(panelData.length > 2){
			content = getPanel("g", panelData[panelData.length - 1]);
			$("div#gallery").append(content);
			for(i=0; i<panelData.length-1; i++){
				content = getPanel("g", panelData[i]);
				$("div#gallery").append(content);
			}
		} else {
			for(i=0; i<panelData.length; i++){
				content = getPanel("g", panelData[i]);
				$("div#gallery").append(content);
			}
		}
		
		for(i=0; i<panelData.length; i++){
			imgs[i] = new CheckLoad("g" + panelData[i].id, panelData[i].image);
		}
		
		num = panelData.length;
		resetPanel();
		fleXenv.initByClass("panelText");
		
		$("div#gallery div.image img").live("click", function(){
			$(this).parent("div").parent("div").parent("div.content").dequeue().animate({"top": "-300px"}, 800, "easeOutExpo");
		});
		$("div#gallery div.moreButton img").live("click", function(){
			$(this).parent("div").parent("div").parent("div.content").dequeue().animate({"top": "-300px"}, 800, "easeOutExpo");
		});
		$("div#gallery div.closeButton img").live("click", function(){
			$(this).parent("div").parent("div").parent("div").parent("div.content").dequeue().animate({"top": 0}, 800, "easeOutExpo");
		});
		if(location.href.indexOf("/?id=g") != -1){
			var specifyId = "#" + location.href.substring(location.href.indexOf("/?id=g") + 5);
			
			if($(specifyId).length == 1){
				$.scrollTo("#gallery", {duration: 700, easing: "easeOutExpo", offset: {top: -20}});
				var specifyPos = 0;
				$("#gallery > .topPanelA01").each(function(e){
					if("#" + $(this).attr("id") == specifyId){
						specifyPos = e;
						console.log("e:" + e);
						console.log("specifyPos:" + specifyPos);
					}
				});
				if(specifyPos == 0){
					g.showGalleryPrev();
				}
				if(specifyPos > 1){
					var j = 1;
					while(j<specifyPos){
						console.log(j);
						g.showGalleryNext();
						j++;
					}
				}
			}
		}
	}
	
	function resetPanel(){
		basePos = document.documentElement.clientWidth < 980 ? Math.round(980 / 2) - 295 : Math.round(document.documentElement.clientWidth / 2) - 295;
		elemBasePos = basePos + 145 - 500;
		$("#galleryController").css("left", basePos + "px");
		for(i=0; i<num; i++){
			if(i == 0){
				$("div#gallery .topPanelA01:eq(" + i + ")").css("left", (elemBasePos + 500 * i - 140) + "px");
			} else {
				$("div#gallery .topPanelA01:eq(" + i + ")").css("left", (elemBasePos + 500 * i) + "px");
			}
		}
	}

	this.resetPanel = function(){
		resetPanel();
	}
	
	this.showGalleryPrev = function(){
		$("div#gallery .topPanelA01").dequeue();
		for(i=0; i<num; i++){
			$("div#gallery .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i + 1)) + "px"}, 800, "easeOutExpo");
		}

		var elem = $("div#gallery .topPanelA01:eq(" + (num - 1) + ")");
		$("div#gallery .topPanelA01:eq(" + (num - 1) + ")").remove();
		$("div#gallery").prepend(elem);
		$("div#gallery .topPanelA01:eq(0)").dequeue().css("left", (elemBasePos + 500 * (-1) - 140) + "px");
		$("div#gallery .topPanelA01:eq(0)").animate({"left": (elemBasePos - 140) + "px"}, 800, "easeOutExpo");
	}
	
	this.showGalleryNext = function(){
		$("div#gallery .topPanelA01").dequeue();
		for(i=0; i<num; i++){
			if(i == 1){
				$("div#gallery .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i - 1) - 140) + "px"}, 800, "easeOutExpo");
			} else {
				$("div#gallery .topPanelA01:eq(" + i + ")").animate({"left": (elemBasePos + 500 * (i - 1)) + "px"}, 800, "easeOutExpo");
			}
		}

		var elem = $("div#gallery .topPanelA01:eq(0)");
		$("div#gallery .topPanelA01:eq(0)").remove();
		$("div#gallery").append(elem);
		$("div#gallery .topPanelA01:eq(" + (num - 1) + ")").dequeue().css("left", (elemBasePos + 500 * (num)) + "px");
		$("div#gallery .topPanelA01:eq(" + (num - 1) + ")").animate({"left": (elemBasePos + 500 * (num - 1)) + "px"}, 800, "easeOutExpo");
	}
}
