var direction = 1;
var spin_on = false;
var down = true;
var r = 1; //ball pointer
var x=0;
var z=0; // array pointer
var rate = 50;
var numb_balls=12;
var x_offset = 300;
var y_offset = 30;

function startx(){
	down = true;
	spin_on = true;	
	if(z==0){
		if(Math.random()>.5) direction = 1;
		else direction = -1;
	}
}

function stopx(){
	down = false;
	return;
}

function login(){
	$.get("email/login.php","",function(data){
		alert(data);	
	});
}

function spin(){
	if(spin_on == false){
		z = 0;
		return;
	}
	if (z < 0){  // after finishing return the ball to position 1
		document.getElementById("logo" + r).style.visibility = "hidden";
		document.getElementById("logo1").style.visibility = "visible";
		document.getElementById("SOgif").style.top = y_offset + "px";
		document.getElementById("SOgif").style.left = x_offset + "px";
		document.getElementById("spinningBall").style.left = "40px";
		document.getElementById("spinningBall").style.top = "30px";
		spin_on = false;
		return;		
	}
	var object = document.getElementById("SOgif").style;
	object.top = (z + y_offset) + "px"; 
	object.left = (Math.round(x) + x_offset) + "px";
	document.getElementById("logo" + r).style.visibility = "hidden";
	if(down == true){
		r++;
		if(r > numb_balls) 
			r=1;
		if(z<650) z++;
		x = -(z/5) + direction * (z/3.0 + 100)*Math.sin(z/30.0);
	}
	else{ // else going back up
		r--;
		if(r < 1)
			r = numb_balls;
		x = -(z/5) + direction * (z/3.0 + 100)*Math.sin(z/30.0);	
		z--;
	}
	document.getElementById("logo" + r).style.visibility = "visible";
	drop();
	return;
}
function drop(){
	var position = $('#spinningBall').position();
	var yz = position.top;
	var xz = position.left;
	if(yz>123 && xz<900 && xz>0){
		if(down == true)
			xz += 5;
		else
			xz -= 10;
	}else{	
		if(down == true){
			yz += 5;
		}
		else{
			if(yz<0)
				xz +=20;
			else
				yz -= 5;
		}
	}
	if(xz>1200) xz = -500;
	$('#spinningBall').css({ "top": yz + "px", "left": xz + "px"});
}

function show(what,source,offset){
	obj = document.getElementById('eventDescription');
	obj.style.display = "block";
	obj.innerHTML = text[what];
	source.style.color = "#090";
	obj.style.width = "600px";	
	obj.style.right = "180px";		
	obj.style.top = (parseInt($(source).offset().top) - offset) + "px";	
}

function hide(source){
		document.getElementById('eventDescription').style.display = "none";
		source.style.color = "#009";
}

$(document).ready(function(e){
 	window.setInterval(spin,50);
    var timerProc;
    // Pre hide everything
 	$(".menuDropBox").hide();
 	$(".menuItem").click(function(){
        $(this).children('.menuDropBox').slideDown(50);
 	});
 	// This adjusts the width of the menu for the crappy old IE6
 	if($.browser.msie && $.browser.version=="6.0"){ 
 		$(".menuItem").css({"width":"178px"});
 	} 	
    $(".menuItem").mouseenter(function(){
        var item = $(this);
		timerProc = setTimeout(function(){item.children('.menuDropBox').slideDown(200)},800);
	});
	$(".menuItem").mouseleave(function(){
        clearTimeout(timerProc);
		$(this).children('.menuDropBox').slideUp(200);
	});
	$(".subMenu").mouseenter(function(){
		$(this).css({'background-color':'#55B'});
		$(this).children('.subMenuLink').css({'color':'#FFF','text-decoration':'underline'});
	});
	$(".subMenu").mouseleave(function(){
		$(this).css({'background-color':'#EEE'});
		$(this).children('.subMenuLink').css({'color':'#030','text-decoration':'underline'});
	}); 
 	$(document).focus(function(){
 		$('.abc').each(function(){
 			hide(this);
 		});
 	});		
});

