// cnool基本功能js
jQuery(document).ready(function($) {
	    //tab页
	    
		settabs('01');
		settabs('02');
		settabs('03');
		settabs('20');
		//settabs('21');
		settabsrandom21('activitydiv');
		//settabs('homebookmenu02');
		//settabs('homebookmenu03');
		//settabs('homebookmenu04');
		//settabs('homebookmenu05');
		//settabs('homebookmenu06');
		//settabs('homebookmenu07');
                //settabs('function');
                //settabsrandom('function');
		//setsearchtab();
		//setweather(0);

        //setstat();

    //$("#scrollDiv").textSlider({line:4,speed:500,timer:5000});
		 
		 t = setInterval('playnext(20)', 3000);
		 
		  $("div.homeBook-body20 a").mouseover(function() {
                clearInterval(t);
          
            });	
        $("div.homeBook-body20 a").mouseout(function() {
                t = setInterval('playnext(20)', 3000);
            });
            
           t1 = setInterval('playnext("03")', 8000);

			$("#nature201101").mouseover(function() {
                clearInterval(t1);
				//alert($("#homeBook-menu03+div>div").html());
          
            });	
			$("#nature201101").mouseout(function() {
                t1 = setInterval('playnext("03")', 8000);
            });

			
			$("#nature201102").mouseover(function() {
                clearInterval(t1);
				//alert($("#homeBook-menu03+div>div").html());
          
            });	
			$("#nature201102").mouseout(function() {
                t1 = setInterval('playnext("03")', 8000);
            });

			$("#homeBook-menu03 H2.homemenutitle SPAN").mouseover(function() {
                clearInterval(t1);
				//alert($("#homeBook-menu03+div>div").html());
          
            });	

        $("#homeBook-menu03 H2.homemenutitle SPAN").mouseout(function() {
                t1 = setInterval('playnext("03")', 8000);
            });
            //搜索
           setSearch();
           //天气
           setWeather();
           //统计
           setstat();
           //随机图片
           $('ul#randompic').reorder();
           
            
            
            
            
		
    })
    
    
     function setWeather()
    {
          //鼠标click
         $("#weatherDay,#weatherMore").click(function(e) {
         //alert(e);
            $("#headWeather").show();
            //alert(e);
        });
        
        //鼠标移开事件
         $("#weatherDay,#weatherMore,#headWeather").mouseout(function(e) {
            $("#headWeather").hide();
            //alert(e);
        }); 
        
        //鼠标移开事件
         $("#weatherDay,#weatherMore,#headWeather").mouseover(function(e) {
            $("#headWeather").show();
            //alert(e);
        }); 
        
        
        $("#headWeather a").click(function(e) {
            
            var array = $(this).next().text().split('|');
            $("#weatherPic").attr("src",array[0]);
            $("#weatherDay").html(array[1]);
            $("#weatherInfo").html(array[2]);
            $("#headWeather").hide();
            //alert($(this).text());
        });
    }
    
    
    function setSearch()
    {
          //鼠标click
         $("#headSearchForm div.nowSearch,#headSearchForm div.btnSel").click(function(e) {
            $("#headSel").show();
            //alert(e);
        });
        
        //鼠标移开事件
         $("#headSearchForm div.nowSearch,#headSearchForm div.btnSel,#headSel").mouseout(function(e) {
            $("#headSel").hide();
            //alert(e);
        }); 
        
        //鼠标移开事件
         $("#headSearchForm div.nowSearch,#headSearchForm div.btnSel,#headSel").mouseover(function(e) {
            $("#headSel").show();
            //alert(e);
        }); 
        
        
        $("#headSel a").click(function(e) {
            $("#searchtypediv").html($(this).text());
            $("#searchtype").val($(this).text().replace('快讯','新闻'));
            $("#headSel").hide();
            //alert($(this).text());
        });
        
        
        
        
        $("#headSearchForm div.btnSearch a").click(function(e) {
            $("#headSearchForm").submit();
            //alert(e);
        }); 
        
        
        $('#headSearchForm').submit(function() {
                
                //return false;
         });
         
         
         $("#option.key").keydown(function(e) {
            if (e.keyCode == 13) 
            {
                $("#headSearchForm").submit();
            }
        });  
        
        //astro
         $("#astrosearchform div.img-text11 a").click(function(e) {
            //alert(e);
            $("#type").val($(this).attr("title"));
            $("#astrosearchform").submit();
            
        }); 
         
    }
    
	function settabs(divid)
	{
	    //
        
	
		$("#homeBook-menu"+divid + " H2.homemenutitle SPAN:first").addClass("on");
		$("#homeBook-menu"+divid + " div.homemenubody:gt(0)").hide();
		//alert($("#"+divid + " H2.homemenutitle SPAN").html());
		$("#homeBook-menu"+divid + " H2.homemenutitle SPAN").mouseover(function(){
			//alert('aa');
			$(this).addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
			//alert($(this).index());
			//alert($(".homeBook-body"+divid+":eq("+$(this).index()+")").html());
			$("div.homeBook-body"+divid+":eq("+$(this).index()+")").show().siblings("div.homeBook-body"+divid).hide();
		});
	}
	
	// 播放图片的函数
//	var imgPlay = function() {
//		next = curr + 1;
//                // 若当前图片播放到最后一张，这设置下一张要播放的图片为第一张图片的下标
//		if(curr == count-1) next=0;
//		play(next);
// 
//		curr++;
//                // 在当前图片的下标加1后，若值大于最后一张图片的下标，则设置下一轮其实播放的图片下标为第一张图片的下标，而next永远比curr大1
//		if(curr > count-1) { curr=0; next = curr +1; }
//	};
	
    function playnext(divid)
	{
		//$("#homeBook-menu"+divid + " H2.homemenutitle SPAN:first").addClass("on");
		//$("#homeBook-menu"+divid + " div.homemenubody:gt(0)").hide();
		//alert($("#"+divid + " H2.homemenutitle SPAN").html());
		//$("#homeBook-menu"+divid + " H2.homemenutitle SPAN").mouseover(function(){
			//alert('aa');
		var length = $("#homeBook-menu"+divid + " H2.homemenutitle SPAN").length;
		var index = $("#homeBook-menu"+divid + " H2.homemenutitle SPAN[class='on']").index();
		var newindex = index + 1;
		if(newindex == length) newindex =0;
		
		//alert(newindex + ","+index);
		$("#homeBook-menu"+divid + " H2.homemenutitle SPAN:eq("+newindex+")").addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
		$(".homeBook-body"+divid+":eq("+newindex+")").show().siblings("div.homeBook-body"+divid).hide();
			//$(this).addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
			//alert($(this).index());
			//alert($(".homeBook-body"+divid+":eq("+$(this).index()+")").html());
			
		//});
	}
	
	
	function settabsrandom(divid)
	{
	    //获取总数-1
        var licount = $("#"+divid + " H2.homemenutitle SPAN").length -1;
        //获取随机数
	    var i =Math.round(Math.random()*licount);
		//$("#"+divid + " ul.homemenutitle li:last").addClass("on");
		//$("#"+divid + " div.homemenubody:gt(0)").hide();
		$("#"+divid + " H2.homemenutitle SPAN:eq("+i+")").addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
        $("#"+divid + " div.homemenubody:eq("+i+")").show().siblings("div.homemenubody").hide();
		
		$("#"+divid + " H2.homemenutitle SPAN").mouseover(function(){
			$(this).addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
			$("#"+divid + " div.homemenubody:eq("+$(this).index()+")").show().siblings("div.homemenubody").hide();
		});
	}

    function settabsrandom21(divid) {
        //获取总数-1
        var licount = $("#"+divid + " H2.homemenutitle SPAN").length -1;
        //获取随机数
	    var i =Math.round(Math.random()*licount);
		//$("#"+divid + " ul.homemenutitle li:last").addClass("on");
	    //$("#" + divid + " div.homeBook-body21:gt(0)").hide();
		$("#"+divid + " H2.homemenutitle SPAN:eq("+i+")").addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
		$("#" + divid + " div.homeBook-body21:eq(" + i + ")").show().siblings("div.homeBook-body21").hide();
		
		$("#"+divid + " H2.homemenutitle SPAN").mouseover(function(){
			$(this).addClass("on").siblings(" H2.homemenutitle SPAN").removeClass();
			$("#"+divid + " div.homeBook-body21:eq("+$(this).index()+")").show().siblings("div.homeBook-body21").hide();
        });
    }
	//solosearch
	function setsearchtab()
	{

	    $("div.solo_main_fod a").click(function(){
	    
	        $.each($("div.solo_main_fod a"), function(){   
                $(this).html($(this).text());
            }); 
            
			$(this).html("<b>"+$(this).text()+"</b>");
			
			return false;
		});
		
		//solo搜索
		 $("#solosearchbtn").click(function(){
		    var url = "http://search.cnool.net/search.aspx?option.dfcity=&searchFlag=1&searchtype="+escape($("div.solo_main_fod a b").text())+"&option.key="+escape($("#solosearchkey").val());
		    if($("div.solo_main_fod a b").text() == "店铺")
		    {
		        url = "http://solo.cnool.net/store_search.aspx?key="+escape($("#solosearchkey").val());
		    }
		    $("#solosearchlink").attr("href",url);
		    $("#solosearchlink").attr("target","_blank");
		    $("#solosearchlink").click();
		 });
		 
        //房产搜索
		 $("#housesearchbtn").click(function(){
		    //alert('www.cnool.net');
		    //$("#solosearchlink").click();
		    //var url = 'http://www.cnool.net';
		    //key2
		    var url = "http://so.house.cnool.net/searchcache.aspx?page=1&cityid=2&areaid="+$("#areaid").val()+"&type=1&source=-1&area1=-1&area2=-1&kind=0&price1=-1&price2=-1&key="+escape($("#key2").val())+"&decorate=%c8%ab%b2%bf&room="+$("#room").val()+"&hall="+$("#hall").val()+"&bath="+$("#bath").val()+"";
		    $("#housesearchlink").attr("href",url);
		    $("#housesearchlink").attr("target","_blank");
		    $("#housesearchlink").click();
		 });
		 

	    
	}
	
	
	
	function setweather(weatherday)
	{
	    if(typeof(vnweather) ==  'undefined')   return true;
	    var arrayday = new Array(3);
        arrayday[0] = "今天";
        arrayday[1] = "明天";
        arrayday[2] = "后天";

	    var weatherdata = eval('('+vnweather+')');
	    var data = weatherdata[weatherday];
	    var weatherimages = data.image.split("|");
        $(".solo_left_weather_icon").html("<img src='"+weatherimages[0]+"' width=44 height=44 />");
        tempstr = "<div class=\"solo_left_weather_list\"><b>宁波</b> <a onclick='showweathermenu(event)'>"+arrayday[weatherday]+"</a><img id='weatherimgselect'  onclick='showweathermenu(event)' src=\"images/more_1.gif\"></div>";
        tempstr += "<div class=\"solo_left_weather_list\">"+data.degree+"</div>";
        tempstr += "<div class=\"solo_left_weather_list\">"+data.china+ "</div>";
　      $(".solo_left_weather").html(tempstr);
　      //alert($('#weatherset').val());
　      closediv();
　      
　      
　     

        
     }
     
    function showweathermenu(ev)
    {
        var tempdiv = $("#id_menu_profile");
        if($("#id_menu_profile").html() != null) return false;
        var pos = mousePosition(ev);
        wWidth = 30;
        
        styleStr="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;width:"+wWidth+"px;";
        
        var mydivmenu = "<div class=\"sub_menu\" style=\""+styleStr+"\" id=\"id_menu_profile\"><a onclick='setweather(0)' >今天</a><br /><a onclick='setweather(1)'>明天</a><br /><a onclick='setweather(2)'>后天</a><br /><a onclick='closediv()'>[关闭]</a></div>";
        $(document.body).prepend(mydivmenu);
        $("#id_menu_profile").slideDown(600);
        
     }
     
     
     
    function mousePosition(ev)
    {
        if(ev.pageX || ev.pageY)
        {
            return {x:ev.pageX, y:ev.pageY};
        }
        
        yScroll1 = document.body.scrollTop;
        yScroll2 = document.body.clientTop
        
       if(yScroll1 == 0)
       {
            yScroll1 = document.documentElement.scrollTop;
            yScroll2 = document.documentElement.clientTop;
       }
      
        return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + yScroll1  - yScroll2};
    }
    
    function closediv()
    {
        if($("#id_menu_profile").html() != null)
　      {
　          $("#id_menu_profile").remove();
　      }
    }
    
    function setstat()
	{
		$("a").click(function(){
		    var newsid = $(this).attr("id");

		    if(IsNum(newsid))
		    {
		    var title = $(this).attr("title");
		    var url = $(this).attr("href");
    
		        $.ajax({ 
                    async:false, 
                    url: "http://header.cnool.net/Stat/Stat.ashx?newsid="+newsid+"&title="+escape(title)+"&url="+escape(url)+"&"+new Date().getTime(),  // 跨域URL
                    type: 'GET', 
                    dataType: 'jsonp',  //跨域必须是jsonp
                    jsonp: 'jsoncallback', //默认callback
                    timeout: 5000, 
                    success: function (json) { 
                    }, 
                    error: function(xhr){ 
                       
                    } 
                });

		    }
		});
	}
	
	function IsNum(s)
    {
        if (s!=null && s!="")
        {
            return !isNaN(s);
        }
        return false;
    }
    
    $.fn.reorder = function() {
 
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;
 
    if (childCount > 1) {
      $children.remove();
 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });
 
    }
  }));
}

    
    
    

     

