
jQuery.noConflict();

(function($) {

	$.fn.alignheight = function(options){
		var settings = {
			target:">div",
			alignheightClass:"alignHeight"
		}
		if(options){
			$.extend(settings, options);
		}
		this.each(function(){
			var height_array = new Array();
			target = $(settings.target,this);
			target.each(function(i){
				$(this).addClass(settings.alignheightClass);
				height_array.push(Number($(this).css("height","auto").height()));
			});
			var Height = height_array.sort(function(a,b){return (a-b);}).pop();
			target.height(Height);
		});
		return this;
	}

	/**
	* Flatten height same as the highest element for each row.
	*
	* Copyright (c) 2011 Hayato Takenaka
	* Dual licensed under the MIT and GPL licenses:
	* http://www.opensource.org/licenses/mit-license.php
	* http://www.gnu.org/licenses/gpl.html
	* @author: Hayato Takenaka (http://urin.take-uma.net)
	* @version: 0.0.2
	**/
	$.fn.tile = function(columns) {
		var tiles, max, c, h, last = this.length - 1, s;
		if(!columns) columns = this.length;
		this.each(function() {
			s = this.style;
			if(s.removeProperty) s.removeProperty("height");
			if(s.removeAttribute) s.removeAttribute("height");
		});
		return this.each(function(i) {
			c = i % columns;
			if(c == 0) tiles = [];
			tiles[c] = $(this);
			h = tiles[c].height();
			if(c == 0 || h > max) max = h;
			if(i == last || c == columns - 1)
				$.each(tiles, function() { this.height(max); });
		});
	};

	$.fn.blocklink = function(options){
		var settings = {
			hoverClass:"bl-hover",
			nth:1,
			wraplinks:1
		}
		if(options){
			$.extend(settings, options);
		}
		$(this).each(function(){
			var Link = $("a:eq("+(settings.nth-1)+")",this);
			var Href = Link.attr("href");
			if(Href!=undefined){
				var Target = Link.attr("target");
				$(this).mouseover(function(){
					$(this).addClass(settings.hoverClass);
				}).mouseout(function(){
					$(this).removeClass(settings.hoverClass);
				}).click(function(){
					if(Target=="" || Target==undefined){
						Target="_self";
					}
					if(!bAnchorHover){
						window.open(Href,Target);
					}
				}).css("cursor","pointer");
				var bAnchorHover=false;
				$("a",this).hover(
					function(){bAnchorHover=true},
					function(){bAnchorHover=false}
				);
				if(!settings.wraplinks){
					$("a",this).click(function(){
						window.open(Href,Target);
						return false;
					});
				}
			}
		});
		return this;
	}

	$.fn.extend({
		randomdisplay : function(num) {
			return this.each(function() {
				var chn = $(this).children().hide().length;
				for(var i = 0; i < num && i < chn; i++) {
					var r = parseInt(Math.random() * (chn - i)) + i;
					$(this).children().eq(r).show().prependTo($(this));
				}
			});
		}
	});

	$(function(){
		switch($("body").attr("class")){
			case "junrei" :
				var menu = $("#menu1 a");
				break;
			case "chigai" :
				var menu = $("#menu2 a");
				break;
			case "miryoku" :
				var menu = $("#menu3 a");
				break;
		}
		if(menu!=undefined){
			menu.addClass("over");
		}

		$("img.over").mouseover(function(){
			$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2").replace("_o_o","_o"));
		}).mouseout(function(){
			if(fontSize!=$(this).attr("id")){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"));
			}
		}).each(function(){
			$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"));
		}).click(function(){
			$(this).attr("src",$(this).attr("src").replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"));
		});

		// フォントサイズの切り替え
		if($.cookie("font-size")){
			var fontSize = $.cookie("font-size");
			$("#inbox").addClass(fontSize);
		}else{
			var fontSize = "size1";
		}
		$("#"+fontSize).attr("src",$("#"+fontSize).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"));
		if($("#size1")!=undefined){
			$("#size1").click(function(){
				$("#inbox").removeClass(fontSize);
				$("#"+fontSize).attr("src",$("#"+fontSize).attr("src").replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"));
				fontSize = "size1";
				$("#inbox").addClass(fontSize);
				$.cookie("font-size",fontSize,{path:"/"});
				$("#"+fontSize).attr("src",$("#"+fontSize).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"));
			});
			$("#size2").click(function(){
				$("#inbox").removeClass(fontSize);
				$("#"+fontSize).attr("src",$("#"+fontSize).attr("src").replace(/^(.+)_o(\.[a-z]+)$/, "$1$2"));
				fontSize = "size2";
				$("#inbox").addClass(fontSize);
				$.cookie("font-size",fontSize,{path:"/"});
				$("#"+fontSize).attr("src",$("#"+fontSize).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_o$2"));
			});
		}
		$("#fontsize a").click(function(){
			return false;
		});

		if(location.href.indexOf("file")==0){
			var url = "file://" + location.hostname + location.pathname + location.search;
		}else if(location.href.indexOf("https")==0){
			var url = "https://" + location.hostname + location.pathname + location.search;
		}else{
			var url = "http://" + location.hostname + location.pathname + location.search;
		}
		$("a[href*=#],area[href*=#]").click(function() {
			var id = this.href.substring(this.href.indexOf('#'),this.href.length);
			var hrefUrl = this.href.split(id);
			if(String(hrefUrl[0]) == String(url)) {
				var hash = this.hash;
				if(!hash || hash == "#")
					return false;
				if(id == '#inbox') { 
					$($.browser.safari ? 'body' : 'html')
						.animate({scrollTop: 0}, 500, "easeInOutCubic");
					return false;
				}else if(id != "#"){
					$($.browser.safari ? 'body' : 'html')
						.animate({scrollTop: $(hash).offset().top}, 500, "easeInOutCubic");
					return false;
				}
			}
		});

		$("[randomdisplay]").each(function() {
			$(this).randomdisplay($(this).attr("randomdisplay"));
		});

		$(".text-field").focus(function(){
			$(this).css("background", "#FFE7DF");
		});
		$(".text-field").blur(function(){
			$(this).css("background", "#ffffff");
		});

		$("a img.links, .side-btns a img, .link-left a img, #home #reco-tour img").hover(function(){
			$(this).animate({ opacity: .8 },{ duration: 100, queue: false })
		}, function(){
			$(this).animate({ opacity: 1 },{ duration: 100, queue: false })
		});

		$("#home #reco-tour li, #box-index dl").hover(function(){
			$("img[src$='.jpg'], img[src$='.gif']",this).animate({ opacity: .8 },{ duration: 100, queue: false })
		}, function(){
			$("img[src$='.jpg'], img[src$='.gif']",this).animate({ opacity: 1 },{ duration: 100, queue: false })
		});

		$("#home #reco-tour li").blocklink();
		$("#box-calendar td").blocklink();
		$("#box-index dl").blocklink();

		// 文字サイズ変更で高さ取得
		var h, $e = $('<div>', {text:'A', fontSize:'1em'}).hide().appendTo('#inbox');
		setInterval(function() {
			if(h != $e.css('font-size')) {
				h = $e.css('font-size');
				$.each(['#reco-tour li p.caption','#home #box-plan div div','#box-calendar td','#ticket #point dd'], function(i, v) {
					$(v).css('height', 'auto').each(function() {
						if(this.removeProperty) this.removeProperty('height');
						if(this.removeAttribute) this.removeAttribute('height');
					}).tile();
				});
				$.each(['#box-index dl'], function(i, v) {
					$(v).css('height', 'auto').each(function() {
						if(this.removeProperty) this.removeProperty('height');
						if(this.removeAttribute) this.removeAttribute('height');
					}).tile(2);
				});
			}
		}, 2*$.fx.interval);

	});

})(jQuery);
