
function submitCommentForm(loggedIn) {
	if(!validateForm('comments')) return;
	$('#comments').submit();
	if(loggedIn)
		_gaq.push(['_trackEvent', 'BlogComment', 'PostComment', 'Facebook']);
	else
		_gaq.push(['_trackEvent', 'BlogComment', 'PostComment', 'Anonymous']);
}
function submitSubCommentForm(id) {
	if(!validateForm(id)) return;
	document.getElementById(id).submit();
}

	
function showIntroTout(doSpecialBehavior) {
	var bg = $('#overlay-bg');
	var text = $('#overlay-text');
	 var both = $('#overlay-bg, #overlay-text');
	 var doSpecialBehavior = doSpecialBehavior || false;
	 if(!msie7) {
		both.fadeIn(1000);
	}
	else {
		setTimeout( function() {
			bg.css({opacity: 0}).show().animate({opacity: 0.8},1000);
			text.fadeIn(1000);
		}, 1 );
	}
	introToutOn = true;
	both.click( function(ev) {
		both.fadeOut(1000);
		introToutOn = false;
		if(doSpecialBehavior) {
			if(typeof(thisIsTheIndexPage)!=='undefined') {	
		//		initCrossFade($('#home-tout'));
			}
			if(typeof(thisIsTheVideoPage)!=='undefined') {
				$('a.vid').trigger('click'); 
			}
		}
	});
	_gaq.push(['_trackPageview', '/About/AboutOverlay']);
}

function openGame() {
	// width=650,height=350,
	url = "http://www.whatstherealcost.org/healthyheights";
	window.open(url,'gameWindow','toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
	return false;
}

function more_blog() {
	var tweets = $('div.blog-post2');
	tweets.each( function(i,el) { 
		$(el).fadeIn(1000);
	}); 
	return false;
}

function more_comments() {
	var tweets = $('div.comment');
	tweets.each( function(i,el) {
		$(el).fadeIn(1000); 
	}); 
	return false;
}

function more_twitter() { 
	t_moreClicks--;
	var tweets = $('div#twitter-feed div.twitter-item');
	var nextLimit = t_numShown + t_morePerClick;
	if(t_moreClicks <= 0 || tweets.length <= nextLimit) { 
		$('div#twitter-feed div.more').fadeOut(300);
//		return false;
	} 
	tweets.each( function(i,el) {
		if(i >= t_numShown && i < nextLimit) {
			if(msie) $(el).show();
			else $(el).fadeIn(1000);
		}
	}); 
//	$('#more-debug').html(t_moreClicks);
	t_numShown = nextLimit;
//	return false;
}


function init_expander_arrows() {
	$('img.expander').click( function() {
		var e = $(this).parent().find('.expands');
		if(e.is(':visible')) {
			e.slideUp(100);
			this.original = "images/expand-arrow-right.png";
			this.src = "images/expand-arrow-right-OVER.png";
		}
		else {
			this.original = "images/expand-arrow-down.png"
			this.src = "images/expand-arrow-down-OVER.png";
			e.slideDown(100);
		}
	});
}

function init_dropdown() {
	
	$('.dropdown-tabs li').hover( function(e) {
		var ul = $(this).find('ul');
		ul.fadeIn(200).addClass('black-border');
		$(this).addClass('black-border');
	}, function(e) {
		var ul = $(this).find('ul');
		ul.fadeOut(200);
		$(this).removeClass('black-border');
	});	

}

function split_filename(name) {
	var ix = name.lastIndexOf('.');
	var len = name.length;
	return [name.substr(0,ix), name.substr(ix+1,len-ix-1)];
}

function init_captcha() {
	var inputs = $('input.screen-name, textarea.comment-entry');
	
	inputs.each( function(i,el) {
		var form = $(el).closest('div.comment-form, div.sub-comment-form');
		form.dirty = false;
		var captcha = form.find('div.captcha');
		$(el).focus( function() { captcha.fadeIn(); } );
		$(el).blur( function() {
			if(!form.dirty) {
				if(this.value=='' || this.value==this.initValue) 
					captcha.hide(); 
				else
					form.dirty = true;
			}
		});
	});
}

var MAXCHAR = null;

function update_character_limit() {

}

function init_character_limit(num) {
	MAXCHAR = num;
	var inputs = $('textarea.limit');
	inputs.each( function(i,field) {
		var notice = $(field).parent().find('.limit-notice').eq(0);
		notice.html(num + ' character limit');
		fn = function() {
			var len;
			field.dirty = true;
			if((len = field.value.length) <= MAXCHAR) {
				notice.html('characters remaining: ' + (MAXCHAR-len));
			}
			else {
				notice.html('<b>character limit exceeded!</b>');
			}
		}
		$(field).keyup( fn );
		$(field).keydown( fn );
		$(field).focus( function() {
			notice.fadeIn();
		});
		$(field).blur( function() {
			if(this.value.length==0 || this.value==this.initValue) notice.hide();
		});
	});
}

function init_img_hover() {
	$('img.over').each( function() {
		this.original = this.src;
	});
	$('img.over').hover( function(e) {
		pieces = split_filename(this.original);
		this.src = pieces[0] + '-OVER.' + pieces[1];
	}, function(e) {
		this.src = this.original;
	} );
}

/* all form inputs with class .req are checked for input.
if empty, class .error is added, and div.error gets a message */
function validateForm(id) {
	errorMsg = [];
	errorMsg[0] = "Please fill out the highlighted field(s) and try again";
	errorMsg[1] = "You have entered too many characters in the highlighted field";
	errors = [];
	for(i in errorMsg) errors.push(0);
	if(typeof(MAXCHAR)==='undefined' || MAXCHAR===null)
		alert('function validateForm() requires MAXCHAR to be set.');
	var form = $('#'+id);
	var inputs = $('#'+id + ' .req');
	var limited = $('#'+id + ' .limit');
	var errordiv = $('#'+id + ' div.error');
	var totalErrors = 0;
	if(totalErrors===0) {
		inputs.each( function(i, el) {
			if(el.value.length < 1 || el.value == el.initValue) {
				$(el).addClass('error');
				totalErrors++;
				errors[0]++;
			}
			else {
				$(el).removeClass('error');
			}
		});
	}
	if(totalErrors===0) {
		limited.each( function(i, el) {
			if(el.value.length > MAXCHAR) {
				$(el).addClass('error');
				totalErrors++;
				errors[1]++;
			}
			else {
				$(el).removeClass('error');
			}
		});
	}
	if(totalErrors===0) {
		errordiv.fadeOut();
		errordiv.html('');
		return true;
	}
	else {
		var m="Something's wrong, please try again.";
		if(errors[0]) {
			field = 'fields';
			if(errors[0]==1) field = 'field';
			m = 'Please fill out the highlighted '+field+' and try again.';
		}
		else if(errors[1]) {
			field = 'fields';
			if(errors[1]==1) field = 'field';
			m = "You have entered too many characters in the highlighted " + field;
		}
		errordiv.html(m);
		errordiv.fadeIn();
		return false;
	}
}

function registerDefault2(el,str) {
	el.initValue = str;
	$(el).focus( function() {
		if(el.value==el.initValue) { 
			el.value='';
			$(el).removeClass('grayed');
		}
	});
	$(el).blur( function() {
		if(el.value=='') {
			el.value=el.initValue;
			$(el).addClass('grayed');
		}
	});
	if(el.value==el.initValue) $(el).addClass('grayed');
}
function registerDefault(obj,str) {
	obj.each ( function(i, el) {
		el.initValue = str;
		$(el).focus( function() {
			if(el.value==el.initValue) { 
				el.value='';
				$(el).removeClass('grayed');
			}
		});
		$(el).blur( function() {
			if(el.value=='') {
				el.value=el.initValue;
				$(el).addClass('grayed');
			}
		});
		if(el.value==el.initValue) $(el).addClass('grayed');
	});
}

function thumbsUp(id) {
	$.post('do-thumbsUp.php',{c:id}, function(data) {
		data_array=data.split(" ");
		$('#thumbsUp_count_'+id).html('<b>'+data_array[0]+'</b>');
		$('#thumbsDown_count_'+id).html('<b>'+data_array[1]+'</b>');
		_gaq.push(['_trackEvent', 'Comments', 'MouseClick', 'ThumbsUp']);
	});
}
function thumbsDown(id) {
	$.post('do-thumbsDown.php',{c:id}, function(data) {
		data_array=data.split(" ");
		$('#thumbsUp_count_'+id).html('<b>'+data_array[0]+'</b>');
		$('#thumbsDown_count_'+id).html('<b>'+data_array[1]+'</b>');
		_gaq.push(['_trackEvent', 'Comments', 'MouseClick', 'ThumbsDown']);
	});
}
function doReport(id) {
	$.get('do-report.php?id='+id, function() {
		_gaq.push(['_trackEvent', 'Comments', 'MouseClick', 'Report']);
		
	});
	alert("Your comment has been reported.  Thank you.");
}
		
/*
function init_box() {
	$('div.box').each( function(i, el) {
		this.innerHTML = 
			'<div class="box_top"></div>' + 
			this.innerHTML + 
			'<div class="box_bottom"></div>';
		alert(this.innerHTML);
	});
}
*/