$(document).ready(function() {
	//Main Navitaion
	if(typeof $.fn.hoverIntent == 'function') {
		$('ul.topnav > li', this).hoverIntent({
			sensitivity: 2,
			interval: 50,
			timeout: 100,
			over: showNav,
			out: hideNav
		}
		);
	} else {
	$("ul.topnav > li").hover(function() {
			$(this).find("ul.subnav").slideUp('slow');
			$(this).find("ul.subnav").slideDown('slow').show();
		}, function() {
			$(this).find("ul.subnav").slideUp('slow');
	})
	}

	//Easing Scroll
	$("a[href*='#']").easingScroll({
		easing: "linear",
		duration: 400
	});

	//Load event list
	$('#article ul.events li').each(function(){
		var nid = getUrlVars()["nid"];
		if(nid == 'rouon'){
			var rounews= $(this).find('#rouon');
			openEventList(rounews);
		} 
		if(nid == 'screen_m') {
			var scnews= $(this).find('#screen_m');
			openEventList(scnews);
		}
		$(this).find('a').click(function(){
			openEventList($(this));
			return false;
		});
	});

});

	function openEventList(root){
		var ecnt = root.parent().find('div.event_content');
		// close other events
		$('#article ul.events li').each(function(){
			if($(this).find('a').hasClass('current')) closeEventList($(this).find('a'));
		});
		if(ecnt.size() > 0) { closeEventList(root);}
		else{
			var fpath = root.attr('href');
			root.after('<div class="event_content"></div>');
			var loadcnt = root.parent().find('div.event_content');
			loadcnt.load(fpath, function(){
				$(this).css('height', $(this).height());
				$(this).animate({height:"toggle", opacity:"toggle"}, '1500', 'linear', function(){
					root.text('(•Â‚¶‚é)');
					});
					root.addClass('current');
			});
		}
	}

	function closeEventList(root) {
		var ecnt = root.parent().find('div.event_content');
		ecnt.animate({height:"toggle", opacity:"toggle"}, '1500', 'linear', function(){
			if($('div.event_detail').size() > 0) root.text($(this).find('div.event_detail h2').text());
			if($('#overlay_content').size() > 0) root.text($(this).find('#overlay_content h2').text());
			if(root.hasClass('current')) root.removeClass('current');
			$(this).remove();
		});
	}

	//function for main navigation
	function hideNav() {
		var subnav = $(this).find("ul.subnav");
		if (!subnav) return;
		$(this).find("a:first").toggleClass("current");
		$.data(subnav, 'cancelHide', false);
		setTimeout(function() {
			if (!$.data(subnav, 'cancelHide')) {
						if($.browser.msie) {
							$(subnav).hide();
						} else {
					$(subnav).slideUp(100);
						}
			}
		}, 1);
	}

	function showNav() {
		var subnav = $(this).find('ul.subnav');
		if (!subnav) return;
		$(this).find("a:first").toggleClass("current");
		$.data(subnav, 'cancelHide', true);
		if($.browser.msie) {
				$(subnav).show();
		} else {
			$(subnav).slideDown(200);
		}
		return false;
	 };

//get URL Vars
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i <hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
