File indexing completed on 2025-01-05 03:52:06

0001 /* Nifty Corners Cube - rounded corners with CSS and Javascript
0002 Copyright 2006 Alessandro Fulciniti (a.fulciniti@html.it)
0003 
0004 This program is free software; you can redistribute it and/or modify
0005 it under the terms of the GNU General Public License as published by
0006 the Free Software Foundation; either version 2 of the License, or
0007 (at your option) any later version.
0008 
0009 This program is distributed in the hope that it will be useful,
0010 but WITHOUT ANY WARRANTY; without even the implied warranty of
0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012 GNU General Public License for more details.
0013 
0014 You should have received a copy of the GNU General Public License
0015 along with this program; if not, write to the Free Software
0016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0017  */
0018 
0019 var niftyOk = (document.getElementById && document.createElement && Array.prototype.push);
0020 var niftyCss = false;
0021 
0022 String.prototype.find = function(what) {
0023         return (this.indexOf(what) >= 0 ? true : false);
0024 }
0025 
0026 var NiftyLoad;
0027 var oldonload = document.open();
0028 if (typeof (NiftyLoad) != 'function')
0029         NiftyLoad = function() {
0030         };
0031 if (typeof (oldonload) == 'function')
0032         window.onload = function() {
0033                 oldonload();
0034                 AddCss();
0035                 NiftyLoad()
0036         };
0037 else
0038         window.onload = function() {
0039                 AddCss();
0040                 NiftyLoad()
0041         };
0042 
0043 function AddCss() {
0044         niftyCss = true;
0045         var l = CreateEl("link");
0046         l.setAttribute("type", "text/css");
0047         l.setAttribute("rel", "stylesheet");
0048         l.setAttribute("href", "niftyCorners.css");
0049         l.setAttribute("media", "screen");
0050         document.getElementsByTagName("head")[0].appendChild(l);
0051 }
0052 
0053 function Nifty(selector, options) {
0054         if (niftyOk == false)
0055                 return;
0056 
0057         if (niftyCss == false)
0058                 AddCss();
0059         var i, v = selector.split(","), h = 0;
0060         if (options == null)
0061                 options = "";
0062         if (options.find("fixed-height"))
0063                 h = getElementsBySelector(v[0]).offsetHeight;
0064         for (i = 0; i < v.length; i++)
0065                 Rounded(v[i], options);
0066         if (options.find("height"))
0067                 SameHeight(selector, h);
0068 }
0069 
0070 function Rounded(selector, options) {
0071         var i, top = "", bottom = "", v = new Array();
0072         if (options != "") {
0073                 options = options.replace("left", "tl bl");
0074                 options = options.replace("right", "tr br");
0075                 options = options.replace("top", "tr tl");
0076                 options = options.replace("bottom", "br bl");
0077                 options = options.replace("transparent", "alias");
0078                 if (options.find("tl")) {
0079                         top = "both";
0080                         if (!options.find("tr"))
0081                                 top = "left";
0082                 } else if (options.find("tr"))
0083                         top = "right";
0084                 if (options.find("bl")) {
0085                         bottom = "both";
0086                         if (!options.find("br"))
0087                                 bottom = "left";
0088                 } else if (options.find("br"))
0089                         bottom = "right";
0090         }
0091         if (top == "" && bottom == "" && !options.find("none")) {
0092                 top = "both";
0093                 bottom = "both";
0094         }
0095         v = getElementsBySelector(selector);
0096         for (i = 0; i < v.length; i++) {
0097                 FixIE(v[i]);
0098                 if (top != "")
0099                         AddTop(v[i], top, options);
0100                 if (bottom != "")
0101                         AddBottom(v[i], bottom, options);
0102         }
0103 }
0104 
0105 function AddTop(el, side, options) {
0106         var d = CreateEl("b"), lim = 4, border = "", p, i, btype = "r", bk, color;
0107         d.style.marginLeft = "-" + getPadding(el, "Left") + "px";
0108         d.style.marginRight = "-" + getPadding(el, "Right") + "px";
0109         if (options.find("alias") || (color = getBk(el)) == "transparent") {
0110                 color = "transparent";
0111                 bk = "transparent";
0112                 border = getParentBk(el);
0113                 btype = "t";
0114         } else {
0115                 bk = getParentBk(el);
0116                 border = Mix(color, bk);
0117         }
0118         d.style.background = bk;
0119         d.className = "niftycorners";
0120         p = getPadding(el, "Top");
0121         if (options.find("small")) {
0122                 d.style.marginBottom = (p - 2) + "px";
0123                 btype += "s";
0124                 lim = 2;
0125         } else if (options.find("big")) {
0126                 d.style.marginBottom = (p - 10) + "px";
0127                 btype += "b";
0128                 lim = 8;
0129         } else
0130                 d.style.marginBottom = (p - 5) + "px";
0131         for (i = 1; i <= lim; i++)
0132                 d.appendChild(CreateStrip(i, side, color, border, btype));
0133         el.style.paddingTop = "0";
0134         el.insertBefore(d, el.firstChild);
0135 }
0136 
0137 function AddBottom(el, side, options) {
0138         var d = CreateEl("b"), lim = 4, border = "", p, i, btype = "r", bk, color;
0139         d.style.marginLeft = "-" + getPadding(el, "Left") + "px";
0140         d.style.marginRight = "-" + getPadding(el, "Right") + "px";
0141         if (options.find("alias") || (color = getBk(el)) == "transparent") {
0142                 color = "transparent";
0143                 bk = "transparent";
0144                 border = getParentBk(el);
0145                 btype = "t";
0146         } else {
0147                 bk = getParentBk(el);
0148                 border = Mix(color, bk);
0149         }
0150         d.style.background = bk;
0151         d.className = "niftycorners";
0152         p = getPadding(el, "Bottom");
0153         if (options.find("small")) {
0154                 d.style.marginTop = (p - 2) + "px";
0155                 btype += "s";
0156                 lim = 2;
0157         } else if (options.find("big")) {
0158                 d.style.marginTop = (p - 10) + "px";
0159                 btype += "b";
0160                 lim = 8;
0161         } else
0162                 d.style.marginTop = (p - 5) + "px";
0163         for (i = lim; i > 0; i--)
0164                 d.appendChild(CreateStrip(i, side, color, border, btype));
0165         el.style.paddingBottom = 0;
0166         el.appendChild(d);
0167 }
0168 
0169 function CreateStrip(index, side, color, border, btype) {
0170         var x = CreateEl("b");
0171         x.className = btype + index;
0172         x.style.backgroundColor = color;
0173         x.style.borderColor = border;
0174         if (side == "left") {
0175                 x.style.borderRightWidth = "0";
0176                 x.style.marginRight = "0";
0177         } else if (side == "right") {
0178                 x.style.borderLeftWidth = "0";
0179                 x.style.marginLeft = "0";
0180         }
0181         return (x);
0182 }
0183 
0184 function CreateEl(x) {
0185         return (document.createElement(x));
0186 }
0187 
0188 function FixIE(el) {
0189         if (el.currentStyle != null && el.currentStyle.hasLayout != null
0190                         && el.currentStyle.hasLayout == false)
0191                 el.style.display = "inline-block";
0192 }
0193 
0194 function SameHeight(selector, maxh) {
0195         var i, v = selector.split(","), t, j, els = [], gap;
0196         for (i = 0; i < v.length; i++) {
0197                 t = getElementsBySelector(v[i]);
0198                 els = els.concat(t);
0199         }
0200         for (i = 0; i < els.length; i++) {
0201                 if (els[i].offsetHeight > maxh)
0202                         maxh = els[i].offsetHeight;
0203                 els[i].style.height = "auto";
0204         }
0205         for (i = 0; i < els.length; i++) {
0206                 gap = maxh - els[i].offsetHeight;
0207                 if (gap > 0) {
0208                         t = CreateEl("b");
0209                         t.className = "niftyfill";
0210                         t.style.height = gap + "px";
0211                         nc = els[i].lastChild;
0212                         if (nc.className == "niftycorners")
0213                                 els[i].insertBefore(t, nc);
0214                         else
0215                                 els[i].appendChild(t);
0216                 }
0217         }
0218 }
0219 
0220 function getElementsBySelector(selector) {
0221         var i, j, selid = "", selclass = "", tag = selector, tag2 = "", v2, k, f, a, s = [], objlist = [], c;
0222         if (selector.find("#")) { // id selector like "tag#id"
0223                 if (selector.find(" ")) { // descendant selector like "tag#id tag"
0224                         s = selector.split(" ");
0225                         var fs = s[0].split("#");
0226                         if (fs.length == 1)
0227                                 return (objlist);
0228                         f = document.getElementById(fs[1]);
0229                         if (f) {
0230                                 v = f.getElementsByTagName(s[1]);
0231                                 for (i = 0; i < v.length; i++)
0232                                         objlist.push(v[i]);
0233                         }
0234                         return (objlist);
0235                 } else {
0236                         s = selector.split("#");
0237                         tag = s[0];
0238                         selid = s[1];
0239                         if (selid != "") {
0240                                 f = document.getElementById(selid);
0241                                 if (f)
0242                                         objlist.push(f);
0243                                 return (objlist);
0244                         }
0245                 }
0246         }
0247         if (selector.find(".")) { // class selector like "tag.class"
0248                 s = selector.split(".");
0249                 tag = s[0];
0250                 selclass = s[1];
0251                 if (selclass.find(" ")) { // descendant selector like tag1.classname
0252                                                                         // tag2
0253                         s = selclass.split(" ");
0254                         selclass = s[0];
0255                         tag2 = s[1];
0256                 }
0257         }
0258         var v = document.getElementsByTagName(tag); // tag selector like "tag"
0259         if (selclass == "") {
0260                 for (i = 0; i < v.length; i++)
0261                         objlist.push(v[i]);
0262                 return (objlist);
0263         }
0264         for (i = 0; i < v.length; i++) {
0265                 c = v[i].className.split(" ");
0266                 for (j = 0; j < c.length; j++) {
0267                         if (c[j] == selclass) {
0268                                 if (tag2 == "")
0269                                         objlist.push(v[i]);
0270                                 else {
0271                                         v2 = v[i].getElementsByTagName(tag2);
0272                                         for (k = 0; k < v2.length; k++)
0273                                                 objlist.push(v2[k]);
0274                                 }
0275                         }
0276                 }
0277         }
0278         return (objlist);
0279 }
0280 
0281 function getParentBk(x) {
0282         var el = x.parentNode, c;
0283         while (el.tagName.toUpperCase() != "HTML"
0284                         && (c = getBk(el)) == "transparent")
0285                 el = el.parentNode;
0286         if (c == "transparent")
0287                 c = "#FFFFFF";
0288         return (c);
0289 }
0290 
0291 function getBk(x) {
0292         var c = getStyleProp(x, "backgroundColor");
0293         if (c == null || c == "transparent" || c.find("rgba(0, 0, 0, 0)"))
0294                 return ("transparent");
0295         if (c.find("rgb"))
0296                 c = rgb2hex(c);
0297         return (c);
0298 }
0299 
0300 function getPadding(x, side) {
0301         var p = getStyleProp(x, "padding" + side);
0302         if (p == null || !p.find("px"))
0303                 return (0);
0304         return (parseInt(p));
0305 }
0306 
0307 function getStyleProp(x, prop) {
0308         if (x.currentStyle)
0309                 return (x.currentStyle[prop]);
0310         if (document.defaultView.getComputedStyle)
0311                 return (document.defaultView.getComputedStyle(x, '')[prop]);
0312         return (null);
0313 }
0314 
0315 function rgb2hex(value) {
0316         var hex = "", v, h, i;
0317         var regexp = /([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
0318         h = regexp.exec(value);
0319         for (i = 1; i < 4; i++) {
0320                 v = parseInt(h[i]).toString(16);
0321                 if (v.length == 1)
0322                         hex += "0" + v;
0323                 else
0324                         hex += v;
0325         }
0326         return ("#" + hex);
0327 }
0328 
0329 function Mix(c1, c2) {
0330         var i, step1, step2, x, y, r = new Array(3);
0331         if (c1.length == 4)
0332                 step1 = 1;
0333         else
0334                 step1 = 2;
0335         if (c2.length == 4)
0336                 step2 = 1;
0337         else
0338                 step2 = 2;
0339         for (i = 0; i < 3; i++) {
0340                 x = parseInt(c1.substr(1 + step1 * i, step1), 16);
0341                 if (step1 == 1)
0342                         x = 16 * x + x;
0343                 y = parseInt(c2.substr(1 + step2 * i, step2), 16);
0344                 if (step2 == 1)
0345                         y = 16 * y + y;
0346                 r[i] = Math.floor((x * 50 + y * 50) / 100);
0347                 r[i] = r[i].toString(16);
0348                 if (r[i].length == 1)
0349                         r[i] = "0" + r[i];
0350         }
0351         return ("#" + r[0] + r[1] + r[2]);
0352 }