var sfnt = {};

var SFNT_LOGIN_HTML = "<div id=login_popup><form onsubmit='tiph.user.login(V(\"_login_user_name\"), V(\"_login_password\"));return false;' class=simple><h2>请输入用户名密码</h2><table><tr><th>用户名:</th><td><input type=text id=_login_user_name></td></tr><tr><th>密码:</th><td><input type=password id=_login_password></td></tr><tr class=btns><td colspan=2><button onclick='tiph.user.login(V(\"_login_user_name\"), V(\"_login_password\"));return false;'>登录</button></td></tr></table></form></div>";
var SFNT_REGISTER_HTML = "<div id=login_popup><form onsubmit='tiph.user.register(V(\"_reg_user_name\"), V(\"_reg_email\"), V(\"_reg_password\"));return false;' class=simple><h2>注册新用户</h2><table><tr><th>用户名:</th><td><input type=text id=_reg_user_name></td></tr><tr><th>Email:</th><td><input type=text id=_reg_email></td></tr><tr><th>密码:</th><td><input type=password id=_reg_password></td></tr><tr><th>重复密码:</th><td><input type=password id=_reg_rpt_password></td></tr><tr class=btns><td colspan=2><button onclick='tiph.user.register(V(\"_reg_user_name\"), V(\"_reg_email\"), V(\"_reg_password\"));return false;'>注册</button></td></tr></table></form></div>";

sfnt.login = function() {
	tiph.ui.popup.open(SFNT_LOGIN_HTML);
};

sfnt.register = function() {
	tiph.ui.popup.open(SFNT_REGISTER_HTML);
};

sfnt.onlogin = function() {
	E("current_user").innerHTML = "<a href='http://cubexp.com/user' target=_blank><img src=\""+tiph.user.get_user().logo+"\" class=avator></a><div class=item>"+tiph.user.get_user().name+"</a></div><div class=item><a href='/user' target=_blank>个人中心</a></div><div class=item><a href='#' onclick=\"tiph.user.logout();return false;\">退出</a></div>";
	tiph.ui.popup.close();
}

sfnt.onlogout = function() {
	E("current_user").innerHTML = "<div class=item>您还没有登录</div><div class=item><a href='#' onclick=\"sfnt.login();return false;\">点击登录</a></div><div class=item><a href='http://www.cubexp.com' target=_blank>注册新用户</a></div>";
}

sfnt.flash = {};
sfnt.flash._disable_vote = false;
sfnt.flash._vote_notes = ["很差，完全是在浪费生命", "很差，完全是在浪费生命", "很差，完全是在浪费生命", "很差，完全是在浪费生命", "一般，不妨一看", "一般，不妨一看", "一般，不妨一看", "很好，获得大多数人喜爱的佳作", "很好，获得大多数人喜爱的佳作", "很完美，绝对不容错过"];

sfnt.flash.vote = function(id, score) {
	if (sfnt.flash._disable_vote) return;
	sfnt.flash._disable_vote = true;
	tiph.ajax.request("f/vote/" + id, {"score" : score}, function(data) {
		E("flash_score_big").innerHTML = parseInt(data.score / data.rates);
		var small = "." + (parseInt(data.score * 100 / data.rates) % 100) + "00";
		E("flash_score_small").innerHTML = small.substring(0, 3);
		E("flash_bigstars").style.width = parseInt(data.score * 10 / data.rates) + "%";
		E("flash_rates").innerHTML = data.rates;
		
		var p = parseInt(data.score / data.rates + 0.5);
		var desc = "很差，完全是在浪费生命";
		if (p > 1 && p <= 10) {
			desc =  sfnt.flash._vote_notes[p -1];
		} else  if (p > 10) {
			desc =  sfnt.flash._vote_notes[9];
		}
		E("vote_desc").innerHTML = desc;
	});
};

sfnt.flash.se_my_vote = function(score) {
	sfnt.flash.prevote(score);
	sfnt.flash._disable_vote = true;
}

sfnt.flash.add_fav = function(id) {
	if (tiph.user.get_user() == null) {
		alert("加入收藏前,请先登录!");
		sfnt.login();
		return;
	}
	tiph.ajax.request("f/fav/"+id, null, function(ret) {
		E("favflash").className="viewfav";
		E("favflash").onclick = function() {
			sfnt.flash.view_fav();
			return false;
		}
	});
}

sfnt.flash.view_fav = function() {
	window.open("http://cubexp.com/fav/flash");
}

sfnt.flash.prevote = function(score) {
	if (sfnt.flash._disable_vote) return;

	if (sfnt.flash._vote_timer) {
		clearTimeout(sfnt.flash._vote_timer);
		sfnt.flash._vote_timer = null;
	}
	if (score == 0) {
		sfnt.flash._vote_timer = setTimeout("sfnt.flash.prevote (-1);", 10);
		return;
	}
	
	if (score == -1) E("myvote_desc").innerHTML = "";
	else E("myvote_desc").innerHTML = sfnt.flash._vote_notes[score-1];
	E("smallscore").innerHTML = score <= 0 ? "" : (score + "");
	for (var i = 1; i <= 10; ++i) {
		E("vote" + i).className = score < i ? "" : "selected";
	}
};

sfnt.flash.check_post = function(src) {
	if (tiph.user.get_user() == null) {
		src.blur();
		sfnt.login();
	}
};

sfnt.flash.post = function(id, comment, btn) {
	if (tiph.user.get_user() == null) {
		alert("发表评论前,请先登录!");
		sfnt.login();
		return;
	}
	if (comment == "") {
		alert("请输入点评论再发吧~");
		return;
	}
	btn.disabled = true;
	tiph.ajax.request("f/post/" + id, {"comment" : comment}, function(data) {
		E("comments").innerHTML = data;
		btn.disabled = false;
	}, function(no, error) {tiph.ajax.defaultErrorHandler(no, error); btn.disabled = false;});
};

sfnt.flash.load_comment = function(id, page) {
	tiph.ajax.request("f/comment/" + id, {"page" : page}, function(data) {
		E("comments").innerHTML = data;
	});
}

try {
	tiph.user.attachLogin(sfnt.onlogin);
	tiph.user.attachLogout(sfnt.onlogout);
} catch (e) {}

