/*!
 * sifr/sifr.js
 *
 * Copyright (c) 2010 BlueBear Internet Solutions (www.bluebear.nl)
 * Licensed under the MIT license.
 *
 * Based on MooTools 1.2.x
 * Provides: [sIFR]
 */

var sIFR = new Class({
	Implements : Options,
	options: {/*
		'height': undefined,
		'width': undefined,
		'transform': undefined,
		'underline': undefined,
		'textalign': undefined,
		'textcolor': undefined,
		'linkcolor': undefined,
		'hovercolor': undefined,*/
		'active': true,           // Whether to automatically activate
		'elements': 'h1',         // Elements to replace
		'path': '/scripts/sifr/', // Path containing SWFs
		'font': 'sifr'            // Font and SWF file name
	},
	initialize: function(options) {
		if (Browser.Plugins.Flash.version >= 6) {
			this.setOptions(options);
			this.elements = $$(this.options.elements);
			this.activated = false;
			if (this.options.active) {
				this.activate();
			}
		}
	},
	activate: function() {
		if (this.elements && !this.activated) {
			this.elements.each(this.setup, this);
			this.activated = true;
		}
	},
	deactivate: function() {
		if (this.elements && this.activated) {
			this.elements.each(function(el) {
				var preSIFR = el.getElement('.pre-sIFR');
				el.set('html', preSIFR.get('html'));
			}, this);
			this.activated = false;
		}
	},
	setup: function(el) {
		var preSIFR = new Element('span', {
			'class': 'pre-sIFR',
			'styles': {'display': 'none'},
			'html': el.get('html')
		});
		var dimensions = el.getSize();
		var styles = el.getStyles('padding-top', 'padding-right', 'padding-bottom', 'padding-left', 'text-align', 'text-decoration', 'text-transform', 'color');
		var height = this.options.height || dimensions.y - styles['padding-top'].toInt() - styles['padding-bottom'].toInt();
		var width = this.options.width || dimensions.x - styles['padding-left'].toInt() - styles['padding-right'].toInt();

		var flashvars = {
			'h': height,
			'w': width,
			'offsetTop': styles['padding-top'],
			'underline': ($defined(this.options.underline)) ? true : (styles['text-decoration'] == 'underline') ? true : undefined,
			'textalign': this.options.textalign || styles['text-align'] || undefined,
			'textcolor': this.options.textcolor || styles['color'] || undefined,
			'linkcolor': this.options.linkcolor || this.options.textcolor || styles['color'] || undefined,
			'hovercolor': this.options.hovercolor || this.options.linkcolor || this.options.textcolor || styles['color'] || undefined
		}

		var anchors = [];
		el.getElements('a').each(function(a, i) {
			flashvars['sifr_url_' + i] = a.getProperty('href');
			flashvars['sifr_url_' + i + '_target'] = a.getProperty('target');
			var newA = new Element('a', {
				'href': 'asfunction:_root.launchURL,' + i,
				'html': a.get('html')
			}).replaces(a);
		});

		var transform = this.options.transform || styles['text-transform'];
		switch (transform) {
			case 'upper':
			case 'uppercase':
			this.toUpperCase(el);
			break;

			case 'lower':
			case 'lowercase':
			this.toLowerCase(el);
			break;
		}
		flashvars.txt = el.get('html');

		new Swiff(this.options.path+this.options.font+'.swf', {
			'container': el,
			'height': height,
			'width': width,
			'properties': {'style': 'outline: none;', 'class': 'sIFR'},
			'params': {'wmode': 'transparent'},
			'vars': flashvars
		});

		el.addClass('sIFR-replaced').grab(preSIFR);
	},
	toUpperCase: function(node) {
		for (var i = 0, l = node.childNodes.length; i < l; i++) {
			var n = node.childNodes[i];
			if (n.nodeType == 3) {
				n.nodeValue = n.nodeValue.toUpperCase();
			} else if (n.nodeType == 1) {
				this.toUpperCase(n);
			}
		}
	},
	toLowerCase: function(node) {
		for (var i = 0, l = node.childNodes.length; i < l; i++) {
			var n = node.childNodes[i];
			if (n.nodeType == 3) {
				n.nodeValue = n.nodeValue.toLowerCase();
			} else if (n.nodeType == 1) {
				this.toLowerCase(n);
			}
		}
	}
});

