$(function(){
	var SuperFish = {
		parentJq : null
	,	eachJq : null
	,	maxList : 14 // 各列に最大何列作れるか
	,	closeWait : 250
	,	openWait : 250
	,	ie6 : false
	,	set : function(){
			var parentJq = $('#side_navi_area, #otherList');
			if(parentJq.length){
				this.parentJq = parentJq;
			}else{
				this.parentJq = $('#side_navi_area_technical');
			}
			if(!this.parentJq || this.parentJq.length === 0){
				return false;
			}
			if($.browser.msie && $.browser.version < 7){
				this.ie6 = true;
			}
			this._setIni();
			if(!this.eachJq){
				return false;
			}
			this._setHover();
			this._setUpDown();
		}
	
	,	_setHover : function(){
			this.eachJq.hover(
				function(){
					var eleJq = $(this);
					if(eleJq.data('state') === 'closeStart'){
						eleJq.data('state', 'opened');
						clearTimeout(eleJq.data('closeTimeoutId'));
					}
					SuperFish._aTagIn(eleJq);
					if(eleJq.data('state') === 'closed'){
						SuperFish._openStart(eleJq);
					}
					return false;
				}
			,	function(){
					var eleJq = $(this);
					if(eleJq.data('state') === 'openStart'){
						eleJq.data('state', 'closed');
						clearTimeout(eleJq.data('openTimeoutId'));
					}
					if(eleJq.data('state') !== 'opened'){
						SuperFish._aTagOut(eleJq);
					}else{
						SuperFish._closeStart(eleJq);
					}
					return false;
				}
			);
		}

	,	_closeStart : function(eleJq){
			eleJq.data('state', 'closeStart');
			eleJq.data('closeTimeoutId', setTimeout(close, this.closeWait));
			function close(){
				if(eleJq.data('state') === 'closeStart'){
					$('div.itemList', eleJq.data('state', 'closed').removeClass('sideHover')).hide().css('top','');
					SuperFish._aTagOut(eleJq);
				}
			}
		}
	
	,	_openStart : function(eleJq){
			eleJq.data('state', 'openStart')
			eleJq.data('openTimeoutId', setTimeout(open, this.openWait));
			function open(){
				if(eleJq.data('state') === 'openStart'){
					SuperFish._aTagIn(eleJq);
					eleJq.data('state', 'opened');
					var offsetTop = eleJq.offset().top;
					var itemListJq = $('div.itemList', eleJq);
					var height = itemListJq.height(), top;
					if(offsetTop - $(window).scrollTop() - 0.5 * height < 0){
						top = 0;
					}else if($(window).scrollTop() + $(window).height()
								- offsetTop - 0.5 * height - 8 < 0){
						top = 6 - 1 * height;
					}else{
						top = - 0.5 * height;
					}
					$('div.itemList', eleJq.addClass('sideHover')).css('top', top + 'px').show();
				}
			}
		}
	
	// set a tag hover
	,	_aTagIn : function(liJq){
			setTimeout(function(){
				if(liJq.data('state') !== 'closed'){
					var eleJq = $('h6 a', liJq);
					if(eleJq.data('backgroundFlg') == 1){
						eleJq.css({
							backgroundPosition : eleJq.css('backgroundPosition').replace(/^\s*\-?\d+px/, '-197px')
						});
					}else{
						eleJq.css({
							backgroundPositionX : '-197px'
						});
					}
					eleJq.addClass('sideHoverColor');
				}
			}, 10);
		}
		
	,	_aTagOut : function(liJq){
			setTimeout(function(){
				if(liJq.data('state') !== 'opened'){
					var eleJq = $('h6 a', liJq).removeClass('sideHoverColor');
					if(eleJq.data('backgroundFlg') == 1){
						eleJq.css({
							backgroundPosition : eleJq.css('backgroundPosition').replace(/^\s*\-?\d+px/, '-2px')
						});
					}else{
						eleJq.css({
							backgroundPositionX : '-2px'
						});
					}
				}
			}, 10);
		}
	
	,	_setIni : function(){
			var imgReg = /\.gif/;
			var eachLiJqList = this._setEachEle(this.parentJq);
			if(!eachLiJqList || eachLiJqList.length === 0){
				return false;
			}
			this.eachJq = eachLiJqList;
			eachLiJqList.data('state', 'closed');
			this._setMultiline();
			$('div.itemList', eachLiJqList).hide();
			$('h6 a', eachLiJqList).each(function(){
				var eleJq = $(this);
				var backgroundFlg = 1;
				var backgroundPosition = eleJq.css('backgroundPosition');
				if(!backgroundPosition){
					backgroundFlg = 2;
					eleJq.css('backgroundPositionX', eleJq.css('backgroundPositionX'));
				}else{
					eleJq.css('backgroundPosition', backgroundPosition);
				}
				eleJq.data('backgroundFlg', backgroundFlg);
				if(eleJq.attr('href')==='#'){
					eleJq.click(function(){return false;});
				}
			});
		}

	,	_setEachEle : function(parentJq){
			if(!parentJq)parentJq = this.parentJq;
			return $('li.naviList', parentJq);
		}
		
	,	_setMultiline : function(){
			
			$('dl', this.eachJq).each(function(){
				var dlJq = $(this);
				var ddJq = $('dd', this);
				var ddJqLen = ddJq.length;
				
				if(ddJqLen < SuperFish.maxList + 2){
					return true;
				}
				// 列数を設定する
				var dlNumMax = parseInt(ddJqLen / SuperFish.maxList);
				if(dlNumMax * SuperFish.maxList < ddJqLen){
					++dlNumMax;
				}
				// 行数を設定する
				var eachLenMax = parseInt(ddJqLen / dlNumMax);
				if(dlNumMax * eachLenMax < ddJqLen){
					++eachLenMax;
				}
				
				var dlTemplate = dlJq.clone(true);
				$('dd', dlTemplate).remove();
				$('dt', dlTemplate).html('&nbsp;');
				var dlNum = 1;
				var eachLen = eachLenMax;
				var additionalDlJq;
				var previousJq = dlJq;
				var i;
				for(i = eachLenMax; i < ddJqLen; ++i){
					if(eachLen === eachLenMax){
						eachLen = 0;
						++dlNum;
						if(additionalDlJq)previousJq = additionalDlJq;
						if(dlNum !== dlNumMax){
							additionalDlJq = dlTemplate.clone(true);
						}else{
							additionalDlJq = dlTemplate;
						}
						previousJq.after(additionalDlJq);
					}
					++eachLen;
					ddJq.eq(i).appendTo(additionalDlJq);
				}
				
				var width = 0;
				var parentJq = dlJq.parent();
				$('dl', parentJq).each(function(){
					width += parseInt($(this).outerWidth(), 10) + 2;
				});
				
				if(width > parentJq.width()){
					parentJq.css('width', (width + 1) + 'px');
				}
			});
		}
	
	,	_setUpDown : function(){
			$('h4.sf-dropdown', this.parentJq).click(function(){
				var
					oImg = $('img', this)
				,	sImgSrc = oImg.attr('src')
				,	bOpen = (sImgSrc.indexOf('_open.gif')>-1?true:false)
				,	sNewImgSrc = sImgSrc.replace((bOpen?'_open.gif':'_close.gif'), (bOpen?'_close.gif':'_open.gif'))
				,	$$ = $(this).next('div.sf-dropdown-in')
				;
				oImg.attr('src',sNewImgSrc);
				if(bOpen){
					$$	.addClass('sf-dropdown-close')
						.removeClass('sf-dropdown-open')
					;
				}else{
					if(SuperFish.ie6){
						$$.css('display', 'none')
					}
					
					$$	.addClass('sf-dropdown-open')
						.removeClass('sf-dropdown-close')
					;
					
					if(SuperFish.ie6){
						tmpOpen = SuperFish.openWait;
						SuperFish.openWait = 1;
						SuperFish._openStart($('li:eq(1)', $$));
						SuperFish.openWait = tmpOpen;
						
						tmpClose = SuperFish.closeWait;
						SuperFish.closeWait = 1;
						SuperFish._closeStart($('li:eq(1)', $$));
						SuperFish.closeWait = tmpClose;
						
						$$.css('display', '');
					}
				}
				return false;
			});	
		}
	};
	
	function settingStart(){
		SuperFish.set();
	}
	setTimeout(settingStart, 10);
});


//  アラート表示　110914

/* offset/poster 2011/03/22 */
function cal_warning_poster() {
	window.alert("東北地方太平洋沖地震の影響により、従来から取り扱いのある「ユポタック110」が入荷困難なため今現在、代替品にて対応いたしております。代替品には同等のユポタックではございますが、はく離シートをはがしやすくするスリット（切れ目）が付いておりませんので、なにとぞご容赦の上、ご利用くださいますようよろしくお願いいたします。");
}


