﻿$().ready(function() {
    zomeImg()
})

function zomeImg() {
    $('.zoom a[rel=zoom] img').click(function() {
        var ob = $(this)
        var n = '';
        var src = $(this).attr("src")
        if (src.indexOf("s_") > 0) {
            $(this).before('<span class="loading"><img src="/images/loading.gif" style="vertical-align:middle;border:0px;" />加载图片中 </span>')
            n = src.replace("s_", "")
            var nn = (src.indexOf("?") > 0) ? Math.random() : "?a=" + Math.random()
            $(this).attr("src", n)
            DrawImage(ob, 500)

            $(this).css({ "cursor": "url(images/s.cur)" })
            var h = ''
            h = $('<span class="span"><a href="' + n + '" target="_blank" title="查看原图"></a></span>')
            $(this).before(h)
        }
        else {
            $(this).css({ "cursor": "url(images/b.cur)" })
            var re = /(http:\/\/)(([^\/]*?\/)*)(.*)/i;
            re.test(src)
            n = RegExp.$1 + RegExp.$2 + "s_" + RegExp.$4
            $(this).attr("src", n)
            DrawImage(ob, 100)
            $(".span").remove();
        }
    })

}

function DrawImage(ImgD, w) {
    var img = new Image();
    img.src = ImgD.attr("src")
    var appname = navigator.appName.toLowerCase();
    function a() {
        if (img.width > w) {
            ImgD.width(w);
            ImgD.height(img.height * w / img.width);
        }
        else {
            ImgD.width(img.width);
            ImgD.height(img.height);
        }

        $('.loading').remove();

    }
    if (appname.indexOf("netscape") == -1) {
        //ie
        if (img.width > 0)
            a()
        else {
            img.onreadystatechange = function() {
                if (img.readyState == "complete")
                    a()
            }
        };
    }
    else {
        //firefox
        if (img.width > 0)
            a()
        else {
            img.onload = function() {
                if (img.complete == true) {
                    if (img.width > w) {
                        ImgD.width(w);
                        ImgD.height(img.height * w / img.width);
                    }
                    else {
                        ImgD.width(img.width);
                        ImgD.height(img.height);
                    }
                    // $(".loading").remove();
                }
            }
        }
    }

} 
