/* =====================================================================
*
*   common.js | for IMRC
*
* =================================================================== */

if(!window.IMRC) window.IMRC = {};

/* =====================================================================
*
*   plugins
*
* =================================================================== */

(function($){
	
	//queueを消すstop
	$.fn.exStop = function() {
		return this.queue([]).stop();
	}
	
	//初期値の高さを記憶するslideDown
	$.fn.exSlideDown = function( i_time, i_easing, i_callback ) {
		this.exStop();
		return this.each(function(){
			var $this = $(this);
			var hidden = $this.css("display") == "none";
			$this.show();
			if ( !$this.data("exSlideDownHeight") ) $this.data("exSlideDownHeight",$this.height());
			if ( !$this.data("exSlideDownOverflow") ) $this.data("exSlideDownOverflow",$this.css("overflow"))
			$this.css("overflow","hidden");
			if ( hidden ) $this.height(0);
			$this.animate( { height:$this.data("exSlideDownHeight") }, i_time, i_easing, function(){
				var o = $(this).data("exSlideDownOverflow");
				if ( o ) $(this).css("overflow",o);
				if ( i_callback ) i_callback();
			});
		});
	}
	
	//slideUp
	$.fn.exSlideUp = function( i_time, i_easing, i_callback ) {
		this.exStop();
		return this.each(function(){
			var $this = $(this);
			if ( !$this.data("exSlideDownOverflow") ) $this.data("exSlideDownOverflow",$this.css("overflow"))
			$this.css("overflow","hidden");
			return $this.animate( { height:0 }, i_time, i_easing, function(){
				var o = $(this).data("exSlideDownOverflow");
				$(this).hide();
				if ( o ) $(this).css("overflow",o);
				if ( i_callback ) i_callback();
			});
		});
	}
	
	//opacity:1までフェードイン
	$.fn.exFadeIn = function( i_time ) {
		this.exStop();
		return this.each(function(){
			var $this = $(this);
			if ( $this.css("display") == "none" ) {
				$this.show();
				$this.css( {opacity:0} );
			}
			$this.animate( { opacity:1 }, i_time );
		});
	}
	
	//フェードアウト
	$.fn.exFadeOut = function( i_time ) {
		this.exStop();
		return this.fadeOut( i_time );
	}
	
	//遅延実行プラグイン
	$.fn.startTimer = function( i_function, i_time, i_key ) {
		return this.each(function(){
			$(this).stopTimer( i_key );
			var timers = $(this).data("timers") || {};
			timers[i_key] = setTimeout( i_function, i_time );
			$(this).data("timers",timers);
		});
	}
	
	$.fn.stopTimer = function( i_key ) {
		return this.each(function(){
			var timers = $(this).data("timers") || {};
			if ( timers[i_key] ) clearTimeout( timers[i_key] );
		});
	}
	
	$.fn.overlayDropDown = function(i_options){
		
		var options = $.extend({
			offsetX : 0,
			offsetY : 0,
			imgUrl : null
		}, i_options);
		
		return this.each(function(){
			var imgUrl = options.imgUrl;
			var linkTarget = $(this).find("a").attr("href");
			var $parent = $(this);
			var $btn = $parent;
			var $slave = $parent.children("ul");
			var isSlave = ($slave.length != 0) ? true : false;

			var $over = (function(){
				if(isSlave) {
					return $('<img src="'+imgUrl+'" style="position:absolute; margin-top:-10px" />').prependTo($btn);
				} else {
					return $('<img src="'+imgUrl+'" style="position:absolute; margin-top:-10px" />').prependTo($parent);
				}
			})();
			
			if(!isSlave) {
				$over.css("cursor", "pointer");
				$over.click(function(){
					window.location.href = linkTarget;
				});
			}
			
			$over.hide();
			$btn.hover(function(){

				if(isSlave) {
					$slave.stopTimer();
					$slave.exSlideDown(300);
				}
				
				$over.exStop().exFadeIn(300);
			}, function(){
				
				if(isSlave) {
					$slave.startTimer( function() { $slave.exSlideUp(); $over.exFadeOut(); }, 300 );
				} else {
					$over.exFadeOut();
				}
				
			});
			
			if(isSlave) {
				$slave.hover(function(){
					$slave.stopTimer();
					$slave.exSlideDown();
					$over.exStop().exFadeIn(300);
				}, function(){
					$slave.startTimer( function() { $slave.exSlideUp(); $over.exFadeOut(); }, 300 );
				});
			}
			
		});
		
	}

})(jQuery);

(function($){
		
	 $.fn.simpleTooltip = function( i_options ) {
		
		var defaults = {};
		var options = $.extend( defaults, i_options );
		
		//var content;
		this.each(function(){
			
			
			var targetImage = $("<img>").attr("src", $(this).attr("href"));
			
			var $this = $(this);
			var hoverType = "auto";
				
				
			$(this).data('hoverType', hoverType);
			$(this).hover(function(){
				var thisPos = $(this).position();
				var thisScale = { width : $(this).width(), height : $(this).height() };
	
				var toolTipBody = $('<div id="simpleTooltip"></div>')
					.css("padding", "5px")
					.css("border", "1px solid #CCCCCC")
					.css("background", "F9F9F9")
					.css('display', 'block')
					.append(targetImage); 
				
				if(targetImage != null) {
					var posHorizontal;
					var posVertical;
					$('body')
						.append(toolTipBody);
						
					toolTipBody
						.css("width", targetImage.width())
						.css("height", targetImage.height());
					
					if($(this).data('hoverType') == 'auto') {
						$(this).data('auto', true);
						var tPos = ($(this).offset().top) - ($(window).scrollTop());
						var lPos = ($(this).offset().left) - ($(window).scrollLeft());
						var bPos = tPos + $(this).height();
						var rPos = lPos + $(this).width();
						
						var type =	((tPos - toolTipBody.height()) < 5) ? "bottom" : 
									((lPos - toolTipBody.width()) < 5) ? "right" :
									((bPos + toolTipBody.height()) >= $(window).height() - 5) ? "top" :
									((rPos + toolTipBody.width()) > $(window).width() - 5) ? "left" :
									"top";
						$(this).data('hoverType', type);
					}
					
					switch($(this).data('hoverType')) {
						case 'left' : 
							posHorizontal = (thisPos.left - toolTipBody.width() - 20);
							posVertical = (thisPos.top - toolTipBody.height() / 2);
							break;
							
						case 'right' :
							posHorizontal = (thisPos.left + thisScale.width + 10);
							posVertical = (thisPos.top - toolTipBody.height() / 2);
							break;
							
						case 'top' : 
							posHorizontal = (thisPos.left + (thisScale.width / 2)) - toolTipBody.width() / 2;
							posVertical = (thisPos.top - 20) - toolTipBody.height();
							break;
							
						case 'bottom' : 
							posHorizontal = (thisPos.left + (thisScale.width / 2)) - toolTipBody.width() / 2;
							posVertical = (thisPos.top + thisScale.height + 10);
							break;
							
						default : 
							return;
							break;
					}
					
					$('#simpleTooltip')
						.css("position","absolute")
						.css("top", posVertical + 'px')
						.css("left", posHorizontal + 'px')
						.css("display", "none")
						.fadeIn('fast');
				}
			}, function(){
				if($(this).data('auto')) {
					$(this).data('hoverType', 'auto');
				}
				$('#simpleTooltip').remove();
				
			});
			
			$(this).click(function(e){
				e.preventDefault();
			})
		});
	}

})(jQuery);

(function($){

	$.fn.simpleRollOver = function( i_options ){
		
		var defaults = {
			postfix:"-over"
		};
		var options = $.extend( true, defaults, i_options );
		
		$(this).each(function(){
			var $a = $(this);
			var $i = $a.children('img');
			if(!$i.length && $a.attr("src")) $i = $a;
			if(!$i.length) return;
			
			var up = $i.attr("src");
			var over = up.replace(/\.([a-zA-Z0-9]+)$/,options.postfix+".$1");
			
			$("<img />").attr("src",over);
			$a.hover(
				function(){ $i.attr("src",over); },
				function(){ $i.attr("src",up); }
			);
		});
		
		return this;
	};

	
})(jQuery);

/* =====================================================================
*
*   entry point
*
* =================================================================== */

$(function(){
	
	//global nav dropDown
	$("#GN-About").overlayDropDown({ imgUrl : "/images/common/header/nav-global/about-over.gif", offsetY : -10 });
	$("#GN-Reserch").overlayDropDown({ imgUrl : "/images/common/header/nav-global/report-over.gif", offsetY : -10 });
	$("#GN-Home").overlayDropDown({ imgUrl : "/images/common/header/nav-global/home-over.gif", offsetY : -10 });
	$("#GN-Genga").overlayDropDown({ imgUrl : "/images/common/header/nav-global/genga-over.gif", offsetY : -10 });
	$("#GN-Think").overlayDropDown({ imgUrl : "/images/common/header/nav-global/think-over.gif", offsetY : -10 });
	
	//lightbox
	$('a[rel="lightbox"]').lightBox();
	
	//tooltip
	$("a.exPopup").simpleTooltip();
	
	//bigtarget
	$("a.bigtarget").bigTarget({ clickZone : 'div:eq(1)' });
	$("a.bigtarget2").bigTarget({ clickZone : 'div:eq(0)' });
	
	//rollover
	$(".rollover").simpleRollOver();
	
});
