home.js 10.1 KB
/**
 * @fileOverview 首页JS
 * @author wei.wang@yoho.cn
 * @date 2014-05-21
 */

	var $=require("jquery");
	require("jquery.autocomplete");
	require("../public");
	var tools = require('yoho-tools');
	var Swiper=require("yoho.swiper");
	var box=require('../plugin/box');
	var tips=require("../plugin/tips");
	require('../plugin/lazyloadImage');
	
	//导航栏搜索交互==============================================================================================================
	window.isSearching=false;
		var input_big =$("<input/>").attr("class","search-input-box search-input-box-big")
		.autocomplete('/search/default/words', {
				    resultsClass:  "search-suggest helveticaneue", loadingClass:"loading",
				    width:157, scrollHeight:360, 
				    max:10, selectFirst:false, autoFill:false,
				    formatResult: function(row, data)
				    {
					   return data.replace(/<\/?[^>]*>/g,'');
				    }
				}).result(function(event, data, formatted) 
				{
					if (data.length>150) {
						box.alert("搜索内容超长,最多150个字");
					}
					else
					{
						window.location.href = YohoConfig.mainUrl+'./search?q=' + encodeURIComponent(formatted.replace(/<\/?[^>]*>/g,''));
					}
				    //window.location.href = YohoConfig.mainUrl+'./search?q=' + encodeURIComponent(formatted.replace(/<\/?[^>]*>/g,''));
				}).keydown(function(event){
			if(event.keyCode==13){
				$(this).next().trigger("click");
				
			}
		}).hide();
		var searchButton_big=$("<SPAN/>").attr({"class":"home-search-button",id:"search-button"}).click(function(){
			if($.trim($(this).prev().val())!="")
			{
	
				if ($(this).prev().val().length<150) {
					
					window.location=YohoConfig.mainUrl+"/search?q="+$(this).prev().val();
					return false;
				}
				else
				{
					box.alert("搜索内容超长,最多150个字");
				}
			}
			else
			{
				$(document).trigger("click");
			}
			
		}).hide();
		
		
		
		
		
		var input_small =$("<input/>").attr("class","search-input-box search-input-box-small")
		.autocomplete('/search/default/words', {
				    resultsClass:  "search-suggest helveticaneue", loadingClass:"loading",
				    width:157, scrollHeight:360, 
				    max:10, selectFirst:false, autoFill:false,
				    formatResult: function(row, data)
				    {
					return data.replace(/<\/?[^>]*>/g,'');
				    }
				}).result(function(event, data, formatted) 
				{
					
					if (data.length>150) {
						box.alert("搜索内容超长,最多150个字");
					}
					else
					{
						window.location.href = YohoConfig.mainUrl+'./search?q=' + encodeURIComponent(formatted.replace(/<\/?[^>]*>/g,''));
					}
				    
				}).keydown(function(event){
			if(event.keyCode==13){
				$(this).next().trigger("click");
				
			}
		}).hide();
		var searchButton_small=$("<SPAN/>").attr({"class":"home-search-button",id:"search-button"}).click(function(){
			if($.trim($(this).prev().val())!="")
			{
				if ($(this).prev().val().length<150) {
					
					window.location=YohoConfig.mainUrl+"/search?q="+$(this).prev().val();
					return false;
				}
				else
				{
					box.alert("搜索内容超长,最多150个字");
				}
				
			}
			else
			{
				$(document).trigger("click");
			}
			
		}).hide();
		
		
		
		
		
		$(".search-text-big").append(input_big).append(searchButton_big);
		$(".search-text-small").append(input_small).append(searchButton_small);
		
		$(".search-text-big,.search-text-small").unbind().bind({
			click:function(event){
				if(window.isSearching===false)
				{

					var input=$(this).find(".search-input-box").show();
					$(this).find(".home-search-button").show();
					$(".search-text-content").html("");
					$(this).css({position:"absolute",right:0,"z-index":10,float:"left","border-left":"2px solid #000"}).animate({width:219},500,function(){
						$(".home-search-button").show();
						$(".search-area").find(".icons").hide();
						$(".search-area").find(".getapp").hide();
						
						
					});
					$(this).find(".search-input-box").focus();
					window.isSearching=true;
					input.focus();
					
					
				}
				event.preventDefault();
				return false;
			}
		});
	
	
	
	
	$(document).click(function(){
		if (window.isSearching===true) {
			$(".search-suggest").hide();
			$(".search-area").find(".icons").show();
			$(".search-area").find(".getapp").show();
			$(".search-text").stop().animate({width:73},500,function(){
				$(".search-text-content").html("Search...");
				$(".search-input-box").val("").hide().trigger("blur");
				$(".home-search-button").hide();
				
				window.isSearching=false;
			});
		}
		
	});
	//============================================================================================================================
	
	
	//弹出式菜单=====================================================================================================================
	var mySwiper = new Swiper('.swiper-container2',{
	    pagination: '.pagination-adv',
	    loop:true,
	    autoplay:2000,
		autoStopPlay:false,
	    paginationClickable: true,
            onSlideChangeEnd:function(){
                mySwiper.startAutoplay();
                $(".swiper-container2 .lazy").lazyload();
            }
	 });
	
	
	//=============================================================================================================================
	
	
	
	//二级频道子菜单=======================================================================================================
	$.ajax({
		url:YohoConfig.mainUrl+"/default/default/getnewest",
		cache:false,
		success:function(_data){
			var info=_data;
			var videoData={},videoSubmenu=[],videoSubmenuData={};
			
			if(info.status===true)
			{
				var menuDatas=info.data;
				for(var menu in menuDatas)
				{
					var menuData=menuDatas[menu];
					if(menu.indexOf("-")<0&&menu!="video")
					{
						var content=$("<div/>").attr("class","submenu submenu-"+menu);
						for(var i=0;i<3;i++)
						{
							var item=$("<div/>").attr("class","content-item-menu");
							var a_link=$("<a/>").attr({"class":"link",href:menuData[i].link,target:"_blank"}).appendTo(item);
							var a_img=$("<a/>").attr("class","a-img").appendTo(item);
							if(menuData[i].isVideo===true)
							{
								var a_video=$("<a/>").attr({"class":"a-video",href:menuData[i].link}).appendTo(a_img);
							}
							var img=$("<img/>").attr("src",menuData[i].cover).appendTo(a_img);
							var title=$("<span/>").attr("class","text").html(menuData[i].title).appendTo(item);
							var subTitle=$("<span/>").attr("class","text detail").html(menuData[i].subtitle).appendTo(item);
							item.appendTo(content);
						}
						$(".menu-item-"+menu).append(content);
					}
					else if(menu=="video")
					{
						//视频子菜单的样式
						videoData=menuData;
					}
					else if(menu.indexOf("video-")>=0)
					{
						videoSubmenu.push(menu.replace("video-",""));
						videoSubmenuData[menu]=menuData;
					}
				}
				
				var self=$(".menu-item-video");
					self.find(".submenu-waiting").remove();
					self.find(".content-item-menu").remove();
					var leftMenu=self.find(".left");
					var randerData=videoData;
					if($(".left-item-choosen").length==0)
					{
						randerData=videoData;

					}
					else
					{
						var menuKey=$(".left-item-choosen").attr("sub");
						self.find(".content-item-menu").remove();
						randerData=videoSubmenuData[menuKey];
					}
					for(var i=2;i>=0;i--)
					{
						var tmpObj=randerData[i];
						var item=$("<div/>").attr("class","content-item-menu");
						var link=$("<a/>").attr({"class":"link",href:tmpObj.link,target:"_blank"}).appendTo(item);
						var a_img=$("<a/>").attr("class","a-img").appendTo(item);
						if(tmpObj.isVideo===true)
						{
							var a_video=$("<a/>").attr({"class":"a-video",href:tmpObj.link}).appendTo(a_img);
						}
						var img=$("<img/>").attr({"src":tmpObj.cover}).appendTo(a_img);
						var title=$("<span/>").attr("class","text").html(tmpObj.title).appendTo(item);
						var subTitle=$("<span/>").attr("class","text detail").html(tmpObj.subtitle).appendTo(item);
						self.find(".left").after(item);
					}

				$(".menu-item-video").mouseover(function(){
					var self=$(this);
					self.find(".submenu-waiting").remove();
					self.find(".content-item-menu").remove();
					var leftMenu=self.find(".left");
					var currentData=videoData;
					if($(".left-item-choosen").length==0)
					{
						currentData=videoData;
						
					}
					else 
					{
						var menuKey=$(".left-item-choosen").attr("sub");
						self.find(".content-item-menu").remove();
						currentData=videoSubmenuData[menuKey];
					}
					for(var i=0;i<3;i++)
					{
						var item=$("<div/>").attr("class","content-item-menu");
						var link=$("<a/>").attr({"class":"link",href:currentData[i].link,target:"_blank"}).appendTo(item);
						var a_img=$("<a/>").attr("class","a-img").appendTo(item);
						if(currentData[i].isVideo===true)
						{
							var a_video=$("<a/>").attr({"class":"a-video",href:currentData[i].link}).appendTo(a_img);
						}
						var img=$("<img/>").attr({"src":currentData[i].cover}).appendTo(a_img);
						var title=$("<span/>").attr("class","text").html(currentData[i].title).appendTo(item);
						var subTitle=$("<span/>").attr("class","text detail").html(currentData[i].subtitle).appendTo(item);
						self.find(".left").after(item);
					}
				});
				$(".submenu-video").bind("mouseover",function(){
					return false;
				});
			}
		}
	});
	//==================================================================================================================
	
	
	//邮件订阅=============================================================================================================
	$(".followus-button").click(function(){
		if($(".follow-us-input-content input").val()=='')
		{
		    box.alert(tips.getTips("emailEmpty"));
		    return;
		}
		if(!tools.IYOHO_isEmail($(".follow-us-input-content input").val()))
		{
		    box.alert(tips.getTips("emailError"));
		    return;
		}
		$(".search-suggest").hide();
		window.open( '/passport/subscribe/index?email='+$(".follow-us-input-content input").val());
	});
	$(".followus-input").keydown(function(event){
		if(event.keyCode==13)
		
		{
			$(".followus-button").trigger("click");
		}
	});
	//===================================================================================================================