jQuery.easing.def = "easeOutCubic";

var slide_dur = 600;
var fade_dur = 300;
var openCount = 0;

function panel_open(board) {
	location.hash = board.data("anchor-name");
	var detail = board.find('div.year-article-right-detail');
	board.find('div.year-article-right-thumbnail').animate({ opacity: 0 }, { queue:false, duration: fade_dur });
	board.find('div.year-article-right-wrap').animate({width: 791, height: detail.height() + 20 }, { queue: false, duration: slide_dur });
	if (++openCount >= 1) {
		slideOpen();
	}
	setTimeout(function() {
		detail.show();
		detail.animate({opacity:1, duration: fade_dur});
	}, fade_dur);
}
function panel_close(board) {
	var slide = function() {
		board.find('div.year-article-right-detail').hide();
	};
	board.find('div.year-article-right-detail').animate({ opacity: 0 }, { queue:false, duration: fade_dur, complete: slide });
	board.find('div.year-article-right-wrap').animate({ width: 435, height: 344 }, { queue: false, duration: slide_dur });
	if (--openCount <= 0) {
		slideClose();
	}
	setTimeout(function() {
		board.find('div.year-article-right-thumbnail').animate({ opacity: 1 }, { queue:false, duration: fade_dur });
	}, fade_dur);
}

$(document).ready(function() {
	$('div#slideboard div.year-article div.year-article-right div.year-article-right-thumbnail').click(function() {
		var board = $(this).parent();
		panel_open(board);
	});
	$('div#slideboard div.year-article div.year-article-right div.year-article-right-close').click(function() {
		var board = $(this).parent().parent().parent();
		panel_close(board);
	});

	$("div#museum_index_close").click(function () {
		$('div#museum_index_expand').slideUp();
		$('div#museum_index_collapse').slideDown();
	});
	$("div#museum_index_button").click(function () {
		$('div#museum_index_expand').slideDown();
		$('div#museum_index_collapse').slideUp();
	});
	
	/*
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 600);
				//return false;
			}
		}
	});
	*/
	
	
  var index_hash = {};
  $('div#museum_index_image table tr td > a').each( function() {
    var elm = $(this);
    var anchor = elm.attr("href").substring(1);
    elm.attr("href", "javascript:void(0);");
    index_hash[anchor] = elm;
    
    var board = $('div#slideboard div.year-article div#'+anchor+"-panel");
    board.data("anchor-name", anchor);
    elm.click(function() {
      scroll(board.offset().top);
      setTimeout(function() {
        panel_open(board);
      }, 400);
    });
  });
  if(location.hash != "") {
    var key = location.hash.substring(1);
    if(key != "" && index_hash[key]) {
      index_hash[key].click();
    }
  }
  $('div.back_to_top a').each( function() {
    var elm = $(this);
    elm.attr("href", "javascript:void(0);");
    elm.click( function() {
      scroll(0);
    });
  });
	
	
	$(window).scroll(function () {
		$('#slideboard-arrow').animate({
			top: Math.max(50, $(document).scrollTop() - $('#chronicle').position().top + 300)
		}, {
			duration: "slow", queue: false
		});
	});
	$('#slideboard-arrow-left').click(function () {
		slideClose();
	});
	$('#slideboard-arrow-right').click(function () {
		slideOpen();
	});
	
	$('a[rel=goods_overlay]').click(function () {
		$('#goods_overlay_image').attr('src', this.href);
		$('#goods_overlay_label').text($(this).attr('title'));
		
        $.blockUI({
			message: $('#goods_overlay'),
			css: {
				cursor: 'default',
				width: '10px',
				height: '10px',
				left: '50%',
				top: '50%',
				borderStyle: 'none'
			},
			overlayCSS: { cursor: 'default' }
		});
		$('.blockOverlay').click($.unblockUI);
		
		return false;
	});
	$('#goods_overlay_close').click(function () {
        $.unblockUI();
		return false;
	});
	
	
	$('div.balloon-source').each(function () {
		var source = $(this);
		var title = source.attr('title').replace(/ /g, '<br/>');
		source.attr('title', '');
		source.hover(function () {
			$('#balloon-content').html(title);
			$('#balloon').css({
				left:source.children().offset().left - $('#balloon').parent().offset().left + 30
				+ (source.children().width() - $('#balloon').width()) / 2 + "px",
				top:source.children().offset().top - $('#balloon').height() - 2 + "px"
			}).animate({ opacity:"show", duration:100 });
		}, function () {
			$('#balloon').animate({ opacity:"hide", duration:100 });
		});
	});
});

function slideOpen() {
	$('#slideboard-left-shadow').animate({ opacity:"show" }, { queue:false, duration: fade_dur });
	//$('#slideboard-right-shadow').animate({ opacity:"hide" }, { queue:false, duration: fade_dur });
	$('#slideboard-arrow-left').animate({ opacity:"show" }, { queue:false, duration: fade_dur });
	$('#slideboard-arrow-right').animate({ opacity:"hide" }, { queue:false, duration: fade_dur });
	$('#slideboard').animate({ left: -354, width: 924+354 }, { queue:false, duration: slide_dur });
}
function slideClose() {
	$('#slideboard-left-shadow').animate({ opacity:"hide" }, { queue:false, duration: fade_dur });
	//$('#slideboard-right-shadow').animate({ opacity:"show" }, { queue:false, duration: fade_dur });
	$('#slideboard-arrow-left').animate({ opacity:"hide" }, { queue:false, duration: fade_dur });
	$('#slideboard-arrow-right').animate({ opacity: (openCount > 0 ? "show" : "hide") }, { queue:false, duration: fade_dur });
	$('#slideboard').animate({ left: 0, width: 924 }, { queue:false, duration: slide_dur });
}

var on_scroll = false;
var scroll_top = 0;
function scroll(top) {
  if( (on_scroll && scroll_top != top) || !on_scroll) {
    on_scroll = true;
    scroll_top = top;
    $($.browser.safari ? 'body' : 'html').animate({scrollTop: top}, {duration: 800, easing: 'swing', complete: function() {on_scroll = false;} });
  }
}





