function setCursorPosition(pos) {
	var g = $('#form_message').get(0);
	if (g.createTextRange) {
		var r = g.createTextRange();
		r.move('character', pos);
		r.select();
	} else {
		g.focus();
		g.setSelectionRange(pos, pos);
	}
}

function getCursorPosition() {
	var s = $('#form_message');
	var g = s.get(0);
	if(g.selectionStart != null) {
		return g.selectionStart
	}
	 else {
		if (!document.selection) {
			return 0;
		}

		var r = document.selection.createRange();
		var d = r.duplicate();
		var l = 0;
		var c = "\001";
		
		d.moveToElementText(g);
		r.text = c;

		l = (d.text.indexOf(c));
		
		r.moveStart('character', -1);
		r.text = '';
		return l;
	}
}

function submissionListener() {
	var s = $('#form_message');
	var g = s.get(0);
	var p = getCursorPosition();

	if (g.settings.has_postfix) {
		var l = s.val().length-26;
		if (p>l) {
			setCursorPosition(l);
		}
	}
	g.listener = setTimeout(submissionListener, 100);
}

function sendSubmission(name, text) {
	if (name != 'From:' && name.length>0 && text.length>25 && text != 'If you want to submit your own,\nthere\'s a form for that.') {
		$.post("/submit", { name: name, text: text },
		  function(greenBubble){
		  	var s = $('#form_message');
			var g = s.get(0);

  			g.settings.has_postfix = false;
  			
			s.val('If you want to submit your own,\nthere\'s a form for that.');	 		
			$('#form_name').val('From:');

		  	$('#bubble_wrapper').append(greenBubble);
		  	
		  	if (!isTouch) {
	  		  	setTimeout("$.scrollTo('.green', 'slow')", 100);
	  		}
	  		else {
	  		}

	 	 });	
	 }
}

function vote(id, direction) {
	var currentRating = parseInt($('#rating'+id).html().replace(/\+/g, ''));
	
	$.post("/vote", { id: id, direction: direction }, function(voteResult) {
		currentRating += parseInt(voteResult);

		if (currentRating>0) {
			newRating = '+';
		} else {
			newRating = '';
		}
		
		newRating += currentRating+"";
	
		$('#rating'+id).html(newRating);
		$('#voting'+id).fadeOut('fast');
	
	});	
}

function loadMore() {
	var loadMore = $('.load_more').html();
	$.post("/more/"+new Date().getTime(), { },
	  function(data){
	  	var bid = new Date().getTime();
 		$('.load_more:first').remove();
 	  	pageTracker._trackPageview("/"+data.id);
	  	$('#bubble_wrapper').append(data.html+'<div class="load_more">'+loadMore+'</div>');
	  	if (!isTouch) {
	  		setTimeout("$.scrollTo('#bubble"+data.id+".more', 'slow')", 100);
	  	} else {
	  	}
	  	setTimeout("$('.bubble').removeClass('more')", 300);
 	 }, 'json');
}

//clean the next few up later.
var infoStatus = 0;

function show_info() {
	hide_share();
	$('#about').show();
	infoStatus = 1;
}

function hide_info() {
	$('#about').hide();
	infoStatus = 0;
}

function toggle_info() {
	if (infoStatus == 0) {
		show_info();
	} else {
		hide_info();
	}
}

var shareStatus = 0;

function show_share() {
	hide_info();
	$('#bar_links').show();
	shareStatus = 1;
}

function hide_share() {
	$('#bar_links').hide();
	shareStatus = 0;
}

function toggle_share() {
	if (shareStatus == 0) {
		show_share();
	} else {
		hide_share();
	}
}
//end cleanables


var isTouch = false;

$(window).ready(function() {

	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		isTouch = true;
/* 		alert('this is an iphone'); */
	}
	
/* 	window.location.hash = firstBubbleId; */
	
	var s = $('#form_message');
	var g = s.get(0);
	
	$('#submit').click(function() {
		sendSubmission($('#form_name').val(), $('#form_message').val());
	});
	
	g.settings = {
		'has_postfix':false, 
		'backup':'', 
		'postfix':", there's an app for that."
	};
	g.listener = false;

	$('#form_name').focus(function() { 
	 	if($(this).val()=='From:') {
	 		$(this).val('');
	 	}
	}).blur(function() {
	 	if($(this).val()=='') {
	 		$(this).val('From:');
	 	}	
	})

	s.val('If you want to submit your own,\nthere\'s a form for that.'
	).focus(function() {
		var s = $('#form_message');
		var g = s.get(0);
		
		if (!g.settings.has_postfix) {
			g.settings.backup = s.val();
			s.val('');
		} 

		submissionListener();
	} // end focus 

	).blur(function() {
		var s = $('#form_message');
		var g = s.get(0);

		if (s.val()=='') {
			s.val('If you want to submit your own,\nthere\'s a form for that.');
		}
		
		if (g.listener) {
			clearTimeout(g.listener);
			g.listener = false;
		}
	} // end blur

	).keyup(function() {
		var s = $('#form_message');
		var g = s.get(0);
		
		// add postfix 
		if (s.val().length>0 && !g.settings.has_postfix) {
			s.val(s.val() + g.settings.postfix);			
			g.settings.has_postfix = true;
			setCursorPosition(s.val().length-26);
		}
		//	panic button; clean up if delete/backspace is able to
		//	mess with the pre- or postfix. revert to backup value 
		if (
			(s.val().length<25 && g.settings.has_postfix) ||
			(g.settings.has_postfix && s.val().indexOf(g.settings.postfix) < 0)
			) {
			s.val(g.settings.backup);
		}

		if (s.val()==g.settings.postfix && g.settings.has_postfix) {
			s.val('');
			g.settings.has_postfix = false;
		}
		
		g.settings.backup = s.val();	
	} //end keyup
	);	
});	