// JavaScript Document

current_floor = 1;
current_type = false;

/* ready */

/* initialize */
function initialize(){
	$("body").append('<div id="elevator-frame"></div>');
	//$("#wrapper").append('<div class="window"></div>');
	$("body").append('<div class="window"></div>');
		$(".window").hide();
	$("body").append('<div id="under"></div>');
		$("#under").hide();
	setCenter($(".window"));
}

function setUnderSize(){
	$("#under").css({"width": $(window).width(), "height": $(window).height() + $(window).scrollTop(), "top": 0, "left": 0});
}
/* title-animate */
function showTitleAnimate(){
	/*$.timer(3000, function(){
		$("h1").jOla({size: false, speed: 150, expansion: 11, rStart: 255, gStart: 255, bStart: 255, rStep: 255, gStep: 255, bStep: 255, bottomstart: 4, bottomadd:-2});
	});*/
}

/* floor */
function preload(imgs){
	$(imgs).each(function(){
		$('<img/>')[0].src = this;
	});
}

function setElevator(){
	$(".elevator").click(function(){
		$("#under").show("slide", 1, function(){
			$.getJSON($(".elevator a").attr("href") + "?j", function(res){
				var offset = $(".elevator").offset();
				$("#elevator-frame").css({top: offset.top, left: offset.left});
				$("#elevator-frame").html(res.content).show("blind", 100, function(){
					$("#elevator-frame").draggable();
					$("#elevator-frame img").click(function(){
						closeElevator();
					});
				});
			});
			$("#under").css({"width": $(document).width(), "height": $(document).height(), "top": 0, "left": 0});
			$("#under").unbind('click').click(function(){
				closeElevator();
			});
		});
		return false;
	});
}

function closeElevator(){
	$("#elevator-frame").hide("blind", 100);
	$("#under").hide();;
}

function setFloor(){
	$(".floor a").click(function(){
		moveFloor($(this).attr("accesskey"));
		return false;
	});
}

/* menu */
function hoverMenu(){
	$(".menu img").hover(
		function(){
			this.src = this.src.replace(".gif", "_over.gif");
		},
		function(){
			this.src = this.src.replace("_over.gif", ".gif");
		}
	);
	$(".elevator img").hover(
		function(){
			this.src = this.src.replace(".gif", "_over.gif");
		},
		function(){
			this.src = this.src.replace("_over.gif", ".gif");
		}
	);
}
function setMenu(){
	$(".menu").unbind('click').click(function(){
		showMenuContent(this);
		return false;
	});
}

function showMenuContent(element){
	$.getJSON($("#" + element.id + " a").attr("href") + "?j", function(res){
		openWindow(element.id, res.content);
		//$("title").html(res.title);
	});
}

/* window-open */
function openWindow(element, content){
	$("#under").show("blind", 1, function(){
		$(".window").append('<div class="close"></div><div class="window-content"></div>');
			$(".window").attr("class", "window " + element).show("blind", 300, function(){
				$(".window").draggable();
				$(".window-content").html(content);
			});
		$(".close").unbind('click').click(function(){
			closeWindow();
		});
		/*$("#under").unbind('click').click(function(){
			closeWindow();
		});*/
		$("#under").css({"width": $(document).width(), "height": $(document).height(), "top": 0, "left": 0});
		$("#under").unbind('click').click(function(){
			closeWindow();
		});
	});
}

function setCenter(element){
	element.css({left: ( $(document).width() / 2 ) - ( element.width() / 2 )});
}

/* window-close */
function closeWindow(){
	$(".window").hide(200).empty("");
	$("#under").hide();
}

/* get-zIndex */
function getZindex(className){
	var z = 0;
	var current = 0;
	$("." + className).each(function(){
		current = parseInt(this.style.zIndex);
		if(current > z) z = current;
	});
	return z + 10;
}
	/* anchor */
	function setAnchor(){
		$("a.anchor").unbind('click').click(function(){

			$("#cast-frame").empty("").hide("blind", 200);
			$("#cast-side").empty("").hide("blind", 200);

			if(!document.getElementById("panel-frame")){
				$(".window").append('<div id="panel-frame" class="window-main"><div id="panel-content"><div class="main-close"></div></div></div>');
			}else{
				$("#panel-frame").empty("");
				$("#panel-frame").append('<div id="panel-content"><div class="main-close"></div></div>');
			}
			$("#panel-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo");

			$.getJSON(this.href + "?j", function(res){
				$("#panel-content .main-close").unbind('click').click(function(){
					//alert("a");
					if(document.getElementById("panel-map")){
						$("#panel-map").animate({"width": 0, left: "+=226"}, 150, function(){
							$(this).html("");
							$("#panel-frame").animate({top: "-=494"}, 200, function(){
								$(this).hide().empty("");
							});
						});
					}else{
						$("#panel-frame").animate({top: "-=494"}, 200, function(){
							$(this).hide().empty("");
						});
					}
				});
				$("#panel-content").append(res.content);
				//setAnchorCast();
			});
			return false;
		});
	}

	function showClientMap(account){
		if(!document.getElementById("panel-map")){
			$(".window").append('<div id="panel-map" class="window-side"></div>');
		}

		//if($("#panel-map").html() == ""){
			$.getJSON("/panel/" + account + "/map?j", function(res){
				$("#panel-map").css({width: 0, left: 0, zIndex: getZindex('window-side')}).animate({width: 226, left: "-=215"}, 300, "easeOutExpo", function(){$("#panel-map").html(res.content)});
			});
		//}
	}
	/* cast */
	function setAnchorCast(){
		$("a.anchor-cast").unbind('click').click(function(){
			if(!document.getElementById("cast-frame")){
				$(".window").append('<div id="cast-frame" class="window-main"><div id="cast-content"><div class="main-close"></div></div></div>');
			}else{
				$("#cast-frame").empty("");
				$("#cast-frame").append('<div id="cast-content"><div class="main-close"></div></div>');
			}
			$("#cast-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo");

			$.getJSON(this.href + "?j", function(res){
				$("#cast-frame .main-close").unbind('click').click(function(){
					$("#cast-side").animate({"width": 0, left: "+=139"}, 150, function(){
						$(this).empty("");
						$("#cast-frame").animate({top: "-=494"}, 200, function(){
							$(this).hide().empty("");
						});
					});
				});
				$("#cast-content").append(res.content);
			});
			return false;
		});
	}

	function showCastClient(account){
		if(!document.getElementById("cast-side")){
			$(".window").append('<div id="cast-side" class="window-side"></div>');
		}

		if($("#cast-side").html() == ""){
			$.getJSON("/cast/" + account + "/client?j", function(res){
				$("#cast-side").css({width: 0, left: 0, zIndex: getZindex('window-side')}).animate({width: 150, left: "-=139"}, 300, "easeOutExpo", function(){$("#cast-side").html(res.content)});
			});
		}
	}

	/* blog */
	function moveBlogList(){
		$(".blog-carousel").animate({left: "+=360"}, 500, "easeInCubic", function(){
			setAnchorBlog();
			setAnchorCast();
			setAnchor();
		});
	}

	function setAnchorBlog(list){
		$("a.anchor-blog").unbind("click").click(function(){
			if(!document.getElementById("blog-frame")){
				$(".window").append('<div id="blog-frame" class="window-main"><div id="blog-content"><div class="main-close"></div></div></div>');
			}else{
				$("#blog-frame").empty("");
				$("#blog-frame").append('<div id="blog-content"><div class="main-close"></div></div>');
			}
			//$("#blog-frame").css("z-index", getZindex());
			$("#blog-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo");
			//$("#blog-frame").show("blind", 300);

			$.getJSON(this.href + "?j", function(res){
				$("#blog-frame .main-close").unbind('click').click(function(){
					$("#blog-side").animate({"width": 0, left: "+=139"}, 150, function(){
						$(this).empty("");
						$("#blog-frame").animate({top: "-=494"}, 200, function(){
							$(this).hide().empty("");
						});
					});
				});
				$("#blog-content").append(res.content);
			});
			return false;
		});
	}

	function showBlogCast(account){
		/*if(!current_blog){
			var current_blog = account;
			alert(current_blog);
		}*/

		if(!document.getElementById("blog-side")){
			$(".window").append('<div id="blog-side" class="window-side"></div>');
		}

		//if($("#blog-side").html() == ""){
		//if(current_blog != account){
			$.getJSON("/blog/" + account + "/cast?j", function(res){
				$("#blog-side").css({width: 0, left: 0, zIndex: getZindex('window-side')}).animate({width: 150, left: "-=139"}, 300, "easeOutExpo", function(){$("#blog-side").html(res.content)});
			});
		//}
	}

	/* event */
	function moveEventList(){
		$(".event-carousel").animate({left: "+=360"}, 500, "easeInCubic");
		setAnchorEvent();
		setAnchor();
	}

	function setAnchorEvent(){
		$("a.anchor-event").unbind("click").click(function(){
			if(!document.getElementById("event-frame")){
				$(".window").append('<div id="event-frame" class="window-main"><div id="event-content"><div class="main-close"></div></div></div>');
			}else{
				$("#event-frame").empty("");
				$("#event-frame").append('<div id="event-content"><div class="main-close"></div></div>');
			}
			$("#event-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo");

			$.getJSON(this.href + "?j", function(res){
				$("#event-frame .main-close").unbind('click').click(function(){
					//$("#event-frame").hide("blind", 300).empty("");
					//$("#event-side").empty("").hide("blind", 200);
					$("#event-side").animate({"width": 0, left: "+=139"}, 150, function(){
						$(this).empty("");
						$("#event-frame").animate({top: "-=494"}, 200, function(){
							$(this).hide().empty("");
						});
					});
				});
				$("#event-content").append(res.content);
			});
			return false;
		});
	}

	function showEventClient(account){
		if(!document.getElementById("event-side")){
			$(".window").append('<div id="event-side" class="window-side"></div>');
		}

		if($("#event-side").html() == ""){
			$.getJSON("/event/" + account + "/client?j", function(res){
				//$("#event-side").show("blind", 300, function(){$("#event-side").html(res.content)});
				$("#event-side").css({width: 0, left: 0, zIndex: getZindex('window-side')}).animate({width: 150, left: "-=139"}, 500, "easeOutExpo", function(){
					$("#event-side").html(res.content)
				});
			});
		}
	}

	/* list */
	function moveClientList(){
		$(".move-content").animate({left: "+=360"}, 500, "easeInCubic");
		setAnchor();
	}

	/* top */
	function setAnchorNewBlog(){
		$("a.anchor-new-blog").unbind("click").click(function(){
			var req = this.href;
			$.getJSON("/blog?j", function(res){
				openWindow('blog1', res.content);
				if(!document.getElementById("blog-frame")){
					$(".window").append('<div id="blog-frame" class="window-main"><div id="blog-content"><div class="main-close"></div></div></div>');
				}else{
					$("#blog-frame").empty("");
					$("#blog-frame").append('<div id="blog-content"><div class="main-close"></div></div>');
				}
				$("#blog-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo", function(){
					$.getJSON(req + "?j", function(res){
						$("#blog-frame .main-close").unbind('click').click(function(){
							$("#blog-frame").hide("blind", 300).empty("");
							$("#blog-side").empty("").hide("blind", 200);
						});
						moveBlogList();
						$("#blog-content").append(res.content);
					});
				});

			});
			return false;
		});
	}
	function setAnchorNewEvent(){
		$("a.anchor-new-event").unbind("click").click(function(){
			var req = this.href;
			$.getJSON("/event?j", function(res){
				openWindow('calendar1', res.content);
				if(!document.getElementById("event-frame")){
					$(".window").append('<div id="event-frame" class="window-main"><div id="event-content"><div class="main-close"></div></div></div>');
				}else{
					$("#event-frame").empty("");
					$("#event-frame").append('<div id="event-content"><div class="main-close"></div></div>');
				}
				$("#event-frame").css({ height: 494, top: -385, zIndex: getZindex('window-main'), display: "block"}).animate({top: "+=494"}, 500, "easeOutExpo", function(){
					$.getJSON(req + "?j", function(res){
						$("#event-frame .main-close").unbind('click').click(function(){
							$("#event-frame").hide("blind", 300).empty("");
							$("#event-side").empty("").hide("blind", 200);
						});
						moveEventList();
						$("#event-content").append(res.content);
					});
				});

			});
			return false;
		});
	}

	/* mail */
	function setAnchorMail(){
		$(".anchor-mail").click(function(){
			if(!document.getElementById("caution")){
				$(".window").append('<div id="caution" class="window-caution"><div id="caution-content"></div><div class="caution-close"><img src="/img/elevator/bt_back.gif" alt="閉じる" /></div></div>');
			}else{
				$("#caution").empty("").append('<div id="caution-content"></div><div class="caution-close"><img src="/img/elevator/bt_back.gif" alt="閉じる" /></div>').show();
			}

			var content = '<p>キャストへのメールは、ギャルミークラブ会員のみ送信できます。ギャルミークラブに会員登録されていないメールアドレスからメールを送信しても、キャストには届きませんのでご注意ください。</p><p>また、ドメイン指定受信をされている場合は、【galmy.jp】ドメインを受信許可してください。キャストからの返信メールを受け取ることが出来ません。</p><div><a href="' + this.href + '">メールを送る</a></div><p>会員登録は以下を読み取り空メールを送信</p><div><img src="/img/club/chart.png" alt="QRコード" /></div>';
			$("#caution-content").html(content);

			$(".caution-close").unbind("click").click(function(){
				$("#caution").hide().empty();
			});

			return false;
		});
	}

	/* guide */
	function setAnchorGuide(){
		/*$(".anchor-guide").click(function(){
			$.getJSON(this.href + "?j", function(res){
				openWindow('guide', res.content);
			});

			return false;
		});*/
	}

/* floor */
function moveFloor(floor){
	if(floor == current_floor) return false;
	closeElevator();
	$.getJSON("/floor/" + floor + "?j", function(res){
		if(floor == 1){
			current_type = false;
			var bg = "/img/1f/floor/1.jpg";
		}else{
			current_type = res.type;
			var rand = Math.floor( Math.random() * 3 ) + 1;
			var bg = "/img/2f/floor/" + current_type + "_" + rand + ".jpg";
		}

		if(floor > current_floor){
			$("#floor").animate({height: 709, top: "-=709"}, 800, "easeInCubic", function(){
				$("h1").html(res.title);
				$("#floor_num img").attr("src", "/img/floor/" + res.floor + ".gif");
				$("#menu").html(res.content);
				$("#floor").css({ height: 0, top: 709, background: "url(" + bg + ")"})
					.animate({height: 709, top: "-=709"}, 800, "easeOutExpo");
				setMenu();
				setElevator();
				hoverMenu();
				showInformation();
				setAnchorGuide();
			});
		}else if(floor < current_floor){
			$("#floor").animate({height: 0, top: "+=709"}, 800, "easeInCubic", function(){
				$("h1").html(res.title);
				$("#floor_num img").attr("src", "/img/floor/" + res.floor + ".gif");
				$("#menu").html(res.content);
				$("#floor").css({ height: 709, top: -709, background: "url(" + bg + ")"})
					.animate({top: "+=709"}, 800, "easeOutExpo");
				setMenu();
				setElevator();
				hoverMenu();
				showInformation();
				setAnchorGuide();
			});
		}
		current_floor = floor;
	});
}
	function showInformation(){
		if(current_floor == 1){
			$("#event").show();
			$("#information").show();
		}else{
			$("#event").hide();
			$("#information").hide();
		}
	}

/* search */

function showSearchResult(){
	var param = "";
	$("#search-form :input").each(function(){
		if(this.type == "checkbox"){
			if(this.checked){
				param += "&" + this.name + "=" + this.value;
			}
		}else{
			if(this.name && this.value){
				param += "&" + this.name + "=" + this.value;
			}
		}
	});
	$.getJSON("/search/result?j" + param, function(res){
		$(".window-content").html(res.content);
	});
}

/* map */
function showMap(x, y){
	if (GBrowserIsCompatible()) {
		maps = new GMap2(document.getElementById("gmap"));
		maps.addControl(new GLargeMapControl());
		maps.addControl(new GMapTypeControl());
		maps.enableScrollWheelZoom();
		//maps.addControl(new svControl());
		maps.setCenter(new GLatLng(x,y), 17);
		var bounds = maps.getBounds();
		$(".window-content").append('<div id="map-frame"></div>');

		//origine-marker
			b_icon = new GIcon();
			b_icon.image = "http://www.galmy.jp/img/map/build.png";
			b_icon.shadow = "http://www.galmy.jp/img/map/build_shadow.png";
			b_icon.iconSize = new GSize(35, 29);
			b_icon.shadowSize = new GSize(35, 29);
			b_icon.iconAnchor = new GPoint(0, 35);
			b_icon.infoWindowAnchor = new GPoint(18, 11);

			g_icon = new GIcon();
			g_icon.image = "http://www.galmy.jp/img/map/galmy.png";
			g_icon.shadow = "http://www.galmy.jp/img/map/galmy_shadow.png";
			g_icon.iconSize = new GSize(35, 29);
			g_icon.shadowSize = new GSize(35, 29);
			g_icon.iconAnchor = new GPoint(0, 35);
			g_icon.infoWindowAnchor = new GPoint(18, 11);

		var maptypes = maps.getMapTypes();
		for(var i = 0; i < maptypes.length; i++){
		  if(maptypes[i].uc == 'm'){
			maptypes[i].getMaximumResolution = function(){
			  return 22;
			};
			continue;
		  }
		}
	}
	setMarkers();
}

function setMarker(id, x, y, label, icon, event){
	var point = new GLatLng(x, y);
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(label);
		if(event){
			$.getJSON('/map/build/' + id + "?j", function(res){
			$("#map-frame").html(res.content);
		});
		}
	});
	maps.addOverlay(marker);
}

function setMapCenter(x, y){
	maps.panTo(new GLatLng(x,y), 17);
}

function showMapMini(x, y){
	if (GBrowserIsCompatible()) {
		m_maps = new GMap2(document.getElementById("gmap-mini"));
		m_maps.addControl(new GLargeMapControl());
		m_maps.addControl(new GMapTypeControl());
		m_maps.enableScrollWheelZoom();
		//maps.addControl(new svControl());
		m_maps.setCenter(new GLatLng(x,y), 17);
		var bounds = m_maps.getBounds();
		//origine-marker
			b_icon = new GIcon();
			b_icon.image = "http://www.galmy.jp/img/map/build.png";
			b_icon.shadow = "http://www.galmy.jp/img/map/build_shadow.png";
			b_icon.iconSize = new GSize(35, 29);
			b_icon.shadowSize = new GSize(35, 29);
			b_icon.iconAnchor = new GPoint(0, 35);
			b_icon.infoWindowAnchor = new GPoint(18, 11);
	}
}

function setMarkerMini(id, x, y, label){
	var point = new GLatLng(x, y);
	var marker = new GMarker(point, b_icon);
	/*GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(label);
		$.getJSON('/map/build/' + id + "?j", function(res){
			$("#map-frame").html(res.content);
		});
	});*/
	m_maps.addOverlay(marker);
}


