( function( $ ) {
	
	$.scrollMenu = function (tgt,conf){
		if( $(tgt).css( 'position' ) != "absolute"){
			$(tgt).css("position","absolute");
			$(tgt).after("<div style='height:"+$(tgt).outerHeight()+"px'></div>");
		}
		
		var posTop = $(tgt).offset().top;

		
		function scroll( tgt, posTop ){		
			$(tgt).dequeue();
							
			var scrollTop = $(document).scrollTop();

			if(scrollTop < posTop){toTop = posTop;}
			else{toTop = scrollTop;}
//console.log(scrollTop+"--"+posTop+"--"+toTop);

			$(tgt).animate({
//				top: toTop-120
				top: toTop-conf.offsetTop
			}, conf.speed, conf.easing);
		};
		
		
		// Animated the box when the page is scrolled.
		$( window ).scroll( function ()
			{
				scroll( tgt, posTop );
			}
		);
	};
	
	$.fn.scrollMenu = function ( conf )
	{
		conf = conf || {};
		conf.speed = conf.speed || 200;
		conf.easing = conf.easing || 'swing';
		conf.container = conf.container || this.parent().attr( 'id' );
		conf.offsetTop = conf.offsetTop || this.offset()['top'];
		
		this.each( function() 
			{
				new $.scrollMenu( this, conf );
			}
		);
		return this;
	};
})( jQuery );


$.extend({
	show_detail: function(id){
		var today = new Date();
		if(typeof(this)=="function"){
			get_file = "../item/"+id+".html?"+today.getTime();					
		}
		else if(typeof(this) == "object"){
			tmp = $(this).attr("href").split("#");
	        get_file = "../item/"+tmp[1]+".html?"+today.getTime();	
		}
		$.get(get_file, function(data){
			$("#detail").remove();
			$("#contents").prepend("<div id='detail' style='display:none'>"+data+"</div>");
			
			var detail_top = Math.max($(document).scrollTop(), 70);
			$('#detail').css('top', detail_top);
			
			$("#detail").fadeIn();
			
			// detail close
			$("#close_btn,#close_btn_top").bind("click",function(){
				$("#detail").fadeOut("normal",function(){
					$("#detail").remove();
				});
				return false;
			});	
			
//			$.labelDrag();
		
			
		});
	}
	
	
});


$(document).ready(function(){
	//search guide
    $.incenses = incenses;
	
	$('#search_guide').bind('click', function(){
		var smTop = $('#searchmove').offset()['top']-20;
		$('#guide_box')
			.css('top', smTop+'px')
			.fadeIn();
	});
	$('#guide_box').bind('click', function(){
		$(this).fadeOut();
		return false;
	});


	// search_table 
//	$("#search_word").bind("keyup",search_accord);
	$("#search_button").bind("click",search_accord);
	$("#search_reset").bind("click",function(){
		$("#search_word").val("");
		search_accord();
	});



	var color={"01":"赤","02":"黄","03":"緑","04":"青","05":"紫","06":"黒","07":"灰","08":"二色"}
	$.each(color,function(key,val){
		$("#color"+key).bind("click",function(){
			$("#search_word").val(val);
			search_color(val);
		})
	});
	$("#searchmove").scrollMenu({'offsetTop':120});


	function search_accord(){
		$("html,body").animate({scrollTop:0},"slow");
		query = $("#search_word").val().split(" ");
		$.each($(".inc_table tr:not(thead tr)"), function(key, val){
			tmp = 0;
			for (n = 0; n < query.length; n++) {
				if ($(this).text().match(new RegExp(query[n], "i"))) {
					tmp++;
				}
			}
			if (tmp == query.length) {
				$(this).fadeIn();
			}
			else {
				$(this).fadeOut();
			}
			
		});
	}

	function search_color(col){
		$("html,body").animate({scrollTop:0},"slow");
		$("#search_word").val(col);
		$.each($(".inc_table tr:not(thead tr)"), function(key, val){
			if(this.children[5].innerHTML == col){
				$(this).fadeIn();
			}
			else{
				$(this).fadeOut();
			}
		});
	}


	// detail open
	$(".name a").bind("click",$.show_detail);

	// location.hash 確認
	if( m = (/([0-9]{6})/.exec(location.hash)||[])[1]||null){
//		$.fn_tt_on({data:m});
//		$.fn_detail_show({data:m}); 
		$.show_detail(m);

	}


});
