var dir = location.pathname;
var ua = navigator.userAgent;

$(function() {

//	alert($dd_height + ', ' + $dd_position);

	var $megadd;

	var $megadd_height = parseInt($('#megadd').css('height'));

	var $upper_height = parseInt($('#upper').css('height'));
	var $container_top = parseInt($('#container').css('top'));
	var $presen_top = parseInt($('#presen').css('top'));
	var $footer_top = parseInt($('#footer').css('top'));
	var $breadcrumb_top = parseInt($('#breadcrumb').css('top'));

//	alert($upper_height);


	$('#cont1').click(function(){

		if(!$megadd) {

			$(this).addClass('open');

			$('#upper').animate({
				height: ($upper_height + $megadd_height) + 'px'
			}, 300);
			$('#upper_call').animate({
				bottom: '-' + $megadd_height + 'px'
			}, 300);
			$('#container').animate({
				top: ($container_top + $megadd_height) + 'px'
			}, 300);
			$('#presen').animate({
				top: ($presen_top + $megadd_height) + 'px'
			}, 300);
			$('#footer').animate({
				top: ($footer_top + $megadd_height) + 'px'
			}, 300);
			$('#breadcrumb').animate({
				top: ($breadcrumb_top + $megadd_height) + 'px'
			}, 300);
			$('#megadd_outer').animate({
				height: $megadd_height + 50 + 'px'
			}, 300);

			$megadd = 1;

		} else {

			$('#megadd_outer').animate({
				height: '0px'
			}, 300);
			$('#breadcrumb').animate({
				top: $breadcrumb_top + 'px'
			}, 300);
			$('#footer').animate({
				top: $footer_top + 'px'
			}, 300);
			$('#presen').animate({
				top: $presen_top + 'px'
			}, 300);
			$('#container').animate({
				top: $container_top + 'px'
			}, 300);
			$('#upper_call').animate({
				bottom: '0px'
			}, 300);
			$('#upper').animate({
				height: $upper_height + 'px'
			}, 300);

			$(this).removeClass('open');

			$megadd = null;
		}

		return false;
	});

	$('.element .bookshelf').scrollable({
		items: '.introbook',
		mousewheel: true
	}).navigator({
		navi: '.booknavi'
	});

	$('.stafflist').scrollable({
		items: '.staffs',
		mousewheel: true
	}).navigator();

	$('.news .elem_inner .newsarticle').scrollable({
		items: '.articles',
		next: '.rolldown',
		prev: '.rollup',
		vertical: true,
		mousewheel: true
	});


	$('.widget .contents_navi .elem_inner ul li a').each(function(){
		if($(this).get(0).href == document.URL)
		$(this).parents('li').attr({'class': 'current'});
	});


	$('.widget .contents_navi .elem_inner ul li').each(function(){

		$('li:last-child').css({'margin-bottom': '0px'});

		$(this).hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
	});


	$('.widget > .contents_navi > .elem_inner > ul > li').not('.current').each(function(i){

		var $margin_left = parseInt($(this).css('margin-left'));

		$(this).hover(function(){
			$(this).stop().animate({
				'margin-left': ($margin_left - 10) + 'px'
			}, 200);
		}, function(){
			$(this).stop().animate({
				'margin-left': $margin_left + 'px'
			}, 200);
		});
	});


	$('.articlelist .articlecell, .articlelist3cell .articlecell, #headline div').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});

	$('.articlelist .articlecell, .articlelist3cell .articlecell, #headline div').click(function(){
		window.location = $(this).find('a').eq(0).attr('href');
	});


	$('a[href^="http"]').not('[href*="' + location.hostname + '"]').click(function(){
		window.open(this.href, '');
		return false;
	});

	$('.articledetail').find('img').each(function(i){

		var parelem = $(this).parent('a').get(0);

		if(parelem) {

			var href = parelem.href;
			var hostname = new RegExp(location.hostname, 'i');
			if (href.match(hostname) && href.match(/.jpg$|.jpeg$|.png$|.gif$/i)) {
				$(this).parent('a').addClass('modal');
			}
		}
	});
	$('a[class=modal]').fancybox({
		'overlayColor': '#000',
		'overlayOpacity': 0.7,
		'transitionIn': 'elastic',
		'transitionOut'	: 'elastic'
	});

	$('a[href^=#], area[href^=#]').not('a[href^=#tab], a[href=#], area[href=#]').each(function(){

		jQuery.easing.quart = function (x, t, b, c, d) {
			return -c * ((t = t / d - 1) * t * t * t - 1) + b;
		};

		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname && this.hash.replace(/#/, '')) {

			var $targetId = $(this.hash),
			$targetAnchor = $('[name=' + this.hash.slice(1) + ']');

			var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;

			if ($target) {

				var targetOffset = $target.offset().top;

				$(this).click(function() {

					$('html, body').animate({
						scrollTop: targetOffset
					}, 500, 'quart');

					return false;
				});
			}
		}
	});

	if (location.hash) {

		var hash = location.hash;

		window.scroll(0, 0)

		$('a[href=' + hash + ']').click();
	}
});


