/**
 *
 * Javascript Widgets 2000
 * Javascript reusable GUI components.
 * Copyright by ChieftainY2k
 *
 * Designed, coded and maintained by ChieftainY2k , www.ChieftainY2k.net
 * ChieftainY2k[at]Y2k-design[dot]net
 * ChieftainY2k[at]vip[dot]interia[dot]pl
 * ChieftainY2k[at]anronet[dot]pl
 *
 * Post all questions regarding licensing to one of the addresses given above.
 *
 */

/*

 Highlight buttons
 ver 1.0

 Usage example:

		<style> @import url(jsw2000/w-hlbutton.css); </style>
		<script src="jsw2000/w-hlbutton.js"></script>
		<script defer> hlbutton_init(); </script>

		<button style="background:#00aa00" >ala ma kota</button><br>
		<button hlbutton="hlcolor:red;theme:xp" id="aa" onmouseover="window.status='bla!'" onmouseout="window.status=''">ala ma kota</button><br>
		<br><br>
		<button hlbutton class="button_green">ala ma kota</button><br>
		<button hlbutton="hlcolor:auto" style="border: solid 1px black">ala ma kota</button><br>
		<button hlbutton="hlcolor:red" style="border: solid 1px black">ala ma kota</button><br>

		<div style="padding:10px; margin:10xp; border: solid 1px black; background:white">
			<button hlbutton="theme:xp" style="display:none">ala ma kota</button>
			<button hlbutton="theme:xp" style="display:none">ala ma kota</button>
			<button hlbutton="theme:xp" style="display:none">ala ma kota</button>
		</div>

		<div style="padding:10px; margin:10xp; border: solid 1px black; background:white">
			<button hlbutton="theme:xp2" style="display:none">ala ma kota</button>
			<button hlbutton="theme:xp2" style="display:none">ala ma kota</button>
			<button hlbutton="theme:xp2" style="display:none">ala ma kota</button>
		</div>


 Important notes:

 	- buttons init background colors must be in the #rrggbb format

*/

/**
 * detect and convert all HL buttons
 *
 * @param defaultAttribs default attribute applied when no "hlbutton" attrib is found
 *
 */
function hlbutton_init(defaultAttribs)
{
	document.hlbuttonsData={};
	//get all buttons
	var elements = document.getElementsByTagName("button");
	//scan for specific attributes
	for(var i=0;i<elements.length;i++)
 	 for(var i2=elements[i].attributes.length-1;i2>=0;i2--)
 	  if (elements[i].attributes[i2].name=="hlbutton" || elements[i].attributes[i2].name=="hlb")
 	   {
 	   		
 	   		if (document.hlbuttonsData[elements[i].id]) continue; //this button has already been initizlized

 	   		//get "hlbutton" attrib value and set default value if necessary
 	   		button_attrib_value = elements[i].attributes[i2].value;
 	   		if (!button_attrib_value) button_attrib_value= defaultAttribs ? defaultAttribs : "hlcolor:auto";

			//hlbutton DOM element found
 	   		var obj = elements[i];
			if (!obj.id) obj.id='hlbutton_'+i;
			//init button data container
			document.hlbuttonsData[obj.id]={};
			//attach events
			eval("obj.attachEvent('onmouseover',function() { hlbutton_mouseover('"+obj.id+"') });");
			eval("obj.attachEvent('onmouseout',function() { hlbutton_mouseout('"+obj.id+"') });");

			//parse attribute for button params
			if (button_attrib_value.length)
			{
				var params = button_attrib_value.split(";");
				for(var pi=0;pi<params.length;pi++)
				{
					tmp = params[pi].split(":");
					param_name = tmp[0]; param_value = tmp[1];
					document.hlbuttonsData[obj.id][param_name] = param_value;
					//alert(obj.id+": "+param_name+":"+param_value)
				};
			};

			//set init params
			theme = document.hlbuttonsData[obj.id]['theme'];
			themePrefix = theme ? theme+"_" : "";
			if (theme) obj.className = themePrefix+"hlbutton_normal";
			//insert icon if available
			if (document.hlbuttonsData[obj.id]['icon'])
			{
				var iconHTML = "<img id='"+obj.id+"-icon' src='"+document.hlbuttonsData[obj.id]['icon']+"' align='absmiddle' border=0>";
				iconAlign = document.hlbuttonsData[obj.id]['icon-align'];
				obj.insertAdjacentHTML( iconAlign=='right' ? "beforeEnd":"afterBegin",iconHTML);
				document.all[obj.id+"-icon"].className = themePrefix+"hlbutton_icon";
			};
			obj.style.display="inline";
			break;
 	   };
};


function hlbutton_hex(n) {var h=n.toString(16); return (h.length%2)?"0"+h:h};
function hlbutton_dec(n) {return parseInt(n,16);};

/**
 * HLbutton mouse event handler
 */
function hlbutton_mouseover(bid)
{
	theme = document.hlbuttonsData[bid]['theme'];
	themePrefix = theme ? theme+"_" : "";

	//apply theme if available
	if (theme) document.all[bid].className = themePrefix+"hlbutton_mouseover";

	//save current back color
	var currentColor = document.all[bid].currentStyle.backgroundColor;
	document.hlbuttonsData[bid]['initcolor'] = currentColor;


	//change button background
	if (document.hlbuttonsData[bid]['hlcolor']=="auto")
	{
		//automatic
		var b1 =  hlbutton_dec(currentColor.substring(1,3)); b1+=(b1<128 ? 128 :16); b1 = hlbutton_hex( b1>255 ? 255 : b1);
		var b2 =  hlbutton_dec(currentColor.substring(3,5)); b2+=(b2<128 ? 128 :16); b2 = hlbutton_hex( b2>255 ? 255 : b2);
		var b3 =  hlbutton_dec(currentColor.substring(5,7)); b3+=(b3<128 ? 128 :16); b3 = hlbutton_hex( b3>255 ? 255 : b3);
		document.all[bid].runtimeStyle.background = "#"+b1+b2+b3;
	}
	else if (document.hlbuttonsData[bid]['hlcolor'])
	{
		//custom
		document.all[bid].runtimeStyle.background = document.hlbuttonsData[bid]['hlcolor'];
	}

	if (document.hlbuttonsData[bid]['icon']) document.all[bid+'-icon'].className = themePrefix+"hlbutton_icon_mouseover";
};

/**
 * HLbutton mouse event handler
 */
function hlbutton_mouseout(bid)
{
	theme = document.hlbuttonsData[bid]['theme'];
	themePrefix = theme ? theme+"_" : "";

	//restore back color
	if (document.hlbuttonsData[bid]['hlcolor']) document.all[bid].runtimeStyle.background = document.hlbuttonsData[bid]['initcolor'];
	//apply theme if available
	if (theme) document.all[bid].className = themePrefix+"hlbutton_normal";
	if (document.hlbuttonsData[bid]['icon']) document.all[bid+'-icon'].className = themePrefix+"hlbutton_icon";
};


