// Stopwatch

var start = 1320750000;
var dday = 1328050800;
var now = 0;
var diff = 0;
var days = 0;
var hours = 0;

var angle = 0;
var src = false;
var R = false;
var img = false;

// Slides
var myScroll = false;
var numberOfSlides = 3;
var currentSlide = 1;
var morphArray = new Array();
var slideDelay = 10000;
var slideTransition = 1000;
var slideIn = Raphael.animation({x: -396}, slideTransition, '<>');
var slideOut = Raphael.animation({x: 0}, slideTransition, "<>");

window.onload = function () {
	
	// Set the stopwatch
	src = document.getElementById('stopwatch-hand').src;
	document.getElementById('hand-holder').innerHTML = "";
	R = Raphael('hand-holder', 220, 220);
	img = R.image(src, 0, 0, 220, 220);
	
	updateTime();	
	
	
	// Start the slideshow
	for(i = 1; i < 4; i++)
	{
		x = i == 1 ? 0 : -396;
		morphArray[i] = Raphael('slide'+i+'outer', 396, 409);
		morphArray[i] = morphArray[i].image(document.getElementById('slide'+i).src, x, 0, 396, 409);
	};
	
	setTimeout("scrollToNext()", slideDelay);
};


function updateTime()
{
	now = Math.round(new Date().getTime() / 1000);
	diff = dday - now;
	days = Math.floor(diff / (60*60*24));
	hours = Math.floor((diff - (days * (60*60*24))) / (60*60));
	
	var countdown = '';
	countdown += '<b>'+days+'</b> day';
	countdown += days != 1 ? 's' : '';
	countdown += ', <b>'+hours+'</b> hour';
	countdown += hours != 1 ? 's' : '';

	document.getElementById('countdown-text').innerHTML = countdown;
	
	updateStopwatch();
	
	setTimeout("updateTime()", 60000);
}


function updateStopwatch()
{
	angle = ((now - start) / (dday - start)) * 360;
	img.animate({transform: "r" + angle}, 1000, "<>");
}


function scrollToNext()
{
	var nextSlide = 0;
	if(currentSlide != numberOfSlides)
	{
		nextSlide = currentSlide + 1;
	}else{
		nextSlide = 1;
	}

	morphArray[currentSlide].animate(slideIn);
	morphArray[nextSlide].animate(slideOut.delay(slideTransition));
	
	currentSlide = nextSlide;
	
	setTimeout("scrollToNext()", slideDelay);
}


function submitEmail()
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.getElementById('signup-button').value;
	if(reg.test(address)){
		document.getElementById('result').innerHTML = 'Thanks for your interest! We\'ll be in touch.';
		executeAjax('content/jigsoar/templates/jigsoar splash/ajax.php?action=signup&email='+address, '');
	}else{
		document.getElementById('result').innerHTML = 'Please enter a valid email address.';
	}
}


