$(function () {
	$.setup.blanklink();
	$.setup.popUpWindow();
	//$.setup.pngfixSet();
	$.setup.barAction();
	$.setup.ie6PicCatch();
	$.setup.naviAnimaition();
	$.setup.boxAnimaition();
	$.yuga.scroll();
	$.yuga.tab();
});

$.setup = {
	
	blanklink: function(options) {
		$('.blank').click(function(){
			window.open(this.href, '_blank');
			return false;
		});
	},
	
	popUpWindow: function(options) {
		$('.popUpWindow').click(function(){
			//html?name=●&amp;scroll=0&amp;width=●&amp;height=●
			var queryString = this.href.replace(/^[^\?]+\??/,' ');
			var params = tb_parseQuery( queryString );
			
			popName = (params['name']);
			popScroll = (params['scroll']*1);
			popWidth = (params['width']*1);
			popHeight = (params['height']*1); 
			url=this.href.split("?")[0];
			
			var popW01;
			var centerH = (screen.height-popHeight)/2;
			var centerW = (screen.width-popWidth)/2;
			
			popW01=window.open(url,popName,'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=yes,scrollbars='+ popScroll +',width='+ popWidth +',height='+ popHeight +',left='+ centerW +',top='+ centerH +'');
			popW01.focus();
			return false; 
		});
		function tb_parseQuery ( query ) {
		   var Params = {};
		   if ( ! query ) {return Params;}// return empty object
		   var Pairs = query.split(/[;&]/);
		   for ( var i = 0; i < Pairs.length; i++ ) {
			 var KeyVal = Pairs[i].split('=');
			 if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
			 var key = unescape( KeyVal[0] );
			 var val = unescape( KeyVal[1] );
			 val = val.replace(/\+/g, ' ');
			 Params[key] = val;
		   }
		   return Params;
		}
	},
	
	pngfixSet : function(options) {
		$(document).pngFix();
	},

	barAction: function(options){
		var openFlag;
		openFlag=true;

		$().oneTime(5555, function closeTopic(){
				$("#sideBarContents").animate({ width: "1px", height: "65px", opacity: 1 }, 1000 );
				$('#sideBarTab').css({ background: 'url(img/contents_title/topic-active.gif) 0 12px'});
				openFlag=false;
		});
		
		$('#sideBarTab').click(function(){
			//console.log("トピックスクリック:"+openFlag);
			if(openFlag){
				$("#sideBarContents").animate({ width: "1px", height: "65px", opacity: 1 }, 800 );
				$('#sideBarTab').css({ background: 'url(img/contents_title/topic-active.gif) 0 12px'});
				openFlag=false;
				}else{
				$("#sideBarContents").animate({ width: "480px", height: "65px", opacity: 1 }, 800 );
				$('#sideBarTab').css({ background: 'url(img/contents_title/topic.gif) 0 12px'});
				openFlag=true;
				}
			//return false;
		});
	},
	
	ie6PicCatch: function(options){//IE画像表示処理
		try {
		document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
	},
	
	naviAnimaition: function(options){
			$('#menuBase > li > a,.subMenu > li > a').css({backgroundPosition: "0 0"})
			.mouseover(function(){$(this).stop().animate({backgroundPosition:"(242px 0)"}, {duration:300})})
			.mouseout(function(){$(this).stop().animate({backgroundPosition:"(0 0)"},{duration:300, complete:function(){
				$(this).css({backgroundPosition: "0 0"})
			}})})
	},
	
	boxAnimaition: function(options){
		//$('.categoryBox a').css({backgroundColor:'FFFFFF'});
		$(".categoryBox a,.categoryBoxLast a,.colTypeCGI")
		.mouseover(function(){
			$(this).stop().animate( { backgroundColor: '#CECECE'}, 300);})
		.mouseout(function(){
			$(this).stop().animate( { backgroundColor: '#FFFFFF'}, 300);
		});
	}

}

$.yuga = {
		// URIを解析したオブジェクトを返すfunction
		Uri: function(path){
			this.originalPath = path;
			//絶対パスを取得
			this.absolutePath = (function(){
				var e = document.createElement('span');
				e.innerHTML = '<a href="' + path + '" />';
				return e.firstChild.href;
			})();
			//絶対パスを分解
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]]; 
			}
		},
		//ページ内リンクはするするスクロール
		scroll: function(options) {
			//ドキュメントのスクロールを制御するオブジェクト
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60
				}, options);
				c.ms = Math.floor(1000/c.fps);
				var timerId;
				var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
				};
				//スクロール中に実行されるfunction
				function move() {
					if (param.stepCount == c.step) {
						//スクロール終了時
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						//通常スクロール時
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					}
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);
						param.lastY = param.startY;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();
			$('a[href^=#], area[href^=#]').not('a[href=#], area[href=#]').each(function(){
				this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
			}).click(function(){
				var target = $('#'+this.hrefdata.fragment);
				if (target.length) {
					scroller.set({
						endY: target.offset().top
					});
					return false;
				}
			});
		},
		//タブ機能
		tab: function(options) {
			var c = $.extend({
				tabNavSelector:'.tabNav',
				activeTabClass:'active'
			}, options);
			$(c.tabNavSelector).each(function(){
				var tabNavList = $(this).find('a[href^=#], area[href^=#]');
				var tabBodyList;
				tabNavList.each(function(){
					this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
					var selecter = '#'+this.hrefdata.fragment;
					if (tabBodyList) {
						tabBodyList = tabBodyList.add(selecter);
					} else {
						tabBodyList = $(selecter);
					}
					$(this).unbind('click');
					$(this).click(function(){
						tabNavList.removeClass(c.activeTabClass);
						$(this).addClass(c.activeTabClass);
						tabBodyList.hide();
						$(selecter).show();
						return false;
					});
				});
				tabBodyList.hide()
				tabNavList.filter(':first').trigger('click');
			});
		},
		//奇数、偶数を自動追加
		stripe: function(options) {
			var c = $.extend({
				oddClass:'odd',
				evenClass:'even'
			}, options);
			$('ul, ol').each(function(){
				//JSでは0から数えるのでevenとaddを逆に指定
				$(this).children('li:odd').addClass(c.evenClass);
				$(this).children('li:even').addClass(c.oddClass);
			});
			$('table, tbody').each(function(){
				$(this).children('tr:odd').addClass(c.evenClass);
				$(this).children('tr:even').addClass(c.oddClass);
			});
		},
		//css3のクラスを追加
		css3class: function() {
			//:first-child, :last-childをクラスとして追加
			$('body :first-child').addClass('firstChild');
			$('body :last-child').addClass('lastChild');
			//css3の:emptyをクラスとして追加
			$('body :empty').addClass('empty');
		}
	}
	
