/*
Copyright Nynodata AS 2007

Replaces IMG elements with "scale" class with SVG or VML imports to get proper scaling.
Note: This relies on the document type being application/xhtml+xml when going for SVG.
*/

if (typeof(Nynodata) == 'undefined') Nynodata = {};

Nynodata.scaleEnabled = true;

Nynodata.useVML = (function() {
    return /msie/i.test(navigator.userAgent); // this doesn't consider IE 3 and IE 4
}).apply();

Nynodata.useSVG = (function() {
    /* Use in Opera and Firefox > 1.5. Safari already has nice img scaling so simply use img. */
    var firefox = navigator.userAgent.match(/firefox\/([0-9]+).([0-9]+)/i);
    if (firefox) {
        // version >= 1.5
        return ((Number(firefox[1]) == 1 && Number(firefox[2] == 5)) || Number(firefox[1] >= 2));
    }
    var opera = navigator.userAgent.match(/opera\/([0-9]+)/i);
    if (opera) {
        // version >= 9
        return Number(opera[1]) >= 9;
    }
    return false;
}).apply();

Nynodata.replaceWithVML = function(image) {
    var div = document.createElement("div");
    div.style.width = image.style.width;
    div.style.height = image.style.height;
    div.className = image.className;
    var rect = document.createElement("vml:rect");
    rect.style.width = "100%";
    rect.style.height = "100%";
    rect.stroked = "f";
    rect.title = image.title;
    var fill = document.createElement("vml:fill");
    fill.src = image.src;
    fill.type = "frame";
    fill.aspect = "atmost";
    rect.appendChild(fill);
    div.appendChild(rect);
    image.parentNode.insertBefore(div, image);
    image.parentNode.removeChild(image);
}

Nynodata.replaceWithSVG = function(image) {
    var SVG_NS = "http://www.w3.org/2000/svg";
    var XLINK_NS = "http://www.w3.org/1999/xlink";
    var HTML_NS = "http://www.w3.org/1999/xhtml";
    var div = document.createElementNS(HTML_NS, "div");
    div.style.width = image.style.width;
    div.style.height = image.style.height;
    div.className = image.className;
    var svg = document.createElementNS(SVG_NS, "svg:svg");
    svg.setAttribute("width", "100%");
    svg.setAttribute("height", "100%");
    svg.setAttribute("version", "1.1");
    svg.setAttribute("title", image.title);
    var svgimg = document.createElementNS(SVG_NS, "svg:image");
    svgimg.setAttribute("width", "100%");
    svgimg.setAttribute("height", "100%");
    svgimg.setAttributeNS(XLINK_NS, "xlink:href", image.src);
    svg.appendChild(svgimg);
    div.appendChild(svg);
    image.parentNode.insertBefore(div, image);
    image.parentNode.removeChild(image);
}

Nynodata.prettyScaleImages = function() {
    var imageReplacer;
    if (!Nynodata.scaleEnabled) return;
    if (Nynodata.useVML) imageReplacer = Nynodata.replaceWithVML;
    else if (Nynodata.useSVG) imageReplacer = Nynodata.replaceWithSVG;
    else return;

    var imgList = document.getElementsByTagName("img");

    for (var i = imgList.length - 1; i >= 0; --i) {
        var img = imgList[i];
        if (/(^|\s)+scale($|\s)+/.test(img.className)) {
            imageReplacer(imgList[i]);
        }
    }
}

if (window.addEventListener) {
    window.addEventListener('load', Nynodata.prettyScaleImages, false);
} else {
    var oldonload = window.onload;
    window.onload = function(e) {
        if (oldonload) oldonload(e);
        Nynodata.prettyScaleImages();
    }
}

