$(function() {
		   
    $.fn.fadeInHandler = function(speed, callback) {
		if ($.browser.msie && $.browser.version < 9) {
			$(this).show();
		} else {
			$(this).fadeIn();
		}
	};
		   
	var autoScroll = false;
	
	function moveSide() {
		var bH = $(window).height();
		var sH = $("#side").height();
		var wT = $(window).scrollTop();
		var offset = $("#side").position();
		var sO = offset.top;
		var tolerance = 10;
		
		if ((sO + sH + tolerance) > (wT + bH)) {
			// too high
			var pos = wT + bH - sH - tolerance;
			
			$('#side').stop().animate({ top: pos + "px" }, { queue: false, duration: 500, easing: 'easeInOutSine' });
		}
		
		if ((sO - tolerance) < wT) {
			// too high
			var pos = wT + tolerance;
			
			$('#side').stop().animate({ top: pos + "px" }, { queue: false, duration: 500, easing: 'easeInOutSine' });
		}	
	}
	
	moveSide();
	
	$(window).scroll(function() {
		if (autoScroll == false) {
			moveSide();
		}
	});
	
	$("#side area").click(function() {
		autoScroll = true;
		var linkClass = $(this).attr("class");
		if ($(this).hasClass("top")) {
			var pos = 389;
		} else if ($(this).hasClass("work")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		} else if ($(this).hasClass("what")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 15;
		} else if ($(this).hasClass("contacts")) {
			var offset = $("a#" + linkClass).offset();
			var pos = offset.top - 38;
		}
		
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
		  var $target = $(this.hash);
		  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		  if ($target.length) {
			var targetOffset = $target.offset().top - 25;
			$('html,body').animate({scrollTop: targetOffset}, {duration: 500 , easing: 'easeInOutSine', complete: function() {
																														   $('#side').animate({ top: pos + "px" }, 
            { queue: false, duration: 500, easing: 'easeInOutSine', complete: function() {
				autoScroll = false;
			}});
																														   }});
		   return false;
		  }
		}
	});
	
	
	
	/*
	// make corner twitch on load
	$(window).load(function() {
		$("#pageflip img").stop().delay(1000).animate({
				width: '100px',
				height: '100px'
			}, 500);
		$(".msg_block").stop().delay(1000).animate({
			width: '100px',
			height: '97px'
		}, 500, function() {
			$("#pageflip img").stop().delay(1000).animate({
				width: '50px',
				height: '52px'
			}, 220);
			$(".msg_block").stop().delay(1000).animate({
				width: '50px',
				height: '50px'
			}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
		});		
	});
	
	// corner flip
	$("#pageflip").hover(function() {
		$("#pageflip img , .msg_block").stop().animate({
			width: '307px',
			height: '319px'
		}, 500);
	} , function() {
		$("#pageflip img").stop().animate({
				width: '50px',
				height: '52px'
			}, 220);
		$(".msg_block").stop().animate({
			width: '50px',
			height: '50px'
		}, 200); //Note this one retracts a bit faster (to prevent glitching in IE)
	});
	*/
	
});
