/*
 * kei taniguchi 
 * http://keitaniguchi.net
 *
 */


$(document).ready(function(event) {
/* -------------------------------------------------------
 * Home Page
-------------------------------------------------------- */
	$('.home img.profile').live('mouseover',function(event){
		$('img.profile').css({"border-color":"#999"});
	});
	$('.home img.profile').live('mouseout',function(event){
		$('img.profile').css({"border-color":"#777"});
	});

//twitter hover
	flag = true;
	$('.twtr-tweets').live('mouseover', function(event){
		if (flag) {
			$('.twtr-tweet').prepend('<div class="translucent"></div>');
			flag = false;
		}
		var target = $(event.target).closest('.twtr-tweet-wrap').parent().children('.translucent');
		target.css({"display":"block", "opacity":"0.1"});
	});
	$('.twtr-tweets').live('mouseout', function(event){
		var target = $(event.target).closest('.twtr-tweet-wrap').parent().children('.translucent');
		target.css({"display":"block", "opacity":"0"});
	});

//twitpic replace
	$('a.twtr-hyperlink').each(function(index){
		var t = $(this).html();
		var r = t.replace(/http:\/\/twitpic.com\//i,"");
		var twit_thumb = "<img src='http://twitpic.com/show/mini/" + r + "' height='18' />"
		$(this).html(twit_thumb);
	});


//tab
	$(".blogUpdatesArea").css({"display":"none"});
	$(".tabArea a").click(function (e) {
		e.preventDefault();
		$(".on").removeClass("on");
		var targetCont = $(this).attr("href");
		$(this).parent().addClass("on");
		$(".content").hide();
		$(targetCont).show();
	}).css({"cursor":"pointer"});


//tab hover
/*	$('li.tabBlogUpdates a').hover(function(e) {
		$(e.target).parent().css({"text-decoration":"underline"});
	}, function(e) {
		$(e.target).parent().css({"text-decoration":"none"});
	});
	$('.tabBlogLatest a').hover(function(e) {
		$(e.target).parent().css({"text-decoration":"underline"});
	}, function(e) {
		$(e.target).parent().css({"text-decoration":"none"});
	});
*/

//tab main image
	$(".blogUpdatesArea h3 a").mouseover(function (e) {
		$("#blog-latest a").attr("href", e.target.href);
		
		const fixed_w = 428;
		const fixed_h = 400;

		$("#blog-latest a img").attr("src", e.target.id);
		$src_img = e.target.id;
//		w = $(e.target).children("img").attr("width");
//		h = $(e.target).children("img").attr("height");
/*
		if (w >= h) {
			$("#blog-latest a img").width(fixed_w);
				$("#blog-latest a img").height(fixed_h * h / w);
		} else {
			$("#blog-latest a img").width(fixed_w * w / h);
			$("#blog-latest a img").height(fixed_h);
		}
*/
	});


/* -------------------------------------------------------
 * Works Page
-------------------------------------------------------- */
	//private works title
	$("a.private-link").hover(function(e) {
		$("#top-private-title").css({"display":"block"});
	}, function(e) {
		$("#top-private-title").css({"display":"none"});
	});

//画像をフェードインする処理
//fade in images
	$(window).load(function(){
		var time = 100;
		$('.photo:hidden').map(function() { // すべての<img>要素を順に処理
			$(this).fadeIn(time);
			time += 120;
		});
	});
	
/*
 * For photo flash effect
 * 
 */
	$('.imglists').live('mouseover', function(event) {
		var target = $(event.target).closest('.hitArea').parent().parent().children('.whiteout');
		var photo = $(event.target).closest('.hitArea').parent().parent().children('.photo');
		var _h = photo.children().children().attr("height");
		var _w = photo.children().children().attr("width");
		target.css({"display":"block", "opacity":"0.4", "width":_w, "height":_h}).fadeOut(70);
		console.log("target", target);
		console.log("photo", photo);
		
	});

/* -------------------------------------------------------
 * Blog Page
-------------------------------------------------------- */
	//画像サイズの取得
	$('.hentry').css({"min-height":""});
	$('.hentry .entry img').each(function(i) {
		var h = $(this).height();
		var dh = $(this).parent().parent().parent().height();
		//if ( (h+80) > 600 ) {
		var mh = h + 80 + "px";
			$(this).parent().parent().parent().parent().css({"height":mh});
		//}
	});

/* -------------------------------------------------------
 * About Page
-------------------------------------------------------- */
	//about english
	$('#english').toggle(function() {
		$('#profile-en .en').show('fast');
	}, function() {
		$('#profile-en .en').hide(200);
	});


/* -------------------------------------------------------
 * Common
-------------------------------------------------------- */


	//center pics area
	_resize();
 });


//window resized function
$(window).scroll(_resize).resize(_resize);


function _resize() {
	
	var _w = ($(document).width() - (windowPadding + photo_right_pad));
//	$(".imglists").css({ "margin" : "0 auto", "width" : _w });
	
	//フォトエリアを中央寄せにする。
	const windowPadding = 30 * 2;
	const photoSize = 85;
	const photo_right_pad = 25;
	const photoArea = photoSize + photo_right_pad; //110px
	const tooltipPaddingTop = 6;
	const tooltipPaddingSide = 20;


	$('div.photo > a > img').mouseenter(function(e) {
		//photo flash
		var _x = $(e.target).position().left;
		var _y = $(e.target).position().top;
		var _w = $(e.target).width();
		var _h = $(e.target).height();


		//周囲のドロップシャドウ
		$(e.target).addClass("current");

		//TODO: タイトルが、フェードアウトする処理

		//tooltip の処理
		var title = $(e.target).attr("title");
		$(".tooltip").text(title);
		var tooltipWidth = $(".tooltip").width();
		var left = parseInt($(e.target).position().left + ($(e.target).width() - tooltipWidth - tooltipPaddingSide) * 0.5) + "px";
		var top = parseInt($(e.target).position().top + $(e.target).height() + tooltipPaddingTop) + "px";
		$(".tooltip").css({"top": top, "left":left, "display":"none"});
		$(".tooltip").css({"opacity":"0.8"}).fadeIn(60);
	}).mouseleave(function(e) {
		//tooltip の処理
		$(".tooltip").css("left","-9999px");
	});
}


//横にマウススクロールする画像がクリックされたとき、画面最大に表示されるための処理。
function invLightBox(e){
	//alert("選択された画像が中央に最大表示されるようになります!");
	$(e.target).kLightBox();
}

//$('#myImageFlow img').lightBox();
;(function($) {
	$.fn.kLightBox = function(options) {
		var opts = $.extend({}, $.fn.kLightBox.defaults, options);
		var h = $(window).height();
		var w = $(window).width();
		return this.each(function() {
			show_overlay($(this),opts);
			show_image($(this), opts);
			show_close_btn($(this), opts);
		});
	};
	//任意のパラメーターを引数で受け取り、
//		背景のサイズ
//		背景の色
//		背景の透明度
//		表示させるときの効果
//		表示させるときのスピード

	//画像をローディングする。
//		ローディングバー
//		表示させるときの効果
//		表示させるときのスピード
	$.fn.kLightBox.defaults = {
			background: "#FF0000",
			height:"200px",
			width:"200px",
			speed: "slow",
			pulses: 2,
			fadeLow: 0.2,
			fadeHigh: 1
	};
	function show_overlay($obj,opts) {
		var _h = $(document.body).height() + "px";
		var _w = $(document.body).width() + "px";
		var _bgcolor = $obj.background;
		$(document.body).prepend('<div id="overlay" class="lightbox"></div>');
		$("#overlay").css({"width":_w, "height":_h, "display":"none", "background-color":_bgcolor}).fadeTo(300, 0.8);
	}
	function show_image($obj, opts) {
		//url を取得
		//画像おく。
		var _src = String($obj.attr("src"));
		var _title = $obj.attr("title");
		var _alt = $obj.attr("alt");
		var _img = '<div id="img_holder" class="lightbox"><img id="fullsize" class="lightbox" src="' + _src + '" title="' + _title + '" alt="' + _alt + '" /></div>';
		$('body').prepend(_img);

		//ここにバグがある！スクロールの量も取得しなくてはならない。
		//（ウインドの高さー画像の高さ）*0.5 のオフセットを前に追加。
		var img_offset = ($(window).height() - $("#fullsize").height())*0.5;

//		var img_offset = ($(document.body).height() - $("#fullsize").height())*0.5;
		//console.log('img_offset: ', img_offset);
		$("#fullsize").before('<div id="img_filling"></div>');
		$("#img_filling").height(img_offset);
		//$("#sticky-footer-push").height(diff);
		//$("#img_holder").fadeTo(400);
		//以下、うまくいかないです。
		//Uncaught Error: INVALID_CHARACTER_ERR: DOM Exception 5
		//$("#fullsize").attr({src:_src, title:"_title", alt:"_alt"});
	}
	function show_close_btn($obj, opts) {
		$(document.body).append('<div class="close_btn lightbox"><h2>close</h2></div>');
		$(".lightbox").one('click', function(event){
			//ここで、remove()にすると、エラーなぜ?
			//Uncaught TypeError: Cannot read property 'handler' of undefined
			$(".lightbox").hide();
			//event.stopPropagation();
		});
	}
})(jQuery);


//random background-color plugin
//usage:	$("target").setRandomColorBackground();
//ほんとうは、ターゲットがバックグラウンドカラーのアトリビュートもっているか判定してほうがよいよね。
//r,g,b をパースしたときに、先頭の０が切り捨てられると、…つまりバグがあります。のちほどやろー。
;(function($){
var bgcolor = "#000000";
$.fn.extend({
	setRandomColorBackground: function(options) {
		//ターゲットオブジェクトを読み込む
		target = this;
		getRandomColor();
		setRandomColor();
		//ランダムなカラーを生成
		function getRandomColor() {
			var r = Math.floor(Math.random() * 0xff);
			var g = Math.floor(Math.random() * 0xff);
			var b = Math.floor(Math.random() * 0xff);
			var rgb = r << 16 | g << 8 | b;
			rgb = rgb.toString(16);
			bgcolor = "#" + rgb;
		}
		//ランダムなカラーを、ターゲットオブジェクトに設定
		function setRandomColor() {
			$(target).css({"background-color":bgcolor});
		}
	}
});
})(jQuery);

