var speed = 15000;
var mouse_speed = 8000;
var startMove_called = false;
var domReady_called = false;
var img_height = null;
var con_height = null;
var topDiff = null;
var current_pos = 0;
var new_time = 0;
var img_top = null;
var direction = null;

$(function(){
	domReady_called = true;
	startMove('domReady');
	
	$('#img_head').mousemove(function(event){
		if (img_height == null)
			img_height = $('#imageID').height();
		if (con_height == null)
			con_height = $('#img_head').height();
		if (topDiff == null)
			topDiff = (-1) * (img_height - con_height);
		if (img_top == null)
			img_top = $('#top_head').height();
		
		$('#imageID').stop();
		if ((event.pageY - img_top) > (con_height / 2)) {
			current_pos = -topDiff + parseFloat($('#imageID').css('top').replace('px', ''));
			new_time = (current_pos * mouse_speed) / (-topDiff);
			direction = 'down';
			$('#imageID').animate({
				top : topDiff
				}, new_time, 'linear', function(){ direction = 'up'; });
		} else {
			current_pos = -parseFloat($('#imageID').css('top').replace('px', ''));
			new_time = (current_pos * mouse_speed) / (-topDiff);
			direction = 'up';
			$('#imageID').animate({
				top : 0
				}, new_time, 'linear', function(){ direction = 'down'; });
		}
	});
	
	$('#img_head').mouseout(function(){
		$('#imageID').stop();
		if (direction == 'down') {
			current_pos = -topDiff + parseFloat($('#imageID').css('top').replace('px', ''));
			new_time = (current_pos * speed) / (-topDiff);
			$('#imageID').animate({
				top : topDiff
				}, new_time, 'linear', function(){
					$('#imageID').animate({
						top : 0
						}, speed, 'linear', startMove)});
		} else if (direction == 'up') {
			current_pos = -parseFloat($('#imageID').css('top').replace('px', ''));
			new_time = (current_pos * speed) / (-topDiff);
			$('#imageID').animate({
				top : 0
				}, new_time, 'linear', startMove);
		}
		direction == null;
	});
});

function startMove(from) {
	if (from == 'onLoad') {
		startMove_called = true;
	}
	
	if (startMove_called && domReady_called) {
		if (img_height == null)
			img_height = $('#imageID').height();
		if (con_height == null)
			con_height = $('#img_head').height();
		if (topDiff == null)
			topDiff = (-1) * (img_height - con_height);
					
		$('#imageID').animate({
			top : topDiff
			},
			speed, 'linear', function(){
				$('#imageID').animate({
					top : 0
					}, speed, 'linear', startMove)});
	}
}
