/* ============================================================
item.js
 javascripts for http://books.livedoor.com/item/
  created: 2008-09-10
  modified:
  $Date:: 2009-08-31 17:09:37#$
  $Author: edge-dev $
  $Revision: 93096 $
============================================================ */

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

	//execute when document's ready
	$(function() {
		//star input
		new cau1Lib.Class.rating("#rateInput");

		//preview
		new cau1Lib.Class.previewWindow(function() {
			this.dialogObj.find("span.title").text(this.targetFormObj.find("input[name='title']").val());
			this.dialogObj.find("span.book_value").text(this.targetFormObj.find("select[name='book_value']").val());
			var icon = this.dialogObj.find("img.icon");
			var src = icon.attr("src");
			icon.attr("src",src.replace(/(\/reviewType\/)(default|\d+)(\.gif)/,"$1" + this.targetFormObj.find("select[name='icon']").val() +"$3"));
			this.dialogObj.find("span.comment").html(this.targetFormObj.find("textarea[name='comment']").val().replace(/\n/g,"<br />"));
			this.dialogObj.find("span.ratings").text(this.targetFormObj.find("select[name='ratings']").val());
		});

		//tag form
		$("#tagInput").click(function() {
			var self = this;
			$("#tagForm").slideDown(function() {$(self).hide();});
		});
		$("#tagForm").submit(function() {
			var self = this;
			//errorもhandleするなら$.ajaxで
			$.post(this.action,$(this).serialize(),function(res) {
				if (res.code == "0") {
					$("div.tagCloud").load("/api/tagCloud?itemid="+self.itemid.value);
					self.tag.value = "";
					$(self).slideUp(function() {
						$("#tagInput").show();
						var msg = $(self).prev(".messageArea").show().text(res.msg);
						window.setTimeout(function(){msg.fadeOut("slow",function() {msg.html("");});},5000);
					});
				} else {
					$(self).prev(".messageArea").show().text(res.msg);
				}
			},"json");
			//prevent default
			return false;
		});

		//reviewVote
		$(".reviewVote").submit(function() {
			var self = this;
			$(self).children("type=image").css("display","none");
			$(self).append("送信中です");
			$.post(this.action,$(this).serialize(),function(res) {
				if (res.code == "0") {
					$(self).html(res.msg);
					$(self).next("p.voted").find("span").text(res.result);
				} else {
					$(self).append($("span").text(res.msg));
				}
			},"json");
			//prevent default
			return false;
		});

		//reportSpam
		$(".reportSpam").submit(function() {
			var self = this;
			$.post(this.action,$(this).serialize(),function(res) {
				if (res.code == "0") {
					$(self).html(res.msg);
				} else {
					$(self).append($("span").text(res.msg));
				}
			},"json");
			//prevent default
			return false;
		});

		//review input
		$("#reviewInput").click(function() {
			var self = this;
			$("#reviewForm").slideDown(function() {$(self).hide();});
		});
		//reopen reviewForm if there are errors
		if ($("#hasErrors").is("ul"))
			$("#reviewInput").click();

	});

	//star rating based on http://examples.learningjquery.com/rating/
	/*************************************************
	create stars from select inside container.
	************************************************/
	cau1Lib.Class.rating = function(container, options) {
		var self = this;
		this.container = $(container);
		this.baseSelect = this.container.find("select:first").hide();
		this.values = $.map(this.baseSelect.find("option"), function(obj) {return $(obj).val();}).sort();
		this.stars = $([]);//empty jQuery object.
		this.defIdx = 0;
		this._createStars();
		this.checkHandler = function(elm) {self._check(elm)};
		this.drainHandler = function() {self._actionDrain()};
		this.fillHandler = function(elm) {self._actionFill(elm)};
		this.drainHandler = function() {self._actionDrain()};
		this.resetHandler = function() {self._actionReset()};
		// fix ie6 background flicker problem.
		if ($.browser.msie == true)
			try{document.execCommand('BackgroundImageCache', false, true);}catch(e){};
	}
	$.extend(cau1Lib.Class.rating.prototype,{
		_createStars: function() {
			var self = this;
			var maxVal = this.values[this.values.length-1];
			$.each(this.values, function(i,val) {
				if ( val == 0) {
					self.stars = self.stars.add($('<div class="cancel" title="Cancel">Cancel</div>').appendTo(self.container));
				} else {
					self.stars = self.stars.add(
						$('<div class="star" title="評価 '+val+'/'+maxVal+'">'+val+'</div>')
						.addClass("star-" + (val % 1? "left": "right"))
						.appendTo(self.container)
					);
				}
			});
			//attach events
			this.stars
			.mouseover(function() {
				self.drainHandler();
				self.fillHandler(this);
			})
			.mouseout(function() {
				self.drainHandler();
				self.resetHandler();
			})
			.click(function() {
				self.checkHandler(this);
			});
		},
		_check: function(elm) {
			var idx = this.stars.index(elm);
			var val = this.values[idx];
			this.baseSelect.val(val);
			this.defIdx = idx + 1;
			this.drainHandler();
			this.resetHandler();
		},
		_actionFill: function(elm){ // fill to the current mouse position.
			var idx = this.stars.index(elm) + 1;
			this.stars
			.slice(0,idx).addClass('hover').end();
		},
		_actionDrain: function() { // drain all the stars.
			this.stars
			.filter('.on').removeClass('on').end()
			.filter('.hover').removeClass('hover').end();
		},
		_actionReset: function(){ // Reset the stars to the default index.
			this.stars.slice(0,this.defIdx).addClass('on').end();
		}
	});
})(jQuery);

