/*
 * fixHeight - jQuery Plugin
 * http://www.starryworks.co.jp/blog/tips/javascript/fixheightjs.html
 *
 * Author Koji Kimura @ STARRYWORKS inc.
 * http://www.starryworks.co.jp/
 * 
 * Licensed under the MIT License
 *
 */


(function($){
	
	var isInitialized = false;
	var parents = [];
	var textHeight = 0;
	var $fontSizeDiv;
	
	$.fn.fixHeight = function() {
		this.each(function(){
			var childrenGroups = getChildren( this );
			
			$.each( childrenGroups, function(){
				
				var $children = $(this);
				if ( !$children.filter(":visible").length ) return;
				
				var row = [];
				var top = 0;
				$children.each(function(){
					if ( top != $(this).position().top ) {
						$(row).sameHeight();
						row = [];
						top = $(this).position().top;
					}
					row.push(this);
				});
				if ( row.length ) $(row).sameHeight();
			});
			
			
		});
		init();
		return this;
	}
	
	$.checkFixHeight = function( i_force ) {
		if ( $fontSizeDiv.height() == textHeight && i_force !== true ) return;
		textHeight = $fontSizeDiv.height();
		$(parents).fixHeight();
	}
	
	$.fn.sameHeight = function() {
		var maxHeight = 0;
		this.css("height","auto");
		this.each(function(){
			if ( $(this).height() > maxHeight ) maxHeight = $(this).height();
		});
		return this.height(maxHeight);
	}
	
	function getChildren( i_parent ) {
		var $parent = $( i_parent );
		
		if ( $parent.data("fixHeightChildrenGroups") ) return $parent.data("fixHeightChildrenGroups");
		var childrenGroups = [];
		
		var $children = $parent.find(".fixHeightChild");
		if ( $children.length ) childrenGroups.push( $children );
		
		var $groupedChildren = $parent.find("*[class*='fixHeightChild']:not(.fixHeightChild)");
		if ( $groupedChildren.length ) {
			var classNames = {};
			$groupedChildren.each(function(){
				var a = $(this).attr("class").split(" ");
				var i;
				var l = a.length;
				var c;
				for ( i=0; i<l; i++ ) {
					c = a[i].match(/fixHeightChild[a-z0-9_-]+/i);
					if ( !c ) continue;
					c = c.toString();
					if ( c ) classNames[c] = c;
				}
			});
			for ( var c in classNames ) childrenGroups.push( $parent.find("."+c) );
		}
		
		if ( !childrenGroups.length ) {
			$children = $parent.children();
			if ( $children.length ) childrenGroups.push( $children );
		}
		
		$parent.data("fixHeightChildrenGroups", childrenGroups );
		parents.push( $parent );
		
		return childrenGroups;
	}
	
	
	function init() {
		if ( isInitialized ) return;
		isInitialized = true;
		$fontSizeDiv = $(document).append('<div style="position:absolute;left:-9999px;top:-9999px;">s</div>');
		setInterval($.checkFixHeight,1000);
		$(window).resize($.checkFixHeight);
		$.checkFixHeight();
		$(window).load( function(){ $.checkFixHeight(true); } );
	}
	
})(jQuery);



jQuery(document).ready(function(){
	$(".fixHeight").fixHeight();
});



/*---------------------------------------------------------------

 jQuery.slideScroll.js
 
 jQuery required (tested on version 1.2.6)
 encoding UTF-8

 Copyright (c) 2008 nori (norimania@gmail.com)
 http://moto-mono.net
 Licensed under the MIT

 jQueryUpdate: 2008-12-24 20:00
 jQueryDate: 2008-12-22 23:30
 
 ----------------------------------------------------------------*/

jQuery.fn.slideScroll = function(options){
	var c = jQuery.extend({
		interval: 20, // 変化はあんまりないかも
		easing: 0.6, // 0.4 ~ 2.0 くらいまで
		comeLink: false
	},options);
	var d = document;
	
	// timerとposのscopeをjQuery.fn.slideScroll内に限定する
	var timer;
	var pos;
	
	// スクロール開始時の始点を得る
	function currentPoint(){
		var current = {
			x: d.body.scrollLeft || d.documentElement.scrollLeft,
			y: d.body.scrollTop || d.documentElement.scrollTop
		}
		return current;
	}
	
	// 現在のウィンドウサイズとターゲット位置から最終到達地点を決める
	function setPoint(){
		
		// 表示部分の高さと幅を得る
		var h = d.documentElement.clientHeight;
		var w = d.documentElement.clientWidth;
		
		// ドキュメントの最大の高さと幅を得る
		var maxH = d.documentElement.scrollHeight;
		var maxW = d.documentElement.scrollWidth;
		
		// ターゲットの位置が maxH(W)-h(w) < target < maxH(W) なら スクロール先をmaxH(W)-h(w)にする
		pos.top = ((maxH-h)<pos.top && pos.top<maxH) ? maxH-h : pos.top;
		pos.left = ((maxW-w)<pos.left && pos.left<maxW) ? maxW-w : pos.left;
	}
	
	// 次のスクロール地点を決める
	function nextPoint(){
		var x = currentPoint().x;
		var y = currentPoint().y;
		var sx = Math.ceil((x - pos.left)/(5*c.easing));
		var sy = Math.ceil((y - pos.top)/(5*c.easing));
		var next = {
			x: x - sx,
			y: y - sy,
			ax: sx,
			ay: sy
		}
		return next;
	}
	
	// 到達地点に近づくまでスクロールを繰り返す
	function scroll(href){
		var movedHash = href;
		timer = setInterval(function(){
			nextPoint();
			
			// 到達地点に近づいていたらスクロールをやめる
			if(Math.abs(nextPoint().ax)<1 && Math.abs(nextPoint().ay)<1){
				clearInterval(timer);
				window.scroll(pos.left,pos.top);
				location.href = movedHash;
			}
			window.scroll(nextPoint().x,nextPoint().y);
		},c.interval);
	}
	
	// URIにhashがある場合はスクロールする
	function comeLink(){
		if(location.hash){
			if(jQuery(location.hash) && jQuery(location.hash).length>0){
				pos = jQuery(location.hash).offset();
				setPoint();
				window.scroll(0,0);
				if(jQuery.browser.msie){
					setTimeout(function(){
						scroll(location.hash);
					},50);
				}else{
					scroll(location.hash);
				}
			}
		}
	}
	if(c.comeLink) comeLink();
	
	// アンカーにclickイベントを定義する
	jQuery(this).each(function(){
		if(this.hash && jQuery(this.hash).length>0 
			&& this.href.match(new RegExp(location.href.split("#")[0]))){
			var hash = this.hash;
			jQuery(this).click(function(){
								   				
				// ターゲットのoffsetを得る
				pos = jQuery(hash).offset();
				
				// スクロール中ならスクロールをやめる
				clearInterval(timer);
				
				// 到達地点を決めてスクロールを開始する
				setPoint();
				scroll(this.href);
				return false;
			});
		}
	});
}

jQuery(document).ready(function(){
	jQuery('a[href*="#"]').filter(function(){
		return !jQuery(this).hasClass('no_scroll');
	}).slideScroll();
});

$(function(){
    $('#gNav a img').hover(
        function() {
            $(this).stop().fadeTo(200,0);
        },
        function() {
            $(this).stop().fadeTo(200,1.0);
        }
    );
	$('#gnav li a, .fbtn').hover(
        function() {
            $(this).stop().fadeTo(200,0.3);
        },
        function() {
            $(this).stop().fadeTo(200,1.0);
        }
    );
});


/*jCarouselLite*/

$(function() {
	$(".jCarouselLite").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		visible : 3,
		speed: 1000,
		auto: 2000
	});
});
