var LightboxPage = Class.create();

LightboxPage.prototype = {
	initialize: function() {
		this.d = null;
		this.params = { redirect:false };
		this.direct = false;
		this.LAST_SCROLL_POSITION_PREV = 0;
		this.LAST_SCROLL_POSITION_NEXT = 0;
		this.SCROLL_COUNT = 0;
		this.WINDOW_OPEN = true;
		this.IE6 = false;
		var appname = navigator.appName;
		if (appname == "Microsoft Internet Explorer") {
      		var appver = navigator.appVersion;
			var p = appver.indexOf("MSIE", 0);
			if(p >= 0) {
				var p2 = appver.indexOf(";", p);
				if(p2 >= 0) {
					var iever = appver.substring(p + 5, p2);
					if (parseInt(iever,10) <= 6) {
						this.IE6 = true;
					}
				}
			}
  		}
	},

	show: function(href) {
		this.LAST_SCROLL_COUNT = 0;
		if ($("prev")) 
			$("prev").parentNode.removeChild($("prev"));
		if ($("next")) 
			$("next").parentNode.removeChild($("next"));
		var url;
		if (/http:\/\/|https:\/\//.test(href))
			url = href;
		else
			url = document.location.protocol + "//" + window.location.host + href;
		url += "?" + new Date().getTime();	
		/*
		if (this.IE6 && /brand/.test(url)) {
			url = url.replace(".html", "_ie6.html");
			if (this.WINDOW_OPEN) {
				DisabledWindowDialog();
				url = url.replace(".html", "_open.html");
				w = kl_windowwidth();
				h = kl_windowheight();
				window.open(url, "brand", 'status=no, menubar=no, toolbar=no, resizeable=yes, scrollbars=yes, height=600, width=917');
				return;
			}
		}
		*/
		this.open();

		new Ajax.Request(url, {
		method:"get",
		asynchronous:false,
		onFailure : function(Response) {
			alert("ファイル「"+url+"」の読み込みに失敗しました。");
			if ($("control_overlay"))
				$("control_overlay").parentNode.removeChild($("control_overlay"));
		},
		onException : function (Response) {
			alert("エラーが発生しました。");
			if ($("control_overlay"))
				$("control_overlay").parentNode.removeChild($("control_overlay"));
		},
		onComplete : (function(lp) {
			return function (Response) {
				var s = Response.responseText;
				if (!(/<content>/.test(s) && /<\/content>/.test(s))) 
					return;
				if (/<prev>/.test(s) && /<\/prev>/.test(s)) {
					var prev = s.substring(
					s.indexOf("<prev>")+"<prev>".length,
					s.lastIndexOf("</prev>")
					);
					var n = document.createElement("div");
					var a = document.createElement("a");
					n.setAttribute("id", "prev");
					a.setAttribute("href", "javascript:lp.show('"+prev+"')");
					a.setAttribute("onmouseover", "Element.addClassName(this, 'on')");
					a.setAttribute("onmouseout", "Element.removeClassName(this, 'on')");
					n.appendChild(a);
					document.body.appendChild(n);
				} 
				if (/<next>/.test(s) && /<\/next>/.test(s)) {
					var next = s.substring(
					s.indexOf("<next>")+"<next>".length,
					s.lastIndexOf("</next>")
					);
					var n = document.createElement("div");
					var a = document.createElement("a");
					n.setAttribute("id", "next");
					a.setAttribute("href", "javascript:lp.show('"+next+"')");
					a.setAttribute("onmouseover", "Element.addClassName(this, 'on')");
					a.setAttribute("onmouseout", "Element.removeClassName(this, 'on')");
					n.appendChild(a);
					document.body.appendChild(n);
				} 
				if (lp.direct) {
				//if (/\.html/.test(window.location.pathname)) {
					var title = s.substring(
					s.indexOf("<title>")+"<title>".length,
					s.lastIndexOf("</title>")
					);
					
					window.document.title = title;
				}
				
				var content = s.substring(
				s.indexOf("<content>")+"<content>".length,
				s.lastIndexOf("</content>")
				);
				lp.window_contents.innerHTML = content;
				if (lp.IE6 && $("Relative")) {
					lp.d.options.position = [0, 0];
				}
				lp.d.open();
				if (!(lp.IE6 && $("Relative"))) {
					lp.d.position();
				}
				lp.position();
				lp.listener();
				
				var n = lp.window_contents.getElementsByTagName("img");
				if (n.length) {
					for (var i = 0; i < n.length; i++) {
						var img = new Image();
						img.src = n[i].src;
						Event.observe(img, "load", (function(i) {
							return function(e) {
								i.src = this.src;
							}
						})(n[i]), false);
					}
				}
				//var n = document.getElementsByClassName("lightboxpage");
				var n = document.getElementsByName("lightboxpage");
				if (n.length) {
					for (var i = 0 ; i < n.length; i++) {
						if (n[i].getAttribute("href") != "javascript:void(0)") {
							n[i].setAttribute("title", n[i].getAttribute("href"));
							n[i].setAttribute("href", "javascript:void(0)");
							Event.observe(n[i], "click", (function(lp) {
								return function(e) {
									lp.show(this.getAttribute("title"));
								}
							})(lp), false);
						}
					}
				}
				if (window.gatrackPageview)
					gatrackPageview();
				
			}
		})(this)
		});
	},
	
	bind: function() {
		this.direct = true;
		var s = document.body.innerHTML;
		s = s.replace("<CONTENT>", "<content>");
		s = s.replace("</CONTENT>", "</content>");
		if (!(/<content>/.test(s) && /<\/content>/.test(s))) 
			return;
			
		if ($("pageprev")) {
			var n = document.createElement("div");
			var a = document.createElement("a");
			n.setAttribute("id", "prev");
			a.setAttribute("href", "javascript:lp.show('"+$("pageprev").href+"')");
			a.setAttribute("onmouseover", "Element.addClassName(this, 'on')");
			a.setAttribute("onmouseout", "Element.removeClassName(this, 'on')");
			n.appendChild(a);
			document.body.appendChild(n);
		} 
		if ($("pagenext")) {
			var n = document.createElement("div");
			var a = document.createElement("a");
			n.setAttribute("id", "next");
			a.setAttribute("href", "javascript:lp.show('"+$("pagenext").href+"')");
			a.setAttribute("onmouseover", "Element.addClassName(this, 'on')");
			a.setAttribute("onmouseout", "Element.removeClassName(this, 'on')");
			n.appendChild(a);
			document.body.appendChild(n);
		} 
		
		var content = s.substring(
		s.indexOf("<content>")+"<content>".length,
		s.lastIndexOf("</content>")
		);
		if ($("container")) {
			Element.setStyle($("container"), {visibility:"hidden"});
			//$("container").parentNode.removeChild($("container"));
		}
		
		this.open();
		this.window_contents.innerHTML = content;
		if (this.IE6 && $("Relative")) {
			this.d.options.position = [0, 0];
		}
		this.d.open();
		if (!(this.IE6 && $("Relative"))) {
			this.d.position();
		}
		
		this.position();
		this.listener();
		
		var n = lp.window_contents.getElementsByTagName("img");
		if (n.length) {
			for (var i = 0; i < n.length; i++) {
				var img = new Image();
				img.src = n[i].src;
				Event.observe(img, "load", (function(i) {
					return function(e) {
						i.src = this.src;
					}
				})(n[i]), false);
			}
		}
		
		var n = document.getElementsByName("lightboxpage");
		if (n.length) {
			for (var i = 0 ; i < n.length; i++) {
				if (n[i].getAttribute("href") != "javascript:void(0)") {
					n[i].setAttribute("title", n[i].getAttribute("href"));
					n[i].setAttribute("href", "javascript:void(0)");
					Event.observe(n[i], "click", (function(lp) {
						return function(e) {
							lp.show(this.getAttribute("title"));
						}
					})(this), false);
				}
			}
		}
		
	},
	
	open: function(href) {
		if (this.window_contents)
			this.window_contents.innerHTML = "";
		if (!this.d) {
			var n = document.createElement("div");
			document.body.appendChild(n);
			this.d = this.dialogFactory(n);
		}
	},

	close: function(params) {
		var undefined;		if (params !== undefined) {			for (key in params) {	  			this.params[key] = params[key];	  		} 		}

		if (this.d) {
			if (this.params.redirect) {				if (/show/.test(window.location.pathname) 				|| /contact/.test(window.location.pathname))					window.location.href = window.location.protocol  + "//" + window.location.host;			}	
			if (this.direct) {
			//if (/\.html/.test(window.location.pathname)) {
				window.location.href = (window.location.protocol + "//" + window.location.host + "/?"+ new Date().getTime());
				return;
			}
			this.d.close();
			//this.d.destroy();
			//this.d = null;
			//delete this.d;
			var x, y;
			if (document.all) {
				x = document.body.scrollLeft;
				y = document.body.scrollTop;
			} else {
				x = pageXOffset;
				y = pageYOffset;
			}
			window.scrollBy(-x, -y);
		}
	},
	
	position: function(scroll) {
		var undefined;
		if (!this.d)
			return;
			
		if (this.d) {
			if (scroll == undefined) {
				scroll = false;
				this.SCROLL_COUNT = 0;
				this.SCROLL_STOP_PREV = false;
				this.SCROLL_STOP_NEXT = false;
			} else {
				this.SCROLL_COUNT++;
			}
			
			var o, d, w, h;
			var y = window.document.body.scrollTop  || window.document.documentElement.scrollTop;
			/*
			if (this.IE6 && $("Relative")) {
				o  = $("Relative").cumulativeOffset();
				d  = $("Relative").getDimensions();
				w = kl_windowwidth();
				h = kl_windowheight();
				var t = 0;
				if (d.height < h) 
					t = parseInt((h / 2) - (d.height) / 2, 10);
				var l = parseInt((w / 2) - (d.width / 2), 10); 
				
				if (y && (d.height < h))
					t = y;
				Element.setStyle(this.d.container, {top: t + "px"});
			}
			*/
			
			
			o = this.d.container.cumulativeOffset();
			d = this.d.container.getDimensions();
			h = window.document.body.clientHeight || window.innerHeight;
			if (this.IE6) 
				h = kl_windowheight();

			
			if (y) {
				if (h < d.height)
					Element.setStyle(this.d.container, { top:y+"px" });
				/*
				if (!scroll && (h < d.height)) {
					if (Prototype.Browser.IE) {
						if (this.SCROLL_COUNT <= 1) {
							Element.setStyle(this.d.container, { top:y+"px" });
						}
					} else {
						Element.setStyle(this.d.container, { top:y+"px" });
					}
				}
				*/
			}
			
			
			
			if (Prototype.Browser.IE) {
				if ($("control_overlay")) {
					var oh = window.document.body.scrollHeight || window.document.documentElement.scrollHeight;
					Element.setStyle($("control_overlay"), { height:oh+"px" });
				}
			}
			

			if (!scroll || (d.height < h)) {
				var bo = this.d.container.cumulativeOffset();
				var bd = this.d.container.getDimensions();
				if (this.IE6 && $("Relative")) {
					bo  = $("Relative").cumulativeOffset();
					bd  = $("Relative").getDimensions();
				}
				
				if ($("prev")) {
					var left_prev;
					var top_prev = bo[1] + (bd.height / 2 - (parseInt(Element.getStyle("prev", "height"), 10) / 2));
					if (d.height < h) {
						if (y) {
							top_prev += y;	
						} else {
							if (this.LAST_SCROLL_POSITION_PREV)
								top_prev -= this.LAST_SCROLL_POSITION_PREV;
						}
						this.LAST_SCROLL_POSITION_PREV = y;
					}
					if (Prototype.Browser.IE) {
						if (this.IE6)
							left_prev = bo[0] - parseInt(Element.getStyle("prev", "width"), 10) - 20;
						else
							left_prev = parseInt(bd.width / 2 - (835 / 2) - 60, 10);
					} else {
						left_prev = bo[0] - parseInt(Element.getStyle("prev", "width"), 10) - 20;
					}
					if (Prototype.Browser.IE) {
						if (this.SCROLL_COUNT <= 1) {
							if (!this.SCROLL_STOP_PREV) {
								Element.setStyle($("prev"), {
								left:left_prev + "px",
								top:top_prev + "px",
								display:"block"
								});
								if (scroll)
									this.SCROLL_STOP_PREV = true;
							}							
						}
					} else {
						Element.setStyle($("prev"), {
						left:left_prev + "px",
						top:top_prev + "px",
						display:"block"
						});
					}
					
				}
		
				if ($("next")) {
					var left_next;
					var top_next = bo[1] + (bd.height / 2 - (parseInt(Element.getStyle("next", "height"), 10) / 2));
					if (d.height < h) {
						if (y) {
							top_next += y;	
						} else {
							if (this.LAST_SCROLL_POSITION_NEXT)
								top_next -= this.LAST_SCROLL_POSITION_NEXT;
						}
						this.LAST_SCROLL_POSITION_NEXT = y;
					}
					if (Prototype.Browser.IE) {
						if (this.IE6)
							left_next = bo[0] + bd.width + 20;
						else
							left_next = parseInt(bd.width / 2 + (835 / 2) + 20, 10);
					} else {
						left_next = bo[0] + bd.width + 20;
					}
					if (Prototype.Browser.IE) {
						if (this.SCROLL_COUNT <= 1) {
							if (!this.SCROLL_STOP_NEXT) {
								Element.setStyle($("next"), {
								left:left_next + "px",
								top:top_next + "px",
								display:"block"
								});
								if (scroll)
									this.SCROLL_STOP_NEXT = true;
							}
						}
					} else {
						Element.setStyle($("next"), {
						left:left_next + "px",
						top:top_next + "px",
						display:"block"
						});
					}
				}
			}
			
			if (2 <= this.SCROLL_COUNT) {
				this.SCROLL_COUNT = 0;
				this.SCROLL_STOP_PREV = false;
				this.SCROLL_STOP_NEXT = false;
			}
		}
	},

	listener: function() {
		var n = document.getElementsByClassName("rollover");
		if (n.length) {
			for (var i = 0; i < n.length; i++) {
				Event.observe(n[i], "mouseover", function() {
					if (!/\-on/.test(this.getAttribute("src"))) {
						var src = this.getAttribute("src");
						var suffix = src.substring(src.lastIndexOf('.'), src.length);
						this.setAttribute("src", src.replace(suffix, "-on" + suffix));
					}
				}, false);
				Event.observe(n[i], "mouseout", function() {
					this.setAttribute("src", this.getAttribute("src").replace("-on", ""));
				}, false);
			}
		}

	},

	dialogFactory: function(e) {  
		this.window_contents = new Element("div", {  
	    	className: "window_contents"  
	 	});
		var d = new Control.Modal(e, Object.extend({  
			overlayOpacity:0.8,  
			className:'window',
			afterOpen: function() {
			},
			afterClose: (function(li) {
				return function(e) {
					if ($("prev")) 
						$("prev").parentNode.removeChild($("prev"));
					if ($("next")) 
						$("next").parentNode.removeChild($("next"));
					if (li.direct) {
					//if (/\.html/.test(window.location.pathname)) {
						window.location.href = window.location.protocol  + "//" + window.location.host;
						//window.top.name = "hasco";
						//window.open((window.location.protocol + "//" + window.location.host + "/?"+ new Date().getTime()), "hasco");
					}
				}
			})(this)
	  	}, {}));
		d.container.insert(this.window_contents);
		return d;
	}
}

var XMLUtil = {
	parse: function(text) {
		if (window.ActiveXObject) {
			var doc = new ActiveXObject("MSXML2.DOMDocument");
			doc.async = false;
			doc.loadXML(text);
			return doc;
		} else if (window.DOMParser) {
			return (new DOMParser()).parseFromString(text, "application/xhtml+xml")
		} else {
			return null;
		}
	}
}