$(document).ready(function(){

	if($.browser.msie && $.browser.version < 7) ie6 = true; else ie6 = false;
	if($.browser.msie) ie = true; else ie = false;

	var fadeduration = 200;
	if(ie) fadeduration = 0;
	
	// we have other content - fade thumbs
	if($('#overlay, #work').length > 0) {
		$('#tt').hide();
		if(ie) $('#thumbs').hide();
		else $('#thumbs').css('opacity', .1);
	}

	// tooltips
	var attachTT = function() { 
		$('#tt').remove();
		if($('#overlay, #work').length == 0) {
			$('body').append('<div id="tt"><a href="'+$(this).attr('href')+'">'+$(this).children('img').attr('alt')+'</a></div>');
			$('#tt').offset({
				left: $(this).offset().left + $(this).width()/2 - $('#tt').width() / 2 - 4,
				top:  $(this).offset().top + $(this).height()/2 - $('#tt').height() / 2 - 4
			});
		}
	};
	
	var removeTimeout = 0;
	
	var removeTT = function(e) {
		removeTimeout = setTimeout(function(){
			$('#tt').remove();
			clearTimeout(removeTimeout);
		}, 200);
	}

	$('div.thumb a').mouseenter(attachTT);
	$('div.thumb a').mouseleave(removeTT);
	$('div.thumb a').mousemove(function(){
		if(removeTimeout) clearTimeout(removeTimeout);
	});
	
	$('#tt').live('mouseenter', function(e) { if(removeTimeout) clearTimeout(removeTimeout); });
	
	if(!ie6) {
	
		// catch internal links
		$('a[href^="/"]').live('click',function(){
			$('#tt').hide();
			var page = $(this).attr('href');
			parent.location.hash = page; // add to page url for bookmarking/refresh

			// homepage = works list
			if (page == '/') {
				$('#overlay, #work').fadeOut(fadeduration, function() { $(this).remove(); });
					if(ie) $('#thumbs').show();
					else $('#thumbs').animate({opacity: 1}, fadeduration);
					
				$('#header ul a').removeClass('active');
				$('#li_works').addClass('active');
			} else {
				$('#overlay, #work').fadeOut(fadeduration, function() { $(this).remove(); });
				if(ie) $('#thumbs').hide();
				$('#thumbs').animate({opacity: .1}, fadeduration, null, function(){
					$.get(page, function(data){ 
						page_content = data;
						$('body').append(page_content);
						
						var scroll_top = $(window).scrollTop();
						$('#overlay').css('top', scroll_top + 120);		// overlay.top = 120
						$('#work').css('top', scroll_top + 20);			// work.top = 20
						
						$('#overlay, #work').hide().fadeIn(fadeduration); 
						
					});
				});
			
			}
			return false;
		});
	
		$('#header ul a').click(function() {
			$('#header ul a').removeClass('active');
			$(this).addClass('active');
		});
		
	}	
	
});
