$(document).ready(function(){
       // detail open
       $(".product_name a").bind("click",$.show_detail);
       $("#items_thumb li a").bind("click",$.show_detail);

	   $.get("../data/sachet_holder_gift.json", function(data){
		 items_data = eval('[' + data + ']');
	   });

       // location.hash
       if( m = (/([0-9]{6})/.exec(location.hash)||[])[1]||null){
               $.show_detail(m);
       }
       
		$("#selected_popup").css("display","none");
		

	$("#items_thumb li a")
		.click(function(){
			$("li.selected").removeClass("selected");
			$(this).parent().addClass("selected");
		})
		.hover(function(){
			$("#selected_popup").css("display","block");
			product_id = $(this).attr("href").substr(1);
			$("#selected_popup").text(items_data[0][product_id].description);
		},function(){
			$("#selected_popup").css("display","none");
		});


});

$.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){
                       $("#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 a").bind("click",function(){
                               $("#detail").fadeOut("normal",function(){
                                       $("#detail").remove();
                               });
                       return false;
                       });       
               });
       }
});
