/*************************************************************
	Class:		elReady
	Version:	1.0
	Author:		Samuel Birch
*************************************************************/
var elReady = new Class({
	
	initialize: function(el, func){
		if($$(el)[0]){
			func();
		}else{
			this.timer = this.check.periodical(10, this, [el, func]);
		}
		
		window.addEvent('domready', function(){
			$clear(this.timer);
		}.bind(this))
	},
	
	check: function(el, func){
		if($$(el)[0]){
			$clear(this.timer);
			func();
		}
	}
	
});
/************************************************************/

/*************************************************************
	Class:		RC
	Version:	1.0
	Author:		Samuel Birch
*************************************************************/
var rc = new Class({
	
	Implements: [Options],
	
	getOptions: function(){
		return {
			
		};
	},
	
	initialize: function(els, options){
		this.setOptions(options);
		
		this.boxes = $$(els);
		
		this.boxes.each(function(el,i){
			
			el.addClass('rc');
			
			var tl = new Element('div', {'class': 'tl'});
			var tr = new Element('div', {'class': 'tr'});
			var c = new Element('div', {'class': 'c'});
			var bl = new Element('div', {'class': 'bl'});
			var br = new Element('div', {'class': 'br'});
			
			var children = el.getChildren();
			c.inject(el, 'top');
			children.inject(c);
			
			tl.inject(c, 'before');
			tr.inject(tl, 'after');
			bl.inject(c, 'after');
			br.inject(bl, 'after');
			
			
			
		}, this);
	}
	
});
/************************************************************/

/**************************************************************

	Script		: Swiff
	Version		: 1.0
	Authors		: Samuel Birch
	Desc		: Adds version checking to the Swiff class

**************************************************************/

Swiff = new Class({

	Extends: Swiff,

	options: {
		version: 8
	},

	initialize: function(path, options){
		this.setOptions(options);
		if (Browser.Plugins.Flash.version >= this.options.version) {
			$(this.options.container).getChildren().destroy();
			this.parent(path, options);
		}else{
			return false;
		}
	}

});

Swiff.CallBacks = {};

Swiff.remote = function(obj, fn){
	var rs = obj.CallFunction('<invoke name="' + fn + '" returntype="javascript">' + __flash__argumentsToXML(arguments, 2) + '</invoke>');
	return eval(rs);
};

/*************************************************************/



/**************************************************************

	Script		: Text Resize Detector
	Version		: 1.2
	Authors		: Samuel birch
	Desc		: Captures when the text is resized.
	Licence		: Open Source MIT Licence

**************************************************************/

var textResizeDetector = new Class({
	getOptions: function(){
		return {
			delay: 200
		};
	},
	
	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		this.funcs = [];
		this.num = 0
		
		this.element = new Element('div').set({
			'id': 'textResizeController_'+$time(),
			'styles': {
				'position': 'absolute',
				'left': '-9999px',
				'top': 0,
				'width': '1em',
				'height': '20em'
			}
		}).inject(document.body);
		this.oldSize = this.element.getCoordinates().height;
		this.defaultSize = this.oldSize;
		//console.log(this.defaultSize/16); //20
		
		this.start();
	},
	
	add: function(func){
		this.funcs.push(func);
	},
	
	defaultCheck: function(){
		if(this.defaultSize/16 != 20){
			this.defaultSize = 20*16;
			//this.options._onChange(this.defaultSize, this.oldSize);
			this.funcs.each(function(el,i){
				el.attempt(this.defaultSize, this.oldSize);
			}, this);
		}
	},
	
	check: function(){
		var newSize = this.element.getCoordinates().height;
		if(this.oldSize != newSize){
			//this.options._onChange(this.defaultSize, newSize, this.oldSize);
			this.funcs.each(function(el,i){
				el.attempt(this.defaultSize, newSize, this.oldSize);
			}, this);
			this.oldSize = newSize;
		}
	},
	
	start: function(){
		this.timer = this.check.periodical(this.options.delay, this);
	},
	
	stop: function(){
		$clear(this.timer);
	}
});
textResizeDetector.implement(new Events);
textResizeDetector.implement(new Options);


/*************************************************************/




/*************************************************************
	Class:		flash text
	Version:	1.0
	Author:		Samuel Birch
*************************************************************/
var flashText = new Class({
	
	Implements: [Options,Events],
	
	getOptions: function(){
		return {
			font: '/_common/swf/custom.swf',
			version: 8,
			resizer: null
		};
	},
	
	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		this.elements = $$(elements);
		this.properties = [];
		
		if(Browser.Plugins.Flash.version >= this.options.version){
			
			$(document.body).addClass('flashText');
			
			this.elements.each(function(el, i){
				
				var size = el.getStyle('font-size').toInt();
				if(this.options.size){
					size = this.options.size;
				}
				
				var props = {
					'colour': el.getStyle('color').replace('#',''),
					'size': size,
					'text': el.get('html').clean(),
					'height': el.getStyle('height').toInt()
				};
				this.properties.push(props);
				
				this.addFlash(el, props);
				
				new Element('span', {'html':props.text, 'class':'flashText'}).inject(el);
				
			}, this);
			
			this.options.resizer.add(/*this.update.bind(this)*/);
		}
		
	},
	
	addFlash: function(el, props){
		var obj = new Swiff(this.options.font, {
			container: el,
			version: this.options.version,
			width: '100%',
			//height: '100%',//size.height+10,
			height: props.height+5,
			params: {
				wMode: 'transparent'
			},
			vars: {
				text: props.text,
				color: props.colour,
				size: props.size
			},
			properties: {
				style: 'outline:none;'
			}
		});
	},
	
	update: function(defaultSize, newSize, oldSize){
		console.log(newSize);
		var percent = ((newSize / oldSize) * 100).toInt();
		if(oldSize){
			this.elements.each(function(el, i){
				el.getChildren().destroy();
				var props = this.properties[i];
					props.size = ((percent/100)*props.size);
				this.addFlash(el, props);
			}, this);
		}
	}
});
/************************************************************/
