$(function() {
	smartRollover();
	opacityRollover();
	externalLink();
	popLink();
	smoothScroll();
	setAccordion();
	setMainPh();

	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
});






/* smartRollover
============================================================================================================ */
function smartRollover() {
	var imgCount = 0;
	var images_pre = new Array();
	$('img[src*="_off."],input[src*="_off."]').each (function(){
		images_pre[imgCount] = new Image();
		images_pre[imgCount].src = $(this).attr("src").replace("_off.", "_on.");
		$(this).hover(
			function () {
				$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
			},
			function () {
				$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
			}
		);
		imgCount ++;
	});
}

/* opacityRollover
============================================================================================================ */
function opacityRollover() {
	$('img[src*="_op."],input[src*="_op."]').hover(
		function () {
			$(this).css('opacity', 0.7);
		},
		function () {
			$(this).css('opacity', 1);
		}
	);
}

/* externalLink
============================================================================================================ */
function externalLink() {
	var notBlank = new Array("hotel-story.ne.jp");

	var n = "";
	for (var i = 0; i < notBlank.length; i ++) if(notBlank[i]) n += ":not([href*='" + notBlank[i] + "'])";
	if(document.domain) n += ":not([href*='" + document.domain + "'])";

	$("a[rel='external'], a[href$='.pdf']").attr("target", "_blank");
	$("a[href^=http]"+n).attr("target", "_blank");
}

/* popLink
============================================================================================================ */
function popLink() {
	$('a[rel=pop]').click( function(e){
		e.preventDefault();
		var wo = window.open($(this).attr('href'), 'map', 'width=740, height=700, menubar=1, toolbar=1, resizable=1 ,scrollbars=1');
		wo.focus();
	})
}


/* smoothScroll
============================================================================================================ */
function smoothScroll() {
	$('a[href^="#"], a[href^="' + location.pathname + '#"]').each (function(){
		var hash = this.hash;
		if(hash.length > 1 && !this['rel']){
			$(this).click(function() {
				goScroll(hash);
				return false;
			})
		}
	});
}
function goScroll(hash) {
	var target = $(hash).offset().top;

	$(($.browser.safari) ? 'body' : 'html')
		.animate({scrollTop: target >= 15 ? target - 15 : target}, 600, 'swing', function(){$(this).unbind("mousewheel DOMMouseScroll");})
		.bind("mousewheel DOMMouseScroll",function(){
			$(this).queue([]).stop();
			$(this).unbind("mousewheel DOMMouseScroll");
		})
}

/* setAccordion
============================================================================================================ */
function setAccordion(){
	$('#sideNavi ul ul').each( function(){
		var ele = $(this);
		var head = $(this).prev();

		if(head.hasClass('current')) $(this).show();

		head.click( function(e){
			e.preventDefault();
			ele.slideToggle(400);
		});

	});
}



/* setMainPh
============================================================================================================ */
function setMainPh() {
	$('.main_phs').each( function(i){
		if($('.thumbs_ph li', this).size() > 0){
			$('.thumbs_ph li', this).append('<div></div>');
			if($('.thumbs_ph li', this).size() > 1){
				phChange('.main_phs:eq(' + i + ') .main_ph li', '.main_phs:eq(' + i + ') .thumbs_ph img');
			}else {
				$('.thumbs_ph li div', this).show();
			}
		}
	});
}
function phChange(l, s){

	var viewTime = 7000;

	var timerID;
	var nowNum;
	var hover_flg = false;
	var cnt = 10;

	$(s).each( function(i){
		$(this).mouseover( function(){
			if(nowNum != i) phView(i, 200, true);
		})
	});

	$(l + ',' + s).hover(
		function(){
			hover_flg = true;
			clearTimeout(timerID);
		},
		function(){
			hover_flg = false;
			clearTimeout(timerID);
			timerID = setTimeout(timerFunc, viewTime);
		}
	)

	function phView(num, time, hf){
		clearTimeout(timerID);
		cnt ++;
		nowNum = num;

		$(s).stop();
		$(s + ' div').stop();

		$(l).stop().each( function(i){
			var thumbs = $(s + ':eq(' + i + ')');
			var border = thumbs.next();
			if(num == i) {
				$(this).show().css({'z-index':cnt, opacity:0}).animate({opacity: 1}, time, 'linear');
				if(hf) {
					thumbs.css('opacity', 1);
					border.show().css('opacity', 1);
				}else {
					thumbs.animate({opacity: 1}, time, 'linear');
					border.show().animate({opacity: 1}, time, 'linear');
				}
			}else {
				if($(this).css('opacity') > 0) $(this).animate({opacity: 0}, time, 'linear');
				if(hf) {
					thumbs.css('opacity', 0.8);
					border.css('opacity', 0).hide();
				}else {
					thumbs.animate({opacity: 0.8}, time, 'linear');
					border.animate({opacity: 0}, time, 'linear', function(){border.hide()});
				}
			}
		})
		timerID = setTimeout(timerFunc, viewTime);
	}


	function timerFunc(){
		if(hover_flg) return;

		var len = $(s).size();
		phView((nowNum+1)%len, 400);
	}

	phView(0, 0);
}




/* contentsSlide
============================================================================================================ */
function contentsSlide(w, c, t, v) {
	var s = c.size();
	var sw = c.width();
	var sh = c.height();

	function slide(n, ff) {
		changeTab(n);

		var t = ff ? 0 : 450;
		if(v) {
			w.stop().animate({top: -(s - 1) * sh + sh * n}, t);
		}else {
			w.stop().animate({left: -sw * n}, t);
		}
	}

	function changeTab(n) {
		t.each( function(i){
			var mt = $(this);
			mt.unbind();
			if(n == i) {
				mt.attr("src", mt.attr("src").replace("_off.", "_on.")).css('cursor', 'default');
			}else {
				mt.attr("src", mt.attr("src").replace("_on.", "_off.")).css('cursor', 'pointer').hover(
					function () {
						$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
					},
					function () {
						$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
					}
				).click( function(){
					slide(i);
				});
			}
		});
	}

	changeTab(0, true);
}




/* contentsChange
============================================================================================================ */
function contentsChange(c, t) {
	var s = c.size();

	function fade(n, ff) {
		changeTab(n);

		var t = ff ? 0 : 300;
		c.each( function(i){
			if(i == n) $(this).stop().animate({opacity: 1}, t, 'linear');
			else $(this).stop().animate({opacity: 0}, t, 'linear');
		});
	}

	function changeTab(n) {
		t.each( function(i){
			var mt = $(this);
			mt.unbind();
			if(n == i) {
				mt.attr("src", mt.attr("src").replace("_off.", "_on.")).css('cursor', 'default');
			}else {
				mt.attr("src", mt.attr("src").replace("_on.", "_off.")).css('cursor', 'pointer').hover(
					function () {
						$(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
					},
					function () {
						$(this).attr("src", $(this).attr("src").replace("_on.", "_off."));
					}
				).click( function(){
					fade(i);
				});
			}
		});
	}

	changeTab(0, true);
}



