// MooTools Plus, <http://www.tobymiller.com/plus>. Copyright (c) 2008 Toby Miller, MIT Style License.

/*
Script: Debug.js
	Output debug messages.

Usage:
	Debug.log('your message');
		or
	Debug.enable('http://www.yourdomain.com/debug.php'); // call once
	Debug.log('your message');

	Note: If you opt to use your own remote logger it should accept a query
	like this: http://www.yourdomain.com/debug.php?key=yourdomain.com&msg=hello

License:
	MIT-style license
*/
var Debug = {
	enable: function(){
		if (!this.enabled){
			this.enabled = true;
			this.ready = false;
			this.remote = (arguments.length > 0) ? arguments[0] : null;
			this.idx = 0;
			this.msgs = ['Debug:enable()'];
			this.outs = [];
		} else {
			this.msgs[this.msgs.length] = 'Debug:enable()';
			this.remote = (arguments.length > 0) ? arguments[0] : null;
			this.process();
		}
	},

	disable: function(){
		this.msgs[this.msgs.length] = 'Debug:disable()';
		if (this.enabled){
			this.enabled = false;
			this.remote = null;
		}
	},

	log: function(msg){
		if (typeof this.enabled == 'undefined') this.enable();
		if (window.console) window.console.log(msg);
		else {
			this.msgs[this.msgs.length] = msg;
			this.process();
		}
	},

	process: function(){
		if (this.enabled){
			if (this.msgs[this.idx]){
				this.ready = true;
				if (window.console){
					window.console.log(this.msgs[this.idx]);
					this.idx++;
					this.process();
				} else if (this.remote != null){
					var ts = new Date().getTime();
					var h = new Hash({'key': window.location.hostname, 'msg': this.msgs[this.idx]});
					var q = this.remote+'?ts='+ts+'&'+h.toQueryString();
					this.outs.push(new Asset.javascript(q));
					this.idx++;
					this.process();
				}
			}
		}
	}
};

/*
Script: RoundedCorners.js
	Find all elements with -moz-border-radius settings and background colors
	and translate those settings into Canvas equivalents for Internet Explorer
	users.

License:
	MIT-style license
*/
var RoundedCorners = {
	translate: function(){
		if (Browser.Engine.trident){
			var selector = (arguments.length > 0) ? arguments[0] : ['address','a','blockquote','caption','cite','code','dd','dfn','dir','div','dl','dt','form','h1','h2','h3','h4','h5','h6','kbd','li','menu','ol','pre','p','samp','table','ul','var'];
			$$(selector).each(function(el){
				var radius = (el.currentStyle) ? el.currentStyle['-moz-border-radius'] : null;
				if (radius == null) radius = el.getStyle('-moz-border-radius-topleft');
				radius = (radius != null) ? radius.toInt() : 0;
				if (radius > 0){
					var bgcolor = el.getStyle('background-color');
					if (bgcolor != 'transparent' && bgcolor != null){
						if (el.get('id') == null) el.set('id', 'auto_' + $random(999, 999999999));
						var coord = el.getCoordinates();
						var container = new Element('div', {
							'styles': $merge(
								{
									'padding': '0px',
									'width': coord.width,
									'height': coord.height
								}, (el.getStyle('position') == 'absolute') ? {
									'position': 'absolute',
									'top': el.getStyle('top'),
									'left': el.getStyle('left')
								} : {
									'position': 'relative',
									'float': el.getStyle('float'),
									'overflow': 'hidden',
									'margin': el.getStyle('margin')
								}
							)
						}).inject(el, 'before');
						var canvas = new Element("canvas", {
							'id': el.get('id') + '_canvas',
							'width': coord.width,
							'height': coord.height,
							'styles': {
								'display': 'inline-block',
								'overflow': 'hidden',
								'position': 'absolute',
								'top': '0px',
								'left': '0px',
								'width': coord.width + 'px',
								'height': coord.height + 'px'
							}
						}).inject(container);
						var ctx = (Browser.Engine.trident) ? G_vmlCanvasManager.initElement(canvas).getContext('2d') : canvas.getContext('2d');
						ctx.fillStyle = bgcolor;
						ctx.moveTo(radius, 0);
						ctx.quadraticCurveTo(0, 0, 0, radius);
						ctx.lineTo(0, coord.height - radius);
						ctx.quadraticCurveTo(0, coord.height, radius, coord.height);
						ctx.lineTo(coord.width - radius, coord.height);
						ctx.quadraticCurveTo(coord.width, coord.height, coord.width, coord.height - radius);
						ctx.lineTo(coord.width, radius);
						ctx.quadraticCurveTo(coord.width, 0, coord.width - radius, 0);
						ctx.lineTo(radius, 0);
						ctx.closePath();
						ctx.fill();
						if (el.getStyle('border-color') != null){
							ctx.strokeStyle = el.getStyle('border-color').split(' ')[0];
							ctx.moveTo(radius, 0);
							ctx.quadraticCurveTo(0, 0, 0, radius);
							ctx.lineTo(0, coord.height - radius);
							ctx.quadraticCurveTo(0, coord.height, radius, coord.height);
							ctx.lineTo(coord.width - radius, coord.height);
							ctx.quadraticCurveTo(coord.width, coord.height, coord.width, coord.height - radius);
							ctx.lineTo(coord.width, radius);
							ctx.quadraticCurveTo(coord.width, 0, coord.width - radius, 0);
							ctx.lineTo(radius, 0);
							ctx.closePath();
							ctx.stroke();
						}
						el.bgcolor = bgcolor;
						el.setStyles({
							'background-color': 'transparent',
							'margin': '0px',
							'border': 'none'
						});
						el.inject(container);
					}
				}
			});
		}
	},
	reTranslate: function(el){
		return;
		if (Browser.Engine.trident){
			var radius = (el.currentStyle) ? el.currentStyle['-moz-border-radius'] : null;
			if (radius == null) radius = el.getStyle('-moz-border-radius-topleft');
			radius = (radius != null) ? radius.toInt() : 0;
			if (radius > 0){
				var bgcolor = el.bgcolor;
				if (bgcolor != 'transparent' && bgcolor != null){
					if (el.get('id') == null) el.set('id', 'auto_' + $random(999, 999999999));
					var coord = el.getCoordinates();
					var container = new Element('div', {
						'styles': $merge(
							{
								'padding': '0px',
								'width': coord.width,
								'height': coord.height
							}, (el.getStyle('position') == 'absolute') ? {
								'position': 'absolute',
								'top': el.getStyle('top'),
								'left': el.getStyle('left')
							} : {
								'position': 'relative',
								'float': el.getStyle('float'),
								'overflow': 'hidden',
								'margin': el.getStyle('margin')
							}
						)
					}).inject(el, 'before');
					var canvas = new Element("canvas", {
						'id': el.get('id') + '_canvas',
						'width': coord.width,
						'height': coord.height,
						'styles': {
							'display': 'inline-block',
							'overflow': 'hidden',
							'position': 'absolute',
							'top': '0px',
							'left': '0px',
							'width': coord.width + 'px',
							'height': coord.height + 'px'
						}
					}).inject(container);
					var ctx = (Browser.Engine.trident) ? G_vmlCanvasManager.initElement(canvas).getContext('2d') : canvas.getContext('2d');
					ctx.fillStyle = bgcolor;
					ctx.moveTo(radius, 0);
					ctx.quadraticCurveTo(0, 0, 0, radius);
					ctx.lineTo(0, coord.height - radius);
					ctx.quadraticCurveTo(0, coord.height, radius, coord.height);
					ctx.lineTo(coord.width - radius, coord.height);
					ctx.quadraticCurveTo(coord.width, coord.height, coord.width, coord.height - radius);
					ctx.lineTo(coord.width, radius);
					ctx.quadraticCurveTo(coord.width, 0, coord.width - radius, 0);
					ctx.lineTo(radius, 0);
					ctx.closePath();
					ctx.fill();
					if (el.getStyle('border-color') != null){
						ctx.strokeStyle = el.getStyle('border-color').split(' ')[0];
						ctx.moveTo(radius, 0);
						ctx.quadraticCurveTo(0, 0, 0, radius);
						ctx.lineTo(0, coord.height - radius);
						ctx.quadraticCurveTo(0, coord.height, radius, coord.height);
						ctx.lineTo(coord.width - radius, coord.height);
						ctx.quadraticCurveTo(coord.width, coord.height, coord.width, coord.height - radius);
						ctx.lineTo(coord.width, radius);
						ctx.quadraticCurveTo(coord.width, 0, coord.width - radius, 0);
						ctx.lineTo(radius, 0);
						ctx.closePath();
						ctx.stroke();
					}
					el.setStyles({
						'background-color': 'transparent',
						'margin': '0px',
						'border': 'none'
					});
					el.inject(container);
				}
			}
		}
	}
};
//window.addEvent('load', function(){ RoundedCorners.translate() });

/*
Script: EnhancedElement.js
	Define additional class names, styles, events, attributes, and properties to
	apply to html elements whenever JavaScript is enabled. These definitions
	are written into a JavaScript object inside an html comment block within the
	particular html element(s) being enhanced.

	Note: Since these instructions are ignored by search engines and screen
	readers they should never contain content or anything else that would
	prevent the current page from being contextually understandable.

	Usage:
	<tag>
		tag content
		<!--{
			'styles': {
				'color': '#f00',
				'background-color': '#000'
			},
			'class': 'harry',
			'className': 'monkey',
			'classes': ['harry', 'monkey', 'fever'],
			'attributes': {
				'checked': 'checked',
				'temperature': '99.7'
			},
			'property1': 'hello',
			'property2': ['hello', 'world'],
			'property3': {
				'key1': 'hello',
				'key2': 'there',
				'key3': 'world'
			},
			'events': {
				'mouseover': function(){ this.set('src', 'on.gif') }
				'mouseover': function(){ this.set('src', 'off.gif') }
			}
		}-->
	</tag>

License:
	MIT-style license
*/
var EnhancedElement = {
	enhance: function(){
		var selector = (arguments.length > 0) ? arguments[0] : ['address','a','big','blockquote','body','b','caption','center','cite','code','dd','dfn','dir','div','dl','dt','em','form','h1','h2','h3','h4','h5','h6','i','kbd','li','menu','ol','option','param','pre','p','samp','select','small','span','strike','strong','sub','sup','table','td','th','title','tr','tt','ul','u','var'];
		$$(selector).each(function(el){
			var html = el.innerHTML.replace(/[\n\r]*/g, '');
			var enhancer = [];
			html = html.replace(/<!--\s*\{/g, '\xD2');
			html = html.replace(/\}\s*-->/g, '\xD8');
			var match = html.match(/\xD2([^\xD8]+)\xD8/);
			if (match){
				for (var i = 0; i < match.length; i++){
					enhancer[enhancer.length] = match[i];
					html = html.replace(/\xD2(.*)\xD8/, '\xD2' + i + '\xD8');
				}
				html = html.replace(/<img[^>]+>/g, '');
				while(html.match(/<[^>]+>[^<]+<\/[^>]+>/)) html = html.replace(/<[^>]+>[^<]+<\/[^>]+>/, '');
				while(html.match(/>[^<]+</)) html = html.replace(/>[^<]+</, '><');
				var truematch = html.match(/\xD2([0-9]+)\xD8/);
				if (truematch) {
					var enhanced = eval('({' + enhancer[truematch[1]] + '})');
					for (var name in enhanced){
						switch(name){
							case 'class':
							case 'className':
								el.addClass(enhanced[name]);
								break;
							case 'classes':
								for (var i = 0; i < enhanced[name].length; i++) el.addClass(enhanced[name][i]);
							case 'styles':
								for (var n in enhanced[name]) el.setStyle(n, enhanced[name][n]);
								break;
							case 'events':
								for (var n in enhanced[name]){
									if ((n == 'domready') || (n == 'load')) window.addEvent(n, (enhanced[name][n]).bind(el));
									else el.addEvent(n, (enhanced[name][n]).bind(el));
								}
								break;
							case 'attributes':
								for (var n in enhanced[name]) el.setAttribute(n, enhanced[name][n]);
								break;
							default:
								if (name.length > 0) el.setProperty(name, enhanced[name]);
								break;
						}
					}
				}
			}
		});
	}
};
window.addEvent('domready', function(){
	EnhancedElement.enhance();
});

/**
 * show/hide appopriate flash elements
 */
var flashMinimum = 9;
if (Browser.Plugins.Flash.version > 0){
	if (Browser.Plugins.Flash.version >= flashMinimum) new Asset.css('/ri/css/flash.css');
	else new Asset.css('/ri/css/upgradeflash.css');
} else new Asset.css('/ri/css/noflash.css');


