$(document).ready(function(){
	refreshTextBoxGrow();
});

function initializeWithDefault()
{
	var boxes = $('.withDefault');
	$.each(boxes, function (index, value) {var element = $(value); element.val(element.attr('defaultValue'));});
	boxes.focus(focusGained);
	boxes.blur(focusLost);
}

function refreshTextBoxDefault()
{
	var boxes = $('.withDefault');
	boxes.unbind('focus');
	boxes.unbind('blur');
	boxes.focus(focusGained);
	boxes.blur(focusLost);
}

function refreshTextBoxGrow()
{
	var textboxes = $('.unboundGrow');
	textboxes.removeClass('unboundGrow');
	textboxes.keyup(resize);
	textboxes.keydown(resize);
}

function resize(event)
{
	code = event.keyCode;
	var current = $(this);
	if (code == '9')
	{
		event.preventDefault();
		tabFocusToTagBox(current);
		return;
	}
	var div = current.siblings(".lineCounter");
	//if (div.hasClass('lc2')){div.css('width','18%');}
	//div.width(current.width());
	div.text(current.val()+"mmmmm");
	if (div.height() < 28) {current.css("height","26px");}
	else {var hpx = div.height()+ 12 + "px"; current.css("height", hpx);}
}

function focusGained(){
	
	var current = $(this);
	if (current.hasClass('withDefault')){
		current.removeClass('withDefault');
		current.addClass('withDefaultUsed');
		current.val('');
		if ((current.attr('inputType') == "password") && (current.attr('type') != "password"))
		{
			//var newItem = current.clone(true, true);
			//newItem.attr('type', "password");
			//current.before(newItem);
			//current.hide();
			//current.blur();
			//newItem.focus();
			var newItem = '<input type="password" inputtype="password" class="signInPasswordField withDefaultUsed" defaultValue="Password..">';
			current.before(newItem);
			var newItemPicked = current.siblings('.withDefaultUsed');
			newItemPicked.focus(focusGained);
			newItemPicked.blur(focusLost);
			if (newItemPicked.parents('.signInRequired').length >0)
			{
				newItemPicked.keyup(loginEnterHandleConv);
			} else {
				newItemPicked.keyup(loginEnterHandle);
			}		
			current.hide();
			current.blur();
			newItemPicked.focus();
		}
	}
}

function focusLost(){
	var current = $(this);
	if (current.val() == ''){
		current.removeClass('withDefaultUsed');
		current.addClass('withDefault');
		current.val(current.attr('defaultValue'));
		if ((current.attr('inputType') == "password") && (current.attr('type') == "password"))
		{
			var originalItem = current.next().show();
			current.remove();
		}
	}	
}

function tabFocusToTagBox (txtBox)
{
	var textBox = txtBox;
	var post = textBox.parents('.post');
	var tagBox = post.find('.tagBox');
	tagBox.click();
}
