$(document).ready(function(){
	// added jquery functions
	String.prototype.rot13 = function rot13() {
		return this.toString().replace(/[a-zA-Z]/g, function(ch) {
			return String.fromCharCode((ch <= "Z" ? 90 : 122) >= (ch = ch.charCodeAt(0) + 13) ? ch : ch - 26);
		});
	}
	//end of addtions
	
	//focus clear fields
	$('.default').each(function(){
		$(this).css({'color' : '#aeaeae'});
		var _default = $(this)[0].defaultValue;//document.getElementById($(this).attr('id')).defaultValue;
		$(this).focus(function(){
			$(this).css({'color' : '#000'});
			if($(this).val() == _default){
				$(this).val('');	
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val(_default);	
				$(this).css({'color' : '#aeaeae'});
			}
		});						
	});
	//end of clear focus
	
	
	//textarea simple autoexpand
	$('textarea').each(function(){
		$(this).css({overflow: 'hidden'});
		$(this).each(function(){
			if($(this)[0].scrollHeight > $(this).height())	$(this).css({height: $(this)[0].scrollHeight + 50});
		});
		$(this).keyup(expandTextarea);
	});
	
	function expandTextarea(){
		if($(this)[0].scrollHeight > $(this).height()){
			$(this).animate({height: $(this)[0].scrollHeight + 50}, {duration:200, queue:false});
		}
	}
	//end of autoexpand
	

	//generic toggler
	$('.toggler').each(function(){
		$('.toggler').next().hide();
	});
	$('.toggler').click(function(){	
		var _h = parseInt($(this).next().css('height'));//height();
		if(_h > 0 || _h == NaN){
			$(this).removeClass('selected');
		}else{
			$(this).addClass('selected');
		}
		
		$(this).next().animate({height: 'toggle'}, 300);
		if($(this).children('a').text() == '[-]'){
			$(this).children('a').text('[+]');
		}else{
			$(this).children('a').text('[-]');
		}
		return false;
	});
	$('.toggler_close').click(function(){
		obj = $(this);
		while($(obj).prev('.toggler').length == 0){
			obj = $(obj).parent();	
		}
		$(obj).prev().trigger('click');
		return false;
	});
	//end of generic toggler
	
	
	//simple alert
	$(".alert").animate({opacity: 1}, 3000).animate({opacity: 0 }, {duration: 600, easing: 'easeInQuad'}).animate({height: 0, paddingTop: 0, paddingBottom: 0, borderWidth: 0, marginBottom: 0, marginTop: 0}, {duration: 500, easing: 'easeInQuad', complete: 
		function(){$(this).remove();}
	});
	//end of alert

	//decode rot13 emails - helps prevent bots from reading
	$('.email-rot13').each(function(){
		$(this).text($(this).text().rot13());	
		$(this).attr('href', "mailto:" + $(this).text());
	});
	//end of decode rot13
	
});
