Namespace.register("UH");

UH.Ex = window.external; UH.tags = []; UH.tag;   //对外的接口

//启动
window.onload = function () {
    U.M.OLM(); U.M.stopRefresh(); _addTag("http://www.1473.cn");  //禁止事件 启动初始化进入1473
    $("#txtInputUrl")[0].onkeydown = $("#S_con")[0].onkeydown = U.M.apply(null, [[U.M.returnkeylogin, ([null, UH.onkeyD])]]); //
}

//生成
function GetJson(xx, name) {
    var e = eval("(" + xx + ")"), tag = UH.Find(name);
    if (tag) { _$(tag).Add(e); UH.ModifyTag(e, tag) }
}

//修改选项卡
UH.ModifyTag = function (e, tag) {
    var img = $("img", $("#" + tag.Name)[0])[0];
    if (e.Shortcut_Icon == "") { UH.MTag(img, tag); }
    else { img.style.display = "block"; if (e.Shortcut_Icon != img.src) { img.src = e.Shortcut_Icon; } }
    $("#" + tag.Name + " .Tag-title")[0].innerHTML = e.Title || e.Url;
    (tag == UH.tag) && ($("#txtInputUrl")[0].value = e.Url);
}

//添加标签
function _addTag(url, name, tf) {
    name = "a" + (name || U.MS.guidNoDash()); tf = tf || false;
    if ($("#" + name)[0]) { $("#" + name)[0].onclick(); }
    else { if (UH.AddTag(url, name, tf)) { addT(url, name); } }
    return name || "";
}

//添加标签页
function addT(url, name) {
    UH.tag = createTag(url, name);
    UH.witch(name); UH.DHBH(); UH.BRsize();
}

//添加浏览器页面
UH.AddTag = function (url, name, tf) {
    return UH.Ex.AddABrowser(url, name, tf);
}

//回车使用
UH.onkeyD = function () {
    var url = this.value;
    if (this.id == "txtInputUrl" && U.M.IsUrl(url)) { url = (url.indexOf("://") > 0 ? "" : "http://") + url; UH.Ex.LoadUrl(url, UH.tag.Name); UH.loadurl(UH.tag.Name); } //直接显示
    else { UH.USCB(this); } //弹出新页面
}

//载入页面
UH.loadurl = function (name) {
    var d = $("#" + name)[0];
    $(".Tag-titlei", d)[0].innerHTML = ""; $$("img", { "className": "Tag-titleimg", "src": "img/loading.gif" }, $(".Tag-titlei", d)[0]);
    $(".Tag-title", d)[0].innerHTML = "Loading...";
}

//搜索
UH.USCB = function (d) {
    _addTag("http://www.sogou.com/web?pid=sogou-site-d8e1344e27a5b08c&query=" + d.value, null, true);
}

//创建标签
function createTag(url, name) {
    var a, b, tag; UH.tags.push(tag = { "Name": name, "Url": url }); $("#txtInputUrl")[0].value = url;
    a = $$("div", { "id": name, "className": "Tag1", "onmouseup": [[UH.Close, ([name])]], "onmousedown": [[UH.witch, ([name])]], "ondblclick": U.M.StopBubble, "onclick": U.M.StopBubble }, $("#Tag")[0]);  // 
    $$("div", { "className": "Tag-close", "onmousedown": U.M.StopBubble, "ondblclick": U.M.StopBubble, "onclick": [[U.M.StopBubble], [CloseTag, ([name])]] }, a);
    b = $$("div", { "className": "Tag-titlei" }, a);
    $$("img", { "className": "Tag-titleimg", "src": "img/loading.gif", "onerror": [[UH.MTag, (["this", tag])]] }, b);
    $$("div", { "className": "Tag-title", "innerHTML": "Loading..." }, a);
    return tag;
}

//修改选项卡
UH.MTag = function (d, tag) {
    d = d || event.srcElement; d.style.display = "none"; $("#" + tag.Name).Child()[1].innerHTML = "";
    $$("div", { "className": "Tag-titlee" }, $("#" + tag.Name).Child()[1]);
}

//标签切换函数
UH.witch = function (name) {
    var d = $("#" + name)[0], tag = UH.Find(name); U.M.StopBubble(); SetVisible(name); UH.ReplaceTag(tag); UH.tag = tag;
    U.MR.DHQH(d, null, [{ "style": { "background": "#FEFEFE"} }, { "style": { "background": "#C9C9C9"}}]);
}

//显示指定的页面
function SetVisible(name) {
    UH.Ex.SetVisible(name);
}

//显示置顶页面的信息
UH.ReplaceTag = function (tag) {
    $("#txtInputUrl")[0].value = tag.Url;
}

//关闭
UH.Close = function (name) {
    (event.button == 2) && (CloseTag(name));
}

//关闭窗体
function CloseTag(name) {
    if (UH.Find(name)) {
        var tag = UH.Find(name), i = UH.DeleteTag(name); $("#Tag")[0].removeChild($("#" + name)[0]); UH.DHBH();
        if (tag == UH.tag && UH.tags.length > 0) { i = i == 0 ? i : i - 1; UH.witch(UH.tags[i].Name); } //查看
        else if (!UH.tags.length) { UH.CloseForm(); }
    }
}

//删除选项
UH.DeleteTag = function (name) {
    var i = UH.Find(name, true); UH.tags.splice(i, 1);
    UH.Ex.CloseOneBrowser(name); return i;
}

//查找指定的
UH.Find = function (name, tf) {
    for (var i = 0; i < UH.tags.length; i++) { if (UH.tags[i].Name == name) { return (tf === true ? i : UH.tags[i]); } }
}

//变化
window.onresize = function () {
    UH.DHBH(); UH.BRsize();
}

//导航栏大小变化
UH.DHBH = function () {
    var w = $("body")[0].offsetWidth - $("#head_Series")[0].offsetWidth, _ut = $("#Tag").childg();
    if (w > 0) {
        $("#Title")[0].style.width = w + "px"; w = ((w - $("#addButton")[0].offsetWidth) / _ut.length) - 4;
        w = w > 190 ? 190 : w; _ut.addAttrArray({ "style": { "width": w + "px"} });
    }
}

//地址栏区域
UH.BRsize = function () {
    var w = $("body")[0].offsetWidth - $("#setting")[0].offsetWidth - $("#RBF")[0].offsetWidth;
    if (w >= 0) {
        $("#Address_Box")[0].style.width = w - 6 + "px"; $(".IpAddress")[0].style.width = w - 30 + "px";
    }
}

//变化
UH.Change = function (d) {
    UH.tag.Url = d.value;
}

//全屏后的鼠标移动
UH.Topwz = function (d, tf) {
    if (!U.M.onmouseout(d)) { UH.Ex.WYDC(tf); }
}

//全屏后的还原
UH.Larger = function () {
    UH.Ex.DFullS();
}

//窗口拖动
UH.Md = function () {
    UH.Ex._Main_Drag(); var xy = U.M.GetMousep();
    UH.Ex.WBSJ(xy["X"], xy["Y"]); U.M.StopBubble();
}

//最小化
UH.Minimized = function () {
    UH.Ex.FormMinimized();
}

//关闭窗体
UH.CloseForm = function () {
    UH.Ex.CloseForm();
}

//页面上一页
UH.Previous = function () {
    UH.Ex.Previous(UH.tag.Name);
}

//下一页
UH.Next = function () {
    UH.Ex.Next(UH.tag.Name);
}

//刷新
function Refresh() {
    //  UH.loadurl(UH.tag.Name); UH.Ex.Refresh(UH.tag.Name);
    UH.Ex.LoadUrl(UH.tag.Url, UH.tag.Name); UH.loadurl(UH.tag.Name);
}

//打开查看的收藏夹
UH.OpenAddFVC = function () {
    UH.Ex.OpenAddFVC(UH.Find(UH.tag.Name, true));
}

//打开收藏夹
UH.OpenFV = function () {
    UH.Ex.OpenFV(UH.Find(UH.tag.Name, true));
}

//显示区
UH.BigScreen = function () {
    UH.Ex.BigScreen();
}

//清除缓存
UH.clean = function () {
    UH.Ex.clean();
}

//设置
UH.Setup = function () {
    UH.Ex.Setup();
}