var ForumphotoObject;
var TopicForm = function()
{
  var _self = this;
  this.init = function()
  {
    this.form = $('#newTopic');
  }

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

  this.topic = function()
  {

	var flagAjax = true;
	flagAjax = validateTopic();

	if(flagAjax){
		$.ajax({
			url: '/forum/index/newtopic',
			type: 'POST',
			data: $('#newTopic').serialize(),
			success: function(transport){
				$('#post-inprogress').hide();
				if (transport == 'true')
		        {
				        if (transport != 'false'){
							document.location = document.location.href;
						}
		        }
			},
			beforeSend: function() {
				$('#post-inprogress').show();
	        }
		});
	}

  }
}

function popupNotLogin(){
	$("#dialog").html('<div class="info-error">Aby korzystać z tej opcji musisz być zalogowany. <a href="/profile/auth/login">Zaloguj się</a></div>');
	$("#dialog").show();
	$("#dialog").attr("title", "Zaloguj się")
	$("#dialog").dialog({
           bgiframe: true,
           autoOpen: false,
           height: 200,
           width: 450,
           modal: true,
           buttons: {
                 'Zamknij': function() { $(this).dialog('close');}
           },
           close: function() {}
     	});
}

function validateTopic(){
	flagValidate = true;
	if($('#textposttopic').val() == ""){
		$('#fckTextareaError').show();
		flagValidate = false;
	}else{
		$('#fckTextareaError').hide();
	}

	if($('#topic_title').val() == ""){
		$('#titleError').show();
		flagValidate = false;
	}else{
		$('#titleError').hide();
	}

	if($('#categoriesListTopic').val() == ""){
		$('#checkedCategory').show();
		flagValidate = false;
	}else{
		$('#checkedCategory').hide();
	}

	return flagValidate;
}

var CreateTopic = function(element){
  this.init = function(element)
  {
    this.root = element;
	flagTopic = true;
	this.root.click(function(event)
    {
    	event.preventDefault();
		//$('#textposttopic').val("");
		if(flagTopic){
			    parent_id ="";
				if($('#top_parent_id')[0]){
					parent_id = $('#top_parent_id').val();
				}
				$("#dialog").load("/forum/index/formtopic",{'top_parent_id': parent_id}, function(transport){
						if(transport != "false"){
							$("#dialog").show();
							var heightF = $('#topic-form').height();
					    	var widthF = $('#topic-form').width();
					    	//$('#showTopic-form').hide();
					    	textareaWidthTopic = $('#textposttopic').width();
					    	textareaHeightTopic = $('#textposttopic').height();
					    	$("#dialog").attr("title", "Dodaj nowy temat")
							$("#dialog").dialog({

					            bgiframe: true,

					            autoOpen: false,

					            height: (heightF+150),
					            width: (widthF+40),
					            modal: true,
					            buttons: {
								'Publikuj': function(){
				                  		var oEditorTopic = FCKeditorAPI.GetInstance('textposttopic') ;
										oEditorTopic.UpdateLinkedField();
									   	rl2.topic(); },
					                  'Zamknij': function() { $(this).dialog('close');}

					            },

					            close: function() {}

					      	});
					      	var FCK_topic_content = new FCKeditor( 'textposttopic' ) ;
					        FCK_topic_content.ToolbarSet = 'veneoMini';
					        FCK_topic_content.Width = textareaWidthTopic;
					        FCK_topic_content.Height = textareaHeightTopic;
					        FCK_topic_content.ReplaceTextarea() ;

			       		}else{
							popupNotLogin();
				        }
				        $('#dialog').dialog('open');

				        $('#newTopic').change(function(){
				        	var oEditorTopic2 = FCKeditorAPI.GetInstance('textposttopic') ;
							oEditorTopic2.UpdateLinkedField();
				        	validateTopic();
				        });


      			});
      			flagTopic = false;
		}else{
			$('#textposttopic').val("");
			$('#topic_title').val("");
			$('#dialog').dialog('open');
		}
	});
  }

}

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


  //if($('forumIframeupload'))
  //{
  	//	ForumphotoObject = new Forumphoto();
  //}
  if($('a.createTopic')[0]){
	  $('a.createTopic').each(function(a){
	   	 	object = new CreateTopic($(this));
	   	 	object.init($(this));
	  });
  }

  if($('topic-form')){
  	rl2 = new TopicForm();
  	rl2.init();
  }
});

