﻿/*
Credits to Brian R. Williams at http://www.ploughdeep.com/onionskin/ for introducing me to the concept of 'onion layered' drop shadows and providing the images used.
Check http://www.ploughdeep.com/onionskin/gallery.html for the images used below.
*/
if (!self.INDIGO) (function (url, async) { //Requires cbase.js
    var http = (self.XMLHttpRequest && new XMLHttpRequest()) || 
        (self.ActiveXObject && (new ActiveXObject("Msxml2.XMLHTTP") || new ActiveXObject("Microsoft.XMLHTTP")));
    http.open("GET", url + ((url.indexOf("?") + 1) ? "&" : "?") + ("crndid=" + new Date().getTime()), !!async);
    if (http.overrideMimeType) http.overrideMimeType("text/javascript");
    http.send(null);
    eval.call(self, http.responseText);
})("/js/cbase.js");

if (!INDIGO.dShadow) (function () {
    INDIGO.dShadow = true;
    
    self.dShadow = function () {
        var reg = /(^|\s)shadow(\s|$)/;
                
        function apply(o) {
            var shadow1 = document.createElement("div");
            var shadow2 = document.createElement("div");
            var shadow3 = document.createElement("div");
            var clear = document.createElement("div");
            var pos = getOPos(o);
            var dim = getDimensions(o);
            var style;
            
            removeShadow(o);
            removeClass(o, "shadow");
            addClass(o, "shadow");
            
            clear.style.clear = "both";
            clear.setAttribute("shadowclear", "shadowclear");
            
            shadow1.style.background = "url('/images/jsimg/shadow.jpg') right bottom no-repeat";
            //shadow1.style.cssFloat = "left";
            shadow2.style.background = "url('/images/jsimg/corner_bl.jpg') left bottom no-repeat";
            shadow3.style.background = "url('/images/jsimg/corner_tr.jpg') right top no-repeat";
            shadow3.style.padding = "0px 14px 14px 0px";
            
            shadow1.appendChild(shadow2);
            shadow2.appendChild(shadow3);
            
            if (o.style.position == "absolute") {
                shadow1.style.width = (dim[0] + 4) + "px";
                shadow1.style.height = (dim[1] + 4) + "px";
                shadow1.style.position = "absolute";
                shadow1.style.left = pos[0] + "px";
                shadow1.style.top = pos[1] + "px";
                if (/(^|\s)draggable(\s|$)/.test(o.className)) {
                    removeClass(o, "draggable");
                    o.draggable = false;
                    o.removeAttribute("draggable");
                    shadow1.draggable = true;
                }
                o.parentNode.insertBefore(shadow1, o);
                shadow3.appendChild(o);
                o.style.position = "";
            } else if (o.style.position == "relative") {
                shadow1.style.position = "relative";
                shadow1.style.left = o.style.left;
                shadow1.style.top = o.style.top;
                shadow1.style.right = o.style.right;
                shadow1.style.bottom = o.style.bottom;
                o.style.position = "";
                shadow1.style.width = (dim[0] + 4) + "px";
                o.parentNode.insertBefore(clear, o);
                o.parentNode.insertBefore(shadow1, clear);
                remove(clear);
                shadow3.appendChild(o);
            } else {
                shadow1.style.width = (dim[0] + 14) + "px";
                o.parentNode.insertBefore(clear, o);
                o.parentNode.insertBefore(shadow1, clear);
                remove(clear);
                shadow3.appendChild(o);
            }
            o.setAttribute("shadowwrapped", "shadowwrapped");

            return shadow1;
        }
        
        function removeShadow(o) {
            if (o.getAttribute("shadowwrapped") === "shadowwrapped") {
                var shadow1 = o.parentNode.parentNode.parentNode;
                var clear = shadow1.nextSibling;
                if (clear && clear.getAttribute && clear.getAttribute && clear.getAttribute("shadowclear") === "shadowclear")
                    remove(clear);
                shadow1.parentNode.insertBefore(o, shadow1);
                remove(shadow1);
                removeClass(o, "shadow");
                o.removeAttribute("shadowwrapped");
            }
        }
        
        function loadShadow() {
            getElementsByAttribute("class", "shadow").forEach(apply);
        }
        window.addEvent("load", loadShadow);
        
        return {
            apply: apply,
            remove: removeShadow
        }
    }();
})();
