/* ---------------------------------------------------------
 * yuga.js 0.7.1 - 優雅なWeb制作のためのJS
--------------------------------------------------------- */
(function($) {

	$(function() {
		$.yuga.rollover();
		$.yuga.scroll();
	});

	//---------------------------------------------------------------------

	$.yuga = {
		// URIを解析したオブジェクトを返すfunction
		Uri: function(path){
			var self = this;
			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]];
			}
			this.querys = {};
			if(this.query){
				$.each(self.query.split('&'), function(){
					var a = this.split('=');
					if (a.length == 2) self.querys[a[0]] = a[1];
				});
			}
		},
		//ロールオーバー
		rollover: function(options) {
			var c = $.extend({
				hoverSelector: '.rollover',
				groupSelector: '.btngroup',
				postfix: '_on'
			}, options);
			//ロールオーバーするノードの初期化
			var rolloverImgs = $(c.hoverSelector).filter(isNotCurrent);
			rolloverImgs.each(function(){
				this.originalSrc = $(this).attr('src');
				this.rolloverSrc = this.originalSrc.replace(new RegExp('('+c.postfix+')?(\.gif|\.jpg|\.png)$'), c.postfix+"$2");
				this.rolloverImg = new Image;
				this.rolloverImg.src = this.rolloverSrc;
			});
			//グループ内のimg要素を指定するセレクタ生成
			var groupingImgs = $(c.groupSelector).find('img').filter(isRolloverImg);

			//通常ロールオーバー
			rolloverImgs.not(groupingImgs).hover(function(){
				$(this).attr('src',this.rolloverSrc);
			},function(){
				$(this).attr('src',this.originalSrc);
			});
			//グループ化されたロールオーバー
			$(c.groupSelector).hover(function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					$(this).attr('src',this.rolloverSrc);
				});
			},function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					$(this).attr('src',this.originalSrc);
				});
			});
			//フィルタ用function
			function isNotCurrent(i){
				return Boolean(!this.currentSrc);
			}
			function isRolloverImg(i){
				return Boolean(this.rolloverSrc);
			}

		},
		//ページ内リンクはするするスクロール
		scroll: function(options) {
			//ドキュメントのスクロールを制御するオブジェクト
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60,
					fragment:''
				}, 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) {
						//スクロール終了時
						setFragment(param.hrefdata.absolutePath);
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						//通常スクロール時
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms);
					} else {
						//キャンセル発生
						if (getCurrentY()+getViewportHeight() == getDocumentHeight()) {
							//画面下のためスクロール終了
							setFragment(param.hrefdata.absolutePath);
							setFocusText();
						}
					}
				}

				function setFocusText(){
					if ('scrollID' in this) {
						for (var i in scrollID) {
							if (param.hrefdata.fragment == (scrollID[i] + "_link")) {
								if (document.getElementsByName(scrollID[i]).length > 0) {
									document.getElementsByName(scrollID[i])[0].focus();
								} else {
									document.getElementsByName(scrollID[i]+"[0]")[0].focus();
								}
								return;
							}
						}
					}
				}
				function setFragment(path){
					location.href = path
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getDocumentHeight(){
					return document.documentElement.scrollHeight || document.body.scrollHeight;
				}
				function getViewportHeight(){
					return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
				}
				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=#], a[href^=#tab], area[href=#], area[href^=#tab]').each(function(){
				this.hrefdata = new $.yuga.Uri(this.getAttribute('href'));
			}).click(function(){
				var target = $('#'+this.hrefdata.fragment);
				if (target.length == 0) target = $('a[name='+this.hrefdata.fragment+']');
				if (target.length) {
					var top    = target.offset().top;
					// chromeだとoffset()で位置が取得できないのでoffsetTopから取得
					if (/chrome/i.test(navigator.userAgent)) {
						top = document.getElementById(this.hrefdata.fragment).offsetTop;
					}
					scroller.set({
						endY: top,
						hrefdata: this.hrefdata
					});
				}
				return false;
			});
		}
	};

})(jQuery);

// Window Open
function openWin(url,name,W,H) {
	popupWin = null;
	popupWin=window.open(url,name,'toolbar=no,status=no,width='+W+',height='+H+',directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');
}
function WinOpen(url,name,features) {
	popupWin = window.open(url, name, features);
}
function openPage(url,name,W,H){
	newWin1 = window.open(url,name,'toolbar=yes,status=yes,width='+W+',height='+H+',menubar=yes,resizable=yes,location=yes,scrollbars=yes');
	newWin1.focus();
}

// movie
function writeSound(fileUrl,strWidth,strHeight){
	var htm = "";
	htm += '<embed src=' + fileUrl;
	htm += ' ShowAudioControls="1"'; 
	htm += ' ShowPositionControls="0"';
	htm += ' showcontrols="1"';
	htm += ' width=' + strWidth;
	htm += ' height=' + strHeight;
	htm += ' autostart="1" align="middle">';

	document.write(htm);
}


/* 全角チェック */
function checkIsZenkaku(value) {
	for (var i = 0; i < value.length; ++i) {
	var c = value.charCodeAt(i);
	//  半角カタカナは不可
		if (c < 256 || (c >= 0xff61 && c <= 0xff9f)) {
			return false;
		}
	}
	return true;
}

/* E-mail形式の正規表現パターン */
function chkRegEmail(str){
	/* @が含まれていて、最後が .(ドット)でないなら正しいとする */
	 var Seiki=/[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i;
	/* 入力された値がパターンにマッチするか調べる */
	if(str.match(Seiki)){
		return true;
	}else{
		return false;
	}
}

/* 郵便番号のチェック */
function checkZip(str)
{
	var data = /^[0-9]+\-[0-9]+$/;
	if(str.match(data)){
		return true;
	}else{
		return false;
	}
}

/* 電話番号のチェック */
function checkTel(str)
{
	var data = /^[0-9]+\-[0-9]+\-[0-9]+$/;
	if(str.match(data)){
		return true;
	}else{
		return false;
	}
}


$(function() {
/* Hint Text
------------------------------------ */
	$('#searchInput, #irSearchInput')
		.blur(function(){
			var $$=$(this);
			if($$.val()=='' || $$.val()==$$.attr('title')){
				$$.css('color', '#999')
					.val($$.attr('title'));
			}
		})
		.focus(function(){
			var $$=$(this);
			if($$.val()==$$.attr('title')){
				$(this).css('color', '#000')
							 .val('');
			}
		})
		.parents('form:first').submit(function(){
			var $$=$('#text');
			if($$.val()==$$.attr('title')){
				$$.triggerHandler('focus');
			}
		}).end()
		.blur();

/* Print
------------------------------------ */
	$('#pagePrint span').click(function(){
		print();
	});


/* Category
------------------------------------ */
	var e = document.createElement("div");
	var s = document.createTextNode("S");
	e.appendChild(s);
	e.style.visibility="hidden"
	e.style.position="absolute"
	e.style.top="0";
	document.body.appendChild(e);
	var defHeight = $('body').height();
	checkBoxSize = function(){

		if(defHeight != $('body').height()){
			$('.categoryBox_02 .item').autoHeight({column:2,clear:1,reset:'reset'});
			$('.categoryBox_03 .item').autoHeight({column:3,clear:1,reset:'reset'});
			$('.categoryBox_04 .item').autoHeight({column:4,clear:1,reset:'reset'});
			$('#latestArea_02 table').autoHeight({height:'height',reset:'reset'});
			$('#downloadAsideBlock .itemWrap .item .inner').autoHeight({height:'height',reset:'reset'});

			defHeight= $('body').height();
		}
	}
	$('.categoryBox_02 .item').autoHeight({column:2,clear:1,reset:'reset'});
	$('.categoryBox_03 .item').autoHeight({column:3,clear:1,reset:'reset'});
	$('.categoryBox_04 .item').autoHeight({column:4,clear:1,reset:'reset'});
	$('#latestArea_02 table').autoHeight({height:'height',reset:'reset'});
	$('#downloadAsideBlock .itemWrap .item .inner').autoHeight({height:'height',reset:'reset'});

	setInterval(checkBoxSize,100);

});

