// JavaScript Document

jQuery.noConflict();

(function ($) {
	$(document).ready(function(){

		$("ul.sub").hide();
	
		$("ul#dropdown > li").each(function () {
			var self = $(this);
	
			if ( $('ul.sub', self).size() ) {
				self.find('ul.sub li a').click(function () {
					location.href = this.href;
					return false;
				});
	
				// そのカテゴリ以下のコンテンツの場合は開いておく。
				var path = location.pathname.substring(1).split('/');
				var href = path[path.length - 1] || "";
				var item = self.find('a[href~=' +  href + ']');
				if ( item.size() ) {
					$("ul.sub", self).show();
					self.data('is_menu_opened', true);
				} else {
					self.data('is_menu_opened', false);
				}
	
				self.click(function(event){
					$("ul#dropdown > li").not(self).each(function () {
						$("ul.sub", this).slideUp("fast");
						$(this).data('is_menu_opened', false);
					});
		
					if (!self.data('is_menu_opened')) {
						$("ul.sub", this).slideDown("fast")
						self.data('is_menu_opened', true);
					} else {
						var link = self.find('span > a:first');
						if ( link.size() ) {
							location.href = link.attr('href');
						} else {
							$("ul.sub", this).slideUp("fast")
							self.data('is_menu_opened', false);
						}
					}
					
					return false;
				});
			}
		});
	});
})(jQuery);
