$(function(){
	var image=$('#content > ul > li'),
		over=function(e,full){
			if(!$(this).hasClass('toggled')){
				var item=$(this),
					items=item.parent().children(),
					total=item.parents('[id!=]:first').width(),
					end=(full)?total:item.find('img:first').width(),
					begin=total/items.length;

				items.stop(true).filter(':not(:eq('+item.index()+'))').animate({
					width:(begin-(end-begin)/~-items.length)^0+'px'
				},'fast');
	
				item.animate({
					width:end^0+'px'
				},'fast','linear',function(){
					if(full)
						item.addClass('toggled');
				});
			}
		},
		out=function(e,out){
			if(!$(this).hasClass('toggled')){
				var item=$(this),
					begin=item.width(),
					items=item.parent().children(),
					end=(item.parents('[id!=]:first').width())/items.length;
	
				items.removeClass('toggled').stop(true).animate({
					width:end^0+'px'
				},'fast','linear',function(){
					if(out) out();
				});
			}
		};

	// change width
	image.delay(200).animate({
		width:image.parents('[id!=]:first').width()/image.parent().children().length
	},'slow','easeOutExpo');
	
	$('#content > ul > li').hover(over,out);

	$('#head .menu > li a').click(function(){
		var link=$(this),
			placeholder=image.get(link.parents('li:first').index()),
			hash=link.attr('href').replace(/[^a-z\d]+/g,'') || 'e'+(Math.random()*1e+5^0),
			done=false;

		// deactivate & hide previous points
		link.parents('.menu').find('a').removeClass('active');
		out.call(placeholder);

		// activate (apply placeholder)
		link.addClass('active');

		// create content placeholder
		(!$('.content:first',placeholder).length)?
			$('<div class="content" />').appendTo(placeholder):
			$('.content > *',placeholder).attr('display','none');

		// load or show from cache
		if(!$('.content > .'+hash,placeholder).length)
			$.ajax({
				url:link.attr('href'),
				success: function(data){
					$('<div />').css('display','block').addClass(hash).html(data).appendTo($('.content:first',placeholder))
					over.call(placeholder,{},true);
				}
			});
		else {
			$('.content > .'+hash,placeholder).attr('display','block');
			over.call(placeholder,{},true);
		}
		return false;
	});

	$('#head .home').click(function(){
		$('#head .menu > li a').removeClass('active');
		out.call($(image.get(0)).removeClass('toggled'));
		return false;
	});
});
