//Function to convert hex format to a rgb color  
function rgb2hex(rgb) {
	
	//see if already returned rgb color - probably not bulletproof here
	//IE returns colors in RGB, go figure
	if (rgb.substr(0,1) == "#") {
		return rgb.substr(1);
	} else {
		rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);  
		function hex(x) {  
			hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");  
	 		return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];  
		}
		return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}
}  

//replace headings
function replaceHeadingGuts (theobject, theheight, fontLetterSpacing, fontOffsetX, fontOffsetY) {
	
	//set defaults
	if (!fontLetterSpacing) { fontLetterSpacing = 0; }
	if (!fontOffsetX) { fontOffsetX = -3; }
	if (!fontOffsetY) { fontOffsetY = 0; }
	
	$(theobject).each(function (){
		contentFiller = $(this).text();
		
		//$(this).css('height', (parseFloat(theheight)+ 5)+"px");

		//in the fck, if an internal anchor is added, the text is wrapped with a <a name="x">content</a>
		//this line pulls the name attribute into the heading itself so internal anchors/links still work
		$(this).attr('id', $(this).find('a').attr('name'));
		
		//if your headings are getting pushed way down because of another floated right
		//element, enable this and set a fixed width
		//if ($("#secondBody").length) {
		//	$(this).css('width', '460px');
		//}
		
		$(this).flashembed({
			//flashembed vars
			src: "/assets/flash/heading.swf",
			version: [9, 0], //required to prevent replacement on mobiles
			wmode: "transparent",
			width: "100%",
			height: "100%",
			scale: "noscale",
			salign: "lt" }, {
			//vars to flash file
			headingText: encodeURIComponent(contentFiller),
			headingColor: "0x" + rgb2hex ($(this).css('color')),
			headingSize: theheight,
			headingLetterSpacing: fontLetterSpacing,
			headingOffsetX: fontOffsetX,
			headingOffsetY: fontOffsetY });
	});
}

/*

//USAGE - be sure to include this in your document.ready function:

function replaceHeading(){
	replaceHeadingGuts("#copyCopy h1", "28");
	replaceHeadingGuts("#copyCopy h2", "22");
	replaceHeadingGuts("#copyCopy h3", "19");
}

$(document).ready(function() { 
	
	//turn off in IE6 to speed things up if you want
	if (!($.browser.msie && $.browser.version=="6.0")) {
		replaceHeading ();
	}

});

*/
