// Serialize a form (field=true&field2=ok) then make it a object ({field:true, field2:"ok"})
// Pretty much basic (no var type checking etc)
function form_to_object(form) {
	var a = {};
	var f = $(form).serialize();
	var pairs = f.split(/&/);
	$(pairs).each(function(i,pair){
		pair = pair.split('=');
		a[pair[0]] = pair[1];
	});
	return a;
}

function share(dest) {
	var url=encodeURIComponent(location.href);
	var title=encodeURIComponent(document.title);
	switch (dest) {
	case 'ff':
		$('body').append('<script type="text/javascript" src="http://friendfeed.com/share/bookmarklet/javascript"></script>');
		break;
	case 'delicious':
		window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+url+'&amp;title='+title, 'delicious','toolbar=no,width=550,height=550');
		break;
	case 'facebook':
		window.open('http://www.facebook.com/share.php?u='+url, 'fb','toolbar=no,width=1000,height=550');
		break;
	case 'stumble':
		window.open('http://www.stumbleupon.com/submit?url='+url+'&title='+title, 'su','toolbar=no,width=1000,height=550');
		break;
	case 'twitter':
		window.open('http://twitter.com/home?status=With%20scr.im%20I%20safely%20share%20my%20email:%20'+url, 'tw','toolbar=no,width=800,height=550');
		break;
	}
	return false;
}

function open_mail() {
	document.location = $('#mailto').attr('href');
}

function scrim() {
	if ($('#mail').val()) {
		$('#submit').after('<div class="ajax">&nbsp;</div>').remove();
		var postdata = form_to_object('#form_scrim');
		postdata['ajax'] = 'y';
		$.post(target, postdata,
			function(data){
				$('#form_scrim').html(data);
				docready();
		}, "html");
	} else {
		alert('Please type in an email!');
		$('#mail').focus();
	}
}

function change() {
	if (!$('#mail').val()) {
		alert('Please type in a key!');
		$('#mail').focus();
		return;
	}
	
	if (!$('#scrim').val()) {
		alert('Please type in a scr.im URL!');
		$('#scrim').focus();
		return;
	}

	$('#submit').after('<div class="ajax">&nbsp;</div>').remove();
	var postdata = form_to_object('#form_change');
	postdata['ajax'] = 'y';
	$.post(target, postdata,
		function(data){
			$('#form_change').html(data);
			docready();
	}, "html");

}

function scrimdelete() {
	if (!$('#mail').val()) {
		alert('Please type in a key!');
		$('#mail').focus();
		return;
	}
	
	$('#submit').after('<div class="ajax">&nbsp;</div>').remove();
	var postdata = form_to_object('#form_delete');
	postdata['ajax'] = 'y';
	$.post(target, postdata,
		function(data){
			$('#form_delete').html(data);
			docready();
	}, "html");

}

function unscrim() {
	if ($('#captcha').val()) {
		var postdata = form_to_object('#form_unscrim');
		postdata['ajax'] = 'y';
		$.post(target, postdata,
			function(data){
				$('#form_unscrim').html(data);
				docready();
		}, "html");
	} else {
		alert('Please copy the letters/digits!');
	}
}

function check_scrim() {
	var val = $('#scrim').val();
	if (val.length < 1)
		return;
	$('#check_scrim').html('&nbsp;&nbsp;&nbsp;').addClass('ajax');
	$.post(target, {scrim: val, action:'check_scrim'},
		function(data){
			$('#check_scrim').removeClass('ajax').html(data);
	}, "html");
	
}

function captcha_init() {
	$('#captcha').remove();
	$('#form_unscrim').prepend('<input type="hidden" name="captcha" id="captcha" value="" />')
	$('#captcha_help').html('');
	$(caps).each(function(i,c){
		$('#captcha_help').append('<li>'+unescape(c)+'</li>');
	});
	$('#captcha_help').css('display','block');
	$('#captcha_help li').click(function(){
		var cap = $(this).html();
		$('#captcha').val(cap);
		$('#form_unscrim').submit();
	});
}

function sanitize_scrim() {
	var v = $('#scrim').val();
	$('#scrim').val( v.toLowerCase().replace(/[^a-z0-9]/, '') );
}

// This $(document).ready() alias will be called after every ajax update
function docready() {
	// home page
	var timeout, current;
	$('#mail').focus();
	$('#scrim').keyup(function(){
		sanitize_scrim();
		if (timeout) clearTimeout(timeout);
		timeout = setTimeout(function(){check_scrim();}, 800);
	});
	
	// scrimmed
	var f = function(){$(this).select();}
	$('#share input').focus(f).click(f);
	$('#anchors .toggle').click(function(){
		var show = $(this).parent().attr('id').replace(/anchor_/, '');
		var hide = (show == 'tips' ? 'share' : 'tips');
		$('#'+hide).slideUp(300);
		$('#'+show).slideDown(300);
		$('#anchor_'+hide+' span').show();
		$('#anchor_'+show+' span').hide();
	});
	
	// reveal
	if ($('#mailto').length) {
		// disabled for now. Not sure how annoying this would be.
		// setTimeout(function(){open_mail();}, 2000);
	}
}

function clipboard_init() {
	clip = new ZeroClipboard.Client();
	clip.addEventListener( 'onComplete', clipboard_complete );
	if(!$.browser.msie) {
		$('#clipboard').show();
		clip.glue( 'clipboard' );
	}
	clip.setHandCursor( true );
	clip.setText( $('#mailto').attr('href').replace(/^mailto:/, '') );
}

function clipboard_complete(client) {
	$('#clipboard').html('email copied');
}

function delicious_getbooks(json) {
	$(document).ready(function(){
		var books = json[0].total_posts.toString();
		$('#delbooks').html(' (saved by <b>'+books+'</b> people)');
	});
}


$(document).ready(function(){docready();});
