
function isFunction(a) { return typeof a == 'function'; }
function isObject(a) { return (a && typeof a == 'object') || isFunction(a); }
function isArray(a) { return isObject(a) && a.constructor == Array; }

var ImgLoaded=false;
/* for image auto resize after loaded */
function drawImage(ImgD, maxWidth, maxHeight){
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        ImgLoaded=true;
        if(image.width/image.height>= maxWidth/maxHeight){
            if(image.width>maxWidth){
                ImgD.width=maxWidth;
                ImgD.height=(image.height*maxWidth)/image.width;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            ImgD.alt=image.width+"×"+image.height;
        }
        else{
            if(image.height>maxHeight){
                ImgD.height=maxHeight;
                ImgD.width=(image.width*maxHeight)/image.height;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            ImgD.alt=image.width+"×"+image.height;
        }
    }
}

/* for resizing images in a container */
function auto_resize_imgs(container, maxWidth, maxHeight, str_event) {

    if(!container) return;
	if(!str_event) str_event = 'onload';

    var img_objs = container.getElementsByTagName('img');
    for(var i=0; i < img_objs.length; i++) {
        img_objs[i][str_event] = function() { 
            if( (this.width > maxWidth || this.height > maxHeight) 
                && this.parentNode.tagName != 'A') {
                this.onclick = function() { window.open(this.src) };
                this.style.cursor = 'pointer';
            }

            drawImage(this, maxWidth, maxHeight);
        };
    }
}


