﻿/// <reference path="../k2046.js" />


/*
**********************************************
*功能:按比例缩图******************************
*编写:Qin Deke********************************
*说明:直接在图片的onload中调用****************
*示例:onload="chkimgsize(this,width,height)"**
*参数:3个参数*********************************
*其中:this表示当前图片对象********************
*width表示要限定的宽度************************
*height表示要限定的高度***********************
**********************************************
*/
function CheckImageSize(element, width, height) {
    var another = new Image();
    another.src = element.src;
    if (another.width != 0 && another.height != 0) {
        if (another.width / another.height >= width / height) {
            if (another.width > width) {
                element.style.width = width + "px";
                element.style.height = ((another.height * width) / another.width) + "px";
            } else {
                element.style.width = another.width + "px";
                element.style.height = another.height + "px";
            }
        }
        else {
            if (another.height > height) {
                element.style.height = height + "px";
                element.width = ((another.width * height) / another.height) + "px";
            } else {
                element.style.width = another.width + "px";
                element.style.height = another.height + "px";
            }
        }
    }
}

K2046.ImageSize = {
    Check: function(options) {
        var el = options.el, width = options.w, height = options.h;
        var another = new Image();
        another.src = el.src;
        if (another.width != 0 && another.height != 0) {
            if (another.width / another.height >= width / height) {
                if (another.width > width) {
                    el.style.width = width + "px";
                    el.style.height = ((another.height * width) / another.width) + "px";
                    if (options.m) {
                        el.style.marginTop = (options.h - el.height) / 2 + "px";
                    }
                } else {
                    el.style.width = another.width + "px";
                    el.style.height = another.height + "px";
                }
            }
            else {
                if (another.height > height) {
                    el.style.height = height + "px";
                    el.style.width = ((another.width * height) / another.height) + "px";
                } else {
                    el.style.width = another.width + "px";
                    el.style.height = another.height + "px";
                }
            }
        }
    }

    , Attach: function(el, width, height, margin) {
        el = K2046.$(el);
        if (el) {
            if (el.tagName && el.tagName.toLowerCase() == "img") {
                var iel = new Image();
                K2046.Events.On(iel, "load", K2046.ImageSize.Check, { el: el, w: width, h: height, m: margin });
                iel.src = el.src;
            }
            else {
                if (el.getElementsByTagName) {
                    var images = K2046.$Tag("img", el);
                    if (images && images.length) {
                        for (var x = 0; x < images.length; x++) {
                            var iel = new Image();
                            K2046.Events.On(iel, "load", K2046.ImageSize.Check, { el: images[x], w: width, h: height, m: margin });
                            iel.src = images[x].src;
                        }
                    }
                }
            }
        }
    }
};