$(document).ready(function(){

	var focused = false;
	var hashover = false;
	var animated = false;
	var autowrapped = false;

	function reduceInput(){

	$('#messageInputTextContainer').animate({marginTop: '46px'}, 100);
	$('#msg').animate({height: '20px'}, 100);
	$('#scrollcover').css('display','none');

		setTimeout(function(){
			$('#msg').css('overflow-y','hidden');
		},250);

		animated = false;
		autowrapped = false;
	}

	function expandInput(){

			$('#messageInputTextContainer').animate({ marginTop: '0px'}, 100);
			$('#msg').animate({height: '66px'}, 100);		
		
			setTimeout(function(){
				$('#scrollcover').css('display','block');
				$('#msg').css('overflow-y','auto');
			},250);
		
			animated = true;
			//log('expanded, animated set to true');
	}

	function countlines(){
			//log('inside coutlines function');
			var nlb = $('#msg').val();
			String.prototype.countchars = function(char){  
		  	return this.split(char).length-1;  
			}
			var numlines = nlb.countchars('\n');

			//log('nlb is '+nlb);
			//log('numlines is '+numlines);
			return numlines;
	}
	
	$('#msg').on({
		copy : function(){
			setTimeout(function(){
				var numlines = countlines();

				if (numlines<1){
					if (animated == true){
						reduceInput();
					}else{
						//log('');
					}
					var currentmsg = $('#msg').val();
					if ( currentmsg == ''){
						$('#reset_x').css('display','none');
					}
				}

			},250);//end of timeout function
		},
		paste : function(){
			setTimeout(function(){		
				var numlines = countlines();

				if (numlines>0){	
					if (animated == false){
						expandInput();
					}else{
						//log('already animated');
					}
				}
			},250);//end of timeout function
		},
		focus : function(){
			var currentmsg = $('#msg').val();
			if ( currentmsg == 'Your Message'){
				$('#msg').val('');
				//log('cleared default message');
			}
		},
		blur : function(){
			var currentmsg = $('#msg').val();
			if ( currentmsg == ''){
				$('#msg').val('Your Message');
				//log('reverted to default message');
			}
		},
		keyup : function(ev){
			var numlines = countlines();
			//log(animated);
			//log(numlines);

			var currentmsg = $('#msg').val();

			$('#ruler').html(currentmsg);
			var rulerLength = $('#ruler').width();
			//log(rulerLength);

			if (animated == false && rulerLength > 300 ){
				if (animated == false){
					expandInput();
					//log('line auto wrapped, so expanded');
					autowrapped=true;
				}
			}
		
			if ( currentmsg !== ''){
			 $('#reset_x').css('display','block');
			}

			if(ev.which == 8 ){

				//log('deleted pressed');

					if (numlines<1 && autowrapped != true){	
						if (animated == true){
							reduceInput();
						}else{
							//log('delete pressed but did not reduce input');
						}
					}

					var currentmsg = $('#msg').val();
					if ( currentmsg == ''){
						$('#reset_x').css('display','none');
						if(animated == true){
							reduceInput();
						}
					}

			}else if(ev.which == 13 ){
				//log('return pressed')

				if (numlines>0){	
					if (animated == false){
					expandInput();
				}else{
					//log('return pressed but already animated');
				}
			}
			}
		
		}
	});

	$('#reset_x').on("click",function(){
	//log('resetting');
	$('#msg').focus();
	
	$('#msg').val('');
	$('#reset_x').hide();

	if (animated == true){		
		reduceInput();		
		//log('cleared text, de-animated');				
	}else{
		//log('cleared text, did not need to de-animate');
	}		
});

});
//end of doc ready function

