// COPYRIGHT 2008 - CitrusMediaGroup - All Rights Reserved

var Mootils = {
  REQUIRED_MOOTOOLS: '1.2.0',
  version: '0.9.0',
  release: '9/13/08',
  host: 'http://jasonscottmckinney.com/',
  load: function(inc) {
    document.write('<script type="text/javascript" src="' + inc + '"><\/script>');
  },
  init: function() {
    if (typeof(MooTools) === 'undefined' || MooTools.version !== Mootils.REQUIRED_MOOTOOLS) {
      throw 'Mootils requires MooTools v' + Mootils.REQUIRED_MOOTOOLS;
      return false;
    }
    $A(document.getElementsByTagName('script')).filter(function(s) {
      return (s.src && s.src.match(/mootils\.js(\?.*)?$/))
    }).each( function(s) {
      var path = s.src.replace(/mootils\.js(\?.*)?$/, 'libs/');
      var includes = s.src.match(/\?.*load=([a-z,]*)/);
      if (includes) {
        includes[1].split(',').each(function(inc) {
          Mootils.load(path + inc + '.js');
        });
      }
    });
  },
  random: function(i) {
    return Math.floor(i * (Math.random() % 1));
  },
  randomRange: function(i, j) {
    return i + Mootils.random(j - i + 1);
  },
  stripAnchor: function(url) {
	  return url.replace(/\#[0-9,a-z]*/, '');  
  },
  getAnchor: function(url) {
    var a = String(url.match(/#\/.*$/));
    if (a) a = a.replace(/#/, '');
    else a = '/';
    return a;
  },
  preload: function() {
	  var d = document;
	  if(d.images) { 
	  	if(!d.pre) d.pre = new Array();
	   	var i, j = d.pre.length, a = prereloadImages.arguments;
	   	for (i = 0; i < a.length; i++) {
			  if ( a[i].indexOf("#") != 0 ) {
			    d.pre[j] = new Image;
			    d.pre[j++].src = a[i];
			  }
			}
		}
	}
}


Mootils.encode = function(str) {
  return Mootils.Encoder.encode(str);
}
Mootils.decode = function(str) {
  return Mootils.Encoder.decode(str);
}
Mootils.Encoder = {
  getReplacement: function(match) {
    Debug.trace('match', match);
    switch (match) {
      case '&':
        return '&amp;';
      case '<':
        return '&lt;';
      case '>':
        return '&gt;';
      case '"':
        return '&quot;';
      case '&amp;':
        return '&';
      case '&lt;':
        return '<';
      case '&gt;':
        return '>';
      case '&quot;':
        return '"';
    }
  },
  encode: function(str) {
    return escape(str.replace(/[<>"&]/g, function(m) { return Mootils.Encoder.getReplacement(m) }));
  },
  decode: function(str) {
    return unescape(str.replace(/[&][(amp|lt|gt|quot)]+;/g, function(m) { return Mootils.Encoder.getReplacement(m) }));
  }
}


Mootils.Console = new Class({
  Implements: Options,
	options: {
  	width: 420,
    height: 600
  },
  initialize: function(options){
    this.setOptions(options);
    if (!$('debugConsole')) this.div = this.create();
    else this.div = $('debugConsole');
    this.div.addEvent('click', function(evt) {
      if (evt.shift) this.clear();
    }.bind(this));
  },
  create: function () {
    return new Element('div', {
      'id': 'debugConsole',
      'styles': {
        'display': 'block',
        'position': 'fixed',
        'right': 0,
        'top': 0,
        'width': this.options.width,
        'height': this.options.height,
        'margin': '4px',
        'padding': '4px',
        'overflow': 'auto',
        'border': '1px solid #999',
        'background-color': '#fff',
        'font': '13px Courier',
        'color': '#444',
        'z-index': 7500
      }
    }).inject(document.body);
  },
  trace: function () {
  	if (!arguments || arguments.length == 0) return false;
  	else if ($type(arguments[0]) == 'arguments') arguments = arguments[0];
  	var str = '';
  	for (var i = 0, l = arguments.length; i < l; i++){
      str += (arguments[i] + ' ');
    }
    this.div.appendText(str);
   	new Element('br').inject(this.div);
    return str;
  },
  clear: function () {
    this.div.innerHTML = '';
  }
});

var Debug = {
	console: '...',
	trace: function() {
		if (this.console === '...') this.console = new Mootils.Console();
		this.console.trace(arguments);
	},
	clear: function() {
    if (this.console) this.console.clear();	
	}
};






Mootils.init();