$(function(){
	initRollovers();

	// GLOBAL NAV
	var config = {
		sensitivity: 2,
		interval: 50,
		over: doOpen,
		timeout: 100,
		out: doClose
	};

	function doOpen() {
		$(this).addClass("hover");
		$('ul:first',this).css('visibility', 'visible');
	}
	function doClose() {
		$(this).removeClass("hover");
		$('ul:first',this).css('visibility', 'hidden');
	}
	$("#gn ul.parent li").hoverIntent(config);

	$("#gn").each(function(){
		var categoryName = $("body").attr("id");
		if(categoryName == "gtop"){
			return;
		}else{
			$("#gn_"+categoryName+" a:first").addClass("current");
		}
	});
	
	// LOCAL NAV
	$("#ln").each(function(){
		var pageName = $("body").attr("class");

		if(pageName == "ctop"){
			return;
		}
		else if(pageName == "schedule"){
			pageName = pageName + schedule_year;
			$("#ln_"+pageName+" a").addClass("current");
		}
		else{
			$("#ln_"+pageName+" a").addClass("current");
		}
	});

});

/* ROLL OVER IMAGES
--------------------------------------- */
function initRollovers() {
	if (!document.getElementById) return

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'over') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}

			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

/* DROPDOWN MENU
--------------------------------------- */
(function($){
	$.fn.hoverIntent = function(f,g) {
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );

		var cX, cY, pX, pY;

		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				pX = cX; pY = cY;
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		var handleHover = function(e) {
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			var ev = jQuery.extend({},e);
			var ob = this;

			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			if (e.type == "mouseover") {
				pX = ev.pageX; pY = ev.pageY;
				$(ob).bind("mousemove",track);
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			} else {
				$(ob).unbind("mousemove",track);
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		return this.mouseover(handleHover).mouseout(handleHover);
	};
	
})(jQuery);


/* PICKUP PHOTO
--------------------------------------- */
function openWin() {
	window.open(openWin.arguments[0],openWin.arguments[1],"toolbar=no,location=no,status=no,directories=no,scrollbars=no,resizable=no,width=275,height=430");
}

/* 広告バナー用
--------------------------------------- */
function bannerInc( imgURL , linkURL , w , h ) {
	//ファイル拡張子の取得
	var extension = imgURL.split(".");
	extension = extension[ extension.length - 1 ];
	if ( extension == "swf" ) {
		flashInc( imgURL , linkURL , w , h , "_blank" );
	} else if ( extension == "jpg" || extension == "gif" ) {
		imgInc ( imgURL , linkURL , w , h , "external" );
	}
}

function imgInc ( img , url , w , h , rel ) {
	this.document.write('<a href="' + url + '" rel="' + rel + '"><img src="' + img + '" alt="" width="' + w + '" height="' + h + '" /></a>');
}

function flashInc ( swf , url , w , h , target ) {
	this.document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + w + '" height="' + h + '" id="flashBanner" align="middle">');
	this.document.write('<param name="allowScriptAccess" value="sameDomain" />');
	this.document.write('<param name="wmode" value="opaque" / >');
	this.document.write('<param name="movie" value="' + swf + '" />');
	this.document.write('<param name="quality" value="high" />');
	this.document.write('<param name="bgcolor" value="#ffffff" />');
	this.document.write('<param name="menu" value="false" />');
	this.document.write('<param name="flashVars" value="linkURL=' + url + '&linkTarget=' + target + '">');
	this.document.write('<embed src="' + swf + '" width="' + w + '" height="' + h + '" align="middle" quality="high" bgcolor="#ffffff" name="flashBanner" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="opaque" flashVars="linkURL=' + url + '&linkTarget=' + target + '" menu="false" />');
	this.document.write('</object>');
}
