var rlpost;
var PostForm = function(){

  this.init =  function()
  {
  	_self = this;
    //this.form = $('#newPost');
    //this.formHelp = $('#parametrsTopic');

    //this.form.submit(this.post());

   // Event.observe(this.form, 'submit', this.post.bindAsEventListener(this));

	/*if($('.setResolvedPost')[0]){
		$('.setResolvedPost')).each(function(el)
		  {
		  /	if(Cookie.get('NL_NAME') == $('topic_owner_name').val()){
		  		el.show();

		  		el.click(function(idobj, event)
		  		{
						var id_post = idobj.substr(13);

						$.ajax({
							url: '/forum/resolvedpost',
							type: 'POST',
							data: ({resolved: id_post}),
							success: function(transport){
								if (transport.responseText == 'true')
						        {
										$(idobj).hide();
										$('icon_resolved_'+id_post).show();
										$('post_'+id_post).addClass('blue');
						        }
							}
						});
			  	});
			}
		  });
	}*/
  }

  this.setElement = function(element)
  {
    this.root = element;
  }

  this.post = function(event)
  {
  	$('#newPost-empty').hide();
	var oEditor = FCKeditorAPI.GetInstance('post_content') ;
	oEditor.UpdateLinkedField();
	if($('#post_content').val() != ""){
		$.ajax({
			url: '/forum/index/newpost',
			type: 'POST',
			data: $('#newPost').serialize() +'&'+ $('#parametrsTopic').serialize(),
			success: function(transport){
				$('#topic-inprogress').hide();
				if (transport == 'true'){
						//$('userLogin-inprogress').hide();
				        if (transport != 'false'){
					        document.location.href = '';
							document.location = $('#urlLastPage').val();
						}else{
							//Element.show($$('#newPost .buttons input')[0]);
							//Element.show($$('#newPost .buttons input')[1]);
						}
		        }
			},
			beforeSend: function() {
				$('#post-inprogress').show();
				//$('#userLogin-inprogress').show();
				//$('#newPost .buttons input').hide();
	        }
		});
	}else{
		$('#newPost-empty').show();
	}
    return true;
  }
}

var CreatePost = function(element){
  this.init = function(element)
  {

  	_self = this;

    this.root = element;
    flagPost = true;

	this.root.click(function(event)
    	{
	    	event.preventDefault();
			if(flagPost){
				$("#dialog").load("/forum/index/formpost",{}, function(transport){
						if(transport != "false"){
							$("#dialog").show();
							var heightF = $('#post-form').height();
					    	var widthF = $('#post-form').width();
					    	//$('#showTopic-form').hide();
					    	textareaWidth = $('#post_content').width();
					    	textareaHeight = $('#post_content').height();
					    	$("#dialog").attr("title", "Dodaj nowy post")
							$("#dialog").dialog({
					            bgiframe: true,
					            autoOpen: false,
					            height: 360,
					            width: 450,
					            modal: true,
					            buttons: {
									  'Publikuj': function(){ rlpost.post() },
					                  'Zamknij': function() { $(this).dialog('close');}

					            },
					            close: function() {}

					      	});
					      	FCK_post_content = new FCKeditor( 'post_content' ) ;
					        FCK_post_content.ToolbarSet = 'veneoMini';
					        FCK_post_content.Width = textareaWidth;
					        FCK_post_content.Height = textareaHeight;
					        FCK_post_content.ReplaceTextarea() ;
					    }else{
							popupNotLogin();
					    }


					     $('#dialog').dialog('open');

					     $('#post_content').val("");

				      	if(element.attr('id').substring(0,8) =='answerid'){
					  		id = element.attr('id').substring(9);
					  		cont = $('#contentposthidden_'+id).val();
					  		cont2 = '[cytat_'+id+']'+cont+'[/cytat_'+id+'] <br/><br/>';
							$('#post_content').val(cont2);

				      	}
		         });
		          flagPost = false;

	        }else{
	        	$('#dialog').dialog('open');
	        	$('#post_content').val("");

		      	if(element.attr('id').substring(0,8) =='answerid'){
			  		id = element.attr('id').substring(9);
			  		cont = $('#contentposthidden_'+id).val();
			  		cont2 = '[cytat_'+id+']'+cont+'[/cytat_'+id+'] <br/><br/>';
					$('#post_content').val(cont2);

		      	}
	        }
	});
   }
}



$(document).ready(function(event)
{

  //if($$('.login').length == 0){
	  if($('a.createPost')[0]){
		  $('a.createPost').each(function(a)
		  {
		   objPost = new CreatePost($(this));
		   objPost.init($(this));

		  });

	  }
  //}

//  if($('post-form')){
	  rlpost = new PostForm();
	  rlpost.init();
  //}

	$('a.showUrl').click(function(event){
		event.preventDefault();
		$(this).parents('td').children('div.popup-cover').css("visibility", "visible");
	});

	$('div.popup-cover a').click(function(event){
		event.preventDefault();
		$(this).parents('td').children('div.popup-cover').css("visibility", "hidden");
	});

/*
  if($('search-filter')){
	 Event.observe($('search-filter'), 'click', function(event){
	 	$('search-filter').submit();
	 }.bind(this));
  }*/



});

