var floatUserInfoBox;
var searchKeyHint = "从你感兴趣的开始...";
window.addEvent('domready', function() {
	/* below this is js hack for IE6 */
	$$("#menu li").each(function(item) {
		item.addEvent("mouseenter", function() {
			this.addClass("lihover");
		});
		item.addEvent("mouseleave", function() {
			this.removeClass("lihover");
		});
	});
	$$("#menume_div p").each(function(item) {
		item.addEvent("mouseenter", function() {
			this.addClass("phover");
		});
		item.addEvent("mouseleave", function() {
			this.removeClass("phover");
		});
	});
	/* above is js hack for IE6 */
	if ($("s_value")) {
		$("s_value").addEvent("focus", function() {
			if (this.value == "输入关键词查找MV") {
				this.value = "";
				this.style.color = "#333333";
			}
		});
		$("s_value").addEvent("blur", function() {
			if (this.value == "") {
				this.value = "输入关键词查找MV";
				this.style.color = "#CCCCCC";
			}
		});
	}
	if ($("s_value2")) {
		$("s_value2").addEvent("focus", function() {
			if (this.value == "输入关键词查找MV") {
				this.value = "";
				this.style.color = "#333333";
			}
		});
		$("s_value2").addEvent("blur", function() {
			if (this.value == "") {
				this.value = "输入关键词查找MV";
				this.style.color = "#CCCCCC";
			}
		});
	}
	$$("a.user_head_image_plus").each(function(item) {
		item.fixpng();
	});
	$$("[hoverClass]").each(function(item) {
		item.addEvent("mouseenter", function(e) {
			this.addClass(item.getProperty("hoverClass"));
		});
		item.addEvent("mouseleave", function(e) {
			this.removeClass(item.getProperty("hoverClass"));
		});
	});
	// playlist image hover play menu
	$$("div.pl_image_box").each(function(item) {
		addPlaylistImageHover(item);
	});
	// video image hover play menu
	$$("div.video_image_box").each(function(item) {
		addVideoImageHover(item);
	});
	// album image hover menu
	$$("div.album_image_box").each(function(item) {
		addAlbumImageHover(item);
	});
	// album image hover menu
	$$("img.photo_small").each(function(item) {
		addPhotoSmallHover(item);
	});
	$$("img.photo_medium").each(function(item) {
		addPhotoMediumHover(item);
	});
	// user info box
	$$("[infoBoxIndex]").each(function(item) {
		var index = item.getProperty("infoBoxIndex");
		var plus = $("plus_" + index);
		if (plus) {
			item.addEvent("mouseenter", function() {
				plus.setStyle("display", "inline");
			});
			item.addEvent("mouseleave", function() {
				plus.setStyle("display", "none");
			});
		}
	});
	if ($("global_search_form")) {
		bindGlobalSearchForm($("global_search_form"));
	}
	if ($("global_search_form_bottom")) {
		bindGlobalSearchForm($("global_search_form_bottom"));
	}
});
function bindGlobalSearchForm(form) {
	var searchKeyText = $(form["key"]);
	searchKeyText.addEvent("focus", function() {
		if (this.value == searchKeyHint) {
			this.value = "";
			this.setStyle("color", "#333333");
		}
	}.bind(searchKeyText));
	searchKeyText.addEvent("click", function() {
		if (this.value == searchKeyHint) {
			this.value = "";
			this.setStyle("color", "#333333");
		}
	}.bind(searchKeyText));
	searchKeyText.addEvent("blur", function() {
		if (this.value == "") {
			this.value = searchKeyHint;
			this.setStyle("color", "#999999");
		}
	}.bind(searchKeyText));
}
function show_user_info(index) {
	if (floatUserInfoBox) {
		floatUserInfoBox.dispose();
	}
	var divId = "item_div_" + index;
	var userInfoDiv = "user_info_" + index;
	if ($(divId) && $(userInfoDiv)) {
		var position = $(divId).getPosition();
		floatUserInfoBox = new Element("div");
		floatUserInfoBox.set("html", $(userInfoDiv).get("html"));
		floatUserInfoBox.addEvent("mouseleave", function() {
			this.dispose();
		}.bind(floatUserInfoBox));
		floatUserInfoBox.setStyles({
			position: "absolute",
			overflow: "hidden",
			display: "block",
			width: "0px",
			height: "0px",
			"z-index": 100,
			left: position.x.toInt() - 2 + "px",
			top: position.y.toInt() - 2 + "px"
		});
		floatUserInfoBox.inject(document.body);
		new Fx.Morph(floatUserInfoBox, {duration: 300, transition: Fx.Transitions.Sine.easeIn}).start({
			'width': [0, 420],
			'height': [0, 220]
		}).start();
	}
}
function addPlaylistImageHover(imageDiv) {
	imageDiv.addEvent("mouseenter", function(e) {
		var floatMenu = this.getElement("div.pl_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "block";
		}
	});
	imageDiv.addEvent("mouseleave", function(e) {
		var floatMenu = this.getElement("div.pl_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "none";
		}
	});
}
function addVideoImageHover(imageDiv) {
	imageDiv.addEvent("mouseenter", function(e) {
		var floatMenu = this.getElement("div.video_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "block";
		}
	});
	imageDiv.addEvent("mouseleave", function(e) {
		var floatMenu = this.getElement("div.video_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "none";
		}
	});
}
function addAlbumImageHover(imageDiv) {
	imageDiv.addEvent("mouseenter", function(e) {
		var floatMenu = this.getElement("div.album_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "block";
		}
	});
	imageDiv.addEvent("mouseleave", function(e) {
		var floatMenu = this.getElement("div.album_image_float_menu");
		if (floatMenu) {
			floatMenu.style.display = "none";
		}
	});
}
function addPhotoSmallHover(image) {
	image.addEvent("mouseenter", function(e) {
		this.addClass("photo_small_hover");
	});
	image.addEvent("mouseleave", function(e) {
		this.removeClass("photo_small_hover");
	});
}
function addPhotoMediumHover(image) {
	image.addEvent("mouseenter", function(e) {
		this.addClass("photo_medium_hover");
	});
	image.addEvent("mouseleave", function(e) {
		this.removeClass("photo_medium_hover");
	});
}
Array.implement({
	explode: function(delimiter) {
		var output = "";
		this.each(function(item, index) {
			if (index > 0) {
				output += delimiter;
			}
			output += item.toString();
		});
		return output;
	}
});

function send_proposal(userId, friendId, userName) {
	if (!$defined(userId) || userId == 0) {
		login_ajax();
		return;
	}
	if (userId == friendId) {
		errorMessage("似乎没必要加自己为好友吧");
		return
	}
	var requestUrl = "/home/friend/send-proposal";
	var params = "friendId=" + friendId;
	new Request.JSON({
		url: requestUrl,
		method : 'post',
		data : params,
		onSuccess : function(action) {
			if (!action.error) {
				successMessage("好友请求已发送给 " + userName);
			} else {
				errorMessage(action.message);
			}
		}
	}).send();
}

function send_message(userId, friendId) {
	if (!$defined(userId) || userId == 0) {
		login_ajax();
		return;
	}
	showModalBoxAjax("发送短消息", "/home/message/ajax/write-message?friendId=" + friendId, 440, 200,
	                 function() {
		                 var form = this.getElement("form");
		                 form.ajaxJSON({
			                 onRequest: function() {
				                 var field = form.elements['content'];
				                 if (field.value.trim() == "") {
					                 errorMessage("请输入消息内容");
					                 field.focus();
					                 return false;
				                 }
				                 if (field.value.trim().length > 1000) {
					                 errorMessage("消息内容不能超过1000字");
					                 field.focus();
					                 return false;
				                 }
				                 return true;
			                 },
			                 onComplete: function(action) {
				                 if (!action.error) {
					                 successMessage("短消息发送成功");
					                 TB_remove();
				                 } else {
					                 errorMessage(action.message);
				                 }
			                 }
		                 });

	                 });
}
function follow(userId, friendId, userName) {
	if (!$defined(userId) || userId == 0) {
		login_ajax();
		return;
	}
	if (userId == friendId) {
		errorMessage("似乎没必要关注自己哦");
		return
	}
	var requestUrl = "/home/friend/follow";
	var params = "friendId=" + friendId;
	new Request.JSON({
		url: requestUrl,
		method : 'post',
		data : params,
		onSuccess : function(action) {
			if (!action.error) {
				successMessage("成功关注悦友 " + userName);
			} else {
				errorMessage(action.message);
			}
		}
	}).send();
}
function login_ajax() {
	showModalBoxAjax("登录音悦台", "/ajax/login-form", 440, 140, function() {
		var form = this.getElement("form");
		form.ajaxJSON({
			onRequest: function() {
				var field = form.elements['email'];
				if (field.value.trim() == "") {
					errorMessage("请输入email");
					field.focus();
					return false;
				}
				field = form.elements['password'];
				if (field.value.trim() == "") {
					errorMessage("请输入密码");
					field.focus();
					return false;
				}
				return true;
			},
			onComplete: function(action) {
				if (!action.error) {
					successMessage("登录成功");
					TB_remove();
					setTimeout(function () {
						document.location = document.location;
					}, 2000);
				} else {
					errorMessage(action.message);
				}
			}
		});
	});
}
function check_login(userId) {
	if (!$defined(userId) || userId == 0) {
		login_ajax();
		return false;
	}
	return true;
}
function show_feed_content(sessionId) {
	var link = $("feed_link_" + sessionId);
	var feedDiv = $("feed_content_" + sessionId);
	if (!link || !feedDiv) {
		return;
	}
	if (feedDiv.getStyle("display") == "none") {
		var requestUrl = "/home/message/read-message-session";
		var params = "sessionId=" + sessionId;
		new Request.HTML({
			url: requestUrl,
			method : 'post',
			data : params,
			update: feedDiv,
			onSuccess : function(action) {
			}
		}).send();
	}
	link.setStyle("font-weight", "normal");
	feedDiv.setStyle("display", "");
}
function show_notification_content(sessionId) {
	var link = $("notification_link_" + sessionId);
	var feedDiv = $("notification_content_" + sessionId);
	if (!link || !feedDiv) {
		return;
	}
	if (feedDiv.getStyle("display") == "none") {
		var requestUrl = "/home/message/read-message-session";
		var params = "sessionId=" + sessionId;
		new Request.JSON({
			url: requestUrl,
			method : 'post',
			data : params,
			onSuccess : function(action) {
			}
		}).send();
	}
	link.setStyle("font-weight", "normal");
	feedDiv.setStyle("display", "");
}
function onSearchFormSubmit() {
	var glolbaSearchForm = $("global_search_form");
	var searchKey = glolbaSearchForm["key"].value.trim();
	if (searchKey == searchKeyHint) {
		glolbaSearchForm["key"].value = "";
	}
	return true;
}
function showComplainDialog(userId) {
	if (!check_login(userId)) {
		return;
	}
	var dialog = showModalBox("请说明投诉团长的理由", "siteAdviceDiv", 400, 240);
	var content = dialog.getElement("form")['content'];
	content.value = "我要投诉**团长**";
	bindSiteAdviceDialog(dialog, "我要投诉**团长**");
}
function showAdviceDialog(userId) {
	if (!check_login(userId)) {
		return;
	}
	var dialog = showModalBox("欢迎留下你的意见，经采用后有积分奖励哦", "siteAdviceDiv", 400, 240);
	bindSiteAdviceDialog(dialog, "请在这里描述您遇到的问题...");
}
function bindSiteAdviceDialog(dialog, initContent) {
	var siteAdviceForm = dialog.getElement("form");
	var siteAdviceContent = $(siteAdviceForm['content']);
	siteAdviceContent.set("html", initContent);
	siteAdviceContent.addEvent("focus", function() {
		if (this.value == initContent) {
			this.value = "";
			this.setStyle("color", "#333333");
		}
	}.bind(siteAdviceContent));
	siteAdviceContent.addEvent("click", function() {
		if (this.value == initContent) {
			this.value = "";
			this.setStyle("color", "#333333");
		}
	}.bind(siteAdviceContent));
	siteAdviceContent.addEvent("blur", function() {
		if (this.value == "") {
			this.value = initContent;
			this.setStyle("color", "#999999");
		}
	}.bind(siteAdviceContent));
	siteAdviceForm.ajaxJSON({
		onRequest: function() {
			var field = siteAdviceForm.elements['content'];
			if (field.value.trim() == "" || field.value.trim() == initContent) {
				errorMessage("请输入您的意见或遇到的问题");
				field.focus();
				return false;
			}
			return true;
		},
		onComplete: function(action) {
			if (!action.error) {
				successMessage("您的意见已经提交成功，我们会尽快处理并通过站内消息回复。");
				TB_remove();
			} else {
				errorMessage(action.message);
			}
		}
	});
}
function executeInProgress(fn, end, start) {
	var start = start || 0;
	if (start == end) {
		return;
	}
	start = start - 0 + Math.ceil((end - start) / 10);
	fn(start);
	setTimeout(function() {
		executeInProgress(fn, end, start);
	}, 5);
}
function doSearch(keyword) {
	var form = $('hidden_search_form');
	form['key'].value = keyword;
	form.submit();
}
function checkTextareaChar(Element, msgBox, max, showLeftCharCount) {
	Element = typeof Element == 'object' ? Element : $(Element);
	msgBox = typeof msgBox == 'object' ? msgBox : $(msgBox);
	var uuid = 'textarea' + (+new Date());
	window[uuid] = setInterval(function() {
		if (!Element) {
			clearInterval(window[uuid]);
			return;
		}
		if (Element.value.length > max) {
			Element.value = Element.value.substr(0, max)
		}
		if (!showLeftCharCount) {
			msgBox.set('html', Element.value.length);
		} else {
			msgBox.set('html', max - Element.value.length);
		}
	}, 250);
	return uuid;
}
function checkFlash() {
	var hasFlash = 0;
	try {
		new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
		hasFlash = 1;
	} catch (e) {
		if (navigator.plugins && navigator.plugins.length > 0 && navigator.plugins["Shockwave Flash"]) {
			hasFlash = 1;
		}
	}
	return hasFlash;
}
function loadFlash(url, boxId, id, width, height, flashvars, write) {
	id = id || 'flashplayer';
	width = width || '100%';
	height = height || '100%';
	flashvars = flashvars || '';
	var flashCode;
	if (Browser.Engine.trident) {
		flashCode = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
		            'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" ' +
		            'width="' + width + '" height="' + height + '" id="' + id + '" name="' + id + '">' +
		            '<param name="allowScriptAccess" value="always" />' +
		            '<param name="allowFullScreen" value="true" />' +
		            '<param name="movie" value="' + url + '" />' +
		            '<param name="loop" value="false" />' +
		            '<param name="menu" value="false" />' +
		            '<param name="quality" value="high" />' +
		            '<param name="bgcolor" value="#000000" />' +
		            '<param name="flashvars" value="' + flashvars + '" />' +
		            '</object>';
	} else {
		flashCode = '<embed width="' + width + '" height="' + height + '" flashvars="' + flashvars +
		            '" bgcolor="#000000" allowfullscreen="true" allowscriptaccess="always" wmode="Opaque" id="' + id + '" name="' + id +
		            '" src="' + url + '" />';
	}
	if (!checkFlash()) {
		flashCode = '您的浏览器不支持Flash';
	}
	if (write) {
		document.write(flashCode);
	} else {
		window.addEvent('domready', function() {
			$(boxId).set('html', flashCode);
		});
	}
}
function initRichTextArea(textAreaId) {
	if (!tinyMCE) {
		alert("should include tinymce.js before site-common.js");
		return;
	}
	tinyMCE.init({
		// General options
		mode:"exact",
		elements:textAreaId,
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,link,forecolor,backcolor,emotions,image,media,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_fonts : '\u5B8B\u4F53=simsun;\u9ED1\u4F53=simhei;\u6977\u4F53=\u6977\u4F53;\u96B6\u4E66=\u96B6\u4E66;\u5E7C\u5706=\u5E7C\u5706;\u4EFF\u5B8B=\u4EFF\u5B8B_GB2312;\u5FAE\u8F6F\u96C5\u9ED1=microsoft yahei;Arial=arial,helvetica,sans-serif;Comic Sans MS=comic sans ms,sans-serif;Georgia=georgia,times new roman,times,serif;Courier New=courier new,courier,monospace;Impact=impact,chicago;Tahoma=tahoma,arial,helvetica,sans-serif;Verdana=verdana,geneva;',
		theme_advanced_font_sizes : "\u4E00\u53F7=26pt;\u5C0F\u4E00=24pt;\u4E8C\u53F7=22pt;\u5C0F\u4E8C=18pt;\u4E09\u53F7=16pt;\u5C0F\u4E09=15pt;\u56DB\u53F7=14pt;\u5C0F\u56DB=12pt;\u4E94\u53F7=10pt;\u5C0F\u4E94=9pt",
		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",
		language : "zh",
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",
		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
}