/**
* jQuery Plugin SuperFlyDOM v0.9g
*
* Create DOM elements on the fly and automatically append or prepend them to another DOM object.
* There are also template functions (tplAppend and tplPrepend) that can take a JSON-formatted
* complex HTML structure, apply a dataset, and therefore add siblings way faster
*
* This plugin is built off of FlyDom 3.0.8, by dohpaz
* [http://dohpaz.mine.nu/jquery/jquery.flydom.html], who was inspired by "Oslow"
* [http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype#comment-176],
* and since I could not get dohpaz code to work with my template (and since dohpaz
* could not get Oslow's code to work), and neither Michael Geary's code nor Sean's
* [http://www.pinkblack.org/itblog/?page_id=22] code were jQuery style and chainable,
* and I wanted a ton of features anyway, while retaining a small code base, I decided
* to rip apart, clean up (JSLint),and add features to their plugins. My hope is that
* this version will be easier to understand, more forgiving and flexible, and maintain
* with future versions of the fantastic framework which is jQuery.
*
* Note: For event attaching using the liveQuery plugin is highly recommended.
*
* Copyright (c) 2007 Charles Phillips [charles at doublerebel dot com]
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* @version  $Id: jquery.superflydom-0.9g.js 9 2007-08-27 00:02:34 polyrhythmic $
*
* @author   Charles Phillips [charles at doublerebel dot com]
* @copyright   (C) 2007. All rights reserved.
*
* @license  http://www.opensource.org/licenses/mit-license.php
* @license  http://www.opensource.org/licenses/gpl-license.php
*
* @package  jQuery Plugins
* @subpackage  SuperFlyDOM
*
* @todo   (dohpaz): Cache basic elements that are created, and if an already existing basic element is
*    asked to be created an additional time, use a copy of the cached element to build from.
*    (Charles): If dohpaz accomplishes this I will be happy to merge it with my code fork.
*
*/
/**
* Create DOM elements on the fly and automatically append them to the current DOM obejct
*
* @uses jQuery
*
* @param   string  element-The name of the DOM element to create (i.e., img, table, a, etc)
* @param   object  attrs  -An optional object of attributes to apply to the element
* @param   string  text   -An optional string for text node to prepend to element
* @param   array   content-An optional array of content (or element children) to append to element
*
* @return  jQuery  element-The jQuery object representing the new element
*
* @since   FlyDom 1.0
*/
( function($){
var el;
if($.elHash === undefined){
var elArray, elHash={};
elArray = "a|abbr|acronym|address|area|b|base|bdo|big|blockquote|body|br|button|caption|cite|code|col|colgroup|dd|del|dfn|div|dl|dt|em|fieldset|form|frame|frameset|h1-h6|head|hr|html|i|iframe|img|input|ins|kbd|label|legend|li|link|map|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|samp|script|select|small|span|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|ul|var".split("|");
for(var i=0;i < elArray.length;i++){ elHash[elArray[i]]=true;}
$.elHash=elHash;
}
if($.browser.msie){
Array.prototype.unshift=function(){
var a=Array.prototype.concat.call(Array.prototype.slice.apply(arguments, [0]), this), i=a.length;
while (i--){ this[i]=a[i];}
return this.length;
}
}
$.fn.extend({
createAppend: function(){
if(arguments.length === 0){ return this;}
var ie, pEl, elH=$.elHash, arg1=arguments[0], arg2=arguments[1], a=1;
pEl=this[0]||this;
if(typeof arg1 === "number"){ arg1 = String(arg1);}
if(typeof arg1 === "string"){
if(arg1 in elH){
ie=$.browser.msie;
el = (ie && arg1 === 'input') ? '<input type="' + arg2.type + '" />': arg1;
el=document.createElement(el);
if(ie && pEl.nodeName.toLowerCase()=== "table" && el.nodeName.toLowerCase() === "tr"){
pEl=pEl.parentNode.getElementsByTagName('tbody')[0]||pEl.appendChild(document.createElement('tbody'));
}
el=pEl.appendChild(el);
if(arg2.constructor === Object){
for(attr in arg2){ $.attr(el, attr, arg2[attr]);}
a++;
}
if(arguments.length > a){
if(arguments[a].constructor === Array){
el=$.fn.createAppend.apply($(el), arguments[a]);
a++;
}
}
} else if (arg1.match(/(<\S[^><]*>)|(&.+;)/g) !== null &&
arg1.tagName.toUpperCase() !== 'TEXTAREA'){
pEl.innerHTML+=arg1;
el=pEl;
} else { el=pEl.appendChild(document.createTextNode(arg1));}
} else { }
if(arguments.length > a){
el=$.fn.createAppend.apply($(pEl), Array.prototype.slice.call(arguments, a));
}
return $(el);
},
createPrepend: function(){
var al=arguments.length;
if(al === 0){ return this;}
var elH=$.elHash, ie, pEl, hCN=this[0].hasChildNodes(), arg_l, arg=[];
arg_l=arg.push(arguments[al-1]);
pEl=this[0]||this;
if(arg[0].constructor === Array){
arg_l=arg.unshift(arguments[al-1-arg_l]);
}
if(arg[0].constructor === Object){
arg_l=arg.unshift(arguments[al-1-arg_l]);
}
if(typeof arg[0] === "number"){ arg[0] = String(arg[0]);}
if(typeof arg[0] === "string"){
if(arg[0] in elH){
ie=$.browser.msie;
el = (ie && arg[0] === 'input') ? '<input type="' + arg[1].type + '" />': arg[0];
arg.shift();
el=document.createElement(el);
if(ie && pEl.nodeName.toLowerCase()=== "table" && el.nodeName.toLowerCase() === "tr"){
pEl=pEl.parentNode.getElementsByTagName('tbody')[0]||pEl.appendChild(document.createElement('tbody'));
}
if(hCN){ el=pEl.insertBefore(el, pEl.firstChild);}
else { el=pEl.appendChild(el);}
if(arg[0].constructor === Object){
for(attr in arg[0]){ $.attr(el, attr, arg[0][attr]);}
arg.shift();
}
if(arg[0] !== undefined&&arg[0].constructor === Array){
el=$.fn.createPrepend.apply($(el), arg[0]);
}
} else if (arg[0].match(/(<\S[^><]*>)|(&.+;)/g) !== null &&
arg[0].tagName.toUpperCase() !== 'TEXTAREA'){
pEl.innerHTML=arg.pop()+pEl.innerHTML;
el=pEl;
} else {
el=document.createTextNode(arg[0]);
el=(hCN) ? pEl.insertBefore(el, pEl.firstChild):pEl.appendChild(el);
}
} else { }
if(al > arg_l){
el=$.fn.createPrepend.apply($(pEl), Array.prototype.slice.call(arguments, 0, -arg_l));
}
return $(el);
},
tplAppend: function(json, tpl){
return $.fn.createAppend.apply(this, tpl.call(json));
},
tplPrepend: function(json, tpl){
return $.fn.createPrepend.apply(this, tpl.call(json));
}
});
})(jQuery);

