/**
 *	T.
 *	公共TWIOO。
 *
 *	必须的类库：
 *		jquery-1.6.2.js
 *
 */

var T = {
	// 所有参数。
	options : {
		domain			: "twioo.com",
		isIE6			: $.browser.msie && $.browser.version == 6.0,				// 判断是否是IE6
		loading			: "../img/global/loading/l5.gif"
	},

	UI : {
		// 添加loading...
		loading : function () {	
			return "<div class=\"loading\">loading...</div>";
		}	
	},

	// 用户
	User : {
		options : {
			box			: "#login"
		},

		// 退出
		quit : function () {
			
		},
		
		// 提示
		tip : {			 
			 show : function () {
				var $_tip = $(".login_tip"),								// 提示box
					$_tip_clse,												// 提示关闭btn
					$_tip_text;

				if (!$_tip.length){
					
					$_tip = $('<div class="login_tip" />').prependTo($(T.User.options.box));
					$_tip_clse = $('<a href="javascript:void(0)" class="login_tip_close" title="关闭" />')
									.appendTo($_tip)
									.click(function(){
										T.User.tip.hide();
									});
					$_tip_text = $("<p />").appendTo($_tip);
					$_tip.fadeIn(200);
				}
				
				/**
				 	确定显示什么错误
				 */

				$_tip_text.empty().html('<b>登录名或密码错误</b>1、请检查登录名大小写是否正确<br />2、请检查密码大小写是否正确');

			 },
			
			 hide : function () {
				var $_tip = $(".login_tip");
				$_tip.fadeOut(100).remove();
			 }
			
		},
		
		// 初始化
		init : function () {
			$(this.options.box).html(T.UI.loading());			
			/*
				异步请求确定是否登录
			*/
			if (1)
			{
				var $_uId	= $("#loginId"),		// 账号
					$_uPwd	= $("#loginPwd");		// 密码

				$(T.User.options.box).load("login.shtml #notLogged");		// 未登录
			//	$(T.User.options.box).load("login.shtml #logged");			// 已登录

				// 账号
				$_uId
					.live("focus", function(){
						if ($(this).val() == "请输入邮箱/手机号码"){
							$(this).val("");
						}
					})
					.live("keyup", function(){
						var $_sList	= $("#tNote"),							// 结果列表
							$_sListItem,									// 数据item
							$_sListItemData = "",							// 所有item数据
							$_optionalEmail = [	"hundsun.com",
												"qq.com",
												"126.com",
												"sina.com",
												"163.com",
												"hotmail.com",
												"yahoo.com.cn",
												"gmail.com",
												"sohu.com"],				// 可选邮箱
							$_val = $(this).val();
							
							// 未创建执行
							if (!$_sList.length){
								$_sList = $('<div id="tNote" />').appendTo($(this).parent());
							}

							/*
								ajax 请求填充结构数据
							*/
							
							$_sList.empty();
							if ($_val != ""){							
								
								$.each($_optionalEmail, function (index, val) {								
									$_sListItem = $('<a href="javascript:void(0)" />')
													.appendTo($_sList)
													.html($_val + "@" + val)				
													.click( function () {
														$(this).parent().prev().val($(this).html());
														$_sList.hide();
													});
								});

								$_sList.slideDown(200);

							}					

					})
					.live("blur", function(){
						var $_sList	= $("#tNote"),							// 结果列表
							$_val = $(this).val();

						if ($_val == ""){
							$(this).val("请输入邮箱/手机号码");
							$_sList.hide();
						}
					});
				
				// 密码
				$_uPwd
					.live("focus", function(){
						if ($(this).val() == "请输入密码"){
							$(this).val("");
						}
					})
					.live("blur", function(){
						if ($(this).val() == ""){
							$(this).val("请输入密码");
						}
					});
			}
			
		}
		
	},
	
	// 搜索
	Search : {
		options : {
			box		: "#search",			// 搜索容器
			id		: "sResult",			// 搜索结果
			sList	: "sList"
		},
		// 获取焦点
		focus : function (el) {
			var $_val = $(el).val();
			if ($_val == "搜索股票，用户，内容"){
				$(el).val("");
			}		
		},
		
		// 失去焦点
		blur : function (el) {
			var $_val = $(el).val();
			if ($_val == "" || $_val == "搜索股票，用户，内容"){
				$(el).val("搜索股票，用户，内容");
			}		
		},
		
		keydown: function(el, event){
			var $_val =  $.trim($(el).val());
			
			if((event.keyCode==38||event.keyCode==40) && $_val.length>0)
			{
				this.keyUpOrDown(event.keyCode);
			}
			else if((event.keyCode==13) && $_val.length>0)
			{
				this.search($_val);
			}
		},
		
		keyUpOrDown: function(eventCode){
				if(eventCode==38)
				{	
					if($("#_sList >li:eq(1)").attr("class")=="hover"){	
						
						T.Search.setcolor($("#_sList li:eq(0)"));
					}
					else if($("#_sList >li:eq(2)").attr("class")=="hover"){
						T.Search.setcolor($("#_sList li:eq(1)"));
					}
					else if($("#_sList >li:eq(0)").attr("class")=="hover"){
						T.Search.setcolor($("#_sList li:eq(2)"));
					}else{
						T.Search.setcolor($("#_sList >li:eq(0)"));
					}
				}
				else
				{	
					if($("#_sList >li:eq(0)").attr("class")=="hover"){	//alert(1);
						T.Search.setcolor($("#_sList li:eq(1)"));
					}else if($("#_sList >li:eq(1)").attr("class")=="hover")
					{
						T.Search.setcolor($("#_sList li:eq(2)"));
					}else if($("#_sList >li:eq(2)").attr("class")=="hover")
					{
						T.Search.setcolor($("#_sList li:eq(0)"));
					}else{
						T.Search.setcolor($("#_sList >li:eq(0)"));
					}
				}
		},
		
		//设置那个下拉菜单被选中
		setcolor:function(obj)
		{
		  $('#_sList>li').removeClass();
		  obj.addClass("hover");
		},
		
		// 输入弹起
		keyup : function (el, ev) {
			var eventCode =  ev.charCode||ev.keyCode;
			if(eventCode==38 || eventCode==40 ||eventCode==13){
				return;
			}
			var i = $("#_sList >li").index($("#_sList >li.hover"));
			i = (i==-1) ? 0: i;
			var $_val =  $.trim($(el).val()),
				$_sBox = $("#" + this.options.id),				// 搜索结果列表BOX
				$_sClose,										// 结果列表BOX 关闭按钮
				$_sList,										// 结果列表
				$_sListItem;									// 数据item
			var $_val_show = $_val.length>8?$_val.slice(1,8)+"..":$_val;
			if ($_val != ""){
				// 未创建执行
				if (!$_sBox.length) {
					$_sBox = $('<div class="answer-list" id="' + this.options.id + '" />').prependTo($(this.options.box));
					$_sClose = $('<a href="javascript:void(0)" class="icon_search_close" title="关闭" />')
						.prependTo($(this.options.box))
						.html("X")
						.click( function () {
							T.Search.hide();
							$(this).remove();
						});
					$_sList = $('<ul />')
								.attr("id","_sList")
								.appendTo($_sBox)
								.html('');
				}else{
					$("#_sList").empty();
				}

				/*
					ajax 请求填充结构数据
				*/				
				$_sListItem = $("<li>")
					.attr("id","_sListItem_stock")	
					.appendTo($("#_sList"))
					.html("包含<b>"+$_val_show+"</b>的股票")					
					.hover(
						function () { $(this).addClass("hover");},
						function () { $(this).removeClass("hover");}
					)
					.click( function(){
						T.Search.search($_val);
					});	
				$_sListItem = $("<li>")
					.attr("id","_sListItem_user")
					.appendTo($("#_sList"))
					.html("名为<b>"+$_val_show+"</b>的用户")						
					.hover(
						function () { $(this).addClass("hover");},
						function () { $(this).removeClass("hover");}
					)
					.click( function(){
						T.Search.search($_val);
					});	
				$_sListItem = $("<li>")
					.attr("id","_sListItem_topic")
					.appendTo($("#_sList"))
					.html("包含<b>"+$_val_show+"</b>的话题")						
					.hover(
						function () { $(this).addClass("hover");},
						function () { $(this).removeClass("hover");}
					)
					.click( function(){
						T.Search.search($_val);
					});											


				$_sBox.slideDown(200);
				T.Search.setcolor($("#_sList >li:eq("+i+")"));
			}else{
				T.Search.hide();
				$(".icon_search_close").remove();
			}
		},
		
		// 隐藏搜索结果列表
		hide : function () {
			$("#" + this.options.id).slideUp(200).remove();
		},
		
		search: function(searchValue, searchType){
			if(searchValue.length==1){
				 var str=",.?;:'\"!@#$%^&*()";
				 if(str.indexOf(searchValue)>=0)
				 {
		          	ymPrompt.alert({message:'不能搜索单个字符！',handler:null});
					return;
				 }
			}
			var i = $("#_sList >li").index($("#_sList >li.hover"));
			i = (i==-1) ? 0: i;
			i = (i==0)? 1: (i==1?0:2);
			$("#searchForm").attr("action", "/op.php?act=search&op=search&HitSearch=1&stype="+i+"&sname="+searchValue).submit();
		},
		
		// 初始化
		init : function (){
			var $_q = $(this.options.box + " .q");
				
				$_q
					.focus(	function () { T.Search.focus($(this));})
					.blur(	function () { T.Search.blur($(this));})
					.keyup( function (event) { T.Search.keyup($(this), event);})
					.keydown( function (event){T.Search.keydown($(this), event);});			
		}	
	},
	
	// 微博
	MicroBlog : {
		options : {
			box			: "#microBlog",							// 微博列表
			pic			: ".mbPic"						// 微博图片
		},
		
		// 视频
		Video : {
			show : function(el, url) {
				$(el).fadeOut().prev().load("microblog-video.shtml?url="+ url);
			},
			hide : function (el){				
				$(el).parent().empty().next().fadeIn();
			}
		},
		// 初始化
		init : function(){
			/**
			 * 鼠标滑过微博列表
			 * ie7- USE ie6+ USE CSS
			 */
			if(T.options.isIE6)
			{			
				$(this.options.box + ">dl")
					.live("mouseover", function(){
						$(this).addClass("hover");
					})
					.live("mouseout", function(){
						$(this).removeClass("hover");
					});
			}
			
			// 图片上绑定点击事件
			$(this.options.pic).live("click", function(){
				var $_box = $(this).parent().parent();	
					$_pic = [
								$(this).attr("smallpic"),			// 小图								
								$(this).attr("largepic"),			// 大图
								$(this).attr("original")			// 原图
							];

				if ($_box.attr("class") == "mbSmallPic"){			// 大图
					$(this).attr("src", $_pic[1]);
					$_box.attr("class", "mbLargePic");					
					
				} else {											// 小图
					$(this).attr("src", $_pic[0]);
					$_box.attr("class", "mbSmallPic");					
				}
			});

			// 收起
			$(".btnRetract").live("click", function(){
				var $_pic = $(this).parent().parent().find(T.MicroBlog.options.pic);
				$_pic.trigger("click");
			});
		}
	},
	
	// 返回顶部
	backTop : function () {
		var $_top = $(".backTop")
			$_sT = $(window).scrollTop();		


		if (!$_top.length) {
			$_top = $('<a href="javascript:void(0)" class="backTop" />')
					.appendTo($("body"))
					.css({
							"position"	: T.options.isIE6 ? "absolute" : "fixed",
							"display"	: $_sT > 0 ? "block" : "none",
							"top"		: $(window).height() + $_sT - $_top.outerHeight() + "px"
					})
					.html('<i class="icon_backtop"></i>返回顶部')
					.click(function(){
						$(document).scrollTop(0);
					});
		} else {
			$_top.css({
				"display"	: $_sT > 0 ? "block" : "none",
				"top"		: T.options.isIE6 ? $(window).height() + $_sT - $_top.outerHeight() : ""
			});
		}
	}
};

/**
 *	stockScroll.
 *	股票滚动。
 */
var stockScroll = {
	options : {
		cur		: 0,				// 记录当前所在位置
		speed	: 300				// 滑动速度
	},
	// 前翻
	Prev : function (el) {
		var $box = $(el).next().children("UL");
		var $boxItem = $box.find("LI");
		var $biH = $boxItem.outerHeight();

		if (this.options.cur > 0) {	
			$box.css("height",$boxItem.length * $biH)
				.stop()
				.animate({top: - (this.options.cur-1) * $biH + "px"}, this.options.speed);

			$(el).attr("class", this.options.cur > 1 ? "icon_ss_prev" : "icon_ss_unprev");
			$(".icon_ss_unnext").attr("class", "icon_ss_next");
			
			this.options.cur --;
		}	
			
	},
	// 后翻
	Next : function (el) {
		var $box = $(el).prev().children("UL");
		var $boxItem = $box.find("LI");
		var $biH = $boxItem.outerHeight();		
		//alert($box.outerHeight());alert($biH);
		if ($boxItem.length - this.options.cur > 1) {		
			//alert($boxItem.length - this.options.cur);
			$box.css("height",$boxItem.length * $biH)
				.stop()
				.animate({top: - (this.options.cur+1) * $biH + "px"}, this.options.speed);		

			$(el).attr("class", $boxItem.length - this.options.cur > 2 ? "icon_ss_next" : "icon_ss_unnext");
			$(".icon_ss_unprev").attr("class", "icon_ss_prev");
			
			 this.options.cur ++;
		}
	}
};


$(document).ready( function () {
	
	// 我的首页
	$("#dropMH")
		.live("mouseover", function(){	$(this).children("ol").show(); })
		.live("mouseout", function(){	$(this).children("ol").hide(); });

	// 返回顶部
	T.backTop();
	$(window).scroll(function(){ T.backTop();});

});
