﻿/*

jQuery sIFR Plugin
	* Version 2.0 Beta 2
	* 2008-07-22 08:32:02
	* URL: http://jquery.thewikies.com/sifr
	* Description: jQuery Sifr Plugin replaces traditional text in a web page with flash text (sIFR).
	* Author: Jonathan Neal
	* Copyright: Copyright (c) 2008 Jonathan Neal under dual MIT/GPL license.
	* JSLint: This javascript file passes JSLint verification.
*//*jslint
		bitwise: true,
		browser: true,
		eqeqeq: true,
		glovar: true,
		nomen: true,
		plusplus: true,
		undef: true,
		white: true
*//*global
		jQuery
*/

/* == jQuery Sifr Plugin (with selectors) == */
jQuery.fn.sifr = function (prefs) {

	/* == lock and load our preferences == */
	var p = jQuery.extend((prefs === false) ? { unsifr: true } : {}, arguments.callee.prefs, prefs);

	/* == if necessary, save our prefs == */
	if (p.save) {
		arguments.callee.prefs = jQuery.extend(p, { save: false });
	}

	/* == we're done if there's no sIFR specified == */
	if (this[0] === document) {
		return;
	}

	/* == if necessary, run a custom function before we begin == */
	if (!p.unsifr && typeof p.before === 'function') {
		p.before.apply(this, [p]);
	}

	/* == do this function on every element we've selected == */
	this.each(function () {

		/* == 't' will mean the jQuery version of 'this' == */
		var t = jQuery(this);

		/* == 'a' will mean the possible '.sIFR-alternate' child of 't' == */
		var a = t.children('.sIFR-alternate');

		/* == if 'a' exists, then it's time to unSifr == */
		if (a) {
			t.html(a.html());

			/* == if unsifr was called, then it's time to go == */
			if (p.unsifr) {
				return;
			}
		}

		if (typeof p.beforeEach === 'function') {
			p.beforeEach.apply(this, [t, p]);
		}

		/* == 's' will mean the newly created child of 't', and it's what will be sIFRed == */
		var s = t.html('<span class="flash-replaced sIFR-replaced">' + (p.content || t.html()).replace(/^\s+|\s+$/g, '') + '</span>').children();

		/* == 'a' will mean another newly created child of 't', and it's where the alternate text goes == */
		a = t.append('<span class="alt sIFR-alternate">' + s.html() + '</span>').children('.sIFR-alternate');

		/* == if 'a' isn't hidden already, make it secret, make it safe == */
		if (a.css('display') !== 'none') {
			a.css('display', 'none');
		}

		/* == make a function which converts "most" colors into the 6 digit hexidecimal equivelent == */
		var toHex = function (c) {
			var h = function (n) {
				if (n === 0 || isNaN(n)) {
					return '00';
				}
				n = Math.round(Math.min(Math.max(0, n), 255));
				return '0123456789ABCDEF'.charAt((n - n % 16) / 16) + '0123456789ABCDEF'.charAt(n % 16);
			};
			c = (c) ? c.replace(/rgb|\(|\)|#$/g, '') : false;
			if (!c) {
				return false;
			}
			if (c.indexOf(',') > -1) {
				c = c.split(', ');
				return '#' + h(c[0]) + h(c[1]) + h(c[2]);
			}
			if (c.search('#') > -1 && c.length <= 4) {
				c = c.split('');
				return '#' + c[1] + c[1] + c[2] + c[2] + c[3] + c[3];
			}
			return c;
		};

		/* == this is a very special addition for textTransform support == */
		if (p.textTransform) {
			if (p.textTransform.toLowerCase() === 'uppercase') {
				s.html(s.html().toUpperCase());
			}
			if (p.textTransform.toLowerCase() === 'lowercase') {
				s.html(s.html().toLowerCase());
			}
			if (p.textTransform.toLowerCase() === 'capitalize') {
				var c = s.html().replace(/\>/g, '> ').split(' ');
				for (var i = 0; i < c.length; i = i + 1) {
					c[i] = c[i].charAt(0).toUpperCase() + c[i].substring(1);
				}
				s.html(c.join(' ').replace(/\> /g, '>'));
			}
		}

		/* == 'f' will mean the flash plugin htmloptions == */
		var f = {
			flashvars: jQuery.extend({
				h: s.height() * (p.zoom || 1),
				offsetLeft: p.offsetLeft || undefined,
				offsetTop: p.offsetTop || undefined,
				textAlign: p.textAlign || (/(left|center|right)/.exec(t.css('textAlign')) || ['left'])[0],
				textColor: toHex(p.color || t.css('color')) || undefined,
				txt: p.content || s.html(),
				underline: (p.underline || (p.underline !== false && t.css('textDecoration') === 'underline')) ? true : undefined,
				w: (p.width || s.width()) * (p.zoom || 1)
			}, p.flashvars),
			height: p.height || s.height(),
			src: (p.path || '') + ((p.path && p.path.substr(p.path.length - 1) !== '/') ? '/' : '') + (p.font || '') + ((p.font && p.font.indexOf('.swf') === -1) ? '.swf' : ''),
			width: p.width || s.width(),
			wmode: 'transparent'
		};

		/* == make some more flash plugin htmloptions == */
		f.flashvars.linkColor = toHex(p.link || t.find('a').css('color')) || f.flashvars.textColor;
		f.flashvars.hoverColor = toHex(p.hover) || f.flashvars.linkColor;

		if (p.zoom) {
			f.flashvars.offsetTop = ((p.offsetTop || 0) + ((s.height() - (s.height() * p.zoom)) / 2)) * (p.zoomTop || 1);
			f.flashvars.offsetLeft = ((p.offsetLeft || 0) + ((s.width() - (s.width() * p.zoom)) / 2)) * (p.zoomLeft || 1);
		}

		/* == hand the mic over to luke lutman and his big flash band == */
		t.flash(
			jQuery.extend(f, p.embedOptions),
			jQuery.extend({
				expressInstall: p.expressInstall || false,
				version: p.version || 7,
				update: p.update || false
			}, p.pluginOptions),
			function (f) {

				/* == now remember how wide and tall things are == */
				var preHeight = t.height();
				var preWidth = t.width();

				/* == the flash bang == */
				s.html(jQuery.fn.flash.transform(f));

				/* == 'e' will mean the embedded flash == */
				var e = s.find(':first');

				e.css({
					verticalAlign: 'text-bottom',
					display: 'inline',
					width: p.width,
					height: p.height
				});
				
				var marginBottom = preHeight - t.height();
				var width = parseInt(e.css('width'), 10) + parseInt(preWidth - t.width(), 10);

				/* == now enforce these non-changes == */
				if (!p.height) {
					e.css({
						marginBottom: marginBottom
					});
				}
				if (!p.width) {
					e.css({
						width: width
					});
				}

				/* == special and strange middle vertical alignment == */
				if (p.height && p.verticalAlign === 'middle') {
					e.css({
						marginTop: Math.floor((p.height - s.height()) / 2),
						marginBottom: Math.round((p.height - s.height()) / 2),
						height: s.height()
					});
					e.attr('height', s.height());
				}

				/* == special and strange bottom vertical alignment == */
				if (p.height && p.verticalAlign === 'bottom') {
					var a = t.find('.sIFR-alternate');
					e.css({
						marginTop: (p.height - s.height()),
						height: s.height()
					});
					e.attr('height', s.height());
				}

				if (p.css) {
					e.css(p.css);
				}
			}
		);

		if (typeof p.afterEach === 'function') {
			p.afterEach.apply(this, [t, p]);
		}
	});

	/* == if necessary, run a custom function after we're done == */
	if (!p.unsifr && typeof p.after === 'function') {
		p.after.apply(this, [p]);
	}

};

/* == jQuery Sifr Plugin (without selectors) == */
jQuery.sifr = function (prefs) {
	jQuery().sifr(jQuery.extend({
		save: true
	}, prefs));
};

/* == jQuery Sifr Plugin (as unSifr) == */
jQuery.fn.unsifr = function () {
	return this.each(
		function () {
			jQuery(this).sifr(false);
		}
	);
};