function ForumPage( strControllerURL ){
	this.ControllerURL = strControllerURL;
	this.Init();
};

ForumPage.prototype.objPostProperties = function( objPostProperties ){
	this.PostProperties = objPostProperties;
};

ForumPage.prototype = new PageControllerClass;
ForumPage.prototype.Init = function(){
	var objSelf = this;
	var postForm = $( "#forumPost" );

	this.SetBusy( true );
	$('img[class^=ratelink]').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitRating( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('a[class=subscriptionlink]').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitSubscription( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('a.sharelinks-help').click(
		function( objEvent ){
			var $this = $(this);
			window.open( $this.attr('href'),this,'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=890,height=450,left=640,top=275' );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('th.last > a').click(
		function( objEvent ){
			var $this = $(this);
			$this.attr('href',$this.attr('rel'));
			return(true);
		}
	);
	$('th.count > a').click(
		function( objEvent ){
			var $this = $(this);
			$this.attr('href',$this.attr('rel'));
			return(true);
		}
	);

	$('#adv_preview_btn').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitAdvPreview($this,postForm);
			objEvent.preventDefault();
			return( false );
		}
	);

	$('#preview_btn').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitPreview($this,postForm);
			objEvent.preventDefault();
			return( false );
		}
	);
	$('#previewedit_btn').click(
		function( objEvent ){
			objSelf.ToggleEditPreview(false);
			objEvent.preventDefault();
			return( false );
		}
	);
	
	$('#advance_btn').click(
		function( objEvent ){
			postForm.find("input[name='advanced']").val(true);
			postForm.submit();
			return( false );
		}
	);

	$('#post_confirm').click(
		function( objEvent ){
			var $this = $(this);
			postForm.find("input[name='confirmed']").val(true);
			$this.attr('disabled','disabled');
			try { $this.attr('type','button'); } catch(e) { };
			objEvent.preventDefault();
			postForm.submit();
			return( false );
		}
	);

	$('select[name="ReplyAlbumID"]').change(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitViewAlbumThumbnail( $this );
		}
	);

	$("td.topics>img").hover(
		function(objEvent){
			var $this = $(this);
			objSelf.ShowTopicPreview($this,objEvent);
			return(false);
		},
		function(){$("p#topic-preview").remove();}
	);

	$("td.topics>img").mousemove(
		function(objEvent){$("p#topic-preview").css("top",(objEvent.pageY-10)+"px").css("left",(objEvent.pageX+10)+"px");}
	);

	setInterval(function(){ 
		$('td.status img[src*="green"]').fadeOut(200);
		$('td.status img[src*="green"]').fadeIn(800);
	},15000);


	this.SetBusy( false );
};

ForumPage.prototype.ShowTopicPreview = function( jImg, objEvent ) {
	$("body").append("<p id='topic-preview'><img src='/index.cfm/do/api.forums.getTopicPreviewThumb/id/"+ jImg.attr("id") +"/preview.jpg' alt='Loading...' /></p>");
	$("p#topic-preview").css("top",(objEvent.pageY-10)+"px").css("left",(objEvent.pageX+10)+"px").fadeIn("fast");
};


ForumPage.prototype.SubmitViewAlbumThumbnail = function( jSelect ){
	var objSelf = this;
	$("#newtopicarea").css('background','url(/index.cfm/do/api.gallery.viewGalleryThumbnail/resize/80,80/id/'+jSelect.val()+'/format/view'+jSelect.val()+'.png) top right no-repeat');
};


// This submits the rating.
ForumPage.prototype.SubmitRating = function( jLink ){
	var objSelf = this;
	if(objSelf.GetBusy()){objSelf.AlertBusy();}else{objSelf.SetBusy(true);};
	var objPostProperties = {
		"do": "api.rating.rate",
		"id": jLink.attr("id"),
		"rateType":jLink.attr("title"),
		"rateindex": jLink.attr("alt")
		};
	$.get(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitRatingHandler( objResponse , jLink );
		},
		"json"
		);
};

ForumPage.prototype.SubmitRatingHandler = function( objResponse , jLink ){
	if (objResponse.SUCCESS){
		if(objResponse.DATA >= 0) { var inputClass = "green" } else { var inputClass = "red" }
		var jTarget = '#rating_'+jLink.attr("id");
		var jElement = $(jTarget);
			jElement.children().remove();
			jElement.append([
				"<span class='"+inputClass+"'>",
				objResponse.DATA,
				"</span>"
			].join(""));
			this.SetBusy(false);
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};

ForumPage.prototype.SubmitSubscription = function( jLink ){
	var objSelf = this;
	if (objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	$.get(jLink.attr('href'),'',function(objResponse){objSelf.SubmitSubscriptionHandler(objResponse,jLink);},"json");
};

ForumPage.prototype.SubmitSubscriptionHandler = function( objResponse , jLink ){
	if (objResponse.SUCCESS){
		jLink.children().remove();
		jLink.html(objResponse.DATA);
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};


ForumPage.prototype.SubmitPreview = function(jButton,jForm){
	var objSelf = this;
	if (objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties = {
		"do": "api.forums.previewReply",
		"TopicID": jForm.find("input[name='topicid']").val(),
		"Message": jForm.find("textarea[name='message']").val()
		};
	$.post(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitPreviewHandler(objResponse,jButton,jForm);
		},
		"json"
		);
};

ForumPage.prototype.SubmitPreviewHandler = function(objResponse,jButton,jForm){
	var objSelf = this;
	if(objResponse.SUCCESS){
		objSelf.ToggleEditPreview(true);
		$("#previewArea").html(objResponse.DATA);
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};

ForumPage.prototype.ToggleEditPreview = function(swt){
	var previewArea = $("#previewArea");
	var txtarea = $("textarea#message");
	var editbtn = $('#previewedit_btn');
	var previewbtn = $('#preview_btn');
	if(swt) {
		previewArea.css("display","block");
		previewArea.html('');
		txtarea.css("display","none");
		editbtn.css("visibility","visible");
		previewbtn.css("visibility","hidden");
	} else {
		editbtn.css("visibility","hidden");
		previewArea.css("display","none");
		previewArea.html('');
		txtarea.css("display","block");
		previewbtn.css("visibility","visible");
	};
};

FCKeditor_OnComplete = function(editorInstance) {
	document.getElementById(editorInstance.Name + '___Frame').style.height = '450px';
	ForumPage.prototype.setEditorInstance(editorInstance);
};

ForumPage.prototype.setEditorInstance = function(editorInstance){
	var objSelf = this;
	this.EditorInstance=editorInstance;
	if(objSelf.ReplyQuoteID) { objSelf.SubmitGetReplyQuote(); };
};
ForumPage.prototype.getEditorInstance = function() { return this.EditorInstance; };


ForumPage.prototype.setReplyQuoteID = function(id) {this.ReplyQuoteID=id;};
ForumPage.prototype.getReplyQuoteID = function() {return this.ReplyQuoteID;};


ForumPage.prototype.SubmitGetReplyQuote = function() {
	var objSelf = this;
	$.get('/index.cfm/do/api.forums.getQuoteHTML/ObjectID/'+objSelf.ReplyQuoteID,'',function(objResponse){objSelf.setReplyQuoteHandler(objResponse);},"json");
};

ForumPage.prototype.setReplyQuoteHandler = function(objResponse){
	var objSelf = this;
	if(objResponse.SUCCESS){
		objSelf.EditorInstance.InsertHtml('&nbsp;');
		objSelf.EditorInstance.InsertHtml(objResponse.DATA);
	};
};

ForumPage.prototype.SubmitAdvPreview = function(jButton,jForm){
	var objSelf = this;
	if (objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties = {
		"do": "api.forums.advPreviewReply",
		"TopicID": jForm.find("input[name='topicid']").val(),
		"Message": objSelf.getEditorInstance().GetData()
		};
	$.post(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.SubmitAdvPreviewHandler(objResponse,jButton,jForm);
		},
		"json"
		);
};

ForumPage.prototype.SubmitAdvPreviewHandler = function(objResponse,jButton,jForm){
	var objSelf = this;
	if(objResponse.SUCCESS){
		$("#adv-preview-area").html(objResponse.DATA);
		$("#adv-preview-area > div").css("border","1px dashed #f47c15");
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};

