//	add a class of .js to the body to allow targetted styling.
jQuery(document).ready(function() {
	jQuery('body').addClass('js');
});


jQuery(document).ready(function() {
	jQuery("a.internal").each(function() {
		var target = this.href.split('#')[1];
		var current_url = document.location.href;
		if (current_url.indexOf('#'+target) != -1) {
			jQuery('#'+target).find(':input:first').focus();
		} else {
			jQuery('#'+target).hide();
		}
		jQuery(this).click(function() {
			var target = this.href.split('#')[1];
			jQuery('#'+target).slideToggle('fast');
			return false;
		});
	});
});

function selectReplacement(obj) {
	obj.className += ' replaced';
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	var opts = obj.options;
	for (var i=0; i<opts.length; i++) {
		var selectedOpt;
		if (opts[i].selected) {
			selectedOpt = i;
			break;
		}
	}
	for (var i=1; i<opts.length; i++) {
		var li = document.createElement('li');
		var link = document.createElement('a');
		li.appendChild(link);
		li.className = opts[i].className;
		link.selIndex = opts[i].index;
		link.selectID = obj.id;
		link.setAttribute('href','#');
		jQuery(link).click(function() {
			jQuery(this).parent().parent().find('a').removeClass('selected');
			jQuery(this).addClass('selected');
			selectMe(this);
			return false;
		});
		if (i === selectedOpt) {
			link.className = 'selected';
		}
		ul.appendChild(li);
	}
	obj.parentNode.insertBefore(ul,obj);
}
function selectMe(obj) {
	setVal(obj.selectID, obj.selIndex);
}
function setVal(objID, selIndex) {
	var obj = document.getElementById(objID);
	obj.selectedIndex = selIndex;
}

jQuery(document).ready(function() {
	jQuery('.edit .survey select').each(function() {
		selectReplacement(this);
	});
});

jQuery(document).ready(function() {
	jQuery('.edit form p input,.edit form p select,.edit form p textarea').focus(function() {
		jQuery('.focused').removeClass('focused');
		jQuery(this).parent().addClass('focused');
	});
});

jQuery(document).ready(function() {
jQuery('.feedback').fadeIn(2000);
	//jQuery('.feedback').slideDown('medium');
});

jQuery(document).ready(function() {
	jQuery(this).click(function() {
		jQuery('.feedback.positive').animate({
			opacity: 0
		},1000);
	});
});

jQuery(document).ready(function() {
	if($.date_input) {
		jQuery($.date_input.initialize);
	}
});

jQuery(document).ready(function() {
	$('.note_shown').css('visibility','visible');
});

// buzzbox character counter
$(document).ready(function() {
	var input = $('#buzzbox textarea');
	if(input.length==0) return; // don't do anything unless we have a buzzbox input
	$('#buzzbox textarea').after('<p id="buzzbox_char_count">&nbsp;</p>');
	var submit = $('#buzzbox .new_message input[type=image]');
	var charCounter = $('#buzzbox_char_count');
	var updateCharCount = function() {
		var limit = 140;
		var count = input.val().length;
		charCounter.html(count+"/"+limit+" used");
		if(count > limit) {
			submit.hide();
			charCounter.addClass('over_char_limit');
		} else {
			submit.show();
			charCounter.removeClass('over_char_limit');
		}
	};
	window.setInterval(updateCharCount,300);
});