/*************************

Cross Browser Gradient Backgrounds
v1.0
Last Revision: 11.03.2005
steve@slayeroffice.com

please leave this notice in tact.

should you improve upon this code, please
let me know so that I may update the version
hosted on slayeroffice

--- to use --
reference this file (on your own server) as a javascript src
in the head of your document. give the elements
you want a gradient background applied to a class as such:

class="gradient 000000 ffffff horizontal"

See http://slayeroffice.com/code/gradient/ for more examples.


*************************/

//window.addEventListener?window.addEventListener("load",createGradient,false):window.attachEvent("onload",createGradient);

function createGradient(nodeId, startColor, endColor, orientation) {
	if(!document.getElementById)return;

	var node = document.getElementById(nodeId);
	if(node == null) return;

	if(document.all && !window.opera) {
		node.style.width = node.offsetWidth + "px";
		orientation == "horizontal"?gType = 1:gType = 0;
		node.style.filter = "progid:DXImageTransform.Microsoft.Gradient(GradientType="+gType+",StartColorStr=\"#" + startColor + "\",EndColorStr=\"#" + endColor + "\")";
	} else {
		var colorArray = createColorPath(startColor,endColor);
		var x=0;var y=0;var w=null;var h=null;
		if(orientation == "horizontal") {
			w=Math.round(node.offsetWidth/colorArray.length);
			if(!w)w=1;
			h = node.offsetHeight;
		} else {
			h = Math.round(node.offsetHeight/colorArray.length);
			if(!h) h =1;
			w = node.offsetWidth;
		}
		x = findPosX(node);
		y = findPosY(node);
		var cont = true;
		makeGrandParent(node);
		var nodeRight = parseInt(document.defaultView.getComputedStyle(node, "").getPropertyValue("width")) + getAbsoluteObjectLeft(node);
		tmpDOM = document.createDocumentFragment();
		//alert(colorArray.length);
		for(p=0;p<colorArray.length;p++) {
			g = document.createElement("div");
			g.setAttribute("style","position:absolute;z-index:0;top:" + y + "px;left:" + x + "px;height:" + h + "px;width:" + w + "px;background-color:rgb(" + colorArray[p][0] + "," + colorArray[p][1] + "," + colorArray[p][2] + ");");
			orientation == "horizontal"?x+=w:y+=h;
			tmpDOM.appendChild(g);
			/*
			//if(cont)
				//cont = confirm('y = ' + (orientation == 'vertical' && y>=node.style.height));
			if(cont)
				cont = confirm('x value is ' + (orientation == 'horizontal' && x>= nodeRight+10));
			//if(cont)
				//cont = confirm('orientation = ' + orientation);
			if(cont)
				cont = confirm('x>= nodeRight+10 is ' + ( x >= nodeRight+10 ));
			if(cont)
				cont = confirm('x = ' + x);
			if(cont)
				cont = confirm('nodeRight = ' + (nodeRight+10));
			//if(cont)
			//	cont = confirm('length = '+ colorArray.length);
			*/
				
			if((orientation == 'vertical' && y>=node.style.height) || 
				 (orientation == 'horizontal' && x>= nodeRight+10 )){
				break;
			}
		}
		node.appendChild(tmpDOM);
		tmpDOM = null;
	}
}
function getAbsoluteObjectLeft(obj) {
	var aLeft = 0;
	while (obj!=document) {
		var addTo = obj.offsetLeft;
		if (addTo && obj.className) {
			//alert(obj.className + " : " + addTo);
			aLeft += addTo;
		}
		obj = obj.parentNode;
	}
	return aLeft;
}

function getGradientObjects() {
	a = document.getElementsByTagName("*");
	objs = new Array();
	for(i=0;i<a.length;i++) {
		c = a[i].className;
		if(c != "") if(c.indexOf("gradient") == 0) objs[objs.length] = a[i];
	} 
	return objs;
}
	
function createColorPath(color1,color2) {
	colorPath = new Array();
	colorPercent = 1.0;
	do {
		colorPath[colorPath.length]=setColorHue(longHexToDec(color1),colorPercent,longHexToDec(color2));
		colorPercent-=.009;
	} while(colorPercent>0);
	return colorPath;
}
		
function setColorHue(originColor,opacityPercent,maskRGB) {
	returnColor=new Array();
	for(w=0;w<originColor.length;w++) returnColor[w] = Math.round(originColor[w]*opacityPercent) + Math.round(maskRGB[w]*(1.0-opacityPercent));
	return returnColor;
}

function longHexToDec(longHex) {
	return new Array(toDec(longHex.substring(0,2)),toDec(longHex.substring(2,4)),toDec(longHex.substring(4,6)));	
}

function toDec(hex) {	
	return parseInt(hex,16);
}
	
function makeGrandParent(obj) {
	disp = document.defaultView.getComputedStyle(obj,'').display;
	disp == "block"?nSpan = document.createElement("div"):	nSpan = document.createElement("span");
	mHTML = obj.innerHTML;
	obj.innerHTML = "";
	nSpan.innerHTML = mHTML;
	nSpan.setAttribute("style","position:relative;z-index:10;");
	obj.appendChild(nSpan);
}
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
