var url = window.location.href;
var title = document.title;

$(function () {

    //QQbox
    var h = ($(window).height() - $("#divQQMini").height()) / 2;
    $("#divQQMini").animate({
        right: 1, top: h
    }, 800);

    $(window).scroll(function () {
        $("#divQQbox").hide();
        $("#divQQMini").show();
        $("#divQQMini").stop().animate({
            right: 1, top: $(window).scrollTop() + h
        }, 800);
    });

    $("#divQQMini").mouseover(function () {
        $("#divQQMini").hide();
        $("#divQQbox").show();
        $("#divQQbox").stop().animate({
            right: 1, top: $(window).scrollTop() + ($(window).height() - $("#divQQbox").height()) / 2
        }, 800);
    });

    // textCSS
    $("input[type='text'], textarea").focus(function () {
        $(this).addClass("sffocus");
    });
    $("input[type='text'], textarea").blur(function () {
        $(this).removeClass("sffocus");
    });

    //headNavCSS
    var url = location.href;
    var href = url.split("?");
    $(".headNav a[href='?" + href[1] + "']").addClass("headNavSelected");

});

//tipswindow 配置文件
if (typeof Util == "undefined") var Util = {};
//配置信息
Util.config = {
    JSfile: "/js/tipswindow/",					//设置JS文件夹路径
    loadingICO: "/js/tipswindow/loading.gif"		//默认载入时图片路径
};
document.write("<scr" + "ipt language=javascript src=\"/js/tipswindow/XY_Base.js\"></scr" + "ipt>");
//会员登录界面
jQuery(document).ready(function ($) {
    Util.loadJS("XY_Dialog.js", function () {
        $("#myLogin").click(function () {
            Util.Dialog({
                boxID: "divlogin",
                fixed: true,
                showtitle: false,
                showbg: true,
                content: "iframe:/member/user/login.aspx?url=" + url,
                border: { opacity: "0", radius: "3" },
                position: { top: "-500" },
                ofns: function () {
                    var p = Util.safeRange("divlogin");
                    $("#divlogin").animate({ top: p.centerY }, 300);
                }
            });
            return false;
        });
    });
});

//分页跳转
function gotoPage(urlStr) {
    //这里还需要实现一些对于输入页码的安全性验证。比如不能为空，必须是数字这些。
    var page = document.getElementById("PageInput").value;
    window.location.href = urlStr + "&page=" + page;
}

//打印
function Print() {
    bdhtml = window.document.body.innerHTML;
    sprnstr = "<!--startprint-->";
    eprnstr = "<!--endprint-->";
    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML = prnhtml;
    window.print();
}

//添加收藏夹
function AddFavorite() {
    try {
        window.external.addFavorite(url, title);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        }
        catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

//设为首页
function SetHome(obj) {
    try {
        obj.style.behavior = 'url(#default#homepage)';
        obj.setHomePage(url);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', url);
        }
    }
}

/*
scaling     是否等比例自动缩放
width       图片最大高
height      图片最大宽
loadpic     加载中的图片路径
*/
(function ($) {
    jQuery.fn.LoadImage = function (settings) {
        settings = jQuery.extend({
            scaling: true,
            width: 500,
            height: 500,
            loadpic: ""
        }, settings);
        return this.each(function () {
            $.fn.LoadImage.Showimg($(this), settings);
        });
    };
    $.fn.LoadImage.Showimg = function ($this, settings) {
        var src = $this.attr("src");
        var img = new Image();
        img.src = src;
        var autoScaling = function () {
            if (settings.scaling) {
                if (img.width > 0 && img.height > 0) {
                    if (img.width / img.height >= settings.width / settings.height) {
                        if (img.width > settings.width) {
                            $this.width(settings.width);
                            $this.height((img.height * settings.width) / img.width);
                        }
                        else {
                            $this.width(img.width);
                            $this.height(img.height);
                        }
                    }
                    else {
                        if (img.height > settings.height) {
                            $this.height(settings.height);
                            $this.width((img.width * settings.height) / img.height);
                        }
                        else {
                            $this.width(img.width);
                            $this.height(img.height);
                        }
                    }
                }
            }
        }
        //正确处理自动读取缓存图片
        if (img.complete) {
            autoScaling();
            return;
        }
        $this.attr("src", "");

        var loading = $("<img alt=\"加载中…\" title=\"图片加载中…\" src=\"" + settings.loadpic + "\" />");
        $this.hide();
        $this.after(loading);
        $(img).load(function () {
            autoScaling();
            loading.remove();
            $this.attr("src", this.src);
            $this.fadeIn('slow');
        });
    }
})(jQuery);