$(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 ++;
	});



	// opacity Rollover

	$('img[src*="_op."],input[src*="_op."]').hover(
		function () {
			$(this).css('opacity', 0.7);
		},
		function () {
			$(this).css('opacity', 1);
		}
	);
	$('.no-link img').unbind().css('opacity', 0.25);



	// external

	var notBlank = new Array("");

	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");

	if(!location.href.match(/^http/)){
		$("a[href$='/']").not("a[href^='http']").each( function(){
			$(this).attr('href', $(this).attr('href') + 'index.html');
		});
		$("a[href*='/#']").not("a[href^='http'],a[href$='.html']").each( function(){
			var n = $(this).attr('href').lastIndexOf("/#") + 1;
			$(this).attr('href', $(this).attr('href').substring(0, n) + 'index.html' + $(this).attr('href').substring(n));
		});
	}



	// add last
	$(".has_last > *:last-child").addClass("last");




	// smoothScroll

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




	// navi

	var current_num = 100;
	var navi_timer;
	$('#headContainer > ul > li').each (function(i){
		if($('ul', this).size() > 0){
			$('img', this).mouseover( function(){
				subNaviShow($(this), (i - 1));
			});
			$('ul', this).width($('#headContainer > ul').width()).css('background-position', (36 + (i-1) * 103) + 'px 0');
		}else {
			$('img', this).hover(
				function () {
					if(i == 0) subNaviHide(100);
					$(this).css('opacity', 0.6);
				},
				function () {
					$(this).css('opacity', 1);
				}
			);
		}

		if($(this).hasClass('current')) {
			current_num = (i - 1);
			subNaviShow($('img', this), (i - 1), true);
		}
	});
	$('#headContainer > ul').hover(
		function () {
			clearTimeout(navi_timer);
		},
		function () {
			navi_timer = setTimeout( function(){
				if(current_num != 100) {
					subNaviShow($('#headContainer > ul > li:eq(' + (current_num + 1) + ') img'), current_num);
				}else {
					subNaviHide(current_num);
				}
			}, 2000);
		}
	);


	function subNaviShow(navi, num, ff) {
		subNaviHide(num);

		navi.css('opacity', 0.6);
		var ele = $('#headContainer ul ul:eq(' + num + ')');
		if(ele.queue().length <= 1) ele.slideDown(ff ? 0 : 300);
	}

	function subNaviHide(num) {
		$('#headContainer ul img').css('opacity', 1);
		var ele = $('#headContainer ul ul').not($('#headContainer ul ul:eq(' + num + ')')).not(':hidden');
		ele.slideUp(200);
	}




	// side
	$('#naviArea li a').hover(
		function () {
			$(this).parent().addClass('hover');
		},
		function () {
			$(this).parent().removeClass('hover');
		}
	);

});

function smoothScroll(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");
		})
}

