var softid = "-1"; 
var  city = "北京";
function writeUserData(str) {
    var file = external.getModulePath() + "zgl\\index\\x.dat";
    var fos = external.createObject("FileOutputStream");
    fos.open(file);
    fos.write(str);
    fos.close();
    fos = null;
    var file = external.getModulePath() + "zgl\\index\\dat\\g.dat";
    var fos = external.createObject("FileOutputStream");
    fos.open(file);
    fos.write("0");
    fos.close();
    fos = null;
}

function zhuti($zhuti) {
    if ($zhuti == "") return;
    if ($zhuti == "default") {
        $("html").css({
            background: "url(css/default.bmp)"
        });
    } else if ($zhuti.substr(0, 1) == "#") {
        $("html").css({
            background: $zhuti
        });
    } else {
        $("html").css({
            background: "url(" + $zhuti + ")"
        });
    }
}

function readUserData() {
    var file = external.getModulePath() + "zgl\\index\\dat\\g.dat";
    var fis = external.createObject("FileInputStream");
    fis.open(file);
    str = fis.read();
    fis.close();
    fis = null;
    if (str == "1") {
        var file = external.getModulePath() + "zgl\\index\\dat\\z.dat";
        var fis = external.createObject("FileInputStream");
        fis.open(file);
        str = fis.read();
        fis.close();
        fos = null;
        $("html").css({
            background: str
        });
    } else {
        var file = external.getModulePath() + "zgl\\index\\x.dat";
        var fis = external.createObject("FileInputStream");
        fis.open(file);
        str = fis.read();
        fis.close();
        fis = null;
        zhuti(str.split(",")[0]);
    }
}

$(document).ready(function() {
    $.fn.jPicker.defaults.images.clientPath = "lib/images/";
    $("#cpicker").jPicker({
        window: {
            title: "请选择样式",
            expandable: true,
            effects: {
                type: "slide",
                speed: {
                    show: "fast",
                    hide: "fast"
                }
            },
            liveUpdate: false,
            position: {
                y: "bottom",
                x: "right"
            }
        },
        localization: {
            text: {
                title: "自定义主题颜色",
                newColor: "新颜色",
                currentColor: "当前",
                ok: "确定",
                cancel: "取消"
            },
            tooltips: {
                colors: {
                    newColor: "New Color - Press &ldquo;OK&rdquo; To Commit",
                    currentColor: "Click To Revert To Original Color"
                },
                buttons: {
                    ok: "选择颜色",
                    cancel: "取消"
                },
                saturation: {
                    radio: "Set To &ldquo;Saturation&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Saturation&rdquo; Value (0-100%)"
                },
                value: {
                    radio: "Set To &ldquo;Value&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Value&rdquo; Value (0-100%)"
                },
                red: {
                    radio: "Set To &ldquo;Red&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Red&rdquo; Value (0-255)"
                },
                green: {
                    radio: "Set To &ldquo;Green&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Green&rdquo; Value (0-255)"
                },
                blue: {
                    radio: "Set To &ldquo;Blue&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Blue&rdquo; Value (0-255)"
                },
                alpha: {
                    radio: "Set To &ldquo;Alpha&rdquo; Color Mode",
                    textbox: "Enter A &ldquo;Alpha&rdquo; Value (0-100)"
                },
                hex: {
                    textbox: "Enter A &ldquo;Hex&rdquo; Color Value (#000000-#ffffff)",
                    alpha: "Enter A &ldquo;Alpha&rdquo; Value (#00-#ff)"
                }
            }
        }
    }, function(color, context) {
        var all = color.val("all");
        var file = external.getModulePath() + "zgl\\index\\dat\\z.dat";
        var fos = external.createObject("FileOutputStream");
        fos.open(file);
        fos.write("#" + all.hex);
        fos.close();
        fos = null;
        var file = external.getModulePath() + "zgl\\index\\dat\\g.dat";
        var fos = external.createObject("FileOutputStream");
        fos.open(file);
        fos.write("1");
        fos.close();
        fos = null;
        $("html").css({
            background: "#" + all.hex
        });
    });
    readUserData();
    $(".huanfu").click(function() {
        writeUserData($(this).attr("zhuti") + "," + $(this).attr("index"));
        zhuti($(this).attr("zhuti"));
        if ($(this).attr("zhuti").substr(0, 1) != "#") document.execCommand("Refresh");
    });
});

$(function() {
    $.get("../zixun/rp", function(data) {
        $.post(httphost + "user/?yhid=" + data, function(json) {
            softid = json.softid;
            $("#userinfo").html(json.info);
            $("#softtitle").html(json.softinfo);
        }, "jsonp");
    });
    var thsQuote = external.createObject("Quote");
    $("[tid]").bind("click", function() {
        /**if (softid == -1) {
            thsQuote.switchPage({
                id: 5346
            });
            return false;
        }
        var x = parseFloat($(this).attr("tid")) + 6500;
if (softid.indexOf(x) == -1) thsQuote.switchPage({
            id: x
        }); else 
**/        
 var x = parseFloat($(this).attr("tid")) ;
if($(this).attr("rid"))
{
 var rid=$(this).attr("rid");
 if(rid=='1'){
   if(tequan.length>1){
   if(tequan[1]=='0'){
     x=37361;
   }else{
    if($(this).attr("zijin"))
	{
	   if(tequan[2]==''||parseFloat(tequan[2])<=parseFloat($(this).attr("zijin"))){
		x=37361;
	   }		
	}
   }
 }
 }
}
thsQuote.switchPage({
            id: x        
				});
        return false;
    });
    var stockData = {
        getData: function(codelist, params, callback) {
            var thsQuote = external.createObject("Quote");
            var reqObj = {
                code: codelist,
                type: params.join(","),
                onready: function() {
                    var codeArr = codelist.split(",");
                    var data = {};
                    for (var i = 0, pLength = params.length; i < pLength; i++) {
                        var param = {
                            code: codelist,
                            type: params[i]
                        };
                        var retStr = thsQuote.getData(param);
                        if (retStr != "({})") {
                            retObj = eval("(" + retStr + ")");
                        } else {
                            retObj = null;
                        }
                        if (retObj !== null) {
                            for (var j = 0, cdLength = codeArr.length; j < cdLength; j++) {
                                if (typeof data[codeArr[j]] == "undefined") {
                                    data[codeArr[j]] = {};
                                }
                                data[codeArr[j]][params[i]] = retObj[codeArr[j]][params[i]];
                            }
                        } else {
                            for (var j = 0, cdLength = codeArr.length; j < cdLength; j++) {
                                if (typeof data[codeArr[j]] == "undefined") {
                                    data[codeArr[j]] = {};
                                }
                                data[codeArr[j]][params[i]] = null;
                            }
                        }
                    }
                    callback(data);
                }
            };
            var flag = thsQuote.request(reqObj);
        }
    };
    var st = null;
    function setbg(obj, c, bc) {
        color = obj.css("color");
        bg = obj.css("background");
        obj.css({
            color: c,
            background: bc
        });
        st = setTimeout(function() {
            obj.css({
                background: ""
            });
        }, 1e3);
    }
    var xto = null;
    var db = 0;
    var getDataByCode = function(codelist) {
        var params = [ "ZQMC", "new", "open", "high", "open" ];
        stockData.getData(codelist, params, function(data) {
            var d = [];
            var dd = [];
            var dd1 = [];
            var codeArr = codelist.split(",");
            dd.push();
            if (db == 0) {
                for (var j in codeArr) {
                    d.push("<tr>");
                    for (var i in params) {
                        if (i == 4) continue;
                        d.push("<td class='t" + i + "' id='" + codeArr[j] + i + "'>");
                        if (data[codeArr[j]][params[i]] != null) {
                            if (i != 0) {
                                d.push(ForDight(data[codeArr[j]][params[i]], 2));
                            } else {
                                d.push(data[codeArr[j]][params[i]]);
                            }
                        } else {
                            d.push("-");
                        }
                        d.push("</td>");
                    }
                    d.push("</tr>");
                }
                $("#gui").find("tbody").html(d.join(""));
                db++;
            } else {
                var fcode = false;
                for (var j in codeArr) {
                    fcode = false;
                    for (var i in params) {
                        if (i == 0) continue;
                        if (ForDight(data[codeArr[j]][params[1]], 2) == $("#" + codeArr[j] + "1").html()) {
                            fcode = true;
                            continue;
                        }
                        if (fcode) continue;
                        if ($("#" + codeArr[j] + i).length > 0) {
                            if (data[codeArr[j]][params[i]] != null) {
                                if (data[codeArr[j]][params[1]] > data[codeArr[j]][params[4]]) setbg($("#" + codeArr[j] + i), "#FF0000", "url(skin/tbg.png)"); else if (data[codeArr[j]][params[1]] < data[codeArr[j]][params[4]]) setbg($("#" + codeArr[j] + i), "#00C800", "url(skin/tbg.png)"); else if (data[codeArr[j]][params[1]] == data[codeArr[j]][params[4]]) setbg($("#" + codeArr[j] + i), "#FFFFFF", "url(skin/tbg.png)");
                                $("#" + codeArr[j] + i).html(ForDight(data[codeArr[j]][params[i]], 2));
                            } else {
                                $("#" + codeArr[j] + i).html("-");
                            }
                        }
                    }
                }
            }
        });
    };
    function t() {
        if (xto != null) clearTimeout(xto);
        getDataByCode("YGAG00,YGAG95,YGAG99,SILVER,TJAGOO,XAGUSD,CONC88");
        xto = setTimeout(t, 4e3);
    }
    var r = parseInt(Math.random() * geyan.length);
    $("#geyan").html(geyan[r]);
    $("#gui").find("thead").html("<tr><th>名称</th><th>现价</th><th>开盘</th><th>最高</th></tr>");
    t();
});
function bdNewsJsonCallBack(json){
    setHtml(json);
  } 
 function getLocalFormatTime(nS) {  
	 s= new Date(parseInt(nS) * 1000);
	 s1=(s.getMonth()+1)+'-'+s.getDate()+' '+s.getHours()+":"+s.getMinutes(); 
	 	return s1;
 } 
 function setHtml(json){ 
 	if(json!=null){ 
			 $d=[];
			 $l=10;
			 d=json.feed.entry; 
			   if(d.length>1){
				   $l = d.length - 1 > 6 ? 6 : d.length - 1;
			 $d.push("<div>" + city + "新闻</div>");
			  for(var i=0;i<$l;i++){ 
			  	$d.push("<div " + (i == 0 ? 'style="font-weight:600;font-size:20px;margin-top:10px;font-family:mini;"' : 'style="margin-top:5px;"') + ">");
                $d.push('<a href="javascript:void(0);" onclick="wkstock.openUrl(\'',d[i]["url"], '\')">', d[i]["title"].length > 14 ? d[i]["title"].substring(0, 14) : d[i]["title"], "</a>&nbsp;&nbsp;");
                $d.push(getLocalFormatTime(d[i]['time']));
                $d.push("</div>");
			  	} 
			  $("#bdnews").html($d.join(""));
			  $d=[];
			}
			else $("#bdnews").html('\u6682\u65f6\u6ca1\u6709\u8be5\u5730\u533a\u7684\u65b0\u95fb');
 	}
   }
$(function() {
    var local_cities = {
        "安徽": [ "安庆|3131|0", "蚌埠|3095|0", "亳州|3211|0", "巢湖|3192|0", "池州|3218|0", "滁州|3158|0", "阜阳|3171|0", "合肥|3073|0", "淮北|3119|0", "淮南|3102|0", "黄山|3147|0", "六安|3200|0", "马鞍山|3112|0", "宿州|3184|0", "铜陵|3126|0", "芜湖|3084|0", "宣城|3225|0" ],
        "福建": [ "福州|3373|0", "龙岩|3485|0", "南平|3469|0", "宁德|3496|0", "莆田|3423|0", "泉州|3433|0", "三明|3404|0", "厦门|3393|0", "漳州|3452|0" ],
        "甘肃": [ "定西|8626|0", "嘉峪关|8570|0", "金昌|8548|0", "酒泉|8604|0", "兰州|8385|0", "临夏|8664|0", "陇南|8637|0", "平凉|8593|0", "庆阳|8615|0", "天水|8559|0", "武威|8576|0", "张掖|8583|0" ],
        "广东": [ "潮州|5685|0", "东莞|5588|0", "佛山|5611|0", "广州|5496|0", "河源|5560|0", "江门|5600|0", "揭阳|5690|0", "茂名|5643|0", "梅州|5570|0", "清远|5666|0", "汕头|5530|0", "汕尾|5583|0", "韶关|5543|0", "深圳|5515|0", "阳江|5622|0", "云浮|5698|0", "湛江|5629|0", "肇庆|5653|0", "中山|9480|0", "珠海|5525|0", "惠州|5706|0" ],
        "广西": [ "百色|5992|0", "北海|3407|0", "防城港|5961|0", "贵港|5975|0", "桂林|5918|0", "河池|6018|0", "贺州|6011|0", "柳州|5904|0", "南宁|5887|0", "钦州|5968|0", "梧州|5944|0", "玉林|5982|0" ],
        "贵州": [ "安顺|7274|0", "毕节|7301|0", "贵阳|7231|0", "六盘水|7247|0", "黔东南|7327|0", "黔南|7352|0", "黔西南|7314|0", "铜仁|7284|0", "遵义|7254|0" ],
        "海南": [ "儋州|6295|0", "海口|6246|0", "三亚|6253|0", "万宁|6277|0", "文昌|6265|0", "五指山|6283|0" ],
        "河北": [ "保定|372|0", "沧州|440|0", "承德|428|0", "邯郸|317|0", "衡水|487|0", "廊坊|471|0", "秦皇岛|306|0", "石家庄|251|0", "唐山|286|0", "邢台|346|0", "张家口|406|0" ],
        "河南": [ "安阳|4482|0", "鹤壁|4461|0", "济源|4613|0", "焦作|4447|0", "开封|4392|0", "洛阳|4408|0", "漯河|4514|0", "南阳|4531|0", "平顶山|4431|0", "濮阳|4496|0", "三门峡|4521|0", "商丘|4551|0", "新乡|4468|0", "信阳|4565|0", "许昌|4504|0", "郑州|4372|0", "周口|4581|0", "驻马店|4597|0" ],
        "黑龙江": [ "大庆|2056|0", "大兴安岭|2160|0", "哈尔滨|1968|0", "鹤岗|2019|0", "黑河|2134|0", "鸡西|2042|0", "佳木斯|2113|0", "牡丹江|2097|0", "七台河|2127|0", "齐齐哈尔|1994|0", "双鸭山|2029|0", "绥化|2144|0", "伊春|2072|0" ],
        "湖北": [ "鄂州|4910|0", "恩施|4956|0", "黄冈|4926|0", "黄石|4832|0", "荆门|4902|0", "荆州|4869|0", "潜江|4981|0", "十堰|4856|0", "随州|4952|0", "天门|4975|0", "武汉|4812|0", "仙桃|4969|0", "咸宁|4942|0", "襄樊|4842|0", "孝感|4915|0", "宜昌|4882|0" ],
        "湖南": [ "长沙|5162|0", "常德|5250|0", "郴州|5281|0", "衡阳|5198|0", "怀化|5315|0", "娄底|5334|0", "邵阳|5217|0", "湘潭|5190|0", "湘西|5342|0", "益阳|5273|0", "永州|5298|0", "岳阳|5236|0", "张家界|5266|0", "株洲|5176|0" ],
        "吉林": [ "吉林|1800|0", "白山|1842|0", "长春|1784|0", "辽源|1824|0", "四平|1814|0", "通化|1831|0", "延边|1852|0" ],
        "江苏": [ "常州|2620|0", "淮安|2542|0", "连云港|2531|0", "南京|2494|0", "南通|2597|0", "苏州|9100|0", "宿迁|2555|0", "泰州|2587|0", "无锡|2631|0", "徐州|2514|0", "盐城|2563|0", "扬州|2576|0", "镇江|2610|0" ],
        "江西": [ "抚州|3757|0", "赣州|3693|0", "吉安|3721|0", "景德镇|3650|0", "九江|3665|0", "南昌|3637|0", "萍乡|3657|0", "上饶|3774|0", "新余|3684|0", "宜春|3741|0", "鹰潭|3688|0" ],
        "辽宁": [ "鞍山|1517|0", "本溪|1539|0", "大连|1501|0", "丹东|1552|0", "抚顺|1528|0", "阜新|1600|0", "葫芦岛|1573|0", "锦州|1562|0", "辽阳|1610|0", "盘锦|1593|0", "沈阳|1482|0", "铁岭|1621|0", "营口|1583|0" ],
        "内蒙古": [ "巴彦淖尔|1305|0", "包头|1181|0", "赤峰|1202|0", "鄂尔多斯|1234|0", "呼和浩特|1168|0", "呼伦贝尔|1247|0", "通辽|1221|0", "乌海|1197|0", "乌兰察布|1267|0", "锡林郭勒|1286|0", "兴安盟|1323|0" ],
        "宁夏": [ "固原|8936|0", "石嘴山|8918|0", "吴忠|8926|0", "银川|8908|0" ],
        "青海": [ "西宁|8783|0", "玉树|8823|0" ],
        "山东": [ "滨州|4186|0", "德州|4156|0", "东营|4048|0", "菏泽|4197|0", "济南|3997|0", "济宁|4101|0", "莱芜|4133|0", "聊城|4173|0", "临沂|4137|0", "青岛|4011|0", "日照|4128|0", "泰安|4118|0", "威海|4094|0", "潍坊|4056|0", "烟台|4075|0", "枣庄|4040|0", "淄博|4027|0" ],
        "山西": [ "长治|846|0", "大同|9449|0", "晋城|863|0", "晋中|881|0", "临汾|940|0", "吕梁|965|0", "朔州|871|0", "太原|813|0", "忻州|918|0", "阳泉|842|0", "运城|898|0" ],
        "陕西": [ "安康|8346|0", "宝鸡|8232|0", "汉中|8310|0", "商洛|8362|0", "铜川|8225|0", "渭南|8273|0", "西安|8206|0", "咸阳|8251|0", "延安|8290|0", "榆林|8327|0" ],
        "四川": [ "阿坝|6895|0", "巴中|6881|0", "成都|6693|0", "达州|6827|0", "德阳|6747|0", "甘孜|6915|0", "广安|6839|0", "广元|6771|0", "乐山|6793|0", "凉山|6943|0", "泸州|6736|0", "眉山|6858|0", "绵阳|6757|0", "内江|6786|0", "南充|6809|0", "攀枝花|6731|0", "遂宁|6781|0", "雅安|6868|0", "宜宾|6823|0", "资阳|6888|0", "自贡|6721|0" ],
        "西藏": [ "拉萨|7916|0", "林芝|8020|0", "那曲|7929|0" ],
        "新疆": [ "乌鲁木齐|9002|0" ],
        "云南": [ "保山|7577|0", "楚雄|7676|0", "大理|7692|0", "德宏|7711|0", "红河|7651|0", "昆明|7528|0", "丽江|7631|0", "临沧|7618|0", "普洱|7602|0", "曲靖|7550|0", "文山|9451|0", "玉溪|7563|0", "昭通|7585|0" ],
        "浙江": [ "杭州|2810|0", "湖州|2872|0", "嘉兴|2861|0", "金华|2887|0", "丽水|2932|0", "宁波|2827|0", "衢州|2901|0", "绍兴|2877|0", "台州|2918|0", "温州|2844|0", "舟山|2911|0" ]
    };
   
    if (remote_ip_info) {
        city = remote_ip_info.city;
        province = remote_ip_info.province;
    }
    if (city == "北京") {
        $cid = "0";
    } else if (city == "上海") {
        $cid = "2354";
    } else if (city == "天津") {
        $cid = "125";
    } else if (city == "重庆") {
        $cid = "6425";
    } else if (city == "钓鱼岛") {
        $cid = "9969";
    }
    $ps = local_cities[province];
    if ($ps.length > 0) {
        for (var i = 0; i < $ps.length; i++) {
            $arr = $ps[i].split("|");
            if ($arr.length == 3) {
                if ($arr[0] == city) {
                    $cid = $arr[1];
                    break;
                }
            }
        }
    }
    
    $.ajax({
        type: "get",
        async: false,
        url: 'http://news.baidu.com/json/local/'+$cid+'.json',
        data: "succ_callback=bdNewsJsonCallBack&ra=" + (new Date()).valueOf(),
        dataType: "jsonp",
        jsonp: "callbackfun",
        jsonpCallback: bdNewsJsonCallBack,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
});