function posPhoto(o) {
	var h = $(o).height();
	var ph = $(o).parent().height();
	$(o).css("margin-top",Math.floor((ph-h)/2));
}


;(function($) {
	$.fn.extend({
		// External Links
		externallink: function() {
			this.each(function() {
				$(this).click(function() {
					window.open($(this).attr("href"),"_blank");
					return false;
				});
			});
		
		}//,	
		
	});
	
})(jQuery);


$(document).ready(function() {
	// pingFix
	$(document).pngFix();
	
	// Bow切り替え
	/*$("select[name='maker'] optgroup").css("disabled","disabled");
	
	$("select[name='inst']").change(function(){
		if (($(this).val()).match(/\sBow$/i)) {
			$("select[name='maker']").val("");
			$("select[name='maker'] optgroup:first").hide();
			$("select[name='maker'] optgroup:last").show();	
		} else if ($(this).val()!="" && $(this).val()!="Other") {
			$("select[name='maker']").val("");
			$("select[name='maker'] optgroup:first").show();
			$("select[name='maker'] optgroup:last").hide();
		} else  {
			$("select[name='maker'] optgroup").show();
		}
	}).change();*/
	
	// Search Help
	if ($("#tips").length) {
		$("#tips").append('<span id="tips-trigger"></span>');
		$("#tips-trigger").hover(
			function () {
	    		$("#tips-bal").fadeIn("fast");
	  		},
	 		 function () {
	    		$("#tips-bal").fadeOut("fast");
	 		}
		).click(function(){
			$("#keyword").focus();
			$("#tips-bal").fadeOut("fast");
		});
	}
	// View: 詳細表示トグル
	$(".detail_toggle .detail").hide();
	$(".detail_toggle .toggle").click(function(e){
		$(this).parent().parent().find(".detail").toggle("normal");
		$(this).toggleClass("opened");
		return false;
	
	});
	
	// View: サムネール切り替え
	
	var busy = false;
	$("a.thumb").each(function(){
		if ($(this).hasClass("selected")) {
			sel = $(this).attr("id").split("_")[1];
			
		}
		$(this).append("<span class=\"bal\"></span>");
		
	}).click(function(){
		var s = $(this).find("img").attr("src");
		var m = s.replace(/_s\.jpg/,"_m.jpg");
		var l = s.replace(/_s\.jpg/,".jpg");
		var n = $(this).attr("id").split("_")[1];
		if (n != sel && !busy) {
			busy = true;
			$("#mainphoto").addClass("busy");
			$("#thumb_"+sel).removeClass("selected");
			$(this).addClass("selected");
			sel = n;
			var im = new Image();
			im.onload = function() {
				$("#mainphoto img").fadeOut("normal", function(){
					$(this).remove();
					$("#mainphoto").attr("href",l);
					$("#mainphoto").append("<img src=\"" + m + "\" style=\"display:none; margin-top:" + Math.floor(($("#mainphoto").height() - im.height)/2) + "px\" />");
					$("#mainphoto img").fadeIn("normal", function(){
						busy = false;
						$("#mainphoto").removeClass("busy");
					});
				});
			}
			im.src = m;
		}
		return false;
	});	
	
	//リストクリック
	$("table.result tr.tbody").hover(
		function () {
    		$(this).toggleClass("hover");
 		},
		function () {
    		$(this).toggleClass("hover");
  		}

	).click(function(){
		location.href = $(this).find("a:first-child").attr("href");
	});
	
	// 外部リンク
	$(".externallink").externallink({});
	
	// サイト内検索
	$("#header-searchform-label").click(function(){
		$('#snav').addClass("search");
		$('#s').focus();
		
		$('body').bind("mousedown", function(e){

 			if (e.target.id !="header-searchform-submit" &&　e.target.id !="s" &&  $('#snav').hasClass("search")) $('#snav').removeClass("search");
		});
	});

	if($("body").hasClass("search")) $("#header-searchform-label").click();
	
	$("#header-searchform").submit(function(e){		
		var qval = $("#s").val();
		
		// trim
		if (String(qval).replace(/^[ 　]*/gim, "").replace(/[ 　]*$/gim, "")=="") {
			$("#s").focus();
			return false;
		}
	});
	
	/* Google Analytics
    -------------------------------------------- */
    $().geekGaTrackPage('UA-10988651-1');
	
});