﻿$(function() {
	///$("#topNav").fadeIn(1600);
	$(".fadeIn").fadeIn(2350);
    if ($("#updatetime").length > 0)
    {
        refresh({act:"time,topstock"}, 60000);
    }
    
    setTimeout("refresh({act:'topstock'}, 180000);", 180000);
        var text = "股票代码/拼音/中文";
        $.include(["http://www.bestgo.com/js/lib/astock.js",homedir+"/js/SuggestFramework.js"], function() {
            var s = function() {
                setTimeout(function() {
                    if(typeof Suggest != "function") 
                    {
                        s();
                        return;
                    }
                    window["searchcode"] = new Suggest("searchcode", $("#searchcode").val(), null, "astock_suggest", StockSuggestConfiguration, null, [200, 800, 0.95, "solid", "#EEE", "#FFF", 3, 3, 0.2, "#000", "#444", "#e5ebfb", false]);
                    if ($("#datagrid").length > 0) 
                    {
                        $.globalEval("var stocknames={"+astock_suggest.replace(/~?(\w*\d{6})`(.*?)`([a-zA-Z0-9*]{0,})~?/gi,',"$1":["$2","$3"]').substr(1)+"}");
                    }
                }, 50);
            };
            s();
        });
        
    if ($("#searchform").length > 0) {
        $("#searchform").submit(function() {
            return searchForm(this);
        });
    }
    $(".tabOrder ul li[class!='title'] a").mouseover(function() {
        var id = $(this).attr("href");
        var pthis = $(this).parents(".tabOrder");
        pthis.find("ul li").removeClass("hover");
        $(this).parent("li").addClass("hover");
        pthis.find(".tabCon").hide();
        $(id).show();
    }).click(function() {
        return false;
    });
    if ($("#ddeTable").length > 0) 
    {
        var th = $("#ddeTable th");
    }
});
function isOpen(){
	var d = new Date();
	var s = new Date(d.format("yyyy/MM/dd") + " 9:29:00");
	var w = new Date(d.format("yyyy/MM/dd") + " 11:30:00");
	var t = new Date(d.format("yyyy/MM/dd") + " 13:00:00");
	var e = new Date(d.format("yyyy/MM/dd") + " 15:01:00");
	var i = d.getUTCDay();
	if (i==6 || i ==0){return false;}
	return ((s.dateDiff("n",d) > 0 && w.dateDiff("n",d)<=0) || (t.dateDiff("n",d)>=0 && e.dateDiff("n",d) < 0))
}
function isClose() {
	var d = new Date();
	var e = new Date(d.format("yyyy/MM/dd") + " 15:00:00");
	return (d.dateDiff("n",e) < 0)
}
function searchForm($t)
{
    var code = $("#searchcode").val();
    if (/(SH|SZ|B$)?\d{6}/gi.test(code)) 
    {
        if (code.length != 8) 
        {
            code = aStockType(parseInt(code)) + code;
            $("#searchcode").val(code);
        }
    }
    return true;
}
function aStockType(code) {
    if (code >= 600000 && code < 700000) {
        return "SH";
    }
    if (code < 600000) {
        return "SZ";
    }
    return "B$";
}
function refresh(data, second, callback)
{
    ajax(data, callback);
    setTimeout(function() 
    {
        refresh(undefined, second, callback);
    }, (second ? second : 60000));
}

function ajax(data, callback)
{
    if (data == undefined)
    {
        data = {};
    }
    var $code = $("#stockcode");
    if ($code.length > 0) 
    {
        data.q = $code.text();
    }
    $.getJSON("/ajax.ashx?dde_json=?", data, function(jd) 
    {
        if (jd.code == 200)
        {
            jd.callback();
        }
        if (callback) {
            callback();
        }
    });
}
    $.extend({
        includePath: '',
        include: function(file, callback)
        {
            var ele; 
            var files = typeof file == "string" ? [file] : file;
            var head = $("head");
            for (var i = 0; i < files.length; i++)
            {
                var name = files[i].replace(/^\s|\s$/g, "");
                var att = name.split('.');
                var ext = att[att.length - 1].toLowerCase();
                var isCSS = (ext == "css");
                var tag = isCSS ? "link" : "script";
                var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
                var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
                if ($(tag + "[" + link + "]").length == 0) {
                    ele = "<" + tag + attr + link + "></" + tag + ">";
                    head.append(ele);
                }
            }

            if (callback) 
            {
                callback();
            }
        }, loading : function(data, css) {
            if ("hide" === data)
            {
                $("#loading").hide();
                return;
            }
            var $$ = $("#loading");
            if ($$.length == 0)
            {
                $$ = $("<div/>", {
                    id : "loading"            
                }).append("<span class='img'></span>").appendTo("body");
            }
            $$.css(css).html(data).show();
        }
    });
function DateAdd(interval,number,date){
number = parseInt(number);
if (typeof(date)=="string"){
date = date.split(/\D/);
--date[1];
eval("var date = new Date("+date.join(",")+")");
}
if (typeof(date)=="object"){
	var date = date
}
switch(interval){
case "y": date.setFullYear(date.getFullYear()+number); break;
case "m": date.setMonth(date.getMonth()+number); break;
case "d": date.setDate(date.getDate()-number); break;
case "w": date.setDate(date.getDate()+7*number); break;
case "h": date.setHours(date.getHour()+number); break;
case "n": date.setMinutes(date.getMinutes()+number); break;
case "s": date.setSeconds(date.getSeconds()+number); break;
case "l": date.setMilliseconds(date.getMilliseconds()+number); break;
}
return date;
}
Date.prototype.format = function(format)
{
   var o = {
     "M+" : this.getMonth()+1,
     "d+" : this.getDate(),
     "h+" : this.getHours(),
     "m+" : this.getMinutes(),
     "s+" : this.getSeconds(),
     "q+" : Math.floor((this.getMonth()+3)/3),
     "S" : this.getMilliseconds()
   }
   if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
     (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   for(var k in o)if(new RegExp("("+ k +")").test(format))
     format = format.replace(RegExp.$1,
       RegExp.$1.length==1 ? o[k] : 
         ("00"+ o[k]).substr((""+ o[k]).length));
   return format;
}
Date.prototype.dateDiff = function(interval,objDate){
    if(arguments.length<2||objDate.constructor!=Date) return undefined;
    switch (interval) {
      case "s":return parseInt((objDate-this)/1000);
      case "n":return parseInt((objDate-this)/60000);
      case "h":return parseInt((objDate-this)/3600000);
      case "d":return parseInt((objDate-this)/86400000);
      case "w":return parseInt((objDate-this)/(86400000*7));
      case "m":return (objDate.getMonth()+1)+((objDate.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
      case "y":return objDate.getFullYear()-this.getFullYear();
      default:return undefined;
    }
}
