/**
 *
 *	Ambiware's jQuery Form Input Manipulator - Last Edit: 10/9/2011
 *	http://ambidev.com/ambiinput
 *
 *	Copyright (c) 2011 Seth Ciasulli
 *	Licensed under GNU General Public License
 *
 *	This jQuery plugin is free to use, duplicate, share, edit, etc.
 *	You are not required to give credit to it's original author.
 *
**/

(function($){
	$.fn.ambiinput=function(o){
		
		//-> plugin options
		var d={
			label:'',
			labelClick:'',
			labelClass:'',
			labelFadeTo:.5,
			labelFadeDur:300,
			divReplace:false,
			length:'',
			showCounter:'',
			counterClass:'',
			counterStart:0,
			counterEnd:0,
			animate:'',
			animateStyle:'',
			animateFrom:'0px',
			animateTo:'0px',
			hiddenDiv:'',
		};
		var o=$.extend(d,o);
		this.each(function(){
			
			//-> setting up variables
			var thisId = '#'+$(this).attr('id');
			var thisClass = $(this).attr('class');
			var thisVal = $(this).val();
			var rand = Math.floor(Math.random()*100001)
			var newDivId = $(this).attr('class').split(' ')[0]+rand;
				
			//-> for encoding purposes (<script> tags to stay html)
			function htmlEncode(a){ return $('<div/>').text(a).html(); }
		    
		    if(o.divReplace==true){
			    //-> add new div to replace textarea
				$(this).after('<div id="'+newDivId+'wrap" style="position:relative;float:'+$(this).css('float')+';display:'+$(this).css('display')+'"><div id="'+newDivId+'rep" class="'+thisClass+'" contentEditable="true">'+htmlEncode(thisVal)+'</div></div>');
				$('#'+newDivId+'rep').css('pointer','text');
				$(this).css('display', 'none');
			
				$('#'+newDivId+'rep').keyup(function(){
				
					//-> port div content to input so form can send
					$(thisId).val($('#'+newDivId+'rep').text());
					
					//-> also do same functions as input would
					if(o.label!=''){
						if($('#'+newDivId+'rep').text().length>0){
							$('#'+newDivId).fadeOut(100);
					
							//-> if hidden div, show when there's text
							if(o.hiddenDiv!=''){ $(o.hiddenDiv).fadeIn(300); }
						}else{
							$('#'+newDivId).fadeIn(500);
					
							//-> if no text, hide div again
							if(o.hiddenDiv!=''){ $(o.hiddenDiv).fadeOut(300); }
						}
					}
					
					//-> restrict length if specified
					if(o.length!=''){
						if($('#'+newDivId+'rep').text().length>o.length){
							$('#'+newDivId+'rep').text($('#'+newDivId+'rep').text().substring(0, o.length));
						}
					}
				
					//-> if counter is showing, start it
					if(o.showCounter=='true'){
						if($('#'+newDivId+'rep').text().length>o.counterStart){
							if(o.count=='down'&&o.length!=''){
								$('#'+newDivId+'count').text(o.length-$('#'+newDivId+'rep').text().length);
							}else{
								$('#'+newDivId+'count').text($('#'+newDivId+'rep').text().length);
							}
						}
					}
					
					//-> if start/end counter, restrict counter, else always display
					if(o.counterStart!=''&&o.counterEnd!=''){
						if($('#'+newDivId+'rep').text().length>o.counterStart&&$('#'+newDivId+'count').is(':hidden')){
							$('#'+newDivId+'count').fadeIn(300);
						}
						if($('#'+newDivId+'rep').text().length<o.counterStart&&$('#'+newDivId+'count').is(':visible')){
							$('#'+newDivId+'count').fadeOut(300);
						}
					}
				}).focus(function(){
				
					//-> if specified, label fading occurs on focus
					if(o.labelFadeTo!=''&&$('#'+newDivId+'rep').text().length==0){
						$('#'+newDivId).fadeTo(o.labelFadeDur, o.labelFadeTo);
					}
					
					//-> if specified, animate actions occur on focus
					if(o.animate=='true'){
						var os = o.animateStyle; var aa = {}; aa[os] = o.animateTo;
						$('#'+newDivId+'rep').animate(aa,o.animateTo);
					}
				}).blur(function(){
				
					//-> if specified, label fading occurs on blur
					if(o.labelFadeTo!=''&&$('#'+newDivId+'rep').text().length==0){
						$('#'+newDivId).fadeTo(o.labelFadeDur, 1);
					}
					
					//-> if specified, animate actions occur on blur
					if(o.animate=='true'){
						var ost = o.animateStyle; var aat = {}; aat[ost] = o.animateFrom;
						$('#'+newDivId+'rep').animate(aat,o.animateFrom);
					}
					
					//-> if hidden div, hide on blur
					if(o.hiddenDiv!=''&&$('#'+newDivId+'rep').text().length==0){
						$(o.hiddenDiv).fadeOut(300);
					}
				});
				
				//-> setting up label
				if(o.label!=''){
					
					//-> add label div to parent
					$('#'+newDivId+'wrap').append('<label id="'+newDivId+'" class="'+o.labelClass+'">'+o.label+'</label>');
					
					//-> position added div correctly
					$('#'+newDivId).css({
						'top':parseInt($(this).css('padding-top')) + parseInt($(this).css('borderTopWidth')),
						'left':parseInt($(this).css('padding-left')) + parseInt($(this).css('borderLeftWidth'))+1,
						'position':'absolute',
						'cursor':'pointer'
					});
					
					//-> make label clickable
					$('#'+newDivId).click(function(){
						$('#'+newDivId+'rep').focus();
					});
					
					//-> label's default display
					if($(this).val().length>0){
						$('#'+newDivId).css('display','none');
					}else{
						$('#'+newDivId).css('display','block');
					}
				}
				
				//-> call counter section
				if(o.showCounter=='true'){
				
					//-> append counter
					$(this).after('<div id="'+newDivId+'count" class="'+o.counterClass+'"></div>');
					if(o.counterStart==''&&o.counterEnd==''){
						$('#'+newDivId+'count').css({
							'display':'block'
						});
					}else{
						$('#'+newDivId+'count').css({
							'display':'none'
						});
					}
					
					//-> position counter correctly
					$('#'+newDivId+'count').css({
						'position':'absolute',
						'bottom':parseInt($(this).position().top) + $(this).parent().height(),
						'right':parseInt($(this).position().left) + parseInt($(this).css('padding-left')) + parseInt($(this).css('borderLeftWidth')) + 2,
					});
				}
			}else{
				$(this).wrap('<div id="'+newDivId+'wrap" style="position:relative;float:'+$(this).css('float')+';display:'+$(this).css('display')+'">');
				
				$(this).keyup(function(){
					
					//-> also do same functions as input would
					if(o.label!=''){
						if($(this).val().length>0){
							$('#'+newDivId).fadeOut(100);
					
							//-> if hidden div, show when there's text
							if(o.hiddenDiv!=''){ $(o.hiddenDiv).fadeIn(300); }
						}else{
							$('#'+newDivId).fadeIn(500);
					
							//-> if no text, hide div again
							if(o.hiddenDiv!=''){ $(o.hiddenDiv).fadeOut(300); }
						}
					}
					
					//-> restrict length if specified
					if(o.length!=''){
						if($(this).val().length>o.length){
							$(this).val($(this).val().substring(0, o.length));
						}
					}
				
					//-> if counter is showing, start it
					if(o.showCounter=='true'){
						if($('#'+newDivId+'rep').text().length>o.counterStart){
							if(o.count=='down'&&o.length!=''){
								$('#'+newDivId+'count').text(o.length-$('#'+newDivId+'rep').text().length);
							}else{
								$('#'+newDivId+'count').text($('#'+newDivId+'rep').text().length);
							}
						}
					}
					
					//-> if start/end counter, restrict counter, else always display
					if(o.counterStart!=''&&o.counterEnd!=''){
						if($(this).val().length>o.counterStart&&$('#'+newDivId+'count').is(':hidden')){
							$('#'+newDivId+'count').fadeIn(300);
						}
						if($(this).val().length<o.counterStart&&$('#'+newDivId+'count').is(':visible')){
							$('#'+newDivId+'count').fadeOut(300);
						}
					}
				}).focus(function(){
				
					//-> if specified, label fading occurs on focus
					if(o.labelFadeTo!=''){
						if($(this).val().length==0){
							$('#'+newDivId).fadeTo(o.labelFadeDur, o.labelFadeTo);
						}else{
							$('#'+newDivId).fadeOut(300);
						}
					}
					
					//-> if specified, animate actions occur on focus
					if(o.animate=='true'){
						var os = o.animateStyle; var aa = {}; aa[os] = o.animateTo;
						$(this).animate(aa,o.animateTo);
					}
				}).blur(function(){
				
					//-> if specified, label fading occurs on blur
					if(o.labelFadeTo!=''){
						if($(this).val().length==0){
							$('#'+newDivId).fadeTo(o.labelFadeDur, 1);
						}else{
							$('#'+newDivId).fadeOut(300);
						}
					}
					
					//-> if specified, animate actions occur on blur
					if(o.animate=='true'){
						var ost = o.animateStyle; var aat = {}; aat[ost] = o.animateFrom;
						$(this).animate(aat,o.animateFrom);
					}
					
					//-> if hidden div, hide on blur
					if(o.hiddenDiv!=''&&$(this).val().length==0){
						$(o.hiddenDiv).fadeOut(300);
					}
				}).change(function(){
				
					//-> if specified, label fading occurs on blur
					if(o.labelFadeTo!=''){
						if($(this).val().length==0){
							$('#'+newDivId).fadeTo(o.labelFadeDur, 1);
						}else{
							$('#'+newDivId).fadeOut(300);
						}
					}
				});
				
				//-> setting up label
				if(o.label!=''){
					
					//-> add label div to parent
					$('#'+newDivId+'wrap').append('<label for="'+$(this).attr('id')+'" id="'+newDivId+'" class="'+o.labelClass+'">'+o.label+'</label>');
					
					//-> position added div correctly
					$('#'+newDivId).css({
						'top':parseInt($(this).css('padding-top')) + parseInt($(this).css('borderTopWidth')),
						'left':parseInt($(this).css('padding-left')) + parseInt($(this).css('borderLeftWidth'))+1,
						'position':'absolute',
						'cursor':'pointer'
					});
					
					//-> label's default display
					if($(this).val().length>0){
						$('#'+newDivId).css('display','none');
					}else{
						$('#'+newDivId).css('display','block');
					}
				}
				
				//-> call counter section
				if(o.showCounter=='true'){
				
					//-> append counter
					$(this).after('<div id="'+newDivId+'count" class="'+o.counterClass+'"></div>');
					if(o.counterStart==''&&o.counterEnd==''){
						$('#'+newDivId+'count').css({
							'display':'block'
						});
					}else{
						$('#'+newDivId+'count').css({
							'display':'none'
						});
					}
					
					//-> position counter correctly
					$('#'+newDivId+'count').css({
						'position':'absolute',
						'bottom':parseInt($(this).position().top) + $(this).parent().height(),
						'right':parseInt($(this).position().left) + parseInt($(this).css('padding-left')) + parseInt($(this).css('borderLeftWidth')) + 2,
					});
				}
			}
		});
	};
})(jQuery);

/**
 *
 *	End
 *
**/
