//网址搜索
var qsearch_cb = function(arr){
	var str;
	var html;	
	$.each(arr,function(i,v){
		html = '';
		if(i == 0){
			str = (arr[0].split('|'))[0];
			return;			
		}
		html = '<li><a href="'+arr[i][4]+'">'+hlk(arr[i])+'</a></li>';
		$('#bests').append(html);						
	});
};
var hlk=function(v){
	var hlw;
	if (v[2]=='url'){
		return v[3];
	} else {
		hlw = v[3].substr(0,v[0])+'<span class="hot2">'+v[3].substring(v[0],v[1]+1)+'</span>'+v[3].substr(v[1]+1);		
		return hlw;		
	}
};
var getBit = function(m, n){
    return (m >> n) & 1
}
//日历
var lunar = function(d){
    var mons = "正二三四五六七八九十冬腊", Cal = [0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95];
    var total, m, n, k, isEnd = false, t = d.getYear();
    if (t < 1900) 
        t += 1900;
    total = (t - 2001) * 365 + Math.floor((t - 2001) / 4) + [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334][d.getMonth()] + d.getDate() - 23;
    if (d.getYear() % 4 == 0 && d.getMonth() > 1) 
        total++;
    for (m = 0; m < 1000; m++) {
        k = (Cal[m] < 0xfff) ? 11 : 12;
        for (n = k; n >= 0; n--) {
            if (total <= 29 + getBit(Cal[m], n)) {
                isEnd = true;
                break;
            }
            total = total - 29 - getBit(Cal[m], n);
        }
        if (isEnd) 
            break;
    }
    var cYear = 2001 + m, cMonth = k - n + 1;
    if (k == 12) {
        if (cMonth == Math.floor(Cal[m] / 0x10000) + 1) 
            cMonth = 1 - cMonth;
        if (cMonth > Math.floor(Cal[m] / 0x10000) + 1) 
            cMonth--;
    }
    var t = "";
    if (cMonth < 1) {
        t += "闰";
        t += mons.charAt(-cMonth - 1);
    }
    else 
        t += mons.charAt(cMonth - 1);
    t += "月";
    t += (total < 11) ? "初" : ((total < 20) ? "十" : ((total < 30) ? "廿" : "卅"));
    if (total % 10 != 0 || total == 10) 
        t += "一二三四五六七八九十".charAt((total - 1) % 10);
    return t;
}

//时间
var date = function(){
    var arg = arguments[0];
    var d = arg ? new Date(arg) : new Date();
    var ymd = d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日", week = ['日', '一', '二', '三', '四', '五', '六'];
    var xz = getCookie('XZ');
    if (!xz) 
        xz = "0";
    return ('<a href=http://wannianli.1616.net>' + ymd + "<br>" + lunar(d) + "&nbsp; &nbsp; 周" + week[d.getDay()] + '</a><a href=http://huangli.1616.net>黄历</a>&nbsp; <a href=http://yuncheng.1616.net/' + xz + '.htm>运程</a>&nbsp; <a class=clocklink href="#" target=_self id="clocklink"> 闹钟 <span id=clockhint style="display:none"></span></a>');
}

//COOKIE
var getCookie = function(n){
    var c = document.cookie.split("; ");
    for (var i = 0; i < c.length; i++) {
        var p = c[i].split("=");
        if (n == p[0]) 
            try {
                return decodeURIComponent(p[1])
            } 
            catch (e) {
                return null
            }
    }
    return null;
} 
$(function(){	
	$.search({
		list:{google:['http://static.5258.net/styles/5258/images/google.gif','谷歌搜索'],soso:['http://static.5258.net/styles/5258/images/soso.gif','soso搜索']},
	    web:{
			se:{
				google:{
					action:'http://www.google.com.hk/search',
					query:'q',
					params:{
					    client:'aff-5258',
						channel:'searchbox',
						newwindow:'1'
					},
					type:21
				},
				soso:{
					action:'http://www.soso.com/q',
					query:'w',
					params:{
						cid:'union.s.wh',
						unc:sosoChannel,
						ie:'utf-8'
					},
					type:41
				}
		    },
			checked:'google'			
		},
	   mp3:{
	   	  se:{
		  	google:{
					action:'http://www.google.cn/music/search',
					query:'q',
					params:{								    
						hl:'zh-CN'
					},
					 type:24 
		   }
		  },
		  checked:'google'		  
	   },
	   video:{
	   	   se:{
		   	 google:{
			 	action:'http://www.google.com.hk/videohp',
				query:'q',
				params:{								    
					hl:'zh-CN'														
			    },
				type:26
		     }					   
	      },
	   checked:'google'	   
	   },
	    image:{
	   	   se:{
		   	 google:{
			 	action:'http://images.google.com.hk/images',
				query:'q',
				params:{								    
				    hl:'zh-CN'														
			    },
				type:25
		   }					   
	   },
	   checked:'google'	   
	},
	  tieba:{
	   	   se:{
		   	 google:{
			 	action:'http://laiba.tianya.cn/laiba/Glue',
				query:'q',
				params:{								    
				    hl:'zh-CN'														
			    },
				type:22
		   }					   
	   },
	   checked:'google'	   
	},
	zhidao:{
	   	   se:{
		   	 google:{
			 	action:'http://wenda.tianya.cn/wenda/search',
				query:'q',
				params:{								    
				    hl:'zh-CN'														
			    },
				type:23
		   }					   
	   },
	   checked:'google'	   
	}
	},'tsearch','cur','tablist','td', 'ff1', 'snpic', 'web', 'tsinput');
	 $('#tsinput').focus();
    $('.topfav a').click(function(){       
		addToFav(location.href, '1616上网导航');
    });
    $('#sethome, #logo').click(function(){
        setHomePage();
    });
    $('#dat_output').html(date());
	    $('#best-sc a').each(function(i){
        $(this).click(function(){
            $('#best-sc a').each(function(){
                $(this).removeClass('cur');
            });
            $(this).addClass('cur');
            switch (i) {
                case 0:
                    $('#other').hide();
                    $('#bests').show();
					$('#fs').show();
					if (!$.isEmptyObject(f_s)) {
						$('#bests').html(f_s);
					}
                    break;
                case 1:
                    $('#bests').hide();
                    $('#other').show();
					$('#fs').hide();
                    $('#ifother').attr('src', 'http://www.1616.net/jd/am/stock.htm');
                    break;
            }
        });
    });
	var f_s = {};
	$('#fs-kw').bind('keyup focus',function(){		
		if ($.isEmptyObject(f_s)) {
		    f_s = $('#bests').contents();						
		}
		$('#bests').empty();		
        var k=($.trim($(this).val()));
	    if(k.length){	   
			$.getScript('http://so.1616.net/qsearch/?q='+euc(k)+'&n=0,36');								
		}else{					
			$('#bests').append(f_s);
		}
	});		
});
