/*
 * yuga.js 0.6.0 - 優雅なWeb制作のためのJS
 *
 * Copyright (c) 2007 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2006-10-30
 * Modified:  2008-05-09
 *
 * jQuery 1.2.3
 * ThickBox 3.1
 */

/*
 * [使用方法] XHTMLのhead要素内で次のように読み込みます。
 
<link rel="stylesheet" href="css/thickbox.css" type="text/css" media="screen" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/thickbox.js"></script>
<script type="text/javascript" src="js/yuga.js" charset="utf-8"></script>

 */

(function($) {

	$(function() {
		// $.yuga.selflink();
		// $.yuga.rollover();
		// $.yuga.externalLink();
		// $.yuga.thickbox();
		// $.yuga.scroll();
		// $.yuga.tab();
		// $.yuga.stripe();
		// $.yuga.css3class();
	});

	//---------------------------------------------------------------------

	$.yuga = {
		// URIを解析したオブジェクトを返すfunction
		Uri: function(path){
			this.originalPath = path;
			//絶対パスを取得
			this.absolutePath = (function(){
				var e = document.createElement('span');
				e.innerHTML = '<a href="' + path + '" />';
				return e.firstChild.href;
			})();
			//絶対パスを分解
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]]; 
			}
		},
		//現在のページと親ディレクトリへのリンク
		selflink: function (options) {
			var c = $.extend({
				hoverClass:'btn',
				selfLinkClass:'current',
				parentsLinkClass:'parents-link',
				postfix: '_stay'
			}, options);
			$('a[href]').each(function(){
				var href = new $.yuga.Uri(this.getAttribute('href'));
				var setImgFlg = false;
				if ((href.absolutePath == location.href) && !href.fragment) {
					//同じ文書にリンク
					$(this).addClass(c.selfLinkClass);
					setImgFlg = true;
				} else if (0 <= location.href.search(href.absolutePath)) {
					//親ディレクトリリンク
					$(this).addClass(c.parentsLinkClass);
					setImgFlg = true;
				}
				if (setImgFlg){
					//img要素が含まれていたら現在用画像（_stay）に設定
					$(this).find('img').each(function(){
						//ロールオーバークラスが設定されていたら削除
						$(this).removeClass(c.hoverClass);
						this.originalSrc = $(this).attr('src');
						this.currentSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, c.postfix+"$1");
						$(this).attr('src',this.currentSrc);
					});
				}
			});
		},
		//ロールオーバー
		rollover: function(options) {
			var c = $.extend({
				hoverSelector: '.btn',
				groupSelector: '.btngroup',
				postfix: '_over'
			}, options);
			//ロールオーバーするノードの初期化
			$(c.hoverSelector).each(function(){
				this.originalSrc = $(this).attr('src');
				this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)$/, c.postfix+"$1");
				this.rolloverImg = new Image;
				this.rolloverImg.src = this.rolloverSrc;
			});
			//通常ロールオーバー
			$(c.hoverSelector).not($(c.groupSelector+' '+c.hoverSelector)).hover(function(){
				$(this).attr('src',this.rolloverSrc);
			},function(){
				$(this).attr('src',this.originalSrc);
			});
			//グループ化されたロールオーバー
			$(c.groupSelector).hover(function(){
				$(this).find(c.hoverSelector).each(function(){
					$(this).attr('src',this.rolloverSrc);
				});
			},function(){
				$(this).find(c.hoverSelector).each(function(){
					$(this).attr('src',this.originalSrc);
				});
			});
		},
		//外部リンクは別ウインドウを設定
		externalLink: function(options) {
			var c = $.extend({
				windowOpen:true,
				externalClass: 'external'
			}, options);
			var e = $('a[href^="http://"]');
			if (c.windowOpen) {
				e.click(function(){
					window.open(this.href, '_blank');
					return false;
				});
			}
			e.addClass(c.externalClass);
		},
		//画像へ直リンクするとthickboxで表示(thickbox.js利用)
		thickbox: function() {
			try {
				tb_init('a[@href$=".jpg"], a[@href$=".gif"], a[@href$=".png"]');
			} catch(e) {
			}	
		},
		//ページ内リンクはするするスクロール
		scroll: function(options) {
			//ドキュメントのスクロールを制御するオブジェクト
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60
				}, options);
				c.ms = Math.floor(1000/c.fps);
				var timerId;
				var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
				};
				//スクロール中に実行されるfunction
				function move() {
					if (param.stepCount == c.step) {
						//スクロール終了時
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						//通常スクロール時
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					}
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);
						param.lastY = param.startY;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();
			$('a[href^=#], area[href^=#]').each(function(){
				this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
			}).click(function(){
				var target = $('#'+this.hrefdata.fragment);
				if (target.length) {
					scroller.set({
						endY: target.offset().top
					});
					return false;
				}
			});
		},
		//タブ機能
		tab: function(options) {
			var c = $.extend({
				tabNavSelector:'.tab-nav',
				activeTabClass:'active'
			}, options);
			$(c.tabNavSelector).each(function(){
				var tabNavList = $(this).find('a[href^=#], area[href^=#]');
				var tabBodyList;
				tabNavList.each(function(){
					this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
					var selecter = '#'+this.hrefdata.fragment;
					if (tabBodyList) {
						tabBodyList = tabBodyList.add(selecter);
					} else {
						tabBodyList = $(selecter);
					}
					$(this).unbind('click');
					$(this).click(function(){
						tabNavList.removeClass(c.activeTabClass);
						$(this).addClass(c.activeTabClass);
						tabBodyList.hide();
						$(selecter).show();
						return false;
					});
				});
				tabBodyList.hide()
				tabNavList.filter(':first').trigger('click');
			});
		},
		//奇数、偶数を自動追加
		stripe: function(options) {
			var c = $.extend({
				oddClass:'odd',
				evenClass:'even'
			}, options);
			$('ul, ol').each(function(){
				//JSでは0から数えるのでevenとaddを逆に指定
				$(this).children('li:odd').addClass(c.evenClass);
				$(this).children('li:even').addClass(c.oddClass);
			});
			$('table').each(function(){
				$(this).children('tr:odd').addClass(c.evenClass);
				$(this).children('tr:even').addClass(c.oddClass);
			});
		},
		//css3のクラスを追加
		css3class: function() {
			//:first-child, :last-childをクラスとして追加
			$('body :first-child').addClass('first-child');
			$('body :last-child').addClass('last-child');
			//css3の:emptyをクラスとして追加
			$('body :empty').addClass('empty');
		}
	};
})(jQuery);
$(document).ready(function() {

$('button').focus(function(){this.blur();});

$('div.specList ul li').before('<li class="line">|</li>');
$('div.specList ul li').css('font-style','italic');
$('div#languageselector ul li').before("<li>/</li>");
$('div.specList ul li:first-child').remove();
$('div#languageselector ul li:first-child').remove();
// slideToggle
// $('body.news li.expanded ul').eq(1).css('display','none');
// $('body.news li.expanded').hover(function(){
// $(this).css({'cursor':'pointer','color':'#867C72'})
// });
$('body.news li.expanded').eq(1).click(function(){
$('body.news li.expanded ul').eq(1).slideToggle('normal');
});
// input form
$(function(){
    $("div.inputBox input,#main input").not('.nextInput,.preInput').focus(function() {
        if($(this).val() == $(this).attr('defaultValue'))
            $(this).val('');
    }).blur(function() {
        if(jQuery.trim($(this).val()) == "") {
            $(this).val($(this).attr('defaultValue'));
        }
    });
});
$('div.configurationBox select').find('option:odd').css('background-color','#f0f1f1');
// To be Enable Hover Effect on IE6
$('input.nextInput,input.preInput').mouseover(function(){$(this).css({'text-decoration':'underline','color':'#867C72'})});
$('input.nextInput,input.preInput').mouseout(function(){$(this).css({'text-decoration':'none','color':'#B3ADAA'})});

$('input[type=submit]').mouseover(function(){$(this).css({'text-decoration':'underline','color':'#867C72'})});
$('input[type=submit]').mouseout(function(){$(this).css({'text-decoration':'none','color':'#B3ADAA'})});

// regionalNavigation divide Lines

$('#regionalNavigation > ul > li.expanded > ul,#regionalNavigation > ul > li.selected > ul').append('<li class="divideLine">---</li>');

$('#regionalNavigation ul li ul li a,#regionalNavigation ul li.selected ul li a,#regionalNavigation ul li ul li ul li a,#regionalNavigation ul li.expanded ul li.selected span').prepend('<span class="indent">- </span>');



// Configuration Elements

$('input[type=checkbox],input[type=radio]').css('border','none');
//$('div#bookingCalendar div.inputBox input[value=Navn],div#bookingCalendar div.inputBox input[value=Telefon],div#bookingCalendar div.inputBox input[value=Epost]').css({'width':'159px','margin-bottom':'5px','padding-right':'0'});

// Arrangementer Show Up TextArea

$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div.inputBox textarea').css({'display':'none'});
$('body.events div#secondary-contents div.inputBox input[value=other],body.eventroompresentation div#secondary-contents div.inputBox input[value=other],div#frontBox div.inputBox input[value=other]').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div.inputBox textarea').fadeIn('normal');
});
$('body.events div#secondary-contents div.inputBox input[value=other],body.eventroompresentation div#secondary-contents div.inputBox input[value=other],div#frontBox div#configurationPop div.inputBox input[value=other]').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeIn('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(0),body.eventroompresentation div#secondary-contents div.inputBox input:eq(0),div#frontBox div#configurationPop div.inputBox input:eq(0)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(1),body.eventroompresentation div#secondary-contents div.inputBox input:eq(1),div#frontBox div#configurationPop div.inputBox input:eq(1)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(2),body.eventroompresentation div#secondary-contents div.inputBox input:eq(2),div#frontBox div#configurationPop div.inputBox input:eq(2)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(3),body.eventroompresentation div#secondary-contents div.inputBox input:eq(3),div#frontBox div#configurationPop div.inputBox input:eq(3)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(4),body.eventroompresentation div#secondary-contents div.inputBox input:eq(4),div#frontBox div#configurationPop div.inputBox input:eq(4)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});
$('body.events div#secondary-contents div.inputBox input:eq(5),body.eventroompresentation div#secondary-contents div.inputBox input:eq(5),div#frontBox div#configurationPop div.inputBox input:eq(5)').click(function(){
	$('body.events div#secondary-contents div.inputBox textarea,body.eventroompresentation div#secondary-contents div.inputBox textarea,div#frontBox div#configurationPop div.inputBox textarea').fadeOut('normal');
});

// FrontPage Slide Effects

	/* Left */
	$("#calendarPop div.popBtn").hover(function(){
	$(this).css({"cursor":"pointer","background":"#464448 url(../Media/Images/Lib/gradient_dark.png) repeat-x left top"}); 
	},function(){
	$(this).css({"cursor":"default","background":"#464448"})
	});
  $('#calendarPop div.popBtn').click(
    function(){
    if($('#calendarPop div.popContainer').offset().top=="490")
    {
    	$('#calendarPop div.popContainer').animate({'top':'-31'},300,"easeInQuart");
    }else if($('#calendarPop div.popContainer').offset().top=="155")
    {
    	$('#calendarPop div.popContainer').animate({'top':'304'},300,"easeInQuart");
    }
    });
  $('#calendarPop div.popBtn').bind("click", function(){
   if($('#configurationPop div.popContainer').offset().top=="155")
    {
    	$('#configurationPop div.popContainer').animate({'top':'304'},300,"easeInQuart");
    }
   });
	/* Right */
	$("#configurationPop div.popBtn").hover(function(){
	$(this).css({"cursor":"pointer","background":"#B2ADAA url(../Media/Images/Lib/gradient_left.png) repeat-x left top"}); 
	},function(){
	$(this).css({"cursor":"default","background":"#B2ADAA"})
	});
  $('#configurationPop div.popBtn').click(
    function(){
    if($('#configurationPop div.popContainer').offset().top=="490")
    {
    	$('#configurationPop div.popContainer').animate({'top':'-31'},300,"easeInQuart");
    }else if($('#configurationPop div.popContainer').offset().top=="155")
    {
    	$('#configurationPop div.popContainer').animate({'top':'304'},300,"easeInQuart");
    }
    });
  $('#configurationPop div.popBtn').bind("click", function(){
   if($('#calendarPop div.popContainer').offset().top=="155")
    {
    	$('#calendarPop div.popContainer').animate({'top':'304'},300,"easeInQuart");
    }
   });

    $('div.hoverBtn:last').css('margin-right','0');

}); 
