/*
Modified to work with jQuery -- Scott Buchanan, 6/1/2006.

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);

String.prototype.find = function (what) {
	return (this.indexOf(what) >= 0 ? true : false);
};

function Nifty (selector, options) {
	if (niftyOk == false) return;
	var v = selector.split(","), h = 0;
	if (options == null) options = "";
	if (options.find("fixed-height")) h = $(v[0]).get()[0].offsetHeight;
	for (var i = 0; i < v.length; i++) Rounded(v[i],options);
};

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");
		if (options.find("tl")) {
			top = options.find("tr") ? 'both' : 'left';
		} else if (options.find("tr")) {
			top="right";
		}
		if (options.find("bl")) {
			bottom = options.find("br") ? 'both' : 'left';
		} else if (options.find("br")) {
			bottom = "right";
		}
	}
	if (top == "" && bottom == "" && !options.find("none")) { top = "both"; bottom = "both"; }
	v = $(selector).get();
	for (i = 0; i < v.length; i++) {
		FixIE (v[i]);
		if (top != "") AddSlices (v[i], 'Top', top, options);
		if (bottom != "") AddSlices (v[i], 'Bottom', bottom, options);
	}
};

function AddSlices (el, vSide, hSide, options) {
	var vSideOpp = (vSide == 'Top') ? 'Bottom' : 'Top';
	
	var d = CreateEl("b");
	d.className = "niftycorners";
	d.style.marginLeft = "-" + getPadding(el, "Left") + "px";
	d.style.marginRight = "-" + getPadding(el, "Right") + "px";
	
	var color, bk, border, btype;
	if (options.find("transparent") || (color = getBk(el)) == "transparent") {
		color = "transparent";
		bk = "transparent";
		border = getParentBk(el);
		btype = 't';
	} else {
		bk = getParentBk(el);
		border = Mix(color, bk);
		btype = 'r';
	}
	d.style.background = bk;
	
	var p = getPadding (el, vSide);
	if (options.find("small")) {
		d.style['margin' + vSideOpp] = (p - 2) + "px";
		btype += "s";
		var lim = 2;
	} else if (options.find("big")) {
		d.style['margin' + vSideOpp] = (p - 10) + "px";
		btype += "b";
		var lim = 8;
	} else {
		d.style['margin' + vSideOpp] = (p - 5) + "px";
		var lim = 4;
	}
	if (vSide == 'Top')
		for (var i = 1; i <= lim; i++) d.appendChild (CreateStrip(i, hSide, color, border, btype));
	else
		for (var i = lim; i > 0; i--) d.appendChild (CreateStrip(i, hSide, color, border, btype));
	el.style['padding' + vSide] = "0";
	
	if (vSide == 'Top') el.insertBefore (d, el.firstChild);
	else 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 getParentBk (x){
	var el = x.parentNode, c;
	while (el.tagName.toUpperCase() != "HTML" && (c = getBk(el)) == "transparent")
		el = el.parentNode;
	if (c == "transparent") c = "#FFFFFF";
	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) {
	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];
};