/* ==============================================================
	
	fadein/out button image function
	for jQuery

============================================================== */

new function() {
	
	var fadeInTime = 800;	// msec
	var fadeOutTime = 400;	// msec
	var offClass = 'off';
	var onClass = 'on';
	
	if ( typeof jQuery == 'undefined' ) {
		return;
	}
	
	jQuery(document).ready( function() {
		init();
	});
	
	/**
	 * initialize
	 */
	function init() {
		
		jQuery( 'a img' ).each( function() {
			
			var src = jQuery(this).attr( 'src' );
			var fadePatern = new RegExp( /.*_off\.[^.]+/ );
			var pngPatern = new RegExp( /.*\.gif$/ );
			var onImage;
			
			if ( src.match( fadePatern ) ) {
				onImage = jQuery(this).clone();
				onImage.
					attr( 'src', src.replace( '_off.', '_on.' ) ).
					addClass( onClass ).
					fadeTo( 0, 0 ).
					css({
						'position': 'absolute',
						'left': '0px',
						'top': '0px'
					});
				
				jQuery(this).
					addClass( offClass ).
					css({
						'position': 'absolute',
						'left': '0px',
						'top': '0px'
					}).
					parent().
						append( onImage ).
						mouseover( onMouseOver ).
						mouseout( onMouseOut ).
						css({
							'display': 'block',
							'position': 'relative'
						}).
						width( jQuery(this).width() ).
						height( jQuery(this).height() );
				
				if ( typeof( DD_belatedPNG ) != 'undefined' ) {
					if ( src.match( pngPatern ) ) {
						DD_belatedPNG.fixPng( this );
						DD_belatedPNG.fixPng( onImage.get(0) );
					}
				}
			}
		});
	}
	
	
	/**
	 * mouseover event( fadein )
	 */
	function onMouseOver( e ) {
		
		var src = jQuery(this).children( 'img.' + offClass ).attr( 'src' );
		var pngPatern = new RegExp( /.*\.gif$/ );
		
		jQuery(this).unbind( 'mouseover', onMouseOver );
		
		if ( src.match( pngPatern ) ) {
			jQuery(this).
				children( 'img.' + offClass ).
					fadeTo( fadeInTime, 0 ).
				end().
				children( 'img.' + onClass ).
					fadeTo( fadeInTime, 2, function(){
						jQuery(this).parent().mouseover( onMouseOver );
					});
		}
		else {
			jQuery(this).
				children( 'img.' + onClass ).
					fadeTo( fadeInTime, 2, function(){
						jQuery(this).parent().mouseover( onMouseOver );
					});
		}
	}
	
	/**
	 * mouseout event( fadeout )
	 */
	function onMouseOut( e ) {
		
		var src = jQuery(this).children( 'img.' + offClass ).attr( 'src' );
		var pngPatern = new RegExp( /.*\.gif$/ );
		
		if ( src.match( pngPatern ) ) {
			jQuery(this).
				children( 'img.' + offClass ).
					fadeTo( fadeOutTime, 2 ).
				end().
				children( 'img.' + onClass ).
					fadeTo( fadeOutTime, 0 );
		}
		else {
			jQuery(this).
				children( 'img.' + onClass ).
					fadeTo( fadeOutTime, 0 );
		}
	}
};

 
/* ==============================================================
	
	Bubbleup function
	for jQuery

============================================================== */


(function($){
	$.fn.bubbleup = function(options) {
		//Extend the default options of plugin
		var opt = $.extend({}, $.fn.bubbleup.defaults, options),tip = null;
		
		return this.each(function() {
			var w=$(this).width();
			$(this).mouseover(function(){
				if(opt.tooltip) {
					tip = $('<div>' + $(this).attr('alt') + '</div>').css({
						fontFamily: opt.fontFamily,
						color: opt.color, 
						fontSize: opt.fontSize, 
						fontWeight: opt.fontWeight, 
						position: 'absolute', 
						zIndex: 100000
					}).remove().css({top:0,left: 0,visibility:'hidden',display:'block'}).appendTo(document.body);
					var position = $.extend({},$(this).offset(),{width:this.offsetWidth,height:this.offsetHeight}),tipWidth = tip[0].offsetWidth, tipHeight = tip[0].offsetHeight;
					tip.stop().css({
						top: position.top - tipHeight, 
						left: position.left + position.width / 2 - tipWidth / 2,
						visibility: 'visible'
					}).animate({top:'-='+(opt.scale/2-w/2)},opt.inSpeed); 
				}
				
				$(this).closest('li').css({'z-index':100000});
				
				$(this).stop().css({'z-index':100000,'top':0,'left':0,'width':w}).animate({
					left:-opt.scale/2+w/2,
					top:-opt.scale/2+w/2,
					width:opt.scale
				},opt.inSpeed);
			}).mouseout(function(){
				
				$(this).closest('li').css({'z-index':100});
				$(this).closest('li').next().css({'z-index':0});
				$(this).closest('li').next().css({'z-index':0});
				$(this).closest('li').next().children('img').css({'z-index':0});
				
				if(opt.tooltip){tip.remove()}
				$(this).stop().animate({left:0,top:0,width:w},opt.outSpeed,function(){
					$(this).css({'z-index':0});
					
				});
			})
		})
	}
	$.fn.bubbleup.defaults = {
		tooltip: false,
		scale:96,
		fontFamily:'Helvetica, Arial, sans-serif',
		color:'#fff',
		//fontSize:12,
		fontWeight:'normal',
		inSpeed:'fast',
		outSpeed:'fast'
	}
})(jQuery);





/* Bubbleup  */
$(function(){

    $("div.fNavBoxFollow ul li img").bubbleup({tooltip: true, scale:50});
    
});



/* ==============================================================
	
	Following scroll function
	for jQuery

============================================================== */
$(function(){

var offset = $("#aside").offset();
var topPadding = 15;

$(window).scroll(function() {
	var asideH = $("#aside").height();
	var articleH = $("#contenntsSectionBox").height();

	if(articleH > asideH){
		if(asideH + ($(window).scrollTop() - offset.top) < articleH){
			if($(window).scrollTop() > offset.top){
				$("#aside").stop().animate({
					marginTop: $(window).scrollTop() - offset.top + topPadding
				});
			}else{
				$("#aside").stop().animate({
					marginTop: 0
				});
			}
		}else{
			$("#aside").stop().animate({
				marginTop: articleH - asideH
			});
		}
	}
});



});
