var crossfadeTimer = null;

$(document).ready(function()
{
  if ($('img.first')[0].complete)
  {
    $('.screenshot').show();
	  crossfadeTimer = setInterval(changeScreenshot, 3000);
  }
  else
  {
    $('img.first').load(function()
  	{
  	  $('.screenshot').show();
  	  crossfadeTimer = setInterval(changeScreenshot, 3000);
  	});
  }

  
	$('a.download').click(function()
	{
		_gaq.push(['_trackPageview', '/outgoing/itunes/' + $(this).attr('id')]);
	});
	
	$('#show_video').click(function()
	{
	  $('#video').center().fadeIn();
	  
	  return false;
	});
	
	$('#close_video').click(function()
	{
	  $('#video').fadeOut();
	  
	  return false;
	});
});

function changeScreenshot()
{
	$('.screenshot:last').animate({opacity: 0}, function () {
		$(this).css({opacity: 1}).prependTo($('#iphone'));
	});
}

/* 
 * Center plugin - center element on screen
 * v0.2 - 12/11/2008
 * - added ability to center in another element besides window
 */
(function($){
	$.fn.center = function(el)
	{
		// Get element dimensions
		var height = $(this).innerHeight();
		var width = $(this).innerWidth();
		
		// Figure out offset of container
		if(el)
		{
			var left = ($(el).innerWidth() - width) / 2;
			var top = ($(el).innerHeight() - height) / 2;
		}
		else
		{
			var scroll = $(window).scrollTop();
			var left = ($(window).width() - width) / 2;
			var top = (($(window).height() - height) / 2) + scroll;
		}
		
		// Make sure lightbox doesn't go above top of window
		if (top < 50)
		{
			top = 50;
		}
		
		$(this).css({left: left, top: top});
		
		return this;
	};
})(jQuery);