/**************************************************************

	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		: uTRD - Universal Text Resize Detector
	Version		: 1.0
	Authors		: Samuel birch
	Desc		: Captures when the text is resized.
	Licence		: Open Source MIT Licence

**************************************************************/

var uTRD = new Class({
	
	Implements: [Options,Events],
	
	getOptions: function(){
		return {
			delay: 200,
			base: 16
		};
	},
	
	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		this.functions = [];
		this.element = new Element('div', {
			'id': 'uTRD_Controller_'+$time(),
			'styles': {
				'position': 'absolute',
				'left': '-1000em',
				'top': 0,
				'width': '1em',
				'height': '20em'
			}
		}).inject($(document.body));
		
		this.oldSize = this.element.getCoordinates().height;
		this.defaultSize = this.oldSize;
		this.checking = false;
	},
	
	start: function(){
		this.checking = true;
		this.timer = this.check.periodical(this.options.delay, this);
	},
	
	stop: function(){
		$clear(this.timer);
	},
	
	check: function(){
		var newSize = this.element.getCoordinates().height;
		if(this.oldSize != newSize){
			var size = 20*this.options.base;
			var percent = ((newSize / size) * 100).toInt();

			this.functions.each(function(el,i){
				el.attempt([percent]);
			}, this);
			this.oldSize = newSize;
		}
	},
	
	defaultCheck: function(){
		if((this.defaultSize/this.options.base).toInt() != 20){
			this.defaultSize = 20*this.options.base;
			var percent = ((this.oldSize / this.defaultSize) * 100).toInt();
			this.functions.each(function(el,i){
				el.attempt([percent, true]);
			}, this);
		}
	},
	
	add: function(func){
		this.functions.push(func);
		this.defaultCheck();
		if(!this.checking){
			this.start();
		}
	}
	
});

/*************************************************************/




/*
Script: Assets.js
	Provides methods to dynamically load JavaScript, CSS, and Image files into the document.

License:
	MIT-style license.
*/

var Asset = new Hash({

	javascript: function(source, properties){
		properties = $extend({
			onload: $empty,
			document: document,
			check: $lambda(true)
		}, properties);
		
		var script = new Element('script', {'src': source, 'type': 'text/javascript'});
		
		var load = properties.onload.bind(script), check = properties.check, doc = properties.document;
		delete properties.onload; delete properties.check; delete properties.document;
		
		script.addEvents({
			load: load,
			readystatechange: function(){
				if (['loaded', 'complete'].contains(this.readyState)) load();
			}
		}).setProperties(properties);
		
		
		if (Browser.Engine.webkit419) var checker = (function(){
			if (!$try(check)) return;
			$clear(checker);
			load();
		}).periodical(50);
		
		return script.inject(doc.head);
	},

	css: function(source, properties){
		return new Element('link', $merge({
			'rel': 'stylesheet', 'media': 'screen', 'type': 'text/css', 'href': source
		}, properties)).inject(document.head);
	},

	image: function(source, properties){
		properties = $merge({
			'onload': $empty,
			'onabort': $empty,
			'onerror': $empty
		}, properties);
		var image = new Image();
		var element = $(image) || new Element('img');
		['load', 'abort', 'error'].each(function(name){
			var type = 'on' + name;
			var event = properties[type];
			delete properties[type];
			image[type] = function(){
				if (!image) return;
				if (!element.parentNode){
					element.width = image.width;
					element.height = image.height;
				}
				image = image.onload = image.onabort = image.onerror = null;
				event.delay(1, element, element);
				element.fireEvent(name, element, 1);
			};
		});
		image.src = element.src = source;
		if (image && image.complete) image.onload.delay(1);
		return element.setProperties(properties);
	},

	images: function(sources, options){
		options = $merge({
			onComplete: $empty,
			onProgress: $empty
		}, options);
		if (!sources.push) sources = [sources];
		var images = [];
		var counter = 0;
		sources.each(function(source){
			var img = new Asset.image(source, {
				'onload': function(){
					options.onProgress.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				}
			});
			images.push(img);
		});
		return new Elements(images);
	}

});



/**************************************************************

	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);