Simpler.Present = {
};

Simpler.Present.Components = [];

Simpler.createClass('Simpler.Present.Component', {
  __create: function (elementId) {
    this.element = YAHOO.util.Dom.get(elementId);
    this.element.component = this;

    var parentNode = this.element.parentNode;
    while (!parentNode.component && parentNode !== document.body) {
      parentNode = parentNode.parentNode;
    }
    this.parentComponent = parentNode.component;
    
    var parentComponent = this.parentComponent;
    while (parentComponent) {
      if (parentComponent.isTypeOf('Simpler.Present.Form')) {
        this.form = parentComponent;
      }
      parentComponent = parentComponent.parentComponent;
    }
    
    this.properties = {};
    if (this.element.firstChild.nodeType === 8) {
      eval('this.properties = ' + this.element.firstChild.data + ';');
    }
  },
  
  initialize: function () {
  }
});

Simpler.createClass('Simpler.Present.DynamicComponent', Simpler.Present.Component, {
  __create: function (elementId) {
    arguments.callee.base.call(this, elementId);
  },

  getContent: function () {
    var callback = {
      success: this.updateContent,
      failure: this.updateContent,
      scope: this
    };
    Simpler.executeUrl(this.properties['contentUrl'], callback);
  },
  
  initialize: function () {
    arguments.callee.base.call(this);

    if (this.properties['contentUrl']) {
      var self = this;
      window.setTimeout(function () {self.getContent();}, 1);
    }
  },
  
  updateContent: function (o) {
    if (o.status === 200) {
      if (o.responseText) {
        var div = document.createElement('DIV');
        div.innerHTML = o.responseText;
        var element = div.childNodes[0];
        if (this.element.parentNode) {
          this.element.parentNode.replaceChild(element, this.element);
        }
      }
    }
    else if (o.status === 399) {
      var self = this;
      window.setTimeout(function () {self.getContent();}, 1);
    }
    else {
      alert(o.status + ', ' + o.statusText);
    }
  }
});

Simpler.Present.Util = {
};

Simpler.Present.Util.HtmlEventAction = {
  evaluateScript: function (script) {
    try {
      eval(script);
    }
    catch (e) {
    }
  }
};

Simpler.createClass('Simpler.Present.Util.Uri', {
  __create: function (uri) {
    var o = {
      strictMode: true,
      key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "querystring", "anchor"],
      q: {
        name: "query",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
      },
      parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
      }
    };
    var m = o.parser[o.strictMode ? "strict" : "loose"].exec(uri);
    var i = 14;

    while (i--) {
      this[o.key[i]] = m[i] || "";
    }
 
    var query = {};
    this[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
      if ($1) {
        query[$1] = decodeURIComponent($2.replace(/\+/g, ' '));
      }
    });

    this[o.q.name] = query;
  },
  
  toUri: function () {
    var uri = '';
    if (this.protocol) {
      uri = uri + this.protocol + "://";
    }
    if (this.authority) {
      uri = uri + this.authority;
    }
    if (this.path) {
      uri = uri + this.path;
    }
    if (this.query) {
      var query = '';
      for (var q in this.query) {
        if (this.query.hasOwnProperty(q)) {
          if (this.query[q]) {
            if (query.length > 0) {
              query = query + '&';
            }
            query = query + q + '=' + encodeURIComponent(this.query[q]);
          }
        }
      }
      uri = uri + '?' + query;
    } 
    if (this.anchor) {
      uri = uri + '#' + this.anchor;
    }
    return uri;
  }
});

/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/
/*
function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
*/

YAHOO.util.Event.onAvailable('onDOMReady', function () {for (var i = Simpler.Present.Components.length - 1; i >= 0; i--) {Simpler.Present.Components[i].initialize();}});

YAHOO.util.Event.onAvailable('onDOMReady', function() {if (window.frameElement && window.frameElement.onDOMReady) window.frameElement.onDOMReady(document);});
