
$(function() {

	$('#send-e-card').click(function() {
		$('.options', this).slideToggle('fast');
	});

	$('#send-e-card .options LI').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		$(this).addClass('selected')
			.siblings().not(this).removeClass('selected')
			.parents('.select:first')
				.find('.value').text($('A', this).text()).end()
				.find('.options').slideUp('fast');

		load_ecard_form();
	});
	
	$('#join').submit(function() {
		var form = this;
		var email = $('.text', this);
		if (email.val().indexOf('@') == -1) {
			alert('Tarkista sähköpostiosoitteesi.');
			email.focus();
			return false;
		}
		var params = {
			'q0': email.val(),
			'ngform_on': 1,
			'ngf_okparams': 'counter=' + $('#sidebar .counter .digits').attr('title')
				+ '&recipient-email=' + encodeURIComponent(email.val())
				+ '&mail_sendnow=1&cm_mail_okparams=' + encodeURIComponent('id=') + '%{ngf_uniq}'
		}
		$.post($(this).attr('action'), params, function(data, status) {
			//var join_more = $('<div/>').html(data).hide();
			var join_more = $(data).hide();
			$('#join-container').fadeOut('', function() {
				$('#join').hide();
				$('#join-container')
					.fadeIn()
					.append(join_more);
				init_autoclear();
				if (data.indexOf('error') != -1 || data.indexOf('already-joined') != -1) {
					join_more.fadeIn();
					window.setTimeout(function() {
						$('#join-container .msg').hide();
						$('#join').show();
					}, 3000);
				} else {
					var params = {};
					$('INPUT[type=text], INPUT[type=hidden], INPUT:checked', join_more).each(function() {
						if (! $(this).hasClass('autoclear-on')) {
							params[$(this).attr('name')] = $(this).val();
						}
					});
					join_more.submit(function() {
						$.post(join_more.attr('action'), params, function(data, textStatus) {
							var msg = $('<div/>').html(data);
							$('#join-container').append(msg.hide());
							var h = window.location.href;
							window.scroll(0, 0);
							$(join_more).slideUp('fast', function() {
								msg.fadeIn();
							});
						});
						return false;
					});
					join_more.slideDown();
				}
			});
		}, 'text');
		return false;
	});
	
	$('#media-choice A').click(function(e) {
		e.preventDefault();
		var that = this;
		var c = $('#media-content');
		c.fadeOut('', function() {
			var url = '/aiti-media-' + $(that).attr('href').substring(1);
			c.load(url, null, function() {
				c.fadeIn();
			});
		});
	});

	$('#stories A').click(function(e) {
		e.preventDefault();
		var that = this;
		if (undefined != $f())
			$f().unload();  // unload flowplayer
		$(that).addClass('selected')
			.siblings().removeClass('selected');
		var c = $('#story');
		c.fadeOut('', function() {
			var url = '/aiti-tarina?id=' + $(that).attr('href').substring(1);
			c.load(url, null, function() {
				c.fadeIn();
			});
		});

	});
	
	$('#aiti-miksi #main H3').click(function(e) {
		e.preventDefault();
		$(this).nextAll('.flowplayer:first').show();
	});

	init_autoclear();	
});


function load_ecard_form() {

	$('#main .container').fadeOut('', function() {
		var that = this;
		$(this).load('/aiti-ilouutinen', null, function() {

			var f = $('FORM', this);
			init_autoclear();
			
			var refresh_preview = function() {
	
				$('.preview .sender .value', f)
					.text($('INPUT.autoclear-off[name=sender-email]', f).val());
	
				$('.preview .recipient .value', f)
					.text($('INPUT.autoclear-off[name=recipient-email]', f).val());
	
				$('.preview .subject .value', f)
					.text($('#send-e-card .current .value').text());
	
				$('.preview .body .subject', f)
					.text($('#send-e-card .current .value').text());
	
				$('.preview .signature .value', f)
					.text($('INPUT.autoclear-off[name=sender-name]', f).val());
			}
			
			refresh_preview();

			$('.autoclear', f).keyup(refresh_preview);
					
			$('.preview .again').click(load_ecard_form);
					
			f.submit(function() {
				var r = $('INPUT[name=recipient-email]');
				if (r.val().indexOf('@') == -1) {
					alert('Tarkista ystäväsi sähköpostiosoite.')
					r.focus();
					return false;
				}
				$('.submit', f).attr('disabled', true);
				$('.wait', f).show();
				$('.preview .letter').css('opacity', .1);
				window.setTimeout(function() {
					var params = {};
					$('INPUT[type=text], INPUT[type=hidden], INPUT:checked', f).each(function() {
						if (! $(this).hasClass('autoclear-on')) {
							params[$(this).attr('name')] = $(this).val();
						}
					});
					params['subject'] = $('.preview .subject .value', f).text();
					params['check'] = '';

					/*$.ajax(f.attr('action'), params, function(data, textStatus) {
						$('.wait', f).hide();
						$('.preview .status .ok').show();

					});*/
					
					/*$.ajax({
						url: f.attr('action'),
						data: params,
						type: 'post',
						contentType: 'application/x-www-form-urlencoded; charset=iso-8859-1',
						dataType: 'text',
						complete: function(data, textStatus) {
							$('.wait', f).hide();
							if (data.indexOf('OK') != -1) {
								$('.preview .status .ok').show();
							} else {
								$('.preview .status .error').show();
							}
						}
					});*/

					$.post(f.attr('action'), params, function(data, textStatus) {
						$('.wait', f).hide();
						$('.preview .status .ok').show();
					});

				}, 1500);
				return false;
			});

			$(that).fadeIn();
		});
	});
}


function init_autoclear() {

	$('INPUT.autoclear, TEXTAREA.autoclear').each(function() {
		$(this).attr('title', $(this).val());
	});

	$('INPUT.autoclear, TEXTAREA.autoclear').focus(function() {
		if ($(this).val() == $(this).attr('title'))
			$(this).val('')
				.removeClass('autoclear-on')
				.addClass('autoclear-off');
	});

	$('INPUT.autoclear, TEXTAREA.autoclear').blur(function() {
		if ($(this).val() == '')
			$(this).val($(this).attr('title'))
				.addClass('autoclear-on')
				.removeClass('autoclear-off');
	});

}

