function initComment()
{
	if(GetCookie("uid")==null){
		$("#comment_submit_button").children("button").attr("onclick","");
		$("#comment_submit_button").children("button").html("登录后才能评论");
		var login = "location.href='/login_reg';return false;";
		$("#comment_box").hide()
		document.getElementById("comment_submit_button").onclick = new Function(login);
	}
}

function getComment(type,item,offset,max)
{	
	$("#comment_list").html("");
	$("#comment_loading").show();
	url = "/ajax/get_comment?type="+type+"&item="+item+"&offset="+offset+"&max="+max;
	$.ajax({type:"get",url:url,success:getCommentReturn});
}

function getCommentReturn(data)
{
	$("#comment_loading").hide();
	parseNode(data);
}

function addComment()
{ 	
	var number = GetCookie("uid");
	var item = $("#item_detail").val();
	var app = $("#app").val();
	
	var content = document.comment.content.value;
	if(content==""||content==null){
		alert("您还没有输入呢:)");
	    return false;
	}
	var re=/^\?(.*)(select%20|insert%20|delete%20from%20|count\(|drop%20table|update%20truncate%20|asc\(|mid\(|char\(|xp_cmdshell|exec%20master|net%20localgroup%20administrators|\"|:|net%20user|\'|%20or%20)(.*)$/gi;
	var e = re.test(content);
	if(e) {
	  alert("对不起，您的输入中含有非法字符!");
	  return false;
	}
	 
	$("#comment_hint").hide();
	$("#comment_submit").hide();
	$("#please_wait").show();
	var url="/elex/module/comment/comment_commit.jsp?item="+item+"&user="+number+"&app="+app;
	url += "&content="+encodeURIComponent(document.comment.content.value);
	$.ajax({type:"get",url:url,success:setComment});
	
	function setComment(data){
		parseNode(data);
	  	$("#comment_submit").show();
	    $("#please_wait").hide();
	     
	    document.comment.content.value = "";
	    $("#comment_box").hide();
	    $("#comment_submit").hide();
	}
}

function unfolder(){
	document.getElementById("comment").style.display =
	(document.getElementById("comment").style.display=="none")?"":"none";
}

function parseNode(data) {
	var xml = data;
  	var nodes=xml.getElementsByTagName("node");
  	for(var i=0 ; i<nodes.length ; i++){
  		var user_number = nodes[i].getElementsByTagName("user_number")[0].firstChild.nodeValue;
  		var user_name = nodes[i].getElementsByTagName("user_name")[0].firstChild.nodeValue;
  		var content = nodes[i].getElementsByTagName("content")[0].firstChild.nodeValue;
  		var create_time = nodes[i].getElementsByTagName("create_time")[0].firstChild.nodeValue;
  		var user_pic = nodes[i].getElementsByTagName("user_pic")[0].firstChild.nodeValue;
  		var innerHTML = "<div class='node'><a href='/people/"+user_number+"' class='profile'><img src='"
		    			+user_pic+"'/></a>"
		    			+"<div class='content'>"
		    			+"<div class='title'><a class='item_link_underline' href='/people/"+user_number+"'>"+user_name+"</a>&nbsp;<span class='item_light_text'>"+create_time+"说:</span></div>"
		    			+"<div class='description'>"+content+"</div>"
		    			+"</div></div>";
  		$("#comment_list").html($("#comment_list").html()+innerHTML);
  	}
}
function check_friend(friend_id,i)
{
	var uid = GetCookie("uid");
	if(uid != null && friend_id != null && uid != friend_id)
	{
		var url = "/is_friend?uid="+uid+"&creator="+friend_id;	
		var isFriend = $.ajax({type:"get",url: url,async: false}).responseText == "yes"?true:false;
		if(isFriend) {
			$("#is_friend"+i).show();
			$("#add_friend"+i).hide();
		}
		else {
			$("#add_friend"+i).show();
			$("#is_friend"+i).hide();
		}
	}
	else{
		$("#add_friend"+i).hide();
	}
}

function add_friend1(id1,id2)
{	
	var url = "/add_friend?uid="+id1+"&friend="+id2;
	var isFriend = $.ajax({url: url,async: true}).responseText == "yes"?true:false;
}

function add_friend(friend_id,i)
{
	var uid = GetCookie("uid");
	if(uid != null && friend_id != null && uid != friend_id)
	{
		var url = "/add_friend?uid="+uid+"&friend="+friend_id;
		var isFriend = $.ajax({url: url,async: false}).responseText == "yes"?true:false;
		if(isFriend)
		{
			$("#is_friend"+i).show();
			$("#add_friend"+i).hide();
		}
		else
		{
			alert("添加失败");
		}
	}
}

function del_friend(friend_id){
 	var con = confirm("您确定要删除该好友吗？");
 	if(con == true){
		var uid = GetCookie("uid");
		if(uid != null && friend_id != null && uid != friend_id)
		{
			var url = "/del_friend?uid="+uid+"&friend="+friend_id;
			var isFriend = $.ajax({url: url,async: false}).responseText == "yes"?true:false;
			if(isFriend)
			{
				window.location.reload();
			}
			else
			{
				alert("删除失败");
			}
		}
	}
}

function change_to_friend(){
	$("#display_invitor").hide();
	$("#display_friend").show();
}
function change_to_invitor(){
	$("#display_invitor").show();
	$("#display_friend").hide();
}function ResetPassword(uid)
{
	var url = "/reset_password?uid="+uid+"&new_password="+document.reset_password.new_password.value+"&new_confirm_password="+document.reset_password.new_confirm_password.value;
	var resetPassword = $.ajax({type:"get",url:url,success:reset_password_return});

	function reset_password_return(data)
	{
		if(resetPassword.responseText=="-1"){  
			alert("重设失败！");
		}else{
			var email = data.getElementsByTagName("email")[0].firstChild.nodeValue;
			var username = data.getElementsByTagName("username")[0].firstChild.nodeValue;
			setCookie(uid,username,document.reset_password.new_password.value,email);
			window.location.href = "http://"+window.location.hostname+"/reset_password_success/"+uid;
		}
	}
}




function hover(this_item) {
 	var one_block_attr = document.createAttribute("class");
	one_block_attr.value = "node_highlight";
	this_item.setAttributeNode(one_block_attr);
}
function nothover(this_item) {
	var one_block_attr = document.createAttribute("class");
	one_block_attr.value = "node";
	this_item.setAttributeNode(one_block_attr);
}
function changeImg(mypic, xw, xl) {
	var width = mypic.width;
	var height = mypic.height;
	var bili = width / height;
	var A = xw / width;
	var B = xl / height;
	if (!(A > 1 && B > 1)) {
		if (A < B) {
			mypic.width = xw;
			mypic.height = xw / bili;
		}
		if (A > B) {
			mypic.width = xl * bili;
			mypic.height = xl;
		}
	}
}
var page_id=null;
var page_onclick=null;
function setPage(id) {
	var pages = document.getElementsByName("page");
	var pageNumberCurrent = id.substring(5,id.length);
	for (var i = 0; i < pages.length; i++) {
		if(pageNumberCurrent>10 && i<(pageNumberCurrent-10)){
			pages[i].style.display="none";
			document.getElementById("page_front").style.display="block";
		}else{
			pages[i].style.display="block";
			if(pages[0].style.display=="block")
				document.getElementById("page_front").style.display="none";
			if(i>10 && i>pageNumberCurrent){
				pages[i].style.display="none";
				document.getElementById("page_back").style.display="block";
			}else{
				pages[i].style.display="block";
				document.getElementById("page_back").style.display="none";
			}
		}
		pages[i].className = "item_link_border";
	}
	if(page_id!=null && page_onclick!=null){
		document.getElementById(page_id).onclick=page_onclick;
	}
	document.getElementById(id).className = "item_link_current";
	page_id = id;
	page_onclick = document.getElementById(id).onclick ;
	document.getElementById(id).onclick = null;
	document.getElementById(id).blur();
}
var order = "create_time";

function set_current(order) {
		var current = $(".title>a[name=" + order + "]");
		current.removeAttr("href");
		current.attr("class","item_link_current");
}

function showhidediv(id){
  try{
    var sbtitle=document.getElementById(id);
    if(sbtitle){
      if(sbtitle.style.display=='block'){
        sbtitle.style.display='none';
      }else{
        sbtitle.style.display='block';
      }
    }
  }catch(e){}
}
function editProfileField(item){
	var first_id = item + "Lable";
	showhidediv(first_id);
	var next_id = item + "Input";
	showhidediv(next_id);
	
    var obj = document.getElementById(item+"text");
    var old = document.getElementById(item+"comment");
    obj.value = old.innerHTML;
}  
function saveProfileField(entry_id,item_id) 
{ 	
    var comment_hint =  entry_id + "hint";
    var comment_submit = entry_id + "SubmitButton";
    var please_wait =  entry_id + "wait";
    var comment_box =  entry_id + "text";
    var comment = entry_id +"comment";
	var number = GetCookie("uid");
	if(number == null){
		return;
	}
	
	obj = document.getElementById(comment_box); 
   	var content = obj.value;

   	if(content == null || content == "") {
		alert("您还没有输入呢:)");
	    return false;
   	}
   	
     var re=/^\?(.*)(select%20|insert%20|delete%20from%20|count\(|drop%20table|update%20truncate%20|asc\(|mid\(|char\(|xp_cmdshell|exec%20master|net%20localgroup%20administrators|\"|:|net%20user|\'|%20or%20)(.*)$/gi;	
     var e = re.test(content);
	if(e) {
	  alert("对不起，您的输入中含有非法字符!");
	  return false;
	}

	var obj = document.getElementById(please_wait); 
	
   	$("#"+comment_hint).hide();
	$("#"+comment_submit).hide();
	$("#"+please_wait).show();
		
    var url="/servlet_gateway/edit_entry_comment/"+ number +"/"+ entry_id +"/"+encodeURIComponent(content);
	
	$.ajax({type:"get",url:url,success:setComment});
	
	function setComment(data){  
		obj.value="";
		obj = document.getElementById(entry_id+"comment");
		obj.innerHTML = content;			
		editProfileField(entry_id);
		$("#"+comment_hint).hide();
	    $("#"+comment_submit).show();
	    $("#"+please_wait).hide();
	}
}
function showContent(id,cateId)
{
	top.location.href = "category_"+cateId+"/"+id;	
}
function scaleSwf(swfWidth /*Number*/,swfHeight /*Number*/)
{
	var swfObj = document.getElementById(type+"_1_0_0_0");
	swfObj.width = swfWidth;
	if(swfHeight < 480){
		swfObj.height = 480;
	}
	else{
		swfObj.height = swfHeight;
	}
}
function scrollSwf(swfHeight /*Number*/)
{  
	var swfObj = document.getElementById(type+"_1_0_0_0");
	
	//alert(swfObj.style.top);
	swfHeight  = Number(swfHeight) - swfObj.style.top;
	
 	window.scrollTo (0,swfHeight);
}

window.onbeforeunload = autoSave;
window.onunload = autoSave;

function autoSave()
{
	if(typeof(type) == "undefined") return;
	
	var swfObj = document.getElementById(type+"_1_0_0_0");
	if(swfObj == null) return;
	try{
	swfObj.autoSave();
	}catch(e){
	}
}

/*
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
 *
 * This is the integration file for JavaScript.
 *
 * It defines the FCKeditor class that can be used to create editor
 * instances in a HTML page in the client side. For server side
 * operations, use the specific integration system.
 */

// FCKeditor Class
var FCKeditor = function( instanceName, width, height, toolbarSet, value )
{
	// Properties
	this.InstanceName	= instanceName ;
	this.Width			= width			|| '100%' ;
	this.Height			= height		|| '200' ;
	this.ToolbarSet		= toolbarSet	|| 'Default' ;
	this.Value			= value			|| '' ;
	this.BasePath		= '/fckeditor/' ;
	this.CheckBrowser	= true ;
	this.DisplayErrors	= true ;

	this.Config			= new Object() ;

	// Events
	this.OnError		= null ;	// function( source, errorNumber, errorDescription )
}

FCKeditor.prototype.Version			= '2.5 Beta' ;
FCKeditor.prototype.VersionBuild	= '16848' ;

FCKeditor.prototype.Create = function()
{
	document.write( this.CreateHtml() ) ;
}

FCKeditor.prototype.CreateHtml = function()
{
	// Check for errors
	if ( !this.InstanceName || this.InstanceName.length == 0 )
	{
		this._ThrowError( 701, 'You must specify an instance name.' ) ;
		return '' ;
	}

	var sHtml = '<div>' ;

	if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
	{
		sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
		sHtml += this._GetConfigHtml() ;
		sHtml += this._GetIFrameHtml() ;
	}
	else
	{
		var sWidth  = this.Width.toString().indexOf('%')  > 0 ? this.Width  : this.Width  + 'px' ;
		var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
		sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ;
	}

	sHtml += '</div>' ;

	return sHtml ;
}

FCKeditor.prototype.ReplaceTextarea = function()
{
	if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
	{
		// We must check the elements firstly using the Id and then the name.
		var oTextarea = document.getElementById( this.InstanceName ) ;
		var colElementsByName = document.getElementsByName( this.InstanceName ) ;
		var i = 0;
		while ( oTextarea || i == 0 )
		{
			if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
				break ;
			oTextarea = colElementsByName[i++] ;
		}

		if ( !oTextarea )
		{
			alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
			return ;
		}

		oTextarea.style.display = 'none' ;
		this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
		this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
	}
}

FCKeditor.prototype._InsertHtmlBefore = function( html, element )
{
	if ( element.insertAdjacentHTML )	// IE
		element.insertAdjacentHTML( 'beforeBegin', html ) ;
	else								// Gecko
	{
		var oRange = document.createRange() ;
		oRange.setStartBefore( element ) ;
		var oFragment = oRange.createContextualFragment( html );
		element.parentNode.insertBefore( oFragment, element ) ;
	}
}

FCKeditor.prototype._GetConfigHtml = function()
{
	var sConfig = '' ;
	for ( var o in this.Config )
	{
		if ( sConfig.length > 0 ) sConfig += '&amp;' ;
		sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
	}

	return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
}

FCKeditor.prototype._GetIFrameHtml = function()
{
	var sFile = 'fckeditor.html' ;

	try
	{
		if ( (/fcksource=true/i).test( window.top.location.search ) )
			sFile = 'fckeditor.original.html' ;
	}
	catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }

	var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
	if (this.ToolbarSet) sLink += '&amp;Toolbar=' + this.ToolbarSet ;

	return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
}

FCKeditor.prototype._IsCompatibleBrowser = function()
{
	return FCKeditor_IsCompatibleBrowser() ;
}

FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
{
	this.ErrorNumber		= errorNumber ;
	this.ErrorDescription	= errorDescription ;

	if ( this.DisplayErrors )
	{
		document.write( '<div style="COLOR: #ff0000">' ) ;
		document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
		document.write( '</div>' ) ;
	}

	if ( typeof( this.OnError ) == 'function' )
		this.OnError( this, errorNumber, errorDescription ) ;
}

FCKeditor.prototype._HTMLEncode = function( text )
{
	if ( typeof( text ) != "string" )
		text = text.toString() ;

	text = text.replace(
		/&/g, "&amp;").replace(
		/"/g, "&quot;").replace(
		/</g, "&lt;").replace(
		/>/g, "&gt;") ;

	return text ;
}

function FCKeditor_IsCompatibleBrowser()
{
	var sAgent = navigator.userAgent.toLowerCase() ;

	// Internet Explorer 5.5+
	if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
	{
		var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
		return ( sBrowserVersion >= 5.5 ) ;
	}

	// Gecko (Opera 9 tries to behave like Gecko at this point).
	if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
		return true ;

	// Opera 9.50+
	if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.5 )
			return true ;

	// Safari 3+
	if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
		return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ;	// Build must be at least 522 (v3)

	return false ;
}
function dig(id){
	var uid = GetCookie("uid");
	if(uid == null){
		alert("请您先登录吧！");
		location.href = "/login_reg";
		return false;
	}
	var type="dig";
	$("#"+id).children(".button").children(".dig").attr("src","/elex/img/dig/dig2.gif");
	$("#"+id).children(".button").children(".dig").attr("onclick","");
	$("#"+id).children(".button").children(".dig").attr("onmouseover","");
	$("#"+id).children(".button").children(".dig").attr("onmouseout","");
	$("#"+id).children(".button").children(".bury").attr("onclick","");
	$("#"+id).children(".button").children(".bury").attr("onmouseover","");
	$("#"+id).children(".button").children(".bury").attr("onmouseout","");
	var url = "/ajax/dig?type="+type+"&id="+id+"&uid="+uid;
	var isDig = $.ajax({url: url,async: false}).responseText == "yes"?true:false;
	if(isDig){
		var dig=parseInt($("#"+id).children(".data").children(".dig").text())+1;
		$("#"+id).children(".data").children(".dig").empty();
		$("#"+id).children(".data").children(".dig").append(dig.toString());
		var sum=parseInt($("#"+id).children(".data").children(".sum").text())+1;
		$("#"+id).children(".data").children(".sum").empty();
		$("#"+id).children(".data").children(".sum").append(sum.toString());
	}
	else
	{
		alert("Dig失败!");
	}
}
function bury(id){
	var uid = GetCookie("uid");
	if(uid == null){
		alert("请您先登录吧！");
		location.href = "/login_reg";
		return false;
	}
	var type="bury";
	$("#"+id).children(".button").children(".bury").attr("src","/elex/img/dig/bury2.gif");
	$("#"+id).children(".button").children(".bury").attr("onclick","");
	$("#"+id).children(".button").children(".bury").attr("onmouseover","");
	$("#"+id).children(".button").children(".bury").attr("onmouseout","");
	$("#"+id).children(".button").children(".dig").attr("onclick","");
	$("#"+id).children(".button").children(".dig").attr("onmouseover","");
	$("#"+id).children(".button").children(".dig").attr("onmouseout","");
	var url = "/ajax/dig?type="+type+"&id="+id+"&uid="+uid;
	var isDig = $.ajax({url: url,async: false}).responseText == "yes"?true:false;
	if(isDig){
		var bury=parseInt($("#"+id).children(".data").children(".bury").text())+1;
		$("#"+id).children(".data").children(".bury").empty();
		$("#"+id).children(".data").children(".bury").append(bury.toString());
		var sum=parseInt($("#"+id).children(".data").children(".sum").text())-1;
		$("#"+id).children(".data").children(".sum").empty();
		$("#"+id).children(".data").children(".sum").append(sum.toString());
	}
	else
	{
		alert("Bury失败!");
	}
}