// avoid conflicts with mootools
jQuery.noConflict();

// dom ready
jQuery(function($)
{
	// IE6 png fix (apply to all png images)
	$('img[src$=.png], input[src$=.png]').ifixpng(); 

	$('#download').click(function(e)
	{
		e.preventDefault();

		$('#headline').hide();
		$(this).hide();

		$('.yomo-options')
			.css({'marginTop':0,'opacity':0})
			.show()
			.animate({'marginTop':40,'opacity':1},{easing:'easeOutQuad'});

		// download interest
		if (tracker) tracker._trackPageview('/home/download-interest');
	});

	// field value toggles
	$('#login-user, #login-pass').each(function()
	{
		$(this).focus(function()
		{
			$(this).removeClass('on');
		});

		$(this).blur(function()
		{
			if (!$(this).val()) $(this).addClass('on');
		});

		$(this).trigger('blur');
	});

	// sms-form send
	var sms_form_timer;
	$('#sms-form').submit(function(e)
	{
		e.preventDefault();

		// clear fields (required)
		$('input:hidden',this).val('');

		$(this).ajaxSubmit
		({
			dataType: 'json',
			success: function(status)
			{
				clearTimeout(sms_form_timer);
				if (status.success) $('#sms-form-success-message').fadeIn(1000);
				else $('#sms-form-error-message').fadeIn(1000);
			},
			error: function()
			{
				clearTimeout(sms_form_timer);
				$('#sms-form-error-message').fadeIn(1000);

				// track sms-download (sms attempts)
				if (tracker) tracker._trackPageview('/home/sms-request-error');
			},
			complete: function()
			{
				sms_form_timer = setTimeout(function(){ $('#sms-form-success-message, #sms-form-error-message').fadeOut(1000); },5000);

				// track sms-download (sms attempts)
				if (tracker) tracker._trackPageview('/home/sms-download');
			}
		});
	});

	// login-form send
	var login_form_timer;
	$('#login-form').submit(function(e)
	{
		e.preventDefault();

		$(this).ajaxSubmit
		({
			dataType: 'json',
			success: function(status)
			{
				clearTimeout(login_form_timer);
				$('#login-status').removeClass('success-message error-message').addClass(status.success?'success-message':'error-message').text(status.message).fadeIn(1000);
				if (status.success) setTimeout(function(){ location.href = '/reader.php' ; },750);

				// track login
				if (tracker) tracker._trackPageview('/home/login');
			},
			error: function()
			{
				clearTimeout(login_form_timer);
				$('#login-status').addClass('error-message').text('Unable to login, please try again').fadeIn(1000);

				// track login error
				if (tracker) tracker._trackPageview('/home/login-request-error');
			},
			complete: function()
			{
				login_form_timer = setTimeout(function(){ $('#login-status').fadeOut(1000); },5000);
			}
		});

		// track mobile-link
		$('#mobile-link').click(function(e)
		{
			if (tracker) tracker._trackPageview('/home/link-download');
		});

		// track exe-link
		$('#exe-link').click(function(e)
		{
			if (tracker) tracker._trackPageview('/home/exe-download');
		});

		// track web app
		$('#web-app-link').click(function(e)
		{
			if (tracker) tracker._trackPageview('/home/web-app');
		});
	});
});