var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(
	function(){
		$("#firsttime > div.slideup > div").toggle(
			function(){
				$("#firsttime").slideUp("slow");
			},
			function(){
				$("#firsttime").slideDown("slow");
			}
		);
		$("#firsttime").slideDown("slow");
		$("#topHeader > div.leftTop > h1").click(
			function() {
				location.href = 'http://bmindful.com/';
			}
		);
		$("#scrollup").slideDown("slow", setNewsTicker());
	}
);

$.ajaxSetup({type: "POST", url: "http://bmindful.com/ajax.php", dataType: "html"});

function setNewsTicker() {
	headline_count = $("div.headline").size();
	$("div.headline:eq("+current_headline+")").css('top','8px');
	headline_interval = setInterval(headline_rotate, 7000);
	$('#scrollup').hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(headline_rotate, 7000);
		headline_rotate();
	});
}

function addAffirmation(affirmationid) {	
	var theData = "function=addAffirmation&affirmationid=" + affirmationid;
	$.ajax({data: theData, success: function(r){ $("#a" + affirmationid).html(r); $("#a" + affirmationid).addClass('mine'); }, error: function() {alert("There was a problem adding this affirmation!\nPlease try again!");}});
}

function removeAffirmation(affirmationid) {
	var theData = "function=removeAffirmation&affirmationid=" + affirmationid;
	$.ajax({data: theData, success: function(r){ $("#a" + affirmationid).html(r); $("#a" + affirmationid).removeClass('mine'); }, error: function() {alert("There was a problem removing this affirmation!\nPlease try again!");}});
}

function removeAffirmationFromProfile(affirmationid) {
	var theData = "function=removeAffirmationFromProfile&affirmationid=" + affirmationid;
	$.ajax({data: theData, success: function(r){ $("#usersaffirmations").html(r); }, error: function() {alert("There was a problem removing this affirmation!\nPlease try again!");}});
}

function addNewAffirmation(f) {
	if (f.affirmation.value.length > 0 && f.tags.value.length > 0) {
		var theData = "function=addNewAffirmation&affirmationtext=" + escape(f.affirmation.value) + "&tags=" + escape(f.tags.value);
		$.ajax({data: theData, success: function(r){ $("#usersaffirmations").html(r); }, error: function() {alert("There was a problem adding your affirmation!\nPlease try again!");}});
		f.affirmation.value = '';
		f.tags.value = '';
		$("#message").remove();
	} else {
		f.submit();
	}
}

function focusCommentBox(url, username) {
	$("#commentBox").focus();
	var current = $("#commentBox").val();
	$("#commentBox").val('In response to [' + username + '\'s comment](' + url + '): \n' + current);
	setCaretPosition($("#commentBox"), $("#commentBox").val().length);
}

function addComment(f) {
	f.action.value = "Adding Comment...";
	f.action.disabled = true;
	return true;
}

function logIn(f) {
	f.action.value = "Logging In...";
	f.action.disabled = true;
	return true;
}

function signUp(f) {
	f.action.value = "Signing You Up...";
	f.action.disabled = true;
	return true;
}

function sendFeedback(f) {
	f.action.value = "Sending Feedback...";
	f.action.disabled = true;
	return true;
}

function sendNewPassword(f) {
	f.action.value = "Generating New Password...";
	f.action.disabled = true;
	return true;
}

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count;
	$("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {$(this).css('top','210px');});
	$("div.headline:eq(" + current_headline + ")").show().animate({top: 8}, "slow");
	old_headline = current_headline;
}

function doGetCaretPosition (ctrl) {
	var CaretPos = 0;
	if (document.selection) {
		ctrl.focus ();
		var Sel = document.selection.createRange();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	} else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

function setCaretPosition(ctrl, pos) {
	if(ctrl.setSelectionRange) {
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	} else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

function loadTwitterInfo(username) {
	var theData = "function=showTwitterInfo&username=" + escape(username);
	$.ajax({data: theData, success: function(r){ $("#twitterInfo").html(r); }, error: function(){ $("#twitterInfo").html("Error Loading Twitter Info!");}});
}