var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

var sponsor_count;
var sponsor_interval;
var old_sponsor = 0;
var current_sponsor = 0;

var tO;

$(document).ready(
	function() {
		bmSendPing();
		fixForms();
		$("#topHeader > div.leftTop > h1").click(
			function() {
				location.href = 'http://bmindful.com/';
			}
		);
		$("#scrollup").slideDown("slow", setNewsTicker());
		setSponsorTicker();
		setForumsDropDown();
	}
);

$.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 setSponsorTicker() {
	sponsor_count = $("div.sponsor").size();
	$("div.sponsor:eq("+current_sponsor+")").css('top','5px');
	sponsor_interval = setInterval(sponsor_rotate,7000); //time in milliseconds
	$('#sponsors').hover(function() {
		clearInterval(sponsor_interval);
	}, function() {
		sponsor_interval = setInterval(sponsor_rotate,7000); //time in milliseconds
		sponsor_rotate();
	});
}

function sponsor_rotate() {
	current_sponsor = (old_sponsor + 1) % sponsor_count; 
	$("div.sponsor:eq(" + old_sponsor + ")").animate({top: -205},"slow", function() {
		$(this).css('top','210px');
	});
	$("div.sponsor:eq(" + current_sponsor + ")").show().animate({top: 5},"slow");  
	old_sponsor = current_sponsor;
}

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();
	if (url) {
		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.url.value = location.href;
	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!");}});
}

function bmSendPing() {
	$.get(location.protocol + '//' + location.host + "/ajax.php", { url: escape(document.location.href), f: 'ping' } );
	setTimeout("bmSendPing()", 60000);
}

function fixForms() {
	$("#friendForm").css("display", "none");
	$("a.friendLink").click(function() {
		$("#friendForm").submit();
		return false;
	});
	$("a.commentLink").click(function() {
		focusCommentBox();
		return false;
	});
}

function setForumsDropDown() {
	$("a#forumNavLink").hover(function(){
		if ($('ul#forumDropDown').length < 1) {
			var p = $(this).position();
			$('body').append('<ul id="forumDropDown"><li class="first"></li><li><a href="http://bmindful.com/forum/affirmation-talk">Affirmation Talk</a></li><li><a href="http://bmindful.com/forum/personal-development">Personal Development</a></li><li><a href="http://bmindful.com/forum/the-cafe">The Cafe</a></li></ul>');
			$('ul#forumDropDown').css("top", (p.top + $(this).parent().height()) + 'px');
			$('ul#forumDropDown').css("left", p.left);
			$('ul#forumDropDown').fadeIn('fast');
			setupForumDropDown();
		} else {
			clearTimeout(tO);
		}
	}, function(){
		tO = setTimeout("removeForumDropDown();", 100);
	});
}

function setupForumDropDown() {
	$('ul#forumDropDown').hover(function(){
		clearTimeout(tO);
	}, function(){
		tO = setTimeout("removeForumDropDown();", 100);
	});
}

function removeForumDropDown() {
	$("ul#forumDropDown").fadeOut('fast', function(){
		$(this).remove();
	});
}