/*-------------------------------------------------------------------------------------------------------------------
 Script Name: www.js
 Author: Tony Wei (魏志國)  tonywei123_at_gmail.com
 Description: 首頁
 Revision History:
   1.0: original version 2011/6/3
-------------------------------------------------------------------------------------------------------------------*/


//----------------------------------------------------------------------
// 核心整體變數
//----------------------------------------------------------------------
var main_mobile = "0";
if ( location.href.indexOf( "http://09" ) > -1 ) {
	main_mobile = location.href.substr( 7, 10 );
} else {
	if ( location.href.indexOf( "/09" ) > -1 ) {
		main_mobile = location.href.substr( location.href.indexOf( "/09" ) + 1, 10 );
	} else {
		if ( location.href.indexOf( "mobile=09" ) > -1 ) {
			main_mobile = location.href.substr( location.href.indexOf( "mobile=09" ) + 7, 10 );
		}
	}
}

// 手機版
var is_phone = false;
var phone_parameter = "";

// 快速刪除模式( 關閉中 )
var is_fast_remove = false;


//----------------------------------------------------------------------
// 登入識別用整體變數
//----------------------------------------------------------------------
var LT_Token		= "";
var LT_Identity		= "";
var LT_FKV			= "";
var LT_Name		= "";



//----------------------------------------------------------------------
// 優先執行區
//----------------------------------------------------------------------
$( window ).bind( "unload", function() {
	window.scrollTo( 0, 0 );
} );


//----------------------------------------------------------------------
// 登入成功
//----------------------------------------------------------------------
function login_ok()
{
	var sName = LT_Name;
	if ( LT_Identity == "2" ) {
		sName = LT_Name.substr( 11 );
		sName = sName.substr( sName.indexOf( " " ) + 1 );
	}
	$( "#a_logout" ).remove();
	$( "A.LOGIN" ).hide().after( "<a id='a_logout' href='javascript:logout();'>" + sName + " 您好　登出</a>" );
	if ( LT_Identity == "2" ) {
		var sMobile	= LT_Name.substr( 0, 10 );
		var sPort		= ( location.href.indexOf( ":8080" ) > -1 ) ? ":8080" : "";
		var sUrl			= "http://" + sMobile + ".21housebook.com.tw" + sPort;
		$( "#li_home" ).html( "<a href='" + sUrl + "'>回到個人首頁</a>" ).show();
		if ( sMobile == main_mobile ) {
			$( "#a_profile" ).show();
		}
		if ( is_phone && main_mobile == "0" ) {
			$( "UL.TAB_SHORT LI" ).eq( 0 ).css( "width", "50%" );
			$( "UL.TAB_SHORT" ).append( "<li><a href='" + sUrl + "/phone/'>經紀人櫥窗</a></li>" );
		}
	}
}

//----------------------------------------------------------------------
// 登出
//----------------------------------------------------------------------
function logout()
{
	$.cookie( "MI_ID",			null,	{ domain: sDomain } );
	$.cookie( "LT_Token",	null,	{ domain: sDomain } );
	$.cookie( "LT_Identity",	null,	{ domain: sDomain } );
	$.cookie( "LT_FKV",			null,	{ domain: sDomain } );
	$.cookie( "LT_Name",	null,	{ domain: sDomain } );
	/*
	$.cookie( "MI_ID",			null );		// 向下相容，清除測試版時留下的 Cookie
	$.cookie( "LT_Token",	null );
	$.cookie( "LT_Identity",	null );
	$.cookie( "LT_FKV",			null );
	$.cookie( "LT_Name",	null );
	*/
	$.cookie( "LT_Token",	  "*",	{ expires: 90, domain: sDomain } );
	location.reload( true );
}

//----------------------------------------------------------------------
// 登入狀態檢查
//----------------------------------------------------------------------
function login_check()
{
	$.post( "/index.aspx?fn=login_check&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].ok ) == "undefined" ) {
				if ( $.cookie( "MI_ID" ) ) {
					relogin();	// 自動重新登入
				} else {
					if ( LT_Token != "" ) {
						//alert( "1--" + LT_Token );
						logout();
					}
				}
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 自動重新登入
//----------------------------------------------------------------------
function relogin()
{
	$.post( "/index.aspx?fn=member_relogin&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
				//alert( data.objlist[ 0 ].msg );
				if ( LT_Token != "" ) {
					//alert( "2--" + LT_Token );
					logout();
				}
			} else {
				LT_Token		= data.objlist[ 0 ].LT_Token;
				LT_Identity	= data.objlist[ 0 ].LT_Identity;
				LT_FKV			= data.objlist[ 0 ].LT_FKV;
				LT_Name		= data.objlist[ 0 ].LT_Name;
				$.cookie( "LT_Token",	LT_Token,		{ expires: 90, domain: sDomain } );
				$.cookie( "LT_Identity",	LT_Identity,	{ expires: 90, domain: sDomain } );
				$.cookie( "LT_FKV",			LT_FKV,			{ expires: 90, domain: sDomain } );
				$.cookie( "LT_Name",	LT_Name,		{ expires: 90, domain: sDomain } );
				login_ok();
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 讀取貼文
//----------------------------------------------------------------------
function object_load( sort_method )
{

	$.post( "/index.aspx?fn=object" + phone_parameter + "&top=20&main_mobile=" + main_mobile + "&sort=" + sort_method + "&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data == null ) {
			alert( "AJAX Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {
				$( "#span_object_list" ).html( data );

				// 自動讀取下方貼文
				// $.autopager();

				// 配置貼文效果
				assign_effect();
			}
		}
	} );

}

//----------------------------------------------------------------------
// 載入先前的資訊 2011/8/15
//----------------------------------------------------------------------
function object_load_next( sort_method, iLastBO_Serial )
{

	$( "#div_next" + iLastBO_Serial ).hide();

	$.post( "/index.aspx?fn=object" + phone_parameter + "&last=" + iLastBO_Serial + "&top=20&main_mobile=" + main_mobile + "&sort=" + sort_method + "&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data == null ) {
			alert( "AJAX Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {

				$( "#span_object_list" ).html( $( "#span_object_list" ).html() + data );

				// 自動讀取下方貼文
				// $.autopager();

				// 配置貼文效果
				assign_effect();
			}
		}
	} );

}

//----------------------------------------------------------------------
// 讀取第一則貼文
//----------------------------------------------------------------------
function object_load_first()
{

	$.post( "/index.aspx?fn=object" + phone_parameter + "&first=true&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data == null ) {
			alert( "AJAX Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {
				$( "#span_object_list" ).prepend( data );

				// 配置貼文效果
				assign_effect();
			}
		}
	} );

}

//----------------------------------------------------------------------
// 讀取單一篇貼文
//----------------------------------------------------------------------
function object_load_one( BO_Serial )
{

	$.post( "/index.aspx?fn=object" + phone_parameter + "&top=1&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ), function( data ) {
		if ( data == null ) {
			alert( "AJAX Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {
				$( "#span_object_list" ).prepend( data );

				// 配置貼文效果
				assign_effect();
			}
		}
	} );

}

//----------------------------------------------------------------------
// 讀取留言
//----------------------------------------------------------------------
function object_comment_load( BO_Serial )
{

	$.post( "/index.aspx?fn=object_comment_load" + phone_parameter + "&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ), function( data ) {
		if ( data == null ) {
			alert( "AJAX Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {
				$( "#ul" + BO_Serial ).html( data );

				// 配置貼文效果
				assign_effect();
			}
		}
	} );

}

//----------------------------------------------------------------------
// 刪除貼文或留言
//----------------------------------------------------------------------
function object_remove( div_object )
{
	var oid = $( div_object ).attr( "id" );
	if ( oid.indexOf( "div_object" ) > -1 ) {
		var BO_Serial = oid.replace( "div_object", "" );
		$.post( "/index.aspx?fn=object_remove&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ) );
		$( "#div_object" + BO_Serial ).remove();
	} else {
		var BC_Serial = oid.replace( "p_comment", "" );

		// 更新「回覆」次數
		var $cc = $( "#li_comment" + BC_Serial ).parent().parent().find( "SPAN[id*=span_cc]" );
		$cc.text( parseInt( $cc.text() ) - 1 );

		$.post( "/index.aspx?fn=object_remove&rand=" + Math.random(), prepare_data( { "BC_Serial":BC_Serial } ) );
		$( "#li_comment" + BC_Serial ).remove();
	}
}

//----------------------------------------------------------------------
// 配置貼文效果
//----------------------------------------------------------------------
function assign_effect()
{

	// 移除最下方的分隔線
	$( "DIV.MSG_ITEM" ).removeClass( "LAST" ).last().addClass( "LAST" );

	is_fast_remove = false;		// 關閉快速刪除模式

	// 清空刪除按鈕
	$( ".CLOSE" ).remove();

	//----------------------------------------------------------------------
	// 貼文 -- 刪除按鈕選單
	if ( LT_Identity == "1" ) {	// 系統管理員

		// 刪除按鈕
		$( ".ro" ).html( "<a href='javascript:void(0);' class='CLOSE'><img src='/images/btn_close.gif' width='11' height='11' /></a>" );
		
		if ( is_phone ) {

			$( "A.CLOSE" ).bind( "click", function() {
				object_remove( $( this ).parent().parent() );
			} );

		} else {

			$( ".CLOSE" ).contextMenu( "menu2", {
				bindings: {
					"remove": function( t ) {
						object_remove( $( t ).parent().parent() );
					},
					"fast_remove": function( t ) {
						object_remove( $( t ).parent().parent() );
						is_fast_remove = true;		// 開啟「快速刪除」模式
					},
					"block_user": function( t ) {
						var BO_Serial = $( t ).parent().parent().attr( "id" ).replace( "div_object", "" );
						var bIsDeleteAll = false;
						if ( confirm( "是否一併刪除此發佈者的「所有貼文」?" ) ) {
							if ( confirm( "Double Confirm：確定要刪除他(她)的「所有貼文」?" ) ) {
								bIsDeleteAll = true;
								$.post( "/index.aspx?fn=object_remove_all&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ), function( data ) {
									if ( data == null ) {
										// alert( "JSON Error!" );
									} else {
										if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
											alert( data.objlist[ 0 ].msg );
										} else {
											// 讀取貼文
											object_load( "new" );
										}
									}
								}, "json" );
							}
						}
						$.post( "/index.aspx?fn=block_add&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ), function( data ) {
							if ( data == null ) {
								// alert( "JSON Error!" );
							} else {
								if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
									alert( data.objlist[ 0 ].msg );
								} else {
									if ( ! bIsDeleteAll ) {
										object_remove( $( t ).parent().parent() );
									}
								}
							}
						}, "json" );
					},
					"block_ip": function( t ) {
						var BO_Serial = $( t ).parent().parent().attr( "id" ).replace( "div_object", "" );
						$.post( "/index.aspx?fn=block_add&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial, "BB_IP":"1" } ), function( data ) {
							if ( data == null ) {
								// alert( "JSON Error!" );
							} else {
								if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
									alert( data.objlist[ 0 ].msg );
								} else {
									object_remove( $( t ).parent().parent() );
								}
							}
						}, "json" );
					}
				}, 
				onContextMenu: function( e ) {
					if ( is_fast_remove ) {
						object_remove( $( e.target ).parent().parent().parent() );
						return false;
					} else {
						return true;
					}
				}
			} );
			
		} // End of if ( is_phone )

	} else {		// 其他身份

		// 刪除按鈕
		$( ".ro_" + LT_Identity + "_" + LT_FKV + ", .ro2_" + LT_Token ).html( "<a href='javascript:void(0);' class='CLOSE'><img src='/images/btn_close.gif' width='11' height='11' /></a>" );
		
		if ( is_phone ) {

			$( "A.CLOSE" ).bind( "click", function() {
				object_remove( $( this ).parent().parent() );
			} );

		} else {

			$( ".CLOSE" ).contextMenu( "menu1", {
				bindings: {
					"remove": function( t ) {
						object_remove( $( t ).parent().parent() );
					},
					"fast_remove": function( t ) {
						object_remove( $( t ).parent().parent() );
						is_fast_remove = true;		// 開啟「快速刪除」模式
					}
				}, 
				onContextMenu: function( e ) {
					if ( is_fast_remove ) {
						object_remove( $( e.target ).parent().parent().parent() );
						return false;
					} else {
						return true;
					}
				}
			} );

		} // End of if ( is_phone )
		
	}

	//----------------------------------------------------------------------
	// 留言 -- 刪除按鈕選單
	if ( LT_Identity == "1" ) {	// 系統管理員

		// 刪除按鈕
		$( ".rc" ).html( "<a href='javascript:void(0);' class='CLOSE2'><img src='/images/btn_close.gif' width='11' height='11' /></a>" );
		
		if ( is_phone ) {

			$( "A.CLOSE2" ).bind( "click", function() {
				object_remove( $( this ).parent().parent() );
			} );

		} else {

			$( ".CLOSE2" ).contextMenu( "menu2", {
				bindings: {
					"remove": function( t ) {
						object_remove( $( t ).parent().parent() );
					},
					"fast_remove": function( t ) {
						object_remove( $( t ).parent().parent() );
						is_fast_remove = true;		// 開啟「快速刪除」模式
					},
					"block_user": function( t ) {
						var BC_Serial = $( t ).parent().parent().attr( "id" ).replace( "p_comment", "" );
						var bIsDeleteAll = false;
						if ( confirm( "是否一併刪除此發佈者的「所有貼文」?" ) ) {
							if ( confirm( "Double Confirm：確定要刪除他(她)的「所有貼文」?" ) ) {
								bIsDeleteAll = true;
								$.post( "/index.aspx?fn=object_remove_all&rand=" + Math.random(), prepare_data( { "BC_Serial":BC_Serial } ), function( data ) {
									if ( data == null ) {
										// alert( "JSON Error!" );
									} else {
										if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
											alert( data.objlist[ 0 ].msg );
										} else {
											// 讀取貼文
											object_load( "new" );
										}
									}
								}, "json" );
							}
						}
						$.post( "/index.aspx?fn=block_add&rand=" + Math.random(), prepare_data( { "BC_Serial":BC_Serial } ), function( data ) {
							if ( data == null ) {
								// alert( "JSON Error!" );
							} else {
								if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
									alert( data.objlist[ 0 ].msg );
								} else {
									if ( ! bIsDeleteAll ) {
										object_remove( $( t ).parent().parent() );
									}
								}
							}
						}, "json" );
					},
					"block_ip": function( t ) {
						var BC_Serial = $( t ).parent().parent().attr( "id" ).replace( "p_comment", "" );
						$.post( "/index.aspx?fn=block_add&rand=" + Math.random(), prepare_data( { "BC_Serial":BC_Serial, "BB_IP":"1" } ), function( data ) {
							if ( data == null ) {
								// alert( "JSON Error!" );
							} else {
								if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
									alert( data.objlist[ 0 ].msg );
								} else {
									object_remove( $( t ).parent().parent() );
								}
							}
						}, "json" );
					}
				}, 
				onContextMenu: function( e ) {
					if ( is_fast_remove ) {
						object_remove( $( e.target ).parent().parent().parent() );
						return false;
					} else {
						return true;
					}
				}
			} );

		} // End of if ( is_phone )

		
	} else {		// 其他身份

		// 刪除按鈕
		$( ".rc_" + LT_Identity + "_" + LT_FKV + ", .rc2_" + LT_Token ).html( "<a href='javascript:void(0);' class='CLOSE2'><img src='/images/btn_close.gif' width='11' height='11' /></a>" );
		
		if ( is_phone ) {

			$( "A.CLOSE2" ).bind( "click", function() {
				object_remove( $( this ).parent().parent() );
			} );

		} else {

			$( ".CLOSE2" ).contextMenu( "menu1", {
				bindings: {
					"remove": function( t ) {
						object_remove( $( t ).parent().parent() );
					},
					"fast_remove": function( t ) {
						object_remove( $( t ).parent().parent() );
						is_fast_remove = true;		// 開啟「快速刪除」模式
					}
				}, 
				onContextMenu: function( e ) {
					if ( is_fast_remove ) {
						object_remove( $( e.target ).parent().parent().parent() );
						return false;
					} else {
						return true;
					}
				}
			} );

		} // End of if ( is_phone )
		
	}
	//----------------------------------------------------------------------

	// 手機版，不提供 fancybox 效果
	if ( is_phone ) {
		return;
	}

	// 圖片
	$( ".BO_Pic" ).fancybox( {
		'titleShow'		: false,
		'transitionIn'	:	'elastic',
		'transitionOut'	: 'elastic'
	} );

	// 影音
	$( ".BO_YoutubeThumb" ).fancybox( {
		'width'					: 427,
		'height'					: 366,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

	// 回覆
	$( ".REPLY" ).fancybox( {
		'width'					: 560,
		'height'					: 270,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

	// 轉貼
	$( ".POST" ).fancybox( {
		'width'					: 560,
		'height'					: 310,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

	// 影音看屋
	$( ".PLAYER" ).fancybox( {
		'width'					: 525,
		'height'					: 525,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

	// MAP
	$( ".MAP" ).fancybox( {
		'width'					: 700,
		'height'					: 430,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

	// 街景
	$( ".STREET" ).fancybox( {
		'width'					: 700,
		'height'					: 430,
		'autoScale'			: false,
		'transitionIn'			: 'none',
		'transitionOut'		: 'none',
		'type'						: 'iframe'
	} );

}

//----------------------------------------------------------------------
// 新增貼文
//----------------------------------------------------------------------
function object_add( parameters_hash )
{
	$.post( "/index.aspx?fn=object_add&rand=" + Math.random(), prepare_data( parameters_hash ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
				// alert( data.objlist[ 0 ].msg );
			} else {
				// 讀取最新一筆資料，並顯示於塗鴉牆
				object_load_first();

				/*
				if ( parameters_hash[ "BO_Pic1" ] != null ) {
					$( ".SHARE_IMG" ).eq( 0 ).trigger( "click" );
				}

				if ( parameters_hash[ "BO_LinkUrl" ] != null || parameters_hash[ "BO_YoutubeTag" ] != null ) {
					$( ".SHARE_LINK" ).eq( 0 ).trigger( "click" );
				}
				*/
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 點推
//----------------------------------------------------------------------
function object_like( BO_Serial )
{
	$.post( "/index.aspx?fn=object_like&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial } ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
				// alert( data.objlist[ 0 ].msg );
			} else {
				if ( data.objlist[ 0 ].ok == "1" ) {
					// 讀取最新的「推」次數
					$( "#span_lc" + BO_Serial ).load( "/index.aspx?fn=count_like&BO_Serial=" + BO_Serial + "&rand=" + Math.random() );
					if ( is_phone ) {
						alert( "推薦指數上升1，感謝您的參與。" );
					} else {
						show_msgBox( "訊息", "推薦指數上升1，感謝您的參與。<br /><br />", "", "" );
					}
				} else {
					if ( is_phone ) {
						alert( "您已經按過「推」了!" );
					} else {
						show_msgBox( "訊息", "您已經按過「推」了!<br /><br />", "", "" );
					}
				}
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 新增留言
//----------------------------------------------------------------------
function object_comment_add( BO_Serial, parameters_hash )
{
	$.post( "/index.aspx?fn=object_comment_add&rand=" + Math.random(), prepare_data( parameters_hash ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
				// alert( data.objlist[ 0 ].msg );
			} else {
				// 讀取最新的留言
				object_comment_load( BO_Serial );

				// 讀取最新的「回覆」次數
				$( "#span_cc" + BO_Serial ).load( "/index.aspx?fn=count_comment&BO_Serial=" + BO_Serial + "&rand=" + Math.random() );
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 轉貼至 Housebook
//----------------------------------------------------------------------
function object_share( parameters_hash )
{
	$.post( "/index.aspx?fn=object_share&rand=" + Math.random(), prepare_data( parameters_hash ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
				// alert( data.objlist[ 0 ].msg );
			} else {
				if ( LT_Identity == "2" ) {
					var sMobile	= LT_Name.substr( 0, 10 );
					if ( sMobile == main_mobile ) {
						// 讀取最新一筆資料，並顯示於塗鴉牆
						object_load_first();
					}
				}
			}
		}
	}, "json" );
}

//----------------------------------------------------------------------
// 累加並讀取轉貼次數
//----------------------------------------------------------------------
function object_share_count( BO_Serial, BS_Type )
{
	$.post( "/index.aspx?fn=object_share_count&rand=" + Math.random(), prepare_data( { "BO_Serial":BO_Serial, "BS_Type":BS_Type } ), function( data ) {
		if ( data == null ) {
			// alert( "JSON Error!" );
		} else {
			if ( data.indexOf( "\"msg\"" ) == -1 ) {
				// 讀取最新的「轉貼」次數
				$( "#span_sc" + BO_Serial ).text( data );
			}
		}
	} );
}


//----------------------------------------------------------------------
// 產生 Cookie 識別碼 ( 16 位阿拉伯數字，開頭 1~9 )
//----------------------------------------------------------------------
var BG_CookieID = "0";
function produce_cookieID()
{
	if ( $.cookie( "BG_CookieID" ) ) {
		BG_CookieID = $.cookie( "BG_CookieID" );
	}
	if ( BG_CookieID == "0" || BG_CookieID.length != 16 ) {
		while ( BG_CookieID == "0" )
		{
			BG_CookieID = new String( Math.round( Math.random() * 10 ) ).substr( 0, 1 );
		}
		while ( BG_CookieID.length < 16 )
		{
			var s = new String( Math.round( Math.random() * 10 ) );
			BG_CookieID += s.substr( s.length - 1, 1 );
		}
		$.cookie( "BG_CookieID", BG_CookieID, { expires: 365, domain: sDomain } );
	}
}

//----------------------------------------------------------------------
// 官網物件 -- 加入追蹤
//----------------------------------------------------------------------
function item_save( BO_ObjectID )
{
	switch ( LT_Identity )
	{
		case "1":
		case "3":
			$.post( "/index.aspx?fn=item_save&rand=" + Math.random(), prepare_data( { "BO_ObjectID":BO_ObjectID } ) );
			show_msgBox( "訊息", "加入完成!<br /><br />", "", "" );
			break;

		case "2":
			show_msgBox( "訊息", "此為會員專屬功能，請您以官網會員的身份登入!<br /><br />", "", "" );
			break;

		case "4":
			show_msgBox( "訊息", "此為會員專屬功能，登入後方能使用!<br /><br />", "", "" );
			break;
	}
}

//----------------------------------------------------------------------
// 右邊「推薦的房屋」整體變數
//----------------------------------------------------------------------
var house_list = new Array();	// 房屋資料陣列
var house_list_page = 1;

// 顯示 10 筆「推薦的房屋」
function show_house_list( bDelay )
{
	var j = 0;
	for ( var i = 10 * ( house_list_page - 1 ) ; ( ( i < 10 * house_list_page ) && ( i < house_list.length ) ) ; i++ )
	{
		var s = house_list[ i ].split( "_" );
		// "226612_品精成雙璽區(店面)_/k049/226612/A2.jpg_0_台中市-西區 精誠17街__2200",
		var sUrl = "http://www.century21.com.tw/index.aspx?fn=item&no=" + s[ 0 ];
		var sHtml = "<ul class='OBJECT'><li class='OBJECT_IMG'><a href='" + sUrl + "' target='_blank'><img src='http://rms.21online.tw/Uploads" + s[ 2 ]
						   + "' width='97' height='70' /></a></li><li class='OBJECT_INFO'><ul><li class='OBJECT_INFO_01'><p class='OJ_01"
						   + ( ( s[ 3 ] == "1" ) ? " TOP" : "" ) + "'><a href='" + sUrl + "' target='_blank'>" + s[ 1 ] + "</a></p></li><li><p class='OJ_03'>" + s[ 4 ]
						   + "</p></li><li><p class='OJ_03'>" + ( ( s[ 5 ] != "" ) ? ( "格局:" + s[ 5 ] ) : "" ) + "</p></li><li><p class='SALE_PRICE'>" + comma( s[ 6 ] )
						   + " 萬</p></li></ul></li></ul>";
		setTimeout( "$( \"DIV.ITEM_LIST\" ).append( \"" + sHtml + "\" )", 10 * j );
		if ( bDelay ) {
			j++;
		}
	}
}


//----------------------------------------------------------------------
// 右上角廣告計時輪播
//----------------------------------------------------------------------
var BL_Second = new Array();		// 原官網為 .net 載入，本站改為 AJAX 載入各廣告秒數及廣告 HTML
var timer1;
var left_banner_index = 0;
function left_banner_timer()
{
	$( "#SLIDE" ).cycle( "next" );
}


//----------------------------------------------------------------------
// 右上角廣告 Flash 下載
//----------------------------------------------------------------------
function load_advertisement()
{
	$( "SPAN.left_banner" ).each( function( index ) {
		var BL_Pic	= $.trim( $( this ).text() );
		var sHtml	= '<embed src="http://www.century21.com.tw/files/CO_BannerLeft/BL_Pic/' + BL_Pic + '" width="222" height="89" wmode="Transparent" />';
		var sScript	= "$( '#" + $( this ).attr( "id" ) + "' ).html( '" + sHtml + "' );";
		setTimeout( sScript, index * 3000 );		// 每隔3秒下載下一個動畫
	} );

	// 右上角廣告開始計時
	timer1 = setTimeout( "left_banner_timer()", BL_Second[ 0 ] * 1000 );
}


//----------------------------------------------------------------------
// 頁面載入執行
//----------------------------------------------------------------------
$( function() {

	// 移到最上方，避免 autopager 讀取太頻繁 ( 僅 IE8 有效，IE7 / Firefox / Chrome 仍會自動捲到重新整理前的位置 )
	// window.scrollTo( 0, 0 );

	if ( location.href.indexOf( "file:" ) == 0 ) {
		return;	// 本機預覽網頁
	}

	//----------------------------------------------------------------------
	// 自動登入
	if ( $.cookie( "LT_Token" ) && $.cookie( "LT_Token" ) != "*" ) {
		LT_Token		= $.cookie( "LT_Token" );
		LT_Identity	= $.cookie( "LT_Identity" );
		LT_FKV			= $.cookie( "LT_FKV" );
		LT_Name		= $.cookie( "LT_Name" );
		if ( LT_Identity != "4" ) {
			login_ok();
		}
		login_check();
	} else {

		// 未登入訪客識別
		produce_cookieID();

		$.post( "/index.aspx?fn=guest_login&rand=" + Math.random(), prepare_data( { "BG_CookieID":BG_CookieID } ), function( data ) {
			if ( data == null ) {
				// alert( "JSON Error!" );
			} else {
				if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
					// alert( data.objlist[ 0 ].msg );
				} else {
					LT_Token		= data.objlist[ 0 ].LT_Token;
					LT_Identity	= data.objlist[ 0 ].LT_Identity;
					LT_FKV			= data.objlist[ 0 ].LT_FKV;
					LT_Name		= data.objlist[ 0 ].LT_Name;
					$.cookie( "LT_Token",		LT_Token,			{ expires: 90, domain: sDomain } );
					$.cookie( "LT_Identity",		LT_Identity,		{ expires: 90, domain: sDomain } );
					$.cookie( "LT_FKV",				LT_FKV,				{ expires: 90, domain: sDomain } );
					$.cookie( "LT_Name",		LT_Name,			{ expires: 90, domain: sDomain } );
				}
			}
		}, "json" );

	} // End of if ( $.cookie( "LT_Token" ) )

	//----------------------------------------------------------------------

	if ( is_phone ) {
		return;	// 手機版，執行到此即結束
	}

	var qs = new Querystring();
	var keyword	= qs.get("k");
	var ktype		= parseInt( qs.get("kt") );

	//----------------------------------------------------------------------
	// 經紀人員搜尋
	if ( keyword != null ) {

		// 隱藏上方 House 櫥窗
		$( "DIV.TI_HOUSE, DIV.SHARE_BOX" ).remove();

		// 因 Querystring 遇到中文會傳回亂碼，因此重新取得關鍵字
		keyword = location.href.substr( location.href.indexOf( "k=" ) + 2 );
		keyword = decodeURI( keyword );
		if ( keyword != "t" ) {
			$( "#keyword" ).val( keyword );
		}

		// 1 內容  2 經紀人員
		if ( ktype == 1 || ktype == 2 ) {

			$( "#ktype OPTION" ).eq( ktype - 1 ).attr( "selected", true );

			// 搜尋結果上方框
			var sHtml = "<div class='RESULT'><p class='TI'>您的搜尋結果</p><p class='TEXT'>" + keyword + "</p></div>";
			$( "#CONTENT_LEFT" ).append( sHtml );

			// 搜尋結果列表
			if ( ktype == 1 ) { // 搜尋內容

				$.post( "/index.aspx?fn=object&top=100&keyword=" + encodeURI( keyword ) + "&rand=" + Math.random(), prepare_data( null ), function( data ) {
					if ( data == null ) {
						alert( "AJAX Error!" );
					} else {
						if ( data.indexOf( "\"msg\"" ) == -1 ) {
							$( "#CONTENT_LEFT" ).append( data );

							// 配置貼文效果
							assign_effect();
						}
					}
				} );

			} else { // 搜尋經紀人員
				$.post( "/index.aspx?fn=search_agent&rand=" + Math.random(), prepare_data( { "keyword":keyword } ), function( data ) {
					if ( data == null ) {
						// alert( "JSON Error!" );
					} else {
						if ( typeof( data.objlist[ 0 ].msg ) != "undefined" ) {
							// alert( data.objlist[ 0 ].msg );
						} else {
							sHtml = "";
							for ( var key in data.objlist[ 0 ].agent_list ) {
								var a = data.objlist[ 0 ].agent_list[ key ].split( "¤" );
								var sName = a[ 1 ];
								sName = sName.replace( keyword, ( "<span class='YELLOW'>" + keyword + "</span>" ) );
								var sLink = "<a href='http://" + a[ 0 ] + ".21housebook.com.tw/'>";
								sHtml = "<div class='MSG_ITEM2 LAST'><span class='WHO2'>" + sLink + "<img src='" + a[ 4 ] + "' width='61' height='61' border='0' /></a>"
											+ "</span><div class='MSG2'><div class='SHARE2'><p class='TI'>" + sLink + sName + "</a></p><p>電話：" + a[ 2 ] + "</p><p>"
											+ "手機：" + a[ 3 ] + "</p></div><!--END SHARE2--></div><!--END MSG2--><ul class='MSG_LIST2'><li><p>" + a[ 5 ] + "</p>"
											+ "</li></ul></div>";
								$( "#CONTENT_LEFT" ).append( sHtml );
							}
							if ( sHtml == "" ) {
								sHtml = "<div class='MSG_ITEM2 LAST'>找不到符合的項目</div>";
								$( "#CONTENT_LEFT" ).append( sHtml );
							}
						}
					}
				}, "json" );
			}

		}	// End of if ( ktype == 1 || ktype == 2 )

		// 物件搜尋 2011/9/17
		if ( ktype == 3 || ktype == 4 ) {
			kt = ktype;
			search_buy_build( keyword );
		}

	//----------------------------------------------------------------------
	} else {	 // of if ( keyword != null )

		// Ctrl + Enter
		$.ctrl( String.fromCharCode( 13 ), function() {
			$( "A.BTN" ).eq( 0 ).trigger( "click" );
		} );

		// 彈性輸入框
		$( "#BO_Text" ).elastic();

		// 表情選單
		try {
			var oHandler = $( ".mydds" ).msDropDown().data( "dd" );
			oHandler.visible( true );
		} catch( e ) {
			alert( "Error: " + e.message );
		}

		// 表情選單的位置，隨著 IE Zoom 100% 125% 會自動變化
		$( window ).resize( function() {
			var btn_left = $( "A.BTN" ).eq( 0 ).offset().left;
			$( "#FACE" ).css( "left", ( btn_left - 100 ) + "px" );
		} );
		
		// 分享近況
		$( "A.BTN" ).eq( 0 ).bind( "click", function() {
			var BO_Text		= $.trim( $( "#BO_Text" ).val() );
			if ( BO_Text.length > 90 ) {
				alert( "最多只允許輸入 90 個字元，請您修改後再重新發表!" );
				return;
			}
			var BO_Mood	= $( "#BO_Mood" ).val();
			if ( BO_Text != "" ) {
				if ( BO_Text.indexOf( "http:" ) > -1 || BO_Text.indexOf( "https:" ) > -1 ) {
					// 自動轉到「分享連結」燈箱
					$( ".SHARE_LINK" ).eq( 0 ).trigger( "click" );
				} else {
					object_add( { "BO_Text":BO_Text, "BO_Mood":BO_Mood } );
					$( "#BO_Text" ).val( "" );
				}
			}
		} );

		// 修改個人資料
		$( "#a_profile" ).bind( "click", function() {
			$( "#profile_LT_Token" ).val( LT_Token );
			$( "#profile_main_mobile" ).val( main_mobile );
			$( "#form_profile" ).submit();
		} );

		// 讀取貼文
		object_load( "new" );

		// Focus
		$( "#BO_Text" ).focus();

		// 排序功能
		$( "#sort1, #sort2" ).bind( "click", function() {
			var c = $( this ).attr( "checked" );
			var no = $( this ).attr( "id" ).substr( 4, 1 );
			if ( c ) {
				if ( no == "1" ) {	// 最新
					object_load( "new" );
					$( "#sort2" ).attr( "checked", false );
				} else {		// 熱門
					object_load( "hot" );
					$( "#sort1" ).attr( "checked", false );
				}
			} else {
				$( this ).attr( "checked", true );
			}
		} );
		
	} // End of if ( keyword != null )

	//----------------------------------------------------------------------

	// 載入中間廣告
	$( "A.TOP_BANNER" ).load( "/index.aspx?fn=banner1" );

	// 載入右上角廣告
	$.get( "/index.aspx", { "fn": "banner2" }, function( data ) {

		eval( data );

		$( "#SLIDE" ).cycle( {
			fx:				"scrollDown",
			speed:		300,
			timeout:	0,
			pager:		"#SLIDE_NAV",
			slideExpr:	"SPAN"
		} );

		// 右上角廣告 Flash 下載 ( 延遲1秒後執行 )
		setTimeout( "load_advertisement()", 1000 );
	} );

	// 載入「推薦的房屋」
	$.post( "/index.aspx?fn=agent_object_list&rand=" + Math.random(), prepare_data( null ), function( data ) {
		if ( data != null && typeof( data.objlist[ 0 ].house_list ) != "undefined" ) {
			house_list = data.objlist[ 0 ].house_list;
			show_house_list( false );
		}
	}, "json" );

	// 看更多推薦的房屋
	$( "#a_more_house" ).bind( "click", function() {
		if ( house_list_page * 10 < house_list.length ) {
			house_list_page++;
			if ( house_list_page * 10 >= house_list.length ) {
				$( "#a_more_house" ).fadeOut( 3000 );
			}
			show_house_list( true );
		} else {
			// house_list_page = 1;		// 已達最後一頁，再從第一頁開始
		}
		// window.scrollTo( 0, 0 );	// 捲回最上面
	} );


	// 數字三位一逗號
	//$.fn.digits = function(){ return this.each( function(){ $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); } ) } 


} );	// End of 頁面載入執行

