/* Nifty Corners Cube - rounded corners with CSS and Javascript
Copyright 2006 Alessandro Fulciniti (a.fulciniti@html.it)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

var niftyOk=(document.getElementById && document.createElement && Array.prototype.push);
var niftyCss=false;

String.prototype.find=function(what){
return(this.indexOf(what)>=0 ? true : false);
}

var oldonload=window.onload;
if(typeof(NiftyLoad)!='function') NiftyLoad=function(){};
if(typeof(oldonload)=='function')
    window.onload=function(){oldonload();AddCss();NiftyLoad()};
else window.onload=function(){AddCss();NiftyLoad()};

function AddCss(){
niftyCss=true;
var l=CreateEl("link");
l.setAttribute("type","text/css");
l.setAttribute("rel","stylesheet");
l.setAttribute("href","/w2pagev/niftyCorners.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

function Nifty(selector,options){
if(niftyOk==false) return;
if(niftyCss==false) AddCss();
var i,v=selector.split(","),h=0;
if(options==null) options="";
if(options.find("fixed-height"))
    h=getElementsBySelector(v[0])[0].offsetHeight;
for(i=0;i<v.length;i++)
    Rounded(v[i],options);
if(options.find("height")) SameHeight(selector,h);
}

function Rounded(selector,options){
var i,top="",bottom="",v=new Array();
if(options!=""){
    options=options.replace("left","tl bl");
    options=options.replace("right","tr br");
    options=options.replace("top","tr tl");
    options=options.replace("bottom","br bl");
    options=options.replace("transparent","alias");
    if(options.find("tl")){
        top="both";
        if(!options.find("tr")) top="left";
        }
    else if(options.find("tr")) top="right";
    if(options.find("bl")){
        bottom="both";
        if(!options.find("br")) bottom="left";
        }
    else if(options.find("br")) bottom="right";
    }
if(top=="" && bottom=="" && !options.find("none")){top="both";bottom="both";}
v=getElementsBySelector(selector);
for(i=0;i<v.length;i++){
    FixIE(v[i]);
    if(top!="") AddTop(v[i],top,options);
    if(bottom!="") AddBottom(v[i],bottom,options);
    }
}

function AddTop(el,side,options){
var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
d.style.marginLeft="-"+getPadding(el,"Left")+"px";
d.style.marginRight="-"+getPadding(el,"Right")+"px";
if(options.find("alias") || (color=getBk(el))=="transparent"){
    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
    }
else{
    bk=getParentBk(el); border=Mix(color,bk);
    }
d.style.background=bk;
d.className="niftycorners";
p=getPadding(el,"Top");
if(options.find("small")){
    d.style.marginBottom=(p-2)+"px";
    btype+="s"; lim=2;
    }
else if(options.find("big")){
    d.style.marginBottom=(p-10)+"px";
    btype+="b"; lim=8;
    }
else d.style.marginBottom=(p-5)+"px";
for(i=1;i<=lim;i++)
    d.appendChild(CreateStrip(i,side,color,border,btype));
el.style.paddingTop="0";
el.insertBefore(d,el.firstChild);
}

function AddBottom(el,side,options){
var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
d.style.marginLeft="-"+getPadding(el,"Left")+"px";
d.style.marginRight="-"+getPadding(el,"Right")+"px";
if(options.find("alias") || (color=getBk(el))=="transparent"){
    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
    }
else{
    bk=getParentBk(el); border=Mix(color,bk);
    }
d.style.background=bk;
d.className="niftycorners";
p=getPadding(el,"Bottom");
if(options.find("small")){
    d.style.marginTop=(p-2)+"px";
    btype+="s"; lim=2;
    }
else if(options.find("big")){
    d.style.marginTop=(p-10)+"px";
    btype+="b"; lim=8;
    }
else d.style.marginTop=(p-5)+"px";
for(i=lim;i>0;i--)
    d.appendChild(CreateStrip(i,side,color,border,btype));
el.style.paddingBottom=0;
el.appendChild(d);
}

function CreateStrip(index,side,color,border,btype){
var x=CreateEl("b");
x.className=btype+index;
x.style.backgroundColor=color;
x.style.borderColor=border;
if(side=="left"){
    x.style.borderRightWidth="0";
    x.style.marginRight="0";
    }
else if(side=="right"){
    x.style.borderLeftWidth="0";
    x.style.marginLeft="0";
    }
return(x);
}

function CreateEl(x){
return(document.createElement(x));
}

function FixIE(el){
if(el.currentStyle!=null && el.currentStyle.hasLayout!=null && el.currentStyle.hasLayout==false)
    el.style.display="inline-block";
}

function SameHeight(selector,maxh){
var i,v=selector.split(","),t,j,els=[],gap;
for(i=0;i<v.length;i++){
    t=getElementsBySelector(v[i]);
    els=els.concat(t);
    }
for(i=0;i<els.length;i++){
    if(els[i].offsetHeight>maxh) maxh=els[i].offsetHeight;
    els[i].style.height="auto";
    }
for(i=0;i<els.length;i++){
    gap=maxh-els[i].offsetHeight;
    if(gap>0){
        t=CreateEl("b");t.className="niftyfill";t.style.height=gap+"px";
        nc=els[i].lastChild;
        if(nc.className=="niftycorners")
            els[i].insertBefore(t,nc);
        else els[i].appendChild(t);
        }
    }
}

function getElementsBySelector(selector){
var i,j,selid="",selclass="",tag=selector,tag2="",v2,k,f,a,s=[],objlist=[],c;
if(selector.find("#")){ //id selector like "tag#id"
    if(selector.find(" ")){  //descendant selector like "tag#id tag"
        s=selector.split(" ");
        var fs=s[0].split("#");
        if(fs.length==1) return(objlist);
        f=document.getElementById(fs[1]);
        if(f){
            v=f.getElementsByTagName(s[1]);
            for(i=0;i<v.length;i++) objlist.push(v[i]);
            }
        return(objlist);
        }
    else{
        s=selector.split("#");
        tag=s[0];
        selid=s[1];
        if(selid!=""){
            f=document.getElementById(selid);
            if(f) objlist.push(f);
            return(objlist);
            }
        }
    }
if(selector.find(".")){      //class selector like "tag.class"
    s=selector.split(".");
    tag=s[0];
    selclass=s[1];
    if(selclass.find(" ")){   //descendant selector like tag1.classname tag2
        s=selclass.split(" ");
        selclass=s[0];
        tag2=s[1];
        }
    }
var v=document.getElementsByTagName(tag);  // tag selector like "tag"
if(selclass==""){
    for(i=0;i<v.length;i++) objlist.push(v[i]);
    return(objlist);
    }
for(i=0;i<v.length;i++){
    c=v[i].className.split(" ");
    for(j=0;j<c.length;j++){
        if(c[j]==selclass){
            if(tag2=="") objlist.push(v[i]);
            else{
                v2=v[i].getElementsByTagName(tag2);
                for(k=0;k<v2.length;k++) objlist.push(v2[k]);
                }
            }
        }
    }
return(objlist);
}

function color2hex(x){
if ( x[0] == '#' )
    return x;

switch (x){
    case "aliceblue":  x = "#F0F8FF"; break;
    case "antiquewhite":  x = "#FAEBD7"; break;
    case "aqua":  x = "#00FFFF"; break;
    case "aquamarine":  x = "#7FFFD4"; break;
    case "azure":  x = "#F0FFFF"; break;
    case "beige":  x = "#F5F5DC"; break; 
    case "bisque":  x = "#FFE4C4"; break; 
    case "black":  x = "#000000"; break; 
    case "blanchedalmond":  x = "#FFEBCD"; break; 
    case "blue":  x = "#0000FF"; break; 
    case "blueviolet":  x = "#8A2BE2"; break; 
    case "brown":  x = "#A52A2A"; break; 
    case "burlywood":  x = "#DEB887"; break; 
    case "cadetblue":  x = "#5F9EA0"; break; 
    case "chartreuse":  x = "#7FFF00"; break; 
    case "chocolate":  x = "#D2691E"; break; 
    case "coral":  x = "#FF7F50"; break; 
    case "cornflowerblue":  x = "#6495ED"; break; 
    case "cornsilk":  x = "#FFF8DC"; break; 
    case "crimson":  x = "#DC143C"; break; 
    case "cyan":  x = "#00FFFF"; break; 
    case "darkblue":  x = "#00008B"; break; 
    case "darkcyan":  x = "#008B8B"; break; 
    case "darkgoldenrod":  x = "#B8860B"; break; 
    case "darkgray":  x = "#A9A9A9"; break; 
    case "darkgreen":  x = "#006400"; break; 
    case "darkkhaki":  x = "#BDB76B"; break; 
    case "darkmagenta":  x = "#8B008B"; break; 
    case "darkolivegreen":  x = "#556B2F"; break; 
    case "darkorange":  x = "#FF8C00"; break; 
    case "darkorchid":  x = "#9932CC"; break; 
    case "darkred":  x = "#8B0000"; break; 
    case "darksalmon":  x = "#E9967A"; break; 
    case "darkseagreen":  x = "#8FBC8F"; break; 
    case "darkslateblue":  x = "#483D8B"; break; 
    case "darkslategray":  x = "#2F4F4F"; break; 
    case "darkturquoise":  x = "#00CED1"; break; 
    case "darkviolet":  x = "#9400D3"; break; 
    case "deeppink":  x = "#FF1493"; break; 
    case "deepskyblue":  x = "#00BFFF"; break; 
    case "dimgray":  x = "#696969"; break; 
    case "dodgerblue":  x = "#1E90FF"; break; 
    case "firebrick":  x = "#B22222"; break; 
    case "floralwhite":  x = "#FFFAF0"; break; 
    case "forestgreen":  x = "#228B22"; break; 
    case "fuchsia":  x = "#FF00FF"; break; 
    case "gainsboro":  x = "#DCDCDC"; break; 
    case "ghostwhite":  x = "#F8F8FF"; break; 
    case "gold":  x = "#FFD700"; break; 
    case "goldenrod":  x = "#DAA520"; break; 
    case "gray":  x = "#808080"; break; 
    case "green":  x = "#008000"; break; 
    case "greenyellow":  x = "#ADFF2F"; break; 
    case "honeydew":  x = "#F0FFF0"; break; 
    case "hotpink":  x = "#FF69B4"; break; 
    case "indianred":  x = "#CD5C5C"; break; 
    case "indigo":  x = "#4B0082"; break; 
    case "ivory":  x = "#FFFFF0"; break; 
    case "khaki":  x = "#F0E68C"; break; 
    case "lavender":  x = "#E6E6FA"; break; 
    case "lavenderblush":  x = "#FFF0F5"; break; 
    case "lawngreen":  x = "#7CFC00"; break; 
    case "lemonchiffon":  x = "#FFFACD"; break; 
    case "lightblue":  x = "#ADD8E6"; break; 
    case "lightcoral":  x = "#F08080"; break; 
    case "lightcyan":  x = "#E0FFFF"; break; 
    case "lightgoldenrodyellow":  x = "#FAFAD2"; break; 
    case "lightgreen":  x = "#90EE90"; break; 
    case "lightgrey":  x = "#D3D3D3"; break; 
    case "lightpink":  x = "#FFB6C1"; break; 
    case "lightsalmon":  x = "#FFA07A"; break; 
    case "lightseagreen":  x = "#20B2AA"; break; 
    case "lightskyblue":  x = "#87CEFA"; break; 
    case "lightslategray":  x = "#778899"; break; 
    case "lightsteelblue":  x = "#B0C4DE"; break; 
    case "lightyellow":  x = "#FFFFE0"; break; 
    case "lime":  x = "#00FF00"; break; 
    case "limegreen":  x = "#32CD32"; break; 
    case "linen":  x = "#FAF0E6"; break; 
    case "magenta":  x = "#FF00FF"; break; 
    case "maroon":  x = "#800000"; break; 
    case "mediumaquamarine":  x = "#66CDAA"; break; 
    case "mediumblue":  x = "#0000CD"; break; 
    case "mediumorchid":  x = "#BA55D3"; break; 
    case "mediumpurple":  x = "#9370DB"; break; 
    case "mediumseagreen":  x = "#3CB371"; break; 
    case "mediumslateblue":  x = "#7B68EE"; break; 
    case "mediumspringgreen":  x = "#00FA9A"; break; 
    case "mediumturquoise":  x = "#48D1CC"; break; 
    case "mediumvioletred":  x = "#C71585"; break; 
    case "midnightblue":  x = "#191970"; break; 
    case "mintcream":  x = "#F5FFFA"; break; 
    case "mistyrose":  x = "#FFE4E1"; break; 
    case "moccasin":  x = "#FFE4B5"; break; 
    case "navajowhite":  x = "#FFDEAD"; break; 
    case "navy":  x = "#000080"; break; 
    case "oldlace":  x = "#FDF5E6"; break; 
    case "olive":  x = "#808000"; break; 
    case "olivedrab":  x = "#6B8E23"; break; 
    case "orange":  x = "#FFA500"; break; 
    case "orangered":  x = "#FF4500"; break; 
    case "orchid":  x = "#DA70D6"; break; 
    case "palegoldenrod":  x = "#EEE8AA"; break; 
    case "palegreen":  x = "#98FB98"; break; 
    case "paleturquoise":  x = "#AFEEEE"; break; 
    case "palevioletred":  x = "#DB7093"; break; 
    case "papayawhip":  x = "#FFEFD5"; break; 
    case "peachpuff":  x = "#FFDAB9"; break; 
    case "peru":  x = "#CD853F"; break; 
    case "pink":  x = "#FFC0CB"; break; 
    case "plum":  x = "#DDA0DD"; break; 
    case "powderblue":  x = "#B0E0E6"; break; 
    case "purple":  x = "#800080"; break; 
    case "red":  x = "#FF0000"; break;
    case "rosybrown":  x = "#BC8F8F"; break;
    case "royalblue":  x = "#4169E1"; break;
    case "saddlebrown":  x = "#8B4513"; break;
    case "salmon":  x = "#FA8072"; break;
    case "sandybrown":  x = "#F4A460"; break;
    case "seagreen":  x = "#2E8B57"; break;
    case "seashell":  x = "#FFF5EE"; break;
    case "sienna":  x = "#A0522D"; break;
    case "silver":  x = "#C0C0C0"; break;
    case "skyblue":  x = "#87CEEB"; break;
    case "slateblue":  x = "#6A5ACD"; break;
    case "slategray":  x = "#708090"; break;
    case "snow":  x = "#FFFAFA"; break;
    case "springgreen":  x = "#00FF7F"; break;
    case "steelblue":  x = "#4682B4"; break;
    case "tan":  x = "#D2B48C"; break;
    case "teal":  x = "#008080"; break;
    case "thistle":  x = "#D8BFD8"; break;
    case "tomato":  x = "#FF6347"; break;
    case "turquoise":  x = "#40E0D0"; break;
    case "violet":  x = "#EE82EE"; break;
    case "wheat":  x = "#F5DEB3"; break;
    case "white":  x = "#FFFFFF"; break;
    case "whitesmoke":  x = "#F5F5F5"; break; 
    case "yellow":  x = "#FFFF00"; break; 
    case "yellowgreen":  x = "#9ACD32"; break; 
    }        
return x;
}

function getParentBk(x){
var el=x.parentNode,c;
while(el.tagName.toUpperCase()!="HTML" && (c=getBk(el))=="transparent")
    el=el.parentNode;
if(c=="transparent") c="#FFFFFF";
c=color2hex(c);
return(c);
}

function getBk(x){
var c=getStyleProp(x,"backgroundColor");
if(c==null || c=="transparent" || c.find("rgba(0, 0, 0, 0)"))
    return("transparent");
if(c.find("rgb")) c=rgb2hex(c);
return(c);
}

function getPadding(x,side){
var p=getStyleProp(x,"padding"+side);
if(p==null || !p.find("px")) return(0);
return(parseInt(p));
}

function getStyleProp(x,prop){
if(x.currentStyle)
    return(x.currentStyle[prop]);
if(document.defaultView.getComputedStyle)
    return(document.defaultView.getComputedStyle(x,'')[prop]);
return(null);
}

function rgb2hex(value){
var hex="",v,h,i;
var regexp=/([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
var h=regexp.exec(value);
for(i=1;i<4;i++){
    v=parseInt(h[i]).toString(16);
    if(v.length==1) hex+="0"+v;
    else hex+=v;
    }
return("#"+hex);
}

function Mix(c1,c2){
c1=color2hex(c1);
c2=color2hex(c2);
var i,step1,step2,x,y,r=new Array(3);
if(c1.length==4)step1=1;
else step1=2;
if(c2.length==4) step2=1;
else step2=2;
for(i=0;i<3;i++){
    x=parseInt(c1.substr(1+step1*i,step1),16);
    if(step1==1) x=16*x+x;
    y=parseInt(c2.substr(1+step2*i,step2),16);
    if(step2==1) y=16*y+y;
    r[i]=Math.floor((x*50+y*50)/100);
    r[i]=r[i].toString(16);
    if(r[i].length==1) r[i]="0"+r[i];
    }
return("#"+r[0]+r[1]+r[2]);
}