/*!
 * Armory JavaScript Library v1.1
 * http://www.marshallad.com/
 *
 * Copyright 2011, Tyler Reid, Marshall Advertising & Design
 * http://www.marshallad.com/
 * 
 *
 * Date: Mon May 6 16:55:00 2011
 * Modifed: Mon Oct 12 15:45:00 2011 
 */
 
(function(window) {

var document = window.document,
    navigator = window.navigator,
    location = window.location;

var Armory = (function(){

  var Armory = {

    name: 'Armory',
    version: '1.1',
    _readylist: {},

    ready: function (f, ref){
      if(!ref) ref = null;
      this._readylist.push([f, ref]);
    }
  
  };

  Armory.fn = Armory.prototype = {
    constructor: Armory
  };
  
  return Armory;

})();


//-- ARMORY Events Object --//


Armory.events = (function(){

  var events = {

    _list: [],
    
    armEvent: function (o, eventName){
    
      if(o[eventName]) return;

      o[eventName] = (function(o, eventName){

        return function(o, eventName){ Armory.events.fireEvent(o, eventName); };

      })(o, eventName);
          
    }, 
    
    addListener: function(o, eventName, func, ref){
       
        if(!ref) ref = null;
      
        if( isDOMEvent(eventName) && (o.attachEvent || o.addEventListener) ){
  
      		if(window.attachEvent) {
      			o.attachEvent('on' + eventName, func);
      		}else {
      			o.addEventListener(eventName, func, false);
      		}
    
        }else{
   
	    this._list.push( {object: o, eventName: eventName, func: func, ref: ref } );

        }
        
    },
    
    removeListener: function(o, eventName, func){
    
      if( isDOMEvent(eventName) && (o.detachEvent || o.removeEventListener) ){
    
    		if(window.detachEvent) {
    			o.detachEvent('on' + eventName, func);
    		}else{
    			o.removeEventListener(eventName, func, false);
    		}
    		   
      }else{
      
          var l;

          for(var i=0; i<this._list.length; i++){

        		l = this._list[i];
        		if(l != undefined){
        		  if(l.object == o && l.eventName == eventName && l.func == func) {
        		    this._list[i] = null;
                delete this._list[i];
              }
        		}

          }
      
      }
      
    },

    removeAllListeners: function(o){

      var l;

      for(var i=0; i<this._list.length; i++){

        l = this._list[i];
        if(l[object] == o) delete this._list[i];

      }

    },
    
    fireEvent: function(o, eventName, event){
    
      if(!event) event = { target: o };

      var l, ref;	

      for(var i=0; i<this._list.length; i++){

        l = this._list[i];
        
      	if(l != undefined){
      	
          if(l.object == o && l.eventName == eventName){      
                ref = l.ref ? l.ref : o;
                l.func.call(ref, event);
          }
          
      	}

      }

    }
    
  };
  
  function isDOMEvent(eventName){
  
    var domEvents = "load unload change blur focus error resize click doubleclick mousedown mouseup mousemove mouseover mouseout keydown keyup keypress";
    
    if(domEvents.indexOf(eventName) > -1) return true;
    return false;
         
  }
  
  return events;
  
})();


//-- ARMORY Ready Object (Overwrite) --//


Armory.ready = (function(){

  var hasReady = false;

  var ready = function(f, ref){
  
    if(!ref) ref = null;
    Armory.events.addListener(Armory, Armory.events.Event.READY, f, ref);
      
  }
  
  // Arm the READY event for the Armory Object

  Armory.events.armEvent(Armory, 'ready');
  //if(document.addEventListener) document.addEventListener('DOMContentLoaded', function(){ if(!hasReady) { hasReady = true; Armory.events.fireEvent(Armory, 'ready'); } }, false);
  Armory.events.addListener(window, 'load', function(){ if(!hasReady) { hasReady = true; Armory.events.fireEvent(Armory, 'ready'); } });
  
  // Parse the readylist and add them to the Armory READY event
  
  if(Armory._readylist.length > 0){
    for(var i=0; i<Armory._readylist.length; i++){    
      Armory.events.addListener(Armory, 'ready', Armory._readylist[i][0], Armory._readylist[i][1]);   
    }
    
    Armory._readlist = array();
    
  }
  
  return ready;

})();


//-- ARMORY Select Function --//

  
Armory.select = function (o, single) {

  if(single !== true && single !== false) single = true;

  if(o.constructor == null || o.constructor == undefined) return [o];
	if(!o.constructor || o.constructor.toString() == '') return [o];

	var c = o.constructor.toString();
	var a = [];

	if(c.indexOf('function') < 0) return [o];
	if(c.indexOf('HTML') > 0) return [o];
	if(c.indexOf('Array') > 0) return o;
	if(c.indexOf('String') > 0) {

  	if(o.indexOf('#') == 0) {

  		a = [document.getElementById(o.substr(1))];
  
  	}
  	
	if(o.indexOf('.') == 0) {

		var cstr = o.substr(1);
		var tags = document.getElementsByTagName("*");

		for (var i in tags) {

			var t = tags[i];

			if (t.nodeType == 1) {

				var tc = t.className;
				var p0 = tc == cstr;
				var p1 = false;
				var p2 = false;
				var p3 = false;

				if(!p0) p1 = tc.indexOf(cstr + ' ') == 0;

				if(!p1 && !p0){

					p2 = tc.indexOf(' ' + cstr + ' ') > -1;
					p3 = tc.indexOf(' ' + cstr) == tc.length - cstr.length - 1;

				}

				if (p0 || p1 || p2 || p3) a.push(t);

				}

			}
			
		}

		if(!a.length) return false;
		if(single && a.length == 1) return a[0];
		return a;
		
	}

};


//-- ARMORY Animation Object --//

  
Armory.anim = (function(){

  var anim = {

  	_tweens: {},
  	_fps: 24,
  
  	tween: function (obj, t, params, delay, overwrite) {
  
  		if(!delay) delay = 0;
  		if(overwrite !== false) overwrite = true;
  
  		if(overwrite) this.overwriteTween(obj);

  		var _t = new Armory.anim.Tween(obj);
  
  		_t._objrequest = obj;
  		_t._obj = Armory.select(obj, false);
  		_t._fps = this._fps;
  		if(params.easing) _t._easing = params.easing;
  		_t._frames = _t._easing.call(_t, t);
  
  		for (var i in params) {
  
  			if(i.indexOf('on') == 0 && i != 'easing' && i != 'autoPlay') Armory.events.addListener(_t, i, params[i], _t);
  			if(i.indexOf('on') != 0 && i != 'easing' && i != 'autoPlay') {
  
  				_t._style.push(i);
  				_t._goal.push(v = parseFloat(params[i]));
  				_t._unit.push(params[i].toString().replace(v.toString(), ''));
  			}
  
  		}
  
  		for (var i=0; i<_t._obj.length; i++) {
  
  			_t._init.push(this.getObjInit(_t._obj[i], _t._style));
  
  		}
  
  		this._tweens[_t._instanceID] = _t;
  		if(params.autoPlay !== false) setTimeout(function(){ _t.play(); }, delay * 1000);
  
  		return _t;
  
  	},
  
  	removeTween: function (t) {

		if (this._tweens[t]){

			Armory.events.removeAllListeners(this._tweens[t]);
  			delete this._tweens[t];
  
  		}

  
  	},
  
  	overwriteTween: function (objreq) {
  
  		for (var i in this._tweens) {

  			if(this._tweens[i]._objrequest == objreq) this._tweens[i].kill();
  
  		}
  
  	},
  
  
  //-- Tween Object definition
  //--------------------------
  
  
  	Tween: function (obj) {

		this._instanceID = 'Tween' + Math.floor(10000 * Math.random());
  
  		this._int = null;
  		this._fps = null;         // internal reference to the object's timer function
  		this._objrequest = obj;   // the original 'obj' request made to the tween() function
  		this._obj = Armory.select(obj, false);           // object(s) being acted on
  		this._frames = [];        // tween frames
  		this._currentframe = 0;   // current tween frame
  		this._style = [];         // the style(s) to animate
  		this._init = [];          // the object(s) initial conditions
  		this._goal = [];          // the goal conditions
  		this._unit = [];          // units of measure on each style
  		this._easing = Armory.anim.easing.None.easeNone;	// reference to the easing function to use for animation
  		this._increment = 1;      // direction in which to play the tween (1: forward, -1: reverse)
  		this._playing = false;    // tween is currently playing
  
  		this.progress = 0;
  
  		this.play = function () {
  
  			var tween = this;
  
  			if(this._int) clearInterval(this._int);
  			this._int = setInterval(function(){ tween.frame(); }, Math.round(1000 / this._fps));
  			this._playing = true;
  		}
  
  		this.pause = function () {
  			clearInterval(this._int);
  			this._playing = false;
  		}
  
  		this.stop = function () {
  			this.pause();
  			this._currentframe = 0;
  			this.frame();
  		}
  
  		this.kill = function () {
  			this.pause();
  			Armory.anim.removeTween(this);
  		}
  
  		this.frame = function () {
  
 			for(var i=0; i<this._obj.length; i++){
  
  				var o = this._obj[i];
  				var n = this._init[i];
  
  				for(var j=0; j<this._style.length; j++){
  
  					var s = this._style[j];
  					var v;
  
  					if(this._currentframe == 0) {
  
  						v = n[j];
  
  					}else if(this._currentframe == this._frames.length - 1) {
  
  						v = this._goal[j];
  
  					}else{
  
  						v = n[j] + (this._goal[j] - n[j]) * this._frames[this._currentframe];
  
  					}
  
  					v += this._unit[j];
  
  					if(s == 'alpha'){
  
  						if('opacity' in o.style){
  							s = 'opacity';
  						} else {
  							s = 'filter';
  							v = 'alpha(opacity=' + (v * 100) + ')';
  						}
  
  					}

  
  					o.style[s] = v;
  
  				}
  
  			}
  
  			if(this._currentframe == 0 && this._increment < 0) {
  
  				this.pause();
  
  			}
  
  			this.progress = this._frames[this._currentframe];
  
  			if(this._currentframe == 0 && this._increment > 0 && this._playing) {
  
  				Armory.events.fireEvent(this, Armory.events.TweenEvent.START, this.tweenEvent());
  
  			}

  			Armory.events.fireEvent(this, Armory.events.TweenEvent.PROGRESS, this.tweenEvent());
  
  			if(this._currentframe == this._frames.length - 1 && this._increment > 0 && this._playing) {

  				this.pause();
 				Armory.events.fireEvent(this, Armory.events.TweenEvent.COMPLETE, this.tweenEvent());
  
  			}
    
  			if(this._playing) this._currentframe = this._currentframe + this._increment;

  
  		}
  
  		this.seek = function (t) {
  
  			this._currentframe = Math.floor(t * this._fps);
  			this.frame();
  
  		}
  
  		this.reverse = function () {
  
  			this._increment = this._increment > 0 ? -1 : 1;
  			this.play();
  
  		}
  		
  		this.tweenEvent = function(){

        		return (function(t){

					return {
          					target: t,
          					object: (t._obj.length > 1) ? t._obj : t._obj[0],
          					progress: t.progress
					};

				})(this);   
      
      		}

  		Armory.events.armEvent(this, Armory.events.TweenEvent.START);
  		Armory.events.armEvent(this, Armory.events.TweenEvent.PROGRESS);
  		Armory.events.armEvent(this, Armory.events.TweenEvent.COMPLETE);
  
  	},
  
  
  //-- Utility functions for controlling the Anim object
  //----------------------------------------------------
  
  
  	setFPS: function (fps) {
  
  		this._fps = fps;
  
  	},
  
  	getObjInit: function (o, styles) {
  
  		var init = [];
  		var val = null;
  
  		for(var i=0; i<styles.length; i++){
  
  			var s = styles[i];
  			
  			switch(s){
  				case 'alpha':
  					val = parseFloat(o.style.opacity);
  					if(!val && !('opacity' in o.style) ) val = /\d{1,3}/.exec(o.style.filter) / 100;
  					if(isNaN(val)) val = 1;
  				break;
  				case 'height':
  				case 'width':
  				case 'top':
  				case 'left':
  					val = parseInt(o.style[s]);
   				  if(isNaN(val)){
              val = o['offset' + s.substr(0, 1).toUpperCase() + s.substr(1)];
            }				
  				break;
  				default:
  					val = parseInt(o.style[s]);
  				break;
  			}
  
  			if(!val) val = 0;
  
  			init.push(val);
  
  		}
  
  		return init;
  
  	},                        
  
  	setCSS: function (o, style) {			// 'o' is the object to change, 'style' is an object
  
  		var obj = Armory.select(o, false);
  
  		for(var i=0; i<obj.length; i++){
  
  			var o = obj[i];
  
  			for(var s in style) {
  
  				var v = style[s];
  
  				if(s == 'alpha'){
  
  					if('opacity' in o.style){
  						s = 'opacity';
  					} else {
  						s = 'filter';
  						v = 'alpha(opacity=' + (v * 100) + ')';
  					}
  
  				}
  			
  				if(s == 'className'){
            o.className = v;
          }else{
            o.style[s] = v;
          }
  
  			}
  
  		}

 
  	},
  
  
  //-- Easing function for use in the tweens
  //----------------------------------------
  
  
  	easing: {
  
  		None: {
  
  			easeNone: function (t) {
  
  				var frames = [];
  				var fnum = Math.round(this._fps * t);
  
  				for(var i=0; i<fnum; i++) {
  
  					frames.push(i / fnum);
  
  				}

				frames[0] = 0;
				frames[frames.length - 1] = 1;
  
  				return frames;
  
  			}
  
  		},
  
  		Quad: {
  
  			easeIn: function (t) {
  
  				var frames = [];
  				var fnum = Math.round(this._fps * t);
  
  				for(var i=0; i<fnum; i++) {
  
  					frames.push( Math.pow((i / fnum), 2) );
  
  				}
  
				frames[0] = 0;
				frames[frames.length - 1] = 1;

  				return frames;
  
  			},
  
  			easeOut: function (t) {
  
  				var frames = [];
  				var fnum = Math.round(this._fps * t);
  
  				for(var i=0; i<fnum; i++) {
  
  					frames.push( -1 * Math.pow(((i / fnum) - 1), 2) + 1 );
  
  				}
  
				frames[0] = 0;
				frames[frames.length - 1] = 1;

  				return frames;
  
  			},
  
  			easeInOut: function (t) {
  
  				var frames = [];
  				var fnum = Math.round(this._fps * t);
  
  				for(var i=0; i<fnum; i++) {
  
  					if(i / fnum < 0.5){
  
  						frames.push( 2 * Math.pow((i / fnum), 2) );
  
  					}else{
  
  						frames.push( -2 * Math.pow(((i / fnum) - 1), 2) + 1 );
  
  					}
  
  				}
  
				frames[0] = 0;
				frames[frames.length - 1] = 1;

  				return frames;
  
  			}
  
  		}
  
  	}
      
  }
  
  return anim;
  
})();


//-- ARMORY Ajax Object --//

  
Armory.ajax = (function(){

  var ajax = {
  
    name: 'Ajax',

  	xmlhttp: null,
  	data: null,
  	url: null,
  	args: null,
  	func: null,
  	method: 'GET',
  	parse: true,
  	async: true,
  
  	init: function() {
  	 
  		var a = this;
  		
  		if(window.XMLHttpRequest){
  			this.xmlhttp = new XMLHttpRequest();
  		}else{
  			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		}

  		this.xmlhttp.onreadystatechange = function(e) {
  		       
        var evnt = {};
        evnt.target = Armory.ajax;
        evnt.readyState = a.xmlhttp.readyState;
        evnt.status = evnt.readyState == 4 ? a.xmlhttp.status : false;
        evnt.statusText = evnt.status ? a.xmlhttp.statusText : null;
          
        Armory.events.fireEvent(Armory.ajax, Armory.events.HTTPStatusEvent.HTTP_STATUS, evnt);
  
  			if(a.xmlhttp.readyState == 4 && a.xmlhttp.status == 200) {

  				if(a.parse){
            a.data = parseResponse(a.xmlhttp.responseText);
          }else{
            a.data = a.xmlhttp.responseText;
          }

  				if(a.func) Armory.events.addListener(Armory.ajax, Armory.events.Event.COMPLETE, a.func);
  				
  				evnt.data = a.data;
  				evnt.responseText = a.xmlhttp.responseText; 				
  			  Armory.events.fireEvent(Armory.ajax, Armory.events.Event.COMPLETE, evnt);

				  if(a.func) Armory.events.removeListener(Armory.ajax, Armory.events.Event.COMPLETE, a.func);

  			}
  
  		}
    
  	},
  
  	send: function(surl, sargs, sfunc, smethod, sparse, sasync) {
  		
		if(!this.xmlhttp) this.init();
  
  		if(surl)		this.url = surl;
  		if(sargs)		this.args = sargs;
  		if(sfunc)		this.func = sfunc;
  		if(smethod)		this.method = smethod.toUpperCase();
  		if(sparse === false)	this.parse = sparse;
  		if(sasync === false)	this.async = sasync;

  		var args = args_to_string(this.args);
  		var url = this.url + (args && this.method == 'GET' ? "?" + args : "");
  
  		this.xmlhttp.open(this.method, url, this.async);

  		if(this.method == 'POST') {
  
  			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  			this.xmlhttp.setRequestHeader("Content-Length", args.length);
  			this.xmlhttp.setRequestHeader("Connection", "close");
  
  		}
  
  		try {

			  this.xmlhttp.send(args);
			  Armory.events.fireEvent(Armory.ajax, Armory.events.Event.OPEN);
      
		  } catch(err) {
      
		  	var e = {target: this, error: err};      
		  	Armory.events.fireEvent(Armory.ajax, Armory.events.IOErrorEvent.ERROR, e);
      
		  }  		
  
  	}
    	
  };
  
  function args_to_string(args) {
  
		if(!args) return null;

		var argstr = false;
		var c = args.constructor.toString();

		if(c.indexOf('String') >= 0) {
			argstr = args;
		}else if(c.indexOf('Object') >= 0) {
			var arr = [];
			for(var i in args){
				arr.push(i + "=" + encodeURIComponent(args[i]));
			}
			argstr = arr.join("&");
		}

		return argstr;	
  
  };
  
  function parseResponse(res) {
  
		var fchar = res.charAt(0);
		var lchar = res.charAt(res.length - 1);
		var data;
		
		if(fchar == "[" && lchar == "]"){
			eval("data = " + res + ";");
		}else if(fchar == "{" && lchar == "}"){
			eval("data = " + res + ";");
		}else{
			data = res;
		}

		return data;			
  
  };
  
  return ajax;
  
})();


//-- ARMORY Cookies Object --//

  
Armory.cookies = (function(){

  var cookies = {
  
    setCookie: function(name, value, expires, path){
      if(!path) path = '/';
      var expdate = new Date();
      expdate.setTime(expdate.getTime() + (expires * 1000 * 60) );
      document.cookie = name + '=' + escape(value) + ( (expires) ? ';expires=' + expdate.toGMTString() : '') + ';path=' + path;
    },
    
    getCookie: function(name){
      
      var carr = document.cookie.split(';');
      for(var i=0; i<carr.length; i++){
        carr[i] = carr[i].split('=');
        carr[i][0] = carr[i][0].replace(/^\s+|\s+$/, '');
        if(carr[i][0] == name) {
          return carr[i][1];
          break;
        }
      }
            
      return null;      
    
    },
    
    deleteCookie: function(name, path){
      if(!path) path = null;
      if(Armory.cookies.getCookie(name)) Armory.cookies.setCookie(name, '', -10000, path);
    }
  
  };
  
  return cookies;

})();


//-- ARMORY Utilities Object --//

  
Armory.utils = {

};

//-- ARMORY Events.Event Object --//

Armory.events.Event = {

  START: 'start',
  STOP: 'stop',
  PROGRESS: 'progress',
  COMPLETE: 'complete',
  PAUSE: 'pause',
  CHANGE: 'change',
  FRAME: 'frame',
  INIT: 'init',
  BLUR: 'blur',
  FOCUS: 'focus',
  LOAD: 'load',
  UNLOAD: 'unload',
  ERROR: 'error',
  READY: 'ready',
  RESIZE: 'resize',
  OPEN: 'open'

}

//-- ARMORY Events.MouseEvent Object --//

Armory.events.MouseEvent = {

  CLICK: 'click',
  DOUBLE_CLICK: 'doubleclick',
  MOUSE_DOWN: 'mousedown',
  MOUSE_UP: 'mouseup',
  MOUSE_MOVE: 'mousemove',
  MOUSE_OVER: 'mouseover',
  MOUSE_OUT: 'mouseout'
  
}  

//-- ARMORY Events.KeyboardEvent Object --//

Armory.events.KeyboardEvent = {

  KEY_DOWN: 'keydown',
  KEY_UP: 'keyup',
  KEY_PRESS: 'keypress'

}

//-- ARMORY Events.TweenEvent Object --//

Armory.events.TweenEvent = {

  START: 'onStart',
  STOP: 'onStop',
  PROGRESS: 'onProgress',
  COMPLETE: 'onComplete'

}

//-- ARMORY Events.HTTPStatusEvent Object --//

Armory.events.HTTPStatusEvent = {

  HTTP_STATUS: 'httpstatus'

}

//-- ARMORY Events.IOErrorEvent Object --//

Armory.events.IOErrorEvent = {

  ERROR: 'ioerror'

}

window.Armory = Armory;

})(window);
