$(function () {
	
	sizing();
	
	var hyperspace = new Hyperspace();
	hyperspace.draw();
		
	$("#canv").fadeIn('fast');
	$(window).resize(function() {
		sizing();
		hyperspace.draw();
	});
	
	//smooth scroll
	jQuery.easing.quart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	jQuery.fn.smooth_scroll = function () {
		jQuery('html,body').stop().animate({
			'scrollTop' : this.offset().top
		}, 900, 'quart');
	};
	$('a[href*=#]').live('click', function(){
		var path = location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'');
		var host = location.hostname == this.hostname;
		if (!path || !host) return;
		var hash = this.hash.slice(1);
		jQuery(this.hash + ',[name=' + hash +']').eq(0).smooth_scroll();
	});

	//nav blog
	$('#navBlogEntries, #navPickupEntries').hover(
		function(){
			$(this).find('.panel').stop().fadeIn(100, function () {
				$(this).css('opacity', 1);
			});
		},
		function(){
			$(this).find('.panel').stop().fadeOut(100, function () {
				$(this).css('opacity', 0);
			});
		}
	);
	$('#navBlogTags').toggle(
		function(){
			$('#navTags').fadeIn(200);
		},
		function(){
			$('#navTags').fadeOut(300);
		}
	);
	
	//tag
	$('ul#filter_me').liveFilter('fade');
	$('ul#filter_me2').liveFilter('fade');

	//nu-
	$('#nu').toggle(
		function(){
			$(this).hover(function () {
				$(this).text('ヌ？ ');
			}, function () {
				$(this).text('ヌー ');
			});
		},
		function(){
			$(this).unbind('mouseenter').unbind('mouseleave').text('');
		}
	);

	$(window).keyup(function (e) {
		if (!(e.keyCode == 37 || e.keyCode == 39)) return;
		if ($(e.source).is(':input')) return;
		var top = parseInt(
			window.pageYOffset
			|| document.documentElement.scrollTop
			|| document.body.scrollTop
		);
		if (e.keyCode == 37) {
			var prev;
			$('.entryWrapper').each(function () {
				var offset = parseInt($(this).offset().top);
				if (offset < top) {
					prev = this;
					return;
				};
				$(prev || this).smooth_scroll();
				return false;
			});
			return;
		};
		var last;
		$('.entryWrapper').each(function () {
			var offset = parseInt($(this).offset().top);
			if (offset <= top) return;
			$(this).smooth_scroll();
			last = true;
			return false;
		});
		if (last) return;
		$('.pager:last').smooth_scroll();
	});

	// Pickup Entries
	google.setOnLoadCallback(function () {
		var feed = new google.feeds.Feed('http://b.hatena.ne.jp/entrylist?mode=rss&url=http%3A%2F%2Ftech.kayac.com%2F&sort=count&threshold=3&include_news=1');
		feed.setNumEntries(5);
		feed.load(function (result) {
			if (result.error) return;
			var html = '';
			$('.pickupEntries .listEntry').html(
				'<li>' +
				$(result.feed.entries).map(function () {
					var title = this.title.split(/\s*:\s*/);
					title.pop();
					return '<a href="' + this.link
						+ '" rel="bookmark">'
						+ title.join('') + '</a>';
				}).get().join('</li>\n<li>')
				+ '</li>'
			);
		});
	});
});

function sizing(){
	$("#canv").attr({height:$("#wrapper").height()});
	$("#canv").attr({width:$("#wrapper").width()});
}

var Hyperspace = function(){
	
	var canvas = document.getElementById('canv');
	var ctx = canvas.getContext('2d');
	
	//background
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	
	var star = new Array(10);
	for( i=0; i<star.length; i++ ){
		star[i] = new Array(7);
		star[i][0] = Math.random()*canvas.width;	// x
		star[i][1] = Math.random()*canvas.height;	// y
		star[i][2] = Math.random()*canvas.width/3 + canvas.width/14;	// r
		star[i][3] = Math.random()*0.05 + 0.2;
		star[i][7] = true;
		var r = Math.random()*255;	// R
		var g = Math.random()*255;	// G
		var b = Math.random()*255;	// B
		r = Math.ceil(r);
		g = Math.ceil(g);
		b = Math.ceil(b);
		star[i][4] = r;		//r
		star[i][5] = g;		//g
		star[i][6] = b;		//b
	}
	
	//draw -------------------------------------
	var draw = function draw(){
		
		//reset
		ctx.globalCompositeOperation = "source-over";
		//ctx.fillStyle = "rgba(255, 255, 255, 1)";
		ctx.fillStyle = "rgba(0, 0, 0, 1)";
		ctx.fillRect(0, 0, canvas.width, canvas.height);
		
		ctx.globalCompositeOperation = "lighter";
		
		//point
		for(i=0; i<star.length; i++){
			ctx.beginPath();
			var edgecolor1 = "rgba(" + star[i][4] + "," + star[i][5] + "," + star[i][6] + ",0.93)";
			var edgecolor2 = "rgba(" + star[i][4] + "," + star[i][5] + "," + star[i][6] + ",0.6)";
			var edgecolor3 = "rgba(" + star[i][4] + "," + star[i][5] + "," + star[i][6] + ",0.1)";
			var edgecolor4 = "rgba(" + star[i][4] + "," + star[i][5] + "," + star[i][6] + ",0)";
			var gradblur = ctx.createRadialGradient(star[i][0], star[i][1], 0, star[i][0], star[i][1], star[i][2]);
			gradblur.addColorStop(0,edgecolor1);
			gradblur.addColorStop(0.4,edgecolor1);
			gradblur.addColorStop(0.7,edgecolor2);
			gradblur.addColorStop(0.9,edgecolor3);
			gradblur.addColorStop(1,edgecolor4);
			ctx.fillStyle = gradblur;
			ctx.arc(star[i][0], star[i][1], star[i][2], 0, Math.PI*2, false);
			ctx.fill();
			
			
		}
		
	};
		
	return ({'draw': draw});
	
};

/***********************************************************/
/*                    LiveFilter Plugin                    */
/*                      Version: 1.2                       */
/*                      Mike Merritt                       */
/*             	   Updated: Apr 15th, 2010                 */
/***********************************************************/

(function($){
	$.fn.liveFilter = function (aType) {
		
		// Determine what we are going to be filtering.
		var filterTarget = $(this);
		var child;
		if ($(this).is('ul')) {
			child = 'li';
		} else if ($(this).is('ol')) {
			child = 'li';
		} else if ($(this).is('table')) {
			child = 'tbody tr';
		}
		
		// Declare variables
		var hide;
		var show;
		var filter;
		
		// Input element event
		$('input.filter').keyup(function() {
			
			// Grab the filter value
			filter = $(this).val();
			
			// Grab the ones we need to hide and the ones we need to show
			hide = $(filterTarget).find(child + ':not(:Contains("' + filter + '"))');
			show = $(filterTarget).find(child + ':Contains("' + filter + '")')
			
			// Animate the items we are hiding and showing
			if ( aType == 'basic' ) {
				hide.hide();
				show.show();
			} else if ( aType == 'slide' ) {
				hide.slideUp(500);
				show.slideDown(500);
			} else if ( aType == 'fade' ) {
				hide.fadeTo("fast" , 0.2);
				show.fadeTo("fast" , 1);
				
			}
			
		});		
		
		// Custom expression for case insensitive contains()
		jQuery.expr[':'].Contains = function(a,i,m){
		    return jQuery(a).text().toLowerCase().indexOf(m[3].toLowerCase())>=0;
		}; 

	}

})(jQuery);
