$(document).ready(function(){
	var h1Colour = getElementColourAsHex('h1');
    $('h1').flash(
        { 
            src: '/flash/frutiger-47-cn.swf', 
			wmode: "transparent",
            flashvars: {
                css: [
                    '* { color: #'+h1Colour+'; }',
                    'a { color: #'+h1Colour+'; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { version: 7 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height();
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));						
        }
    );
});

// functions to turn RGB into a Hex. colour
function RGBtoHex(R,G,B) {
	return toHex(R)+toHex(G)+toHex(B);
}

function toHex(N) {
	if (N==null) {
		return "00";
	} else {
		N=parseInt(N); if (N==0 || isNaN(N)) return "00";
		N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
		return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
	}
}

// function to find an element and return the css 'color' property as Hex.
function getElementColourAsHex(el) {
	var elColour = $(el).css('color');
	if(elColour == undefined) {
		return '000000';
	} else {
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			//test for MSIE x.x;
			elColour = elColour.replace('#','');
			return elColour;
		} else {
			elColour = elColour.replace('rgb(','');
			elColour = elColour.replace(')','');
			elColour = elColour.replace(' ','');
			elColour = elColour.split(',');
			elColour = RGBtoHex(elColour[0],elColour[1],elColour[2]);
			return elColour;
		}
	}	
}
