/**
 * Home user panel show
 *
 */

uutuu.Namespace("uutuu.App");

uutuu.App.Export("RankScore");

/**
 * Implements home user panel.
 *
 * Supportted options including:
 * noLoginId:	required
 * LoginId: required
 * user:required
 */
uutuu.Widget.RankScore = uutuu.Class({
	name: "uutuu.Widget.RankScore",
	construct: function(target, options) {
		this.target = $(target);
		this.entry = null;
		this.options = {
			nid: 0,
			needSync : false,
			voteOnStarClicked : false,
			score: 3,
			isFloat: false,
			tpl: '',
			url: js_context.base_url + 'service/score/mark/',
			callback: null,
			event_callback: null
		};
		this.initialize(options);
	},
	methods: {
		initialize: function(options) {
			for (var key in options)
				this.options[key] = options[key];
			
			var obj = this;

			if (this.options.isFloat)
			{
				this.entry = this.target;
				$(document.body).append("<div id=\"addCommentFloat\" class=\"block_score_float\" style=\"position:absolute;display:none;z-index:999;\">" + this.options.tpl + "</div>");
				this.target = $("#addCommentFloat");
				uutuu.util.locate(this.target[0], this.entry[0], "bottom-right", "right-down", 0, -180);
				
				$(this.entry).click(function(){
					if (_login_uid == 0)
					{
						uutuu.util.login({callback:function(sender,args){
							if (js_context && js_context.user && (typeof js_context.user.uid != "undefined"))
								js_context.user.uid = args.uid;
							_login_uid=args.uid;
							uutuu.UI.Dialog.getMessageBox().close();
							uutuu.App.User.instance.renewnav(false);
							if (obj.target.css("display") == "none")
								obj.target.css("display", "");
							else
								obj.target.css("display", "none");
						}});
					}
					else
					{
						if (obj.target.css("display") == "none")
							obj.target.css("display", "");
						else
							obj.target.css("display", "none");
					}
				});
				
				$("#closeBtn", this.target).click(function(evt){
					evt.preventDefault();
					obj.target.css("display", "none");
				});
				
				$("#submitBtn", this.target).click(function(evt){
					evt.preventDefault();
					obj.target.css("display", "none");
					uutuu.Widget.RankScore.voteRank(obj.target, obj.options.nid, 0, obj.options.callback);
				});
				
				$("#rs_radio_" + this.options.score, this.target).attr("checked", true);
				$(".current-rating", this.target).css("width", (this.options.score * 20 ) + "%").attr("curWidth", (this.options.score * 20 ) + "%");
			}			
			
			if (this.options.needSync)
				uutuu.Widget.RankScore.NEED_SYNC.push(this.target);
					
			$(":radio", this.target).click(function(){
				$("#rs_radio_" + this.value, obj.target).attr("checked", true);
				$(".current-rating", obj.target).css("width", (this.value * 20) + "%").attr("curWidth", (this.value * 20 ) + "%").addClass("clicked");
				if (obj.options.needSync)
					for (var i = 0; i < uutuu.Widget.RankScore.NEED_SYNC.length; i++)
					{
						var target = uutuu.Widget.RankScore.NEED_SYNC[i];
						$(".current-rating", target).css("width", (this.value * 20) + "%").attr("curWidth", (this.value * 20 ) + "%").addClass("clicked");
						$("#rs_radio_" + this.value, target).attr("checked", true);
					}
			});
			
			var stars  = [".one-star", ".two-stars", ".three-stars", ".four-stars", ".five-stars"];			
			for (var i = 0; i < stars.length; i++)
			{
				$(stars[i], this.target).mouseover(function(){
					var curShow = $(".current-rating", obj.target);
					if (!curShow.attr("curWidth"))
						curShow.attr("curWidth", curShow.css("width"));
					var rankvalue = parseInt($(this).html());
					var width = rankvalue * 20;
					curShow.css("width", width + "%").addClass("hover");
				}).mouseout(function(){
					var curShow = $(".current-rating", obj.target);
					curShow.removeClass("hover").css("width", curShow.attr("curWidth"));
				}).click(function(evt){
					evt.preventDefault();
					var rankvalue = parseInt($(this).html());
					var width = rankvalue * 20;
					$(".current-rating", obj.target).css("width", width + "%").attr("curWidth", width + "%").addClass("clicked");
					$("#rs_radio_" + rankvalue, obj.target).attr("checked", true);
					if (obj.options.needSync)
					{
						for (var i = 0; i < uutuu.Widget.RankScore.NEED_SYNC.length; i++)
						{
							var target = uutuu.Widget.RankScore.NEED_SYNC[i];
							$(".current-rating", target).css("width", width + "%").attr("curWidth", width + "%").addClass("clicked");
							$("#rs_radio_" + rankvalue, target).attr("checked", true);
						}
					}
					
					if (obj.options.voteOnStarClicked)
					{
						uutuu.Widget.RankScore.voteRank({	target: target, 
															nid: obj.options.nid, 
															rankScore: rankvalue, 
															callback: obj.options.callback,
															event_callback: obj.options.event_callback,
															url: obj.options.url});
					}
				});
			}
		}
	}/*,
	statics: {
		NEED_SYNC: [],
		voteRank: function(target, nid, rankScore, callback) {
			if (_login_uid == 0) {
				uutuu.util.login({callback:function(sender,args){
					if (js_context && js_context.user && (typeof js_context.user.uid != "undefined"))
						js_context.user.uid = args.uid;
					_login_uid=args.uid;
					uutuu.App.User.instance.renewnav(false);
					uutuu.Widget.RankScore.voteRank(target, nid, rankScore, callback);
				}});
			}
			else
			{
				uutuu.UI.Dialog.message("正在保存信息，请稍候...");
				
				if (rankScore == 0)
				{
					rankScore = 3;
					for (var i = 0; i < $(":input[name='rank_score']", target).length; i++)
					{
						if ($(":input[name='rank_score']", target)[i].checked == true)
						{
							rankScore = $(":input[name='rank_score']", target)[i].value;
							break;
						}
					}
				}
				
				$.ajax({
					url: js_context.base_url + 'service/score/mark/',
					type: 'POST',
					data: "score=" + rankScore + "&uid=" + _login_uid + "&nid=" + nid,
					dataType: 'json',
					timeout: 30000,
					error: function(request, type, ex){},
					success: function(data){
						if (data.success == true)
						{
							uutuu.UI.Dialog.getMessageBox().close();
							if (callback)
								callback(nid, data.rank.rank_count, data.rank.rank_score);
						}
						else
						{
							uutuu.UI.Dialog.alert(r.msg);
						}
			   		}
			   	});
			}
		}
	}*/
	,
	statics: {
		NEED_SYNC: [],
		voteRank: function(opt) {
			if (_login_uid == 0) {
				uutuu.util.login({callback:function(sender,args){
					if (js_context && js_context.user && (typeof js_context.user.uid != "undefined"))
						js_context.user.uid = args.uid;
					_login_uid=args.uid;
					uutuu.App.User.instance.renewnav(false);
					uutuu.Widget.RankScore.voteRank(opt);
				}});
			}
			else
			{
				uutuu.UI.Dialog.message("正在保存信息，请稍候...");
				
				if (opt.rankScore == 0)
				{
					opt.rankScore = 3;
					for (var i = 0; i < $(":input[name='rank_score']", opt.target).length; i++)
					{
						if ($(":input[name='rank_score']", opt.target)[i].checked == true)
						{
							opt.rankScore = $(":input[name='rank_score']", opt.target)[i].value;
							break;
						}
					}
				}
				
				$.ajax({
					url: opt.url,
					type: 'POST',
					data: "score=" + opt.rankScore + "&uid=" + _login_uid + "&nid=" + opt.nid,
					dataType: 'json',
					timeout: 30000,
					error: function(request, type, ex){},
					success: function(data){
						if (data.success == true)
						{
							uutuu.UI.Dialog.getMessageBox().close();
							if (opt.callback)
								opt.callback(opt.nid, data.rank.rank_count, data.rank.rank_score);
							if(opt.event_callback)
								opt.event_callback(opt.nid, data);
						}
						else
						{
							uutuu.UI.Dialog.alert(data.message);
						}
			   		}
			   	});
			}
		}
	}
});

jQuery.fn.RankScore = function(options) {
	return this.each(
		function() {
			var copy = {};
			for(var property in options)
				copy[property] = options[property];

			var rs = new uutuu.Widget.RankScore(this, copy);
		}
	);
};
