(function($){
	$.fn.InsideCaptionedInput = function(){
		
		var visibleClick = function(el){
			$(el).addClass('InsideCaptioneInputInitActive');
			$(el).removeClass('InsideCaptioneInputInitPassive');
		
			if( $(el).attr('alt') == $(el).attr('value')){
				$(el).attr('value', '');
			}
		};
	
		var visibleBlur = function(el){
			if(!$(el).attr('value')){
				$(el).attr('value', $(el).attr('alt'));
			}
			$(el).addClass('InsideCaptioneInputInitPassive');
			$(el).removeClass('InsideCaptioneInputInitActive');
		};
	
		var hiddenClick = function(el){
			var hiddenId = '#' + ($(el).attr('id').substr(0, $(el).attr('id').length - 6));
			$(hiddenId).show().focus();
			el.hide();
		};
	
		var hiddenBlur = function(el){
			return;
			//alert($('#' + ($(el).attr('id').substr(0, $(el).attr('id').length - 6))).length);
			if (el.attr('value') && el.attr('value') != el.attr('alt')) {
				var hiddenId = '#' + ($(el).attr('id').substr(0, $(el).attr('id').length - 6));
				$(hiddenId).attr('value', el.attr('value'));
				$(hiddenId).show().addClass('HtmlInsideCaptionedInput').
					addClass('InsideCaptioneInputInitPassive');
				
				$(el).remove();
				
				$(hiddenId).focus(function(){
					visibleClick($(hiddenId));
				});
				
				$(hiddenId).blur(function(){
					visibleBlur($(hiddenId));
				});
			}
			else{
				visibleBlur(el);
			}
		};
		
		$(this).each(function() {
			if(!$(this).attr('id')){
				$(this).attr('id',  ('id' + Math.random()).replace('.', ''));
			}
			/*
			if (!$(this).attr('alt')) {
				$(this).attr('alt', $(this).attr('value'));
			}
			*/
			var NewInput = $(document.createElement('input')).attr('value', $(this).attr('value')).
				attr('alt', $(this).attr('value')).
				attr('id', $(this).attr('id') + '-proxy').addClass('InsideCaptioneInputInitPassive').
					addClass('HtmlInsideCaptionedInput');
				
			$(this).attr('value', '');
			$(this).removeClass('HtmlInsideCaptionedInput');
			$(this).hide();
			$(this).after(NewInput);
			$(this).blur(function(){
				if(!$(this).attr('value') || $(this).attr('value') == $(this).attr('alt')){
					$(this).hide();
					$('#' + $(this).attr('id') + '-proxy').show();
				}
			});	  
  		});
		
		$('.HtmlInsideCaptionedInput').each(function(){
			$(this).attr('alt', $(this).attr('value'));
			var len = $(this).attr('id').length;
			//alert($(this).attr('id').substring(len-6));
			
			var clickFunc;
			var blurFunc;
			if($(this).attr('id').substring(len-6) == '-proxy')
			{
				//alert($(this).attr('id'));
				clickFunc = hiddenClick;
				blurFunc = hiddenBlur; 
			}
			else
			{
				clickFunc = visibleClick;
				blurFunc = visibleBlur;
			}
			
			$(this).focus(function(){
				clickFunc($(this));
			});
			
			$(this).blur(function(){
				 blurFunc($(this));
			});
		});	
		
	};
})(jQuery);
