( function($) {
	var _ie6 = true;
	if (typeof document.body.style.maxHeight != "undefined")
		_ie6 = false;
	
	var _animateFlag = true;
	var _width = 940;
	var _disWidth = 31;
	var _dur = { duration:300, easing:"easeOutCubic" };
	var _delTime = 310;
	
	var _openFlag = false;
	var _nowV = -1;
	
	var _cInter = null;
	
	function animateClickHandler() {
		var flag = true;
		if( _mobile ) {
			if( _openFlag ) {
				flag = ( _nowV == $(this).data("i") );
			} else {
				flag = false;
			}
		}
		if( flag ) {
			var tgt = $(this).find("a");
			if( tgt.length > 0 ) {
				location.href = tgt.eq(0).attr("href");
			}
		} else {
			$(this).trigger("mouseenter");
		}
		return false;
	}
	function animateHandler( mode ) {
		return function() {
			if( _animateFlag && _nowV != $(this).data("i") ) {
				if( _openFlag ) {
					closeBox( $(this).data("i") );
				} else {
					_animateFlag = false;
					switch( mode ) {
						case "open":
							openBoxHandler( $(this).data("i") );
						break;
						case "close":
							closeBox( $(this).data("i") );
						break;
					}
				}
			}
		}
	}
	function openBoxHandler( num ) {
		var anm;
		_nowV = num;
		$("#iaBox .nBox").unbind("mouseenter");
		switch( num ) {
			case 0:
				anm = new OpenBoxWorks();
				anm.play();
			break;
			case 1:
				anm = new OpenBoxStaff();
				anm.play();
			break;
			case 2:
				anm = new OpenBoxEdusys();
				anm.play();
			break;
		}
	}
	function escapeBox( num ) {
		var cnt = 0;
		$("#iaBox .nBox").each( function(i) {
			if( num != i ) {
				$(this).stop(true, false).animate( { left:_width-_disWidth*2+cnt*_disWidth, "padding-left":30 }, _dur )
					.find("h2").stop(true, false).animate( { left:32 }, _dur );
				cnt++;
			}
		} );
	}
	function normalBox( num ) {
		$("#iaBox .nBox").each( function(i) {
			if( num != $(this).data("i") ) {
				$(this).stop(true, false).animate( { left:$(this).data("pos").left, "padding-left":0 }, _dur )
					.find("h2").stop(true, false).animate( { left:10 }, _dur );
			}
		} );
	}

	/* #############################################################
		OpenBox
	############################################################# */
		function OpenBox_animate( num ) {
			// this is dummy.
		}
		function OpenBox_repeat( num ) {
			if(
				( this.mode == "play" && num < this.alimit ) ||
				( this.mode == "rewind" && num >= 0 )
			) {
				this.animate( num );
			} else animateEnd( this.mode, this.rewindId );
		}
		function OpenBox_play() {
			this.mode = "play";
			this.repeat( 1 );
		}
		function OpenBox_rewind( num ) {
			this.mode = "rewind";
			this.repeat( this.alimit-2 );
			if( num >= 0 )
				this.rewindId = num;
		}
	function OpenBox() {
		this.alimit = 0;
		this.rewindId = -1;
	}
	new OpenBox();
	OpenBox.prototype.animate = OpenBox_animate;
	OpenBox.prototype.repeat = OpenBox_repeat;
	OpenBox.prototype.rewind = OpenBox_rewind;
	OpenBox.prototype.play = OpenBox_play;
		
	/* #############################################################
		OpenBoxWorks
	############################################################# */
		function OpenBoxWorks_animate( num ) {
			var l = -220;
			if( _ie6 ) l = -230;
			//alert(num+" / "+mode);
			var that = this;
			var func = function() {
				if( that.mode == "play" )
					that.repeat( num+1 );
				else if( that.mode == "rewind" )
					that.repeat( num-1 );
				else
					animateEnd( that.mode );
			}
			switch( num ) {
				case 0:
					that.tgt.find(".m img").stop(true,false).animate( { "margin-left":-65 }, _dur );
					that.tgt.stop(true,false).animate( { left:468 }, _dur );
					that.tgt.find("h2").stop(true,false).animate( { top:0 }, _dur );


					setTimeout( func, _delTime );
				break;
				case 1:
					that.tgt.find(".m img").stop(true,false).animate( { "margin-left":0 }, _dur );
					that.tgt.find(".l01").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".r01").stop(true,false).animate( { "right":0 }, _dur );
					that.tgt.animate( { left:220 }, _dur );
					normalBox( that.num );

					that.tgt.find(".l02").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".l03").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".r02").stop(true,false).animate( { "right":0 }, _dur );
					that.tgt.find(".r03").stop(true,false).animate( { "right":0 }, _dur );
					
					that.tgt.find("h2").stop(true,false).animate( { top:-70 }, _dur );

					that.tgt.css( "z-index", 15 );
					setTimeout( func, _delTime );
				break;
				case 2:
					that.tgt.find(".l01").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".r01").stop(true,false).animate( { "right":-244 }, _dur );
					escapeBox( that.num );

					that.tgt.find(".l02").stop(true,false).delay(50).animate( { "left": l, "top":0 }, _dur );
					that.tgt.find(".l03").stop(true,false).animate( { "left": l, "top":0 }, _dur );
					that.tgt.find(".r02").stop(true,false).delay(50).animate( { "right":-122, "top":0 }, _dur );
					that.tgt.find(".r03").stop(true,false).animate( { "right": -244, "top":0 }, _dur );
					
					that.tgt.find(".view").stop(true,false).animate( { "bottom":-44 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":-40 }, _dur );

					setTimeout( func, _delTime );
				break;
				case 3:
					that.tgt.find(".l02").stop(true,false).css( "left", l ).animate( { "left":l, "top":123 }, _dur );
					that.tgt.find(".l03").stop(true,false).css( "left", l ).delay(50).animate( { "left":l, "top":246 }, _dur );
					that.tgt.find(".r02").stop(true,false).animate( { "right":-122, "top":218 }, _dur );
					that.tgt.find(".r03").stop(true,false).delay(50).animate( { "right":-244, "top":218 }, _dur );
					
					that.tgt.css( "z-index", 30 );
					that.tgt.find(".view").stop(true,false).delay(150).animate( { "bottom":0 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":0 }, _dur );

					setTimeout( func, _delTime );
				break;
			}
		}
	function OpenBoxWorks() {
		this.num = 0;
		this.alimit = 4;
		this.tgt = $("#iaBox .nBox").eq(this.num);
	}
	new OpenBoxWorks();
	inherit( OpenBoxWorks, OpenBox );
	OpenBoxWorks.prototype.animate = OpenBoxWorks_animate;

	/* #############################################################
		OpenBoxStaff
	############################################################# */
		function OpenBoxStaff_animate( num ) {
			var l = -220;
			if( _ie6 ) l = -230;
			
			var l2 = -536;
			if( _ie6 ) l2 = -546;
			//alert(num+" / "+mode);
			var that = this;
			var func = function() {
				if( that.mode == "play" )
					that.repeat( num+1 );
				else if( that.mode == "rewind" )
					that.repeat( num-1 );
				else
					animateEnd( that.mode );
			}
			switch( num ) {
				case 0:
					that.tgt.css( "z-index", 15 );

					that.tgt.find("h2").animate( { top:0 }, _dur );
					that.tgt.find(".m img").stop(true,false).animate( { "margin-left":-45 }, _dur );
					that.tgt.stop(true,false).animate( { left:625 }, _dur );
					normalBox( that.num );

					setTimeout( func, _delTime );
				break;
				case 1:
					that.tgt.find(".m img").stop(true,false).animate( { "margin-left":0 }, _dur );
					that.tgt.find(".r01").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".r02").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".r03").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".l01").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".l02").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.stop(true,false).animate( { left:536 }, _dur );
					escapeBox( this.num );
					
					that.tgt.find("h2").stop(true,false).animate( { top:-70 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":-40 }, _dur );

					setTimeout( func, _delTime );
				break;
				case 2:
					that.tgt.find(".r01").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".r02").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".r03").stop(true,false).animate( { "left":l, "top":122 }, _dur );
					
					that.tgt.find(".l01").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".l02").stop(true,false).animate( { "left":l, "top":0 }, _dur );
					
					that.tgt.find(".view").stop(true,false).animate( { "bottom":-44 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":0 }, _dur );

					setTimeout( func, _delTime );
				break;
				case 3:
					that.tgt.find(".r03").stop(true,false).animate( { "top":246 }, _dur );
					
					var dur = new Object();
					$.extend( dur, _dur );
					dur.step = function() {
						that.tgt.find(".l02").css( "left", $(this).css("left") );
					}
					that.tgt.find(".l01").stop(true,false).animate( { "left":l2 }, dur );
					that.tgt.find(".l02").stop(true,false).animate( { "top":185 }, _dur );

					that.tgt.css( "z-index", 30 );
					that.tgt.find(".view").stop(true,false).delay(150).animate( { "bottom":0 }, _dur );

					setTimeout( func, _delTime );
				break;
			}
		}
	function OpenBoxStaff() {
		this.num = 1;
		this.alimit = 4;
		this.tgt = $("#iaBox .nBox").eq(this.num);
	}
	new OpenBoxStaff();
	inherit( OpenBoxStaff, OpenBox );
	OpenBoxStaff.prototype.animate = OpenBoxStaff_animate;

	/* #############################################################
		OpenBoxEdusys
	############################################################# */
		function OpenBoxEdusys_animate( num ) {
			var l = -161;
			if( _ie6 ) l = -171;
			
			var l2 = -404;
			if( _ie6 ) l2 = -414;
			//alert(num+" / "+mode);
			var that = this;
			var func = function() {
				if( that.mode == "play" )
					that.repeat( num+1 );
				else if( that.mode == "rewind" )
					that.repeat( num-1 );
				else
					animateEnd( that.mode );
			}
			switch( num ) {
				case 0:
					that.tgt.css( "z-index", 15 );

					that.tgt.find("h2").animate( { top:0 }, _dur );
					that.tgt.find(".m img").animate( { "margin-left":-45 }, _dur );
					that.tgt.stop(true,false).animate( { left:783 }, _dur );
					normalBox( that.num );
					
					setTimeout( func, _delTime );
				break;
				case 1:
					that.tgt.find(".m img").stop(true,false).animate( { "margin-left":0 }, _dur );
					that.tgt.find(".r01").stop(true,false).animate( { "right":-158 }, _dur );
					that.tgt.find(".r02").stop(true,false).animate( { "right":-158 }, _dur );
					that.tgt.stop(true,false).animate( { left:404 }, _dur );
					escapeBox( this.num );
					
					that.tgt.find(".lr01").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".lr02").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".lr03").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".ll01").stop(true,false).animate( { "left":0 }, _dur );
					that.tgt.find(".ll02").stop(true,false).animate( { "left":0 }, _dur );

					that.tgt.find("h2").stop(true,false).animate( { top:-70 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":-40 }, _dur );
					
					setTimeout( func, _delTime );
				break;
				case 2:
					that.tgt.find(".lr01").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".lr02").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".lr03").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".ll01").stop(true,false).animate( { "left":l }, _dur );
					that.tgt.find(".ll02").stop(true,false).animate( { "left":l }, _dur );
					
					that.tgt.find(".view").stop(true,false).animate( { "bottom":-44 }, _dur );
					that.tgt.find(".desc").stop(true,false).animate( { "top":0 }, _dur );
					
					setTimeout( func, _delTime );
				break;
				case 3:
					that.tgt.find(".ll01").stop(true,false).animate( { "left":l2 }, _dur );
					that.tgt.find(".ll02").stop(true,false).animate( { "left":l2 }, _dur );

					that.tgt.css( "z-index", 30 );
					that.tgt.find(".view").stop(true,false).delay(150).animate( { "bottom":0 }, _dur );
					
					setTimeout( func, _delTime );
				break;
			}
		}
	function OpenBoxEdusys() {
		this.num = 2;
		this.alimit = 4;
		this.tgt = $("#iaBox .nBox").eq(this.num);
	}
	new OpenBoxEdusys();
	inherit( OpenBoxEdusys, OpenBox );
	OpenBoxEdusys.prototype.animate = OpenBoxEdusys_animate;

	/* #############################################################
		closeBox
	############################################################# */
	function closeBox( num ) {
		var anm;
		if( _openFlag && _animateFlag ) {
			nowCheckEnd();
			_animateFlag = false;
			switch( _nowV ) {
				case 0:
					anm = new OpenBoxWorks();
					anm.rewind( num );
				break;
				case 1:
					anm = new OpenBoxStaff();
					anm.rewind( num );
				break;
				case 2:
					anm = new OpenBoxEdusys();
					anm.rewind( num );
				break;
			}
		}
	}
	function animateEnd( mode, id ) {
		if( mode=="rewind" ) {
			_openFlag = false;
			if( id >= 0 ) {
				openBoxHandler( id );
			} else {
				_animateFlag = true;
			}
		} else {
			nowCheckStart();
			_openFlag = true;
			_animateFlag = true;
		}
	}
	function inherit(subClass, superClass) {
		for (var prop in superClass.prototype) {
			subClass.prototype[prop] = superClass.prototype[prop];
		}
	}
	
	/*  */
	function nowCheckStart() {
		var _now = -1;
		function checkNow() {
			var now = $(this).data("i");
			if( _now < 0 ) _now = now;
			if( _now != now ) {
				nowCheckEnd(now);
			}
		}
		$("#iaBox .nBox").bind("mousemove", checkNow );
	}
	function nowCheckEnd( num ) {
		$("#iaBox .nBox").unbind("mousemove");
		$("#iaBox .nBox").bind("mouseenter", animateHandler("open") );
		if( num>=0 ) {
			$("#iaBox .nBox").eq(num).trigger("mouseenter");
		}
	}
	/*  */
	
	function checkHover() {
		_cInter = setInterval( function() {
			if( _animateFlag && _openFlag ) {
				clearCheck();
				closeBox();
				_nowV = -1;
			}
		}, 400 );
	}
	function clearCheck() {
		if( _cInter ) clearInterval( _cInter );
	}
	
	/* IndexMenu */
	var _indexCloseTimer = null;
	function indexMenuOpen() {
		if( _indexCloseTimer ) clearTimeout(_indexCloseTimer);
		$("#indexMenu p").stop(true, false).animate( {"left":0}, {duration:200, easing:"easeOutCubic"} );
		$("#indexMenuToggle").stop(true, false).animate( {"width":940}, {duration:500, easing:"easeOutCubic"} );
	}
	function indexMenuClose() {
		_indexCloseTimer = setTimeout( function() {
			$("#indexMenu p").stop(true, false).animate( {"left":-10}, {duration:200, easing:"easeOutCubic"} );
			$("#indexMenuToggle").stop(true, false).animate( {"width":0}, {duration:500, easing:"easeOutCubic"} );
		}, 300 );
	}

	var _mobile = (function(){
		return /android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase());
	})();

	$( function() {
		$("#iaBox, #indexMenu").css("opacity", 0);
	} );
	$(window).load( function() {
		$("#iaBox, #indexMenu").css("opacity", 1);
		
		$("#iaBox .nBox").each( function(i) {
			$(this).css("cursor","pointer").data("i", i).data("pos", $(this).position() );
		} ).bind("mouseenter", animateHandler("open") ).bind("click", animateClickHandler);
		$("#iaBox").hover( clearCheck, checkHover );
		
		$("#iaEntry").setDivAnchor();
		$("#indexMenu p, #indexMenuToggle").hover( indexMenuOpen, indexMenuClose );
	} );
} )( jQuery );
