/*****************************************************************************
It is adviced to place the sIFR JavaScript calls in this file, keeping it
separate from the `sifr.js` file. That way, you can easily swap the `sifr.js`
file for a new version, while keeping the configuration.

You must load this file *after* loading `sifr.js`.

That said, you're of course free to merge the JavaScript files. Just make sure
the copyright statement in `sifr.js` is kept intact.
*****************************************************************************/

// Make an object pointing to the location of the Flash movie on your web server.
// Try using the font name as the variable name, makes it easy to remember which
// object you're using. As an example in this file, we'll use Futura.
var calibri = { src: '/sifr/swf/calibri.swf' };
var calibri_bold = { src: '/sifr/swf/calibri-bold.swf' };
var baskerville = { src: '/sifr/swf/baskerville.swf' };
var helvetica_bold = { src: '/sifr/swf/helveticaneue-bold.swf' };
var helvetica = { src: '/sifr/swf/helveticaneue.swf' };
var helvetica_light = { src: '/sifr/swf/helveticaneue_lt.swf' };

// Now you can set some configuration settings.
// See also <http://wiki.novemberborn.net/sifr3/JavaScript+Configuration>.
// One setting you probably want to use is `sIFR.useStyleCheck`. Before you do that,
// read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad>.

// sIFR.useStyleCheck = true;

// Next, activate sIFR:
sIFR.activate(calibri, calibri_bold, baskerville, helvetica, helvetica_bold, helvetica_light);

// If you want, you can use multiple movies, like so:
//
//    var futura = { src: '/path/to/futura.swf' };
//    var garamond = { src '/path/to/garamond.swf' };
//    var rockwell = { src: '/path/to/rockwell.swf' };
//    
//    sIFR.activate(futura, garamond, rockwell);
//
// Remember, there must be *only one* `sIFR.activate()`!

// Now we can do the replacements. You can do as many as you like, but just
// as an example, we'll replace all `<h1>` elements with the Futura movie.
// 
// The first argument to `sIFR.replace` is the `futura` object we created earlier.
// The second argument is another object, on which you can specify a number of
// parameters or "keyword arguemnts". For the full list, see "Keyword arguments"
// under `replace(kwargs, mergeKwargs)` at 
// <http://wiki.novemberborn.net/sifr3/JavaScript+Methods>.
// 
// The first argument you see here is `selector`, which is a normal CSS selector.
// That means you can also do things like '#content h1' or 'h1.title'.
//
// The second argument determines what the Flash text looks like. The main text
// is styled via the `.sIFR-root` class. Here we've specified `background-color`
// of the entire Flash movie to be a light grey, and the `color` of the text to
// be red. Read more about styling at <http://wiki.novemberborn.net/sifr3/Styling>.

$.fn.sIFR = function(font, css, options, ieonly) {
	$.fn.leading = function() {
		var font = parseFloat($(this).css('font-size'));
		var lineheight = parseFloat($(this).css('line-height'));
		leading = ($.browser.msie) ? (lineheight - 1) * font : leading = (lineheight - font)/2;
		return leading;
	};
	
	$.fn.color = function() {
		var rgb = $(this).css('color');
		return colorToHex(rgb);
	};
	
	$.fn.background = function() {
		var bg = $(this).css('background-color');
		return colorToHex((bg != 'transparent' && bg != "rgba(0, 0, 0, 0)") ? bg : $(this).parent().background());
	}
	
	if($(this).length > 0) {
		selector = this["selector"];
		
		if($(this).css('font-variant') == "small-caps") {
			$(this).text(function(index, text) {
				return text.toUpperCase();
			});
		}
		
		sIFROptions = {
			selector: selector,
			wmode: 'transparent',
			css: '.sIFR-root {'
				+ 'leading:' + $(this).leading() + '; '
				+ 'color: ' + $(this).color() + '; '
				+ 'background-color: ' + $(this).background() + "; " // Stops certain browsers messing up. Hopefully.
				+ css + '} '
				+ 'a { color: #006b82; text-decoration: none;}'
				+ 'a:hover { color: #006b82; text-decoration: underline }'
		};
		
		if(options) {
			(options.offsetTop) ? sIFROptions.offsetTop = options.offsetTop : "";
			(options.tuneHeight) ? sIFROptions.tuneHeight = options.tuneHeight : "";
			(options.preventWrap) ? sIFROptions.preventWrap = options.preventWrap : "";
			(options.tuneWidth) ? sIFROptions.tuneWidth = options.tuneWidth : "";
		}
		
		//Force correct height.
		if(!$.browser.msie) {
			$(this).css('height', $(this).height() + "px");
		}
		
		sIFR.replace(font, sIFROptions);
	}
	return this;
};

function colorToHex(color) {
    if (color.substr(0, 1) === '#') {
    	//IE tends to just keep colours in hex.
        return color;
    }
    
    
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);

    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    
    Number.prototype.leading_zero = function(radix) {
   		return (this < radix) ? "0" + this.toString(radix) : this.toString(radix); 
    };
    
    return "#" + red.leading_zero(16) + green.leading_zero(16) + blue.leading_zero(16);
};

/** Remember to add sIFRised elements to the line in scripts.js **/
/* sIFR.replace(calibri_bold, {
	  selector: '#content h2',
	  wmode: 'transparent',
	  css: '.sIFR-root {color: #006b82;}'
}); 
/*
sIFR.replace(baskerville, {
	selector: '.content #sidebar h2',
	  wmode: 'transparent',
	  css: '.sIFR-root {color: #006b82; leading: 5;}'
}); */

