var Data_ScriptUtil = {
    scriptCount: 538,
    loadTimes: 0,
    timeOut: 100,
    getData: function (src, status, callback) {
        var loader = document.createElement("script");
        loader.type = "text/javascript";
        loader.language = "javascript";
        loader.id = "scriptLoader_" + Data_ScriptUtil.scriptCount;
        loader.src = src + "&Status=" + status;
        //window.open(loader.src);
        var head = document.getElementsByTagName('head').item(0);

        if (head) {
            head.appendChild(loader);
            Data_ScriptUtil.scriptCount++;
        }

        if (callback != null) {

            var eventCallBack = setInterval(function () {
                Data_ScriptUtil.loadTimes++;

                var isOk = eval('typeof (' + status + ') != "undefined"');

                if (isOk) {
                    if (eval(status) == "OK") {
                        callback();
                        clearInterval(eventCallBack);
                        Data_ScriptUtil.delScriptLoader();
                    }
                }

                if (Data_ScriptUtil.loadTimes > 100) {
                    Data_ScriptUtil.onTimeOut();
                    Data_ScriptUtil.loadTimes = 0;
                    clearInterval(eventCallBack);
                    Data_ScriptUtil.delScriptLoader();
                }
            }, 1000);
        }
    },
    delScriptLoader: function () {
        for (var id = Data_ScriptUtil.scriptCount - 1; id > 0; id--) {

            if (document.getElementById("scriptLoader_" + id)) {
                document.getElementById("scriptLoader_" + id).parentNode.removeChild(document.getElementById
("scriptLoader_" + id));
            }
            else {
                id = 0;
            }
        }
    },
    onTimeOut: function () {
        //alert('Time Out');
    },
    getRandomNumber: function () {
        var date = new Date();
        return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(),
date.getSeconds(), date.getMilliseconds()) + Math.random().toString().replace(".", "");
    }
};

var StockData = {
    loaderUrl: "http://data.585.com/OpenDataHandler.ashx?action=openstockdata&rn=@rn&topcount=10",
    getZhiShu: function (dataSource, reLoadTime, callback) {

        var url = StockData.load("stockquotation_more[" + dataSource + "]", "stockid=SH000001,SZ399001");
        //window.open(url);
        Data_ScriptUtil.getData(url, 'zhishuStatus', callback);

        if (reLoadTime != -1) {
            StockData.loadTimes++;
            var zhishuGetEvent = setInterval(function () {
                Data_ScriptUtil.getData(url, 'zhishuStatus', callback);
            }, reLoadTime);
        }
    },
    stockDataUrl: "http://u.laoqianzhuang.com/getStockInfo.axd?type=@type&stockid=@stockid&top=@top",
    getMyStockInfo: function (stockid, top, callback) {
        var sourceUrl = StockData.stockDataUrl.replace("@type", "mystock").replace("@stockid", stockid).replace
("@top", top);
        Data_ScriptUtil.getData(sourceUrl, 'mystockStatus', callback);
    },
    getHoldStockInfo: function (stockid, top, callback) {
        var sourceUrl = StockData.stockDataUrl.replace("@type", "holdStock").replace("@stockid", stockid).replace
("@top", top);
        Data_ScriptUtil.getData(sourceUrl, 'holdStockStatus', callback);
    },
    getStockNotice: function (stockid, callback) {
        stockid = stockid.toUpperCase().replace('SH', '').replace('SZ', '');
        var sourceUrl = StockData.stockDataUrl.replace("@type", "stocknotice").replace("@stockid", stockid).replace
("@top", 1);
        Data_ScriptUtil.getData(sourceUrl, 'stockNotickStatus', callback);
    },
    load: function (dataSource, selectCondition) {

        var sourceUrl = StockData.loaderUrl.replace("@rn", Data_ScriptUtil.getRandomNumber());

        if (dataSource.length > 0) {
            sourceUrl = sourceUrl + "&source=" + dataSource;

        }

        if (selectCondition) {
            sourceUrl = sourceUrl + "&" + selectCondition;
        }
        return sourceUrl;
    }
};

var UserData = {
    isLoadTimes: 0,
    coin: 0,
    uid: -1,
    userName: 'system',
    isVip: -1,
    isCheckEmail: -1,
    emailImgPath: 'http://u.laoqianzhuang.com/Themes/Default/images/@filename',
    isCheckPhone: -1,
    phoneImgPath: 'http://u.laoqianzhuang.com/Themes/Default/images/@filename',
    isCheckIDCard: -1,
    idImgPath: 'http://u.laoqianzhuang.com/Themes/Default/images/@filename',
    isCheckBankCard: -1,
    bankCardImgPath: 'http://u.laoqianzhuang.com/Themes/Default/images/@filename',
    init: function (uid, callback) {


        var head = document.getElementsByTagName("head").item(0);
        var css = document.createElement("link");
        css.href = "http://common.laoqianzhuang.com/css/top_head.css";
        css.rel = "stylesheet";
        css.type = "text/css";
        css.id = "baseCss" + Math.random();
        head.appendChild(css);



        UserData.getCoinAndVip(uid, function () { UserData.isLoadTimes++ });
        //UserData.getAuthentications(uid, function () {  UserData.isLoadTimes++ });
        UserData.getUserMessage(uid, function () { UserData.isLoadTimes++ });


        var EventInit = window.setInterval(function () {
            if (UserData.isLoadTimes == 2) {
                if (typeof (Coin) == "undefined") return;

                UserData.coin = Coin;
                UserData.isVip = Vip.IsVip;
                UserData.Message = UserMessage.message;
                UserData.userName = UserName;
                //                UserData.isCheckEmail = Authentications.IsCheckEmail == 1 ? true : false;

                //                UserData.emailImgPath = UserData.isCheckEmail ? UserData.emailImgPath.replace("@filename", "em_1.gif") : UserData.emailImgPath.replace("@filename", "em.gif");

                //                UserData.isCheckPhone = Authentications.IsCheckPhone == 1 ? true : false;
                //                UserData.phoneImgPath = UserData.isCheckPhone ? UserData.phoneImgPath.replace("@filename", "s_1.gif") : UserData.phoneImgPath.replace("@filename", "s.gif");
                //                UserData.isCheckIDCard = Authentications.IsCheckID == 1 ? true : false;
                //                UserData.idImgPath = UserData.isCheckIDCard ? UserData.idImgPath.replace("@filename", "sy_1.gif") : UserData.idImgPath.replace("@filename", "sy.gif");

                //                UserData.isCheckBankCard = Authentications.IsCheckPayment == 1 ? true : false;

                //                UserData.bankCardImgPath = UserData.isCheckBankCard ? UserData.bankCardImgPath.replace("@filename", "x_1.gif") : UserData.bankCardImgPath.replace("@filename", "x.gif");


                if (callback != null) {
                    callback();
                }
                clearInterval(EventInit);
                UserData.isLoadTimes = 0;
            }
        }, 1000);

    },
    userCoinAndVipDataSourceUrl: 'http://pay.lqz.cn/UserInfoHandler.axd?uid=@uid',
    userAuthenticationsUrl: 'http://u.laoqianzhuang.com/UserAuthenticationsHandler.axd?uid=@uid',
    getCoinAndVip: function (uid, callback) {
        var url = UserData.userCoinAndVipDataSourceUrl.replace('@uid', uid);
        Data_ScriptUtil.getData(url, 'headCoinAndVipStatus', callback);
    },
    userCoinSourceUrl: 'http://pay.lqz.cn/CoinHandler.axd?uid=@uid',
    getCoin: function (uid, callback) {
        var url = UserData.userCoinSourceUrl.replace('@uid', uid);
        Data_ScriptUtil.getData(url, 'otherCoinStatus', callback);
    },
    getAuthentications: function (uid, callback) {
        var url = UserData.userAuthenticationsUrl.replace('@uid', uid);
        Data_ScriptUtil.getData(url, 'AuthenticationStatus', callback);
    },
    getUserMessage: function (uid, callback) {
        var url = "http://bbs.laoqianzhuang.com/login/userid.php?uid=" + uid;
        Data_ScriptUtil.getData(url, 'UserMessageStatus', callback);
    },
    setWebHeadHTML: function (id, uid) {

        UserData.init(uid, function () {
            var url = window.location.href;


            //头像

            var photo = "<div class=\"photo\">";
            photo += "<img alt=\"\" src=\"http://ucenter.laoqianzhuang.com/avatar.php?uid=" + uid + "&amp;size=small\" />";
            photo += "</div>";



            //用户信息

            var userInfo = "<div class=\"user\">";
            userInfo += "<p style=\"line-height:18px;\">";

            //vip信息
            if (UserData.isVip) {
                userInfo += "<a href=\"http://h.laoqianzhuang.com/home.php?mod=space&amp;do=home\" style=\"margin:0px;color:red\">" + UserData.userName + "</a>　";
            }
            else {
                userInfo += "<a href=\"http://h.laoqianzhuang.com/home.php?mod=space&amp;do=home\" style=\"margin:0px;\">" + UserData.userName + "</a>　";
            }
            userInfo += " <span><a title=\"" + Vip.Tip + "\" href=\"###\" onclick=\"UserData.VipIconClick()\" class=\"pimg\" style=\"margin:0px;\"><img src=\"" + Vip.VipImageUrl + "\" alt=\"" + Vip.Tip + "\">[<span style=\"color:red\">升级</span>]</a>&nbsp;&nbsp;&nbsp;</span>";


            //            userInfo += " <span><a title=\"" + Authentications.EmailTip + "\" href=\"http://u.laoqianzhuang.com/emailcheck.aspx\" style=\"margin:0;\"><img src=\"" + UserData.emailImgPath + "\"></a></span>";
            //            userInfo += " <span><a title=\"" + Authentications.PhoneTip + "\" href=\"http://u.laoqianzhuang.com/phonecheck.aspx\" style=\"margin:0;\"><img src=\"" + UserData.phoneImgPath + "\"></a></span>";
            //            userInfo += " <span><a title=\"" + Authentications.PaymentTip + "\" href=\"http://u.laoqianzhuang.com/paymentcheck.aspx\" style=\"margin:0;\"><img src=\"" + UserData.bankCardImgPath + "\"></a></span>";
            //            userInfo += " <span><a title=\"" + Authentications.IDTip + "\" href=\"http://u.laoqianzhuang.com/idcheck.aspx\" style=\"margin:0;\"><img src=\"" + UserData.idImgPath + "\"></a></span>";


            //金币
            userInfo += "| <span class=\"mory\">金币:" + UserData.coin + "</span>";
            userInfo += "<a  style=\"margin:0 2px 0 2px;color:red;\" onclick=\"return UserData.RechargeClick()\"  href=\"http://u.laoqianzhuang.com/topup.aspx\" class=\"chong_a\">[充值]</a> ";


            //短消息
            if (UserData.Message != 0) {
                userInfo += "|<span class=\"dxiao\" >&nbsp;</span><a style=\"margin:0 8px 0 0\"  href=\"http://h.laoqianzhuang.com/home.php?mod=space&do=pm&filter=newpm\">短消息(" + UserData.Message + ")</a>";
            } else {
                userInfo += "|<span class=\"dxiao\" >&nbsp;</span><a style=\"margin:0 8px 0 0\"  href=\"http://h.laoqianzhuang.com/home.php?mod=space&do=pm&filter=newpm\">短消息</a>";
            }

            //退出


	   if(url.indexOf('weibo')>0)
	       userInfo += "|<a href=\"http://t-weibo.laoqianzhuang.com/?mod=login&code=logout\">退出</a></p>";
	      else
            userInfo += "|<a href=\"http://u.laoqianzhuang.com/SynLogin.aspx?action=logOut&url=" + url + "\">退出</a></p>";




            //第二行开始
            //个人中心
            userInfo += "<p><span class=\"uso\"><a style=\"margin:0 8px 0 0\" class=\"la\" id=\"usersetup\" href=\"http://h.laoqianzhuang.com/home.php\" onmouseout=\"UserData.hideMyCenterMenu()\" onmouseover=\"UserData.showMyCenterMenu ()\">个人中心</a>";

            //会员中心
            userInfo += "<span class=\"vuso\"><a style=\"margin:0 8px 0 0\" class=\"la\" id=\"usersetup\" href=\"http://u.laoqianzhuang.com/\"  onmouseout=\"UserData.hideSettingMenu()\" onmouseover=\"UserData.showSettingMenu()\">会员中心</a>";

            //好友 
            userInfo += "<span class=\"mfr\"><a style=\"margin:0 8px 0 0\" href=\"###\" onclick=\"openFriendDialog()\">好友</a></span>";

            //自选股
            userInfo += "<span class=\"mzhi\"><a style=\"margin:0 8px 0 0\" href=\"http://u.laoqianzhuang.com/default.aspx\">自选股</a></span>";

            //提醒
            userInfo += "<span class=\"mtin\"><a style=\"margin:0 8px 0 0\" href=\"http://h.laoqianzhuang.com/home.php?mod=space&amp;do=notice\">提醒</a></span>";


            //userInfo += "|<a href=\"http://h.laoqianzhuang.com/home.php?mod=task&amp;item=new\">任务</a></p>";
            userInfo += "</p></div>";


            document.getElementById(id).innerHTML = photo + userInfo;
            UserData.setPageTitle(UserData.Message);
        });

    },
    setPageTitle: function (msgCount) {
        var title = document.title;
        var titleIndex = 0;
        var msg = "您有" + msgCount + "条短消息";
        var msgBlank = "";
        for (var qqqq = 0; qqqq < msg.length - 1; qqqq++) {
            msgBlank = msgBlank + "　";
        }
        var arr = ["【" + msg + "】" + title, "【" + msgBlank + "】" + title];
        if (msgCount > 0) {
            setInterval(function () {
                if (titleIndex >= arr.length) titleIndex = 0;
                document.title = arr[titleIndex++];
            }, 1000);
        }
    },
    setZhiShu: function (id) {
        StockData.getZhiShu("zhishu", 10000, function () {

            var zhishuContext = "";
            var change1 = zhishu.SH000001.PriceNew - zhishu.SH000001.PriceLastClose;
            var changePer1 = ((change1 / zhishu.SH000001.PriceLastClose) * 100).toFixed(2) + "%";
            changePer1 = change1 > 0 ? "+" + changePer1 : changePer1;
            var c1 = change1 > 0 ? "red" : "green";
            var c3 = change1 > 0 ? "redico" : "greenico";

            var change2 = zhishu.SZ399001.PriceNew - zhishu.SZ399001.PriceLastClose;
            var changePer2 = ((change2 / zhishu.SZ399001.PriceLastClose) * 100).toFixed(2) + "%";
            changePer2 = change2 > 0 ? "+" + changePer2 : changePer2;
            var c2 = change2 > 0 ? "red" : "green";
            var c4 = change2 > 0 ? "redico" : "greenico";

            zhishuContext += "<div class=\"hangqing\"><a href='http://t.laoqianzhuang.com/SH000001'>上证指数</a>：<font class=\"" + c1 + "\">" + zhishu.SH000001.PriceNew + "　" + changePer1 + "</font><span class=\"" + c3 + "\">&nbsp;</span>　";
            zhishuContext += "<a href='http://t.laoqianzhuang.com/SZ399001'>深证指数</a>：<font class=\"" + c2 + "\">" + zhishu.SZ399001.PriceNew + "　" + changePer2 + "</font><span class=\"" + c4 + "\">&nbsp;</span></div>";

            document.getElementById(id).innerHTML = zhishuContext;
        });
    },

    showSettingMenu: function () { },
    hideSettingMenu: function () { },

    showMyCenterMenu: function () { },
    hideMyCenterMenu: function () { },

    VipIconClick: function () { },

    RechargeClick: function () { }

};

var StockGame = {
    getStockGameInfo: function (uids, callback) {
        var url = 'http://s.laoqianzhuang.com/GetUserInfoHandler.aspx?uid={@uid}';
        url = url.replace('{@uid}', uids);
        Data_ScriptUtil.getData(url, 'getStockGameInfoStatus', callback);
    }
};
