//ajax facebook style comments
$(function(){
	window.supressHide = false;
	$('#thewire_large-textarea').autogrow();
	$('textarea:not([name=message],[name=description])').autogrow();

	fbCommentBox = function($commentBox,drop){
		var textData = '';
		try { 
			if(drop!=undefined)
			{
				
				var format = drop.dataTransfer.types ? "text/plain" : "Text";
                textData = drop.dataTransfer.getData (format);
			}
		}
		catch (e){ }
		
		$parent = $($commentBox).parent().attr('id').replace('commentReply','');
		
//		$($commentBox).val('');
		$($commentBox).parent().hide();
		
		$('#repliesWrap'+$parent).show();
		//console.debug($($commentBox).parent().attr('id'));
		$('#comment'+$parent).show();
		//alert(textData);
		//$('.commentField',$('#comment'+$parent)).val(textData).focus();
		$('.commentField',$('#comment'+$parent)).focus();
		
		$('.uploadImageComment',$('#comment'+$parent)).show()
		
		$('#addImageComment').bind('click',function() { window.supressHide = true; } )
		fileUploader = new AjaxUpload('#addImageComment', {
		  
			action: '/uploadImage.php',					
			name: 'File',					
			autoSubmit: true,					
			responseType: false,					
			onChange: function(file, extension){
			
			  },
			
			onSubmit: function(file, extension) {
				if (! (extension && /^(jpg|png|jpeg|gif)$/i.test(extension)))
				{
						// extension is not allowed
						alert('Error: invalid file extension');
						// cancel upload
						return false;
				}
	
			},
		
			onComplete: function(file, response)
			{  
				button = this._button
				$text = $('textarea',$($(button).parent())).val();
				if(response.indexOf('thumb_') > -1)
				{
					$text = $text + ' <img src="/uploads/'+response+'" alt=\"fancy\" />';
				}
				else
				{
					$text = $text + ' <img src="/uploads/'+response+'" />';
				}
				$('.activeComment',$($($(button).parent()).parent())).show();
				$('.addComment',$($($(button).parent()).parent())).hide();
				$('textarea',$($(button).parent())).val($text);
			}
		});	

		
		
		allowrefresh = false
	}
	

	//
	
	submitComment = function($commentButton)
	{
		window.supressHide=true;
		$parent = $($commentButton).parent().attr('id').replace('comment','');
		$comment = $('.commentField',$('#comment'+$parent)).val();
		$comment = $comment.split("\n").join("<br/>");

		$('.comment',$('#wirePost'+$parent)).hide();
		if($comment=='')
		{
			$($commentButton).parent().hide();				
			$('#commentReply'+$parent).val('Write a comment...');
			allowrefresh = true
			$('#commentReply'+$parent).show();	
			return;
		}
		
//		$('.commentField',$('#comment'+$parent)).val('');
		$($commentButton).parent().show();
		
		$commentPanel = $($commentButton).parent().clone();
		$commentPanel.attr({'id':''});
		$($commentPanel).addClass('awaitingUpdate');
		$('.commentField',$commentPanel).remove();
		$('.commentButton',$commentPanel).remove();
		//console.debug($('strong',$($commentPanel)));
		$('strong',$($commentPanel)).css({'display':'inline'});
		$nameObj = $('strong',$($commentPanel));
		$name = $nameObj.text()
		$nameObj.after('<div style="float: right; width: 91%; margin-top: -3px; overflow: hidden;"><strong>'+$name+'&nbsp;</strong> '+$comment+'</div>').remove();

		
		$('#lastComantLine'+$parent).before($commentPanel);
		$($commentButton).parent().hide();
		
		
		
		
		//$($commentButton).remove();
		//$('.commentField',$('#comment'+$parent)).after('<p>'+$comment+'</p>').remove();
		
		$.ajax({
			 url : '/action/threadwire/add',
			 type: "POST",
			 data: "location=ajax&note="+escape($comment)+'&threadwirepost='+$parent,
			 success: function(msg){
				 //console.debug($parent);
				 $panel = $('.awaitingUpdate');
 				 $($panel).addClass('post-reply').removeClass('awaitingUpdate');
				 
				 $('#commentReply'+$parent).show();
				 $('.startCommentInput',$('#commentReply'+$parent)).val('Write a comment...');
				 allowrefresh = true
		 		window.supressHide=false;
			 }
		});
		$('.commentField',$('#comment'+$parent)).val('')
		$('.uploadImageComment').hide()
		
		//console.debug($comment);
	}
	
	hidePendingComment = function($parent)
	{
		if(window.supressHide) return;
		//$parent = $($commentBox).parent().attr('id').replace('comment','');
//		$('.commentField',$('#comment'+$parent)).val('');
		$('#comment'+$parent).hide();
		$('#commentReply'+$parent).show();
	}
	
	loadAllComments = function(commentLink)
	{
		$id = $(commentLink).parent().attr('id').replace('moreComments','');
		//console.debug($id);
		//alert('dev v1: getting comments '+$id);
		$comments = $.ajax({
		'url'	: '/action/threadwire/getComments?parent='+$id,
		'success' : function(data) {
				$('.post-reply',$(commentLink).parent().parent()).remove();
			    $(commentLink).parent().after(data).remove();
			  //  alert('Load was performed.');.
		
			 
			}
		});
		setTimeout(function() { $('img[alt="fancy"]', $('#repliesWrap' + $id)).each(function() {
					imgSrc = $(this).attr('src');				
					imgSrc = imgSrc.replace('/thumb_' , '/');				
					$(this).wrap('<a class="fancyBox" href="' + imgSrc + '" />');
					$($(this).parent()).fancybox({
							'titleShow'		: false
						});
				} ) },1000);		
		
	}
})