// ---------------------------------------------------------------------
// $Id: dom-prototype.js,v 1.2 2008/06/25 04:19:39 cvsuser Exp $
// ---------------------------------------------------------------------
// Convenience DOM enhancements. Picks up where Prototype left off.
//
// Requires prototype.js.
// ---------------------------------------------------------------------


// ---------------------------------------------------------------------
// Add a bunch of CSS code to the document.
// From http://yuiblog.com/blog/2007/06/07/style/
//
// Prototype doesn't have a function like this one yet!

function dnAddCss (cssCode) {
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    
    if (styleElement.styleSheet) {
        styleElement.styleSheet.cssText = cssCode;
    } else {
        styleElement.appendChild(document.createTextNode(cssCode));
    }
    
    document.getElementsByTagName("head")[0].appendChild(styleElement);
};

// ---------------------------------------------------------------------
// Loads a JavaScript script by writing out a script tag, but checks
// that it hasn't already loaded it before doing so.

function dnLoadScript (url, async) {
    if (!window.dnLoadScript.seen) window.dnLoadScript.seen = { };
    if (window.dnLoadScript.seen[url]) return;
    window.dnLoadScript.seen[url] = true;
    
    if (async) {
        $(document.body).insert(new Element("script", {
            src: url,
            async: "async"
        }));
    }
    else {
        document.write("<script src='"+url+"'></script>");
    }
}

// ---------------------------------------------------------------------
// Safari and Opera have problems with the img.complete property. To work
// around this, call dnImageCompleteWatch() on an img element before
// assigning it a (new) src. Then call dnImageComplete to test if the image
// has finished loading.
//
// Prototype doesn't seem to have anything for this yet.

function dnImageCompleteWatch (img) {
    if (img.complete != null) return img;
    img.dnImageComplete = false;
    img.onload = function (e) { img.dnImageComplete = true; };
    return img;
};

function dnImageComplete (img) {
    if (img.complete != null) return img.complete;
    if (img.dnImageComplete != null) return img.dnImageComplete;
    return false;
};

// ---------------------------------------------------------------------
// Create, show and hide iframe shields for a specified overlay to fix
// the z-index bug and windowed elements in IE6.
//
// Does scriptaculous have anything that does this?

function ieVersion () {
    if (!Prototype.Broswser.IE) return undefined;
    if (!window.ScriptEngineMajorVersion) return 1;
    var verStr = ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion();
    return parseFloat(verStr);
};

function enableShieldedOverlayForIE (id) {
    var ieVersion = ieVersion();
    if (ieVersion == undefined || ieVersion > 5.6) return;
    
    var iframeShield = $(id + ":iframeShield");
    if (!iframeShield) {
        iframeShield = document.createElement("iframe");
        iframeShield.id = id + ":iframeShield";
        iframeShield.style.position = "absolute";
        var zIndex = dnGetStyle($(id), "zIndex");
        if (zIndex) iframeShield.style.zIndex = parseInt(zIndex);
        iframeShield.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        $(id).parentNode.insertBefore(iframeShield, $(id));
    }
    iframeShield.style.width = $(id).offsetWidth + "px";
    iframeShield.style.height = $(id).offsetHeight + "px";
    iframeShield.style.left = $(id).style.left;
    iframeShield.style.top = $(id).style.top;
    iframeShield.style.display = "block";
};

function disableShieldedOverlayForIE (id) {
    var ieVersion = ieVersion();
    if (ieVersion == undefined || ieVersion > 5.6) return;
    
    var iframeShield = $(id + ":iframeShield");
    if (iframeShield) iframeShield.style.display = "none";
};


// ---------------------------------------------------------------------
// DEPRICATED FUNCTIONS
//
// Every function below this point is depricated. Since you're already
// using Prototype, you should use the appropriate Prototype method
// to accomplish what these functions do. They're only still in here
// to provide backwards compatability.
// ---------------------------------------------------------------------


// ---------------------------------------------------------------------
// Get the computed style property of an element.
// From http://www.quirksmode.org/dom/getstyles.html
//
// This function is broken, and depricated! Use Prototype's
// Element.getStyle() instead, which properly works around browser
// incompatabilities.

function dnGetStyle (elem, prop) {
    var x = $(elem);
    if (x.currentStyle)
        var y = x.currentStyle[prop];
	else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(prop);
    return y;
};

// ---------------------------------------------------------------------
// Functions that have been rewritten to take advantage of Prototype,
// while still maintaining backwards compatability. Consider them all
// depricated.

function dnGetElementsByClassName(className, tag, elem){
    var selector = "." + className;
    if (tag) selector = tag + selector;
    if (elem) return $(elem).select(selector);
    else return $$(selector);
};

function dnAddEvent(obj, type, fn) {
    if (type == "dndomload")
        return Event.observe(document, "dom:loaded", fn);
    else if (obj && $(obj).observe)
        return $(obj).observe(type, fn);
    else
        return Event.observe(obj, type, fn);
};

function dnRemoveEvent(obj, type, fn) {
    return $(obj).stopObserving(type, fn);
};

function dnStopPropagation (e) {
    return Event.stop(e);
};

function dnContains (parent, child) {
    return $(child).descendantOf(parent);
};

function dnFindPos(obj) {
    return $(obj).cumulativeOffset();
};

function dnConcatNodeLists () {
    var result = [];
    $A(arguments).each(function (nodelist) {
        result.push($A(nodelist));
    });
    return result.flatten().compact();
};

// ---------------------------------------------------------------------
