$(document).ready(function() {
	
	$("a.anchorLink").anchorAnimate()
	$( '#main-menu' ).scrollFollow();
	$('#submit').click(function() {
		$(this).hide();
		$('#loader').show();
		/*$.ajax({
		  url: "gonder.php",
		  type: "POST",
		  data: $('.cform form').serialize(),
  			 complete: function() {
					    //called when complete
			 },
		
			 success: function() {
					    //called when successful
			 },
		
			 error: function() {
					    //called when there is an error
			 }
		});*/ 
		$('#loader').hide();
		$(this).show();
	});
	$( '#main-menu a' ).click(function() {
		$( '#main-menu a').removeClass('active');
		$(this).addClass('active');
	});
	
});

function zeroPad(num, length)
{
	var numStr = num + '';
	if (numStr.length < length)
	{
		var diff = length - numStr.length;
		for (var i = 0; i < diff; i++)
		{
			numStr = '0' + numStr;
		};
	};
	return numStr;
}

function getScrollPosition()
{
	var position = 
	{
		top		: 0, 
		left 	: 0
	};

	if (typeof window.pageYOffset != 'undefined')
	{
		position = 
		{
			top		: window.pageYOffset, 
			left 	: window.pageXOffset
		};
	}
	else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
	{
		position = 
		{
			top		: document.documentElement.scrollTop, 
			left 	: document.documentElement.scrollLeft
		};
	}
	else if (typeof document.body.scrollTop != 'undefined')
	{
		position = 
		{
			top		: document.body.scrollTop, 
			left 	: document.body.scrollLeft
		};
	};
	return position;
};

$(function()
{
	$('html').addClass('js');
	$('#container').fadeIn('slow');
	$('body').mousemove(function(e)
	{
		var mouseCoords = 'mouse x: ' + zeroPad(e.pageX, 4) + ' mouse y: ' + zeroPad(e.pageY, 4);
		
		$('#mouse-coords').text(mouseCoords);
	});
	$('div.section2').each(function()
	{
		$(this).mousemove(function(e)
		{
			var offsets = $(this).offset();
			var pos = 
			{
				x: (e.pageX - offsets.left),
				y: (e.pageY - offsets.top)
			}
			
			var baseExp = 4; 	// 2 ^ 4 == 16
			// baseExp = 5;		// 2 ^ 5 == 32
			
			var w = this.offsetWidth;
			var h = this.offsetHeight;

			// half-width/height
			var hw = w/2;
			var hh = h/2;
			
			// x/y relative to center of element
			var cx = pos.x - hw;
			var cy = pos.y - hh;
			
			// percentage from center to edge
			var px = Math.abs(cx/hw);
			var py = Math.abs(cy/hh);
			
			// exponents
			var ex = px * baseExp;
			var ey = py * baseExp;
			
			// new top/left positions
			var nx = Math.round(Math.pow(2, ex)) * (cx < 0 ? -1 : 1);
			var ny = Math.round(Math.pow(2, ey)) * (cy < 0 ? -1 : 1);
			
			$(this).css('left', nx + 'px');
			$(this).css('top', ny + 'px');
		});
		
		$(this).hover(function(e) // over
		{
			$('#container').addClass('hovered');
			$(this).addClass('hovered');
			$(this).css('z-index', 9999);
		},
		function(e) // out
		{
			$('#container').removeClass('hovered');
			$(this).removeClass('hovered');
			
			var pos = 
			{
				x: parseInt($(this).css('left')),
				y: parseInt($(this).css('top'))
			};
			$(this).css('z-index', 1);
			
			$(this).animate
			({
				top 	: pos.y * -1,
				left 	: pos.x * -1
			}, 50, 'swing').animate
			({
				top 	: pos.y * .75,
				left 	: pos.x * .75
			}, 75, 'swing').animate
			({
				top 	: pos.y * -.5,
				left 	: pos.x * -.5
			}, 50, 'swing').animate
			({
				top 	: pos.y * .25,
				left 	: pos.x * .25
			}, 100, 'swing').animate
			({
				top 	: 0,
				left 	: 0
			}, 100, 'swing', function()
			{
				$(this).css('z-index', 0);
			});
		});
	});
	
	
	// should be in a redraw function
	window.setInterval(function()
	{
		$('span.coords').each(function()
		{
			var offsets = $(this).parent().offset();
			var txt = 'x: ' + zeroPad(Math.round(offsets.left), 4) + ' y: ' + zeroPad(Math.round(offsets.top), 4);
			$(this).text(txt);
		});
		
		// update scroller
		var position 	= getScrollPosition();
		var pageCoords 	= 'page  x: ' + zeroPad(position.left, 4) + ' page  y: ' + zeroPad(position.top, 4);
		$('#page-coords').text(pageCoords);
			
	}, 50);
});
