/*
 * jquery.gplusalbum.js
 *
 * version 0.0.1
 * copyright (c) 2011 cross clover
 * http://www.cross-clover.cc/
 */

(function ($) {
	$.fn.gplusalbum = function ( options ) {
		// オプション設定
		var options = $.extend({
		  'btnNext' : 'album/images/next.png',
		  'btnPrev' : 'album/images/prev.png',
		  'btnClose' : 'album/images/close.png'
		}, options);
		
		// オーバーレイ用ボックス・隠し属性格納フィールドを追加
		$("body")
			.after(
				$("<div />").addClass("gPlusOverlay")
			)
			.after(
				$("<input type='hidden' />")
					.attr("name",  "album-index")
					.attr("id",    "album-index")
					.attr("value", "")
			)
			.after(
				$("<input type='hidden' />")
					.attr("name",  "image-index")
					.attr("id",    "image-index")
					.attr("value", "")
			)
			.after(
				$("<input type='hidden' />")
					.attr("name",  "image-count")
					.attr("id",    "image-count")
					.attr("value", "")
			);
		
		$(this).find(".album").each( function () {
			$(this).find("img").each( function ( i ) {
				switch ( i ) {
					case 0: $(this).addClass("zp"); break;
					case 1: $(this).addClass("ap"); break;
					case 2: $(this).addClass("bp"); break;
					default: $(this).addClass("cp"); break;
				}
			});
		});
		
		// マウスオーバー・リーブ時のアクションを設定
		$(this)
			.delegate(".album", "mouseenter", function () {
				$(this).find("img").each( function () {
						if ( $(this).hasClass("zp") ) { $(this).addClass("zr"); }
						if ( $(this).hasClass("bp") ) { $(this).addClass("br").css("left", "50px"); }
						if ( $(this).hasClass("ap") ) { $(this).addClass("ar").css("left", "-50px"); }
				});
			})
			.delegate(".album", "mouseleave", function () {
				$(this).find("img").each( function ( i ) {
						if ( $(this).hasClass("zp") ) { $(this).removeClass("zr"); }
						if ( $(this).hasClass("bp") ) { $(this).removeClass("br").css("left", ""); }
						if ( $(this).hasClass("ap") ) { $(this).removeClass("ar").css("left", ""); }
				});
			});
		
		// 画像がクリックされた時のアクションを設定
		$(this).find("img").each( function () {
			$(this).click( function () {
				// 元画像の情報を取得
				var src   = $(this).attr("src");
				var title = $(this).attr("alt");
				if ( title == undefined ) { title = "no title"; }
				
				// オーバーレイを表示
				$(".gPlusOverlay")
					.empty()
					.append(
						// 画像表示
						$("<div />")
							.addClass("image-view")
							.append(
								$("<img />")
									.addClass("image")
									.attr("src", src)
							)
							// 閉じるボタン
							.append(
								$("<a />")
									.attr("href", "javascript: void(0);")
									.click(
										function () {
											$(".gPlusOverlay").fadeOut("fast");
										}
									)
									.append(
										$("<img />")
											.addClass("close")
											.attr("src", options.btnClose)
									)
							)
					)
					.append(	// 画像タイトル
						$("<div />")
							.addClass("image-title")
							.append( title )
					)
					.fadeIn("fast");
					
				// img タグの数を取得
				var imageCount = $(this).parent().find("img").length;
				var albumIndex = $(this).parent().index();
				$("#album-index").val( albumIndex );
				$("#image-index").val( $(this).index() );
				$("#image-count").val( imageCount );
				
				// 2つ以上ある場合にナビボタンを追加する
				if ( imageCount >= 2 ) {
					$(".image-view")
						.append(
							$("<a onclick='javascript: nextImage();' />")
								.attr("href", "javascript: void(0);")
								.append(
									$("<img />")
										.addClass("next")
										.attr("src", options.btnNext)
								)
						)
						.append(
							$("<a onclick='javascript: prevImage();' />")
								.attr("href", "javascript: void(0);")
								.append(
									$("<img />")
										.addClass("prev")
										.attr("src", options.btnPrev)
								)
						)
				}
			});
		});
	};
})(jQuery);

/*
 * 次の画像を表示
 */
function nextImage () {
	var albumIndex = parseInt( $("#album-index").val() );
	var album = $(".album:eq(" + albumIndex + ")");
	var imageIndex = parseInt( $("#image-index").val() );
	var imageCount = parseInt( $("#image-count").val() );
	
	if ( albumIndex != undefined && imageIndex != undefined && imageCount != undefined ) {
		var viewIndex = imageIndex + 1;
		if ( viewIndex >= imageCount ) { viewIndex = 0; }
		$("#image-index").val( viewIndex );
		
		// 次の画像を表示
		var image = album.find("img").eq( viewIndex );
		viewImage( image );
	}
}

/*
 * 前の画像を表示
 */
function prevImage () {
	var albumIndex = parseInt( $("#album-index").val() );
	var album = $(".album:eq(" + albumIndex + ")");
	var imageIndex = parseInt( $("#image-index").val() );
	var imageCount = $("#image-count").val();
	
	if ( albumIndex != undefined && imageIndex != undefined && imageCount != undefined ) {
		var viewIndex = imageIndex - 1;
		if ( viewIndex < 0 ) { viewIndex = imageCount - 1; }
		$("#image-index").val( viewIndex );
		
		// 画像を表示
		var image = album.find("img").eq( viewIndex );
		viewImage( image );
	}
}

/*
 * 画像を表示
 */
function viewImage ( image ) {
	// 画像情報を取得
	var src   = image.attr("src");
	var title = image.attr("alt");
	
	$(".image").fadeOut("fast", function () {
		$(".image").attr("src", src).fadeIn("fast");
	});
	$(".image-title").fadeOut("fast", function () {
		$(".image-title").text( title ).fadeIn("fast");
	});
}





