/* ============================================================
topics.js
 javascripts for topics pages
 http://books.livedoor.com/topics/, http://books.livedoor.com/topic/
  created: 2008-09-11
  modified:
  $Date:: 2008-10-29 12:52:22#$
  $Author: nakamurak $
  $Revision: 51687 $
============================================================ */

(function($) {
	//execute right now
	cau1Lib.cssRules.add(".descriptionWrp","display:none");
	cau1Lib.cssRules.add("#topicForm","display:none");
	cau1Lib.cssRules.add("#commentForm","display:none");
	cau1Lib.cssRules.add(".messageArea","display:none");

	//execute when document's ready
	$(function() {

		//show description
		new cau1Lib.Class.showDescription("#showDescription");

		//preview
		new cau1Lib.Class.previewWindow(function() {
			//title
			if (this.targetFormObj.find("input[name='title']"))
				this.dialogObj.find("span.title").html(this.targetFormObj.find("input[name='title']").val());
			//text
			this.dialogObj.find("span.comment").html(this.targetFormObj.find("textarea[name='comment'],textarea[name='content']").val().replace(/\n/g,"<br />"));
			// item img
			var self = this;
			this.targetFormObj.find("input[name^='item']").each(function() {
				var itemId = $(this).val().match(/item\/(\d+)/);
				var idx = $(this).attr("name").match(/^item(.*)/)[1];
				if (itemId) {
					self.dialogObj.find(".itemImage" + idx).show()
					.load("/api/item?id=" + itemId[1] + " img",function() {$(this).find("img").width(45).height(65);});
				} else {
					self.dialogObj.find(".itemImage" + idx).hide();
				}
			});
		});

		//topic form
		$("#topicInputTop").click(function() {
			var target = $("#topicInput").css("display") == "none"? $("#topicForm"): $("#topicInput");
			target = target.offset();
			window.scrollTo(target.left,target.top)
			$("#topicInput").click();
		});
		$("#topicInput").click(function() {
			var self = this;
			$("#topicForm").slideDown(function() {$(self).hide();});
		});

		//comment form
		$("#commentInputTop").click(function() {
			var target = $("#commentInput").css("display") == "none"? $("#commentForm"): $("#commentInput");
			target = target.offset();
			window.scrollTo(target.left,target.top)
			$("#commentInput").click();
		});
		$("#commentInput").click(function() {
			var self = this;
			$("#commentForm").slideDown(function() {$(self).hide();});
		});
		//reopen form if there are errors
		if ($("#hasErrors").is("ul"))
			$("#topicInput, #commentInput").click();

	});
})(jQuery);

cau1Lib.Class.showDescription = function(target,options) {
	var self = this;
	//optional settings
	this.options = $.extend({
		showContent: "div.descriptionWrp",
		xOffset: 20
	},options);
	this.targetObj = $(target);
	this.contentShown = false;
	this.toggleHandler = function(evt) {self._toggleShowHide(evt);};
	this.hideHandler = function() {self._hide();};
	this.targetObj.click(this.toggleHandler);
};
$.extend(cau1Lib.Class.showDescription.prototype,{
	_show: function(x,y) {
		//create DOM if there isn't.
		if (!this.showContentObj)
			this._createContent();
		//show
		var xPos = x - this.showContentObj.width() + this.options.xOffset;
		this.showContentObj.css({left:xPos,top:y}).show();
		this.contentShown = true;
	},
	_hide: function() {
		this.showContentObj.hide();
		this.contentShown = false;
	},
	_toggleShowHide: function(evt) {
		evt.preventDefault();
		evt.stopPropagation();
		if (this.contentShown) {
			this._hide();
		} else {
			this._show(evt.pageX,evt.pageY);
		}
	},
	_createContent: function() {
		this.showContentObj = $(this.options.showContent)
		.css({opacity:"0.9"})
		.hide()
		.click(function(evt) {evt.stopPropagation();})
		.find("p.closeBtn a").click(this.hideHandler)
		.end();
		//hide already opend content on any other area click.
		$(window)
		.bind("click.showContent",this.hideHandler);
	}
});
