//noConflict jQuery
var $j = jQuery.noConflict();

//jQuery customizations
$j.getScript = function(url, callback, cache){ $j.ajax({ type: "GET", url: url, success: callback, dataType: "script", cache: cache }); };

//create a spectrum style serialize (how our urls expect get params)
$j.fn.extend(
	{
		sdnaSerialize:function(){
			return '/' + $j(this).serialize().replace(/&/g,'/').replace(/=/g,':');
		}
	}
);

//convert element type
$j.fn.convertElementTo = function( sType ) {	

	return jQuery(
		this.map(
			function() {
				
				//this jquery item
				var $this = $j(this);
				
				//copy the present attributes
				var attrs = {};
				for(var i=0,attr=null;attr = this.attributes[i];i++){
					if( attr !== null && attr.value !== "null" && attr.value !== "" ){
					 attrs[attr.name] = attr.value;
				   }
				}
				
				//create the element, dumping in the attributes and the children
				var $el = $j("<" + sType + " />")
								.attr( attrs )
								.append( $this.children().clone(true) );
				
				//replace this node with the converted node
				$this.replaceWith( $el );
				
				//return this DOM item
				return $el.get(0);
			}
		)
	);
};

/**
 * Get the container of the selected object
 * 
 * What we will call a DOM "container" is any element with 
 * an HTML 5 data attribute "data-id", with or without a value
 * - this is useful for records/rows/lists (or even lists witin lists)
 *   example: $j('a.click_me').getContainer().find('.inner_list').show('slow');
 * - this container also has a built in function for retreiving the id of that container
 *   example: container.getId()
 * - optionally provide a class to target the correct container, which is useful
 *   in situations where there are nested records
 *   example: $j(this).getContainer('person').find('.inner_list') etc
 *
 * @param string cssClass OPTIONAL container class name to target
 * @author davidbjames
 * @return jQuery object
 */
$j.fn.getContainer = function(cssClass) {
    var cssClass = cssClass || '';
    // note: [data-id] and [data-id=] are with and without values respectively
    if (cssClass.length) {
        var container = this.parents('.' + cssClass + '[data-id]' + ',.' + cssClass + '[data-id=]').eq(0);
    } else {
        var container = this.parents('[data-id],[data-id=]').eq(0);
    }
    var id = (this.length ? container.attr('data-id') : 0);
    /**
     * Get the ID of an identifiable container
     * (actually, it's already done just above, but this
     * closure returns that value. A better way to do it.)
     */
    container.getId = function () {
        return id;
    };
    return container;
};