( function($) {
	var _oHeight	= 0;
	var _cHeight	= 0;
	var _cDuration	= 0;
	var _sHeight	= 0;
	var _sDuration	= 0;
	var _wStep		= 0;
	var _wStepDur	= 15;
	$( function() {
		$.ajax( {
			url:"news_get.php",
			dataType:"json",
			success:loadEnd
		} );
	});
	function loadEnd(json) {
		var news = json.news;
		for( var i=0, len=news.length; i<len; i++ ) {
			$("#newsInner dl")
				.append( $("<dt>").text( news[i].date ) )
				.append( $("<dd>").append( function() {
					if( news[i].link ) return $("<a>", {"href":news[i].link} ).html( news[i].title );
					return news[i].title;
				} ) );
		}
		
		if( !_mobile ) {
			$("#newsInner").css( {"overflow":"hidden"} ).find("dl").css( {"position":"absolute", "left":0, "top":0, "padding-right":15} );
	
			_oHeight	= $("#newsInner").height();
			_cHeight	= $("#newsInner dl").height();
			_cDuration	= _cHeight-_oHeight;
			_sHeight	= _oHeight*(_oHeight/_cHeight);
			_sDuration	= _oHeight-_sHeight;
			_wStep		= (_sDuration*_wStepDur) / _cDuration;
			
			if( _cDuration > 0 ) {
				$("#newsInner").append( $("<div>", {"id":"scrollBar"}) ).mousewheel( mouseWheel );
				$("#scrollBar").css("height", _sHeight ).fadeTo( 1, 0.5 ).mousedown( dragStart );
				$("body").mousemove( dragStep ).mouseup( dragEnd );
				rovEnabled();
			}
		} else {
			$("#newsInner").css("-webkit-overflow-scrolling", "touch");
		}
	}
	
	function rovEnabled() {
		$("#scrollBar").hover( function() {
			$(this).stop(true, false).fadeTo( 200, 1 );
		}, function() {
			$(this).stop(true, false).fadeTo( 200, 0.5 );
		} ).trigger("mouseleave");
	}
	function rovDisabled() {
		$("#scrollBar").trigger("mouseenter").unbind( "mouseenter" ).unbind( "mouseleave" );
	}
	
	var _draggable = false;
	var _sPos = 0;
	function dragStart( e ) {
		_draggable = true;
		_sPos = e.pageY;
		_bPos = pxToNum( $("#scrollBar").css("top") );
		e.preventDefault();
		
		rovDisabled();
	}
	function dragStep(e) {
		if( _draggable ) {
			/* ScrollBar */
			gotoPos( _bPos+(e.pageY-_sPos) );
		}
		e.preventDefault();
	}
	function dragEnd(e) {
		_draggable = false;
		e.preventDefault();
		rovEnabled();
	}
	
	function gotoPos( toPos ) {
		var flag = true;
		if( toPos < 0 ) {
			toPos = 0;
			flag = false;
		} else if( toPos > _sDuration ) {
			toPos = _sDuration;
			flag = false;
		}

		/* ScrollBar */
		$("#scrollBar").css("top", toPos );
		/* Contents */
		$("#newsInner dl").css("top", -_cDuration*(toPos/_sDuration) );
		
		return flag;
	}
	
	function mouseWheel(e, d) {
		var flag = true;
		if( d > 0 ) {
			flag = gotoPos( pxToNum( $("#scrollBar").css("top") )-_wStep );
		} else if( d < 0 ) {
			flag = gotoPos( pxToNum( $("#scrollBar").css("top") )+_wStep );
		}
		if( flag ) {
			e.preventDefault();
		}
	}
	
	var _mobile = (function(){
		return /android|iphone|ipad|ipod/i.test(navigator.userAgent.toLowerCase());
	})();
	function pxToNum( px ) {
		return parseInt(px.replace("px", ""));
	}
} )(jQuery);
