$(function() {
	$("body").hide().delay(500).fadeTo(500,1);
});

$(window).load(function () {
	//initial
	$('#works ul li:last-child').addClass("last");
	$('#works ul > li > p').hide();
	
	var froma = $('#navi-froma').text();
	$('#navi-froma').hover( function() {
		$(this).text($(this).attr("title"));
	}, function() {
		$(this).text(froma);
	});

	
	$('#works ul > li > p > a > img').hover(function(){ 
		$(this).fadeTo(400,0.6);
	},function() {
		$(this).fadeTo(400,1)
	});

	if (window.location.hash) {
		var href = window.location.hash;
		loading (href,"1");
	} else {
		$('#load').load("./index.html #index",function() {
			$('#load > div').fadeTo(800,1);
		});
		var href = "#index";
	}
});

function loading (id,init) {
	$("body").fadeTo(500,1);
	var target = $(id);
	var selecter = id + "-load";
	var load = $(selecter);

	var content = "./index.html " + id + "-load";
	//$("#debug2").html(content);
	
	$('#works ul > li > figure > a').addClass('slideUp').removeClass('selected');
	target.removeClass('slideUp').addClass('selected');
	target.parent().next().slideToggle(400,function() {
		$("a.slideUp").parent().next().slideUp(200);
		
		if (init =="0") {
			if ($(this).is(':hidden')) {
				$('#load').load("./index.html #index",function() {
					$('#load > div').fadeTo(800,1);
					$('#works ul > li > figure > a').removeClass('selected');
				});
			} else { 
				$('#load').load(content,function() {
					$(this).children().fadeTo(800,1,function() {
						window.location.hash = id;
					});
				});
			}
		} else {
			$('#load').load(content,function() {
				$(this).children().fadeTo(800,1,function() {
					window.location.hash = id;
				});
			});
		}
	});
	
	var offset = target.offset();
	$('html,body').animate({scrollTop: offset.top}, 800);
	return false;
}

