﻿/*
 þ THORN: micro FrameWork (jQuery style syntax)
 Author: Rick Glimmer
 Date: 2015-01-28
 Version: 0.1
*/
"use strict";
!(function () {
    //Objects to hold all added functionalities
    var _Chainables = Object.create(null);
    var _Properties = Object.create(null);

    //This is the global selector function
    var THORN = function (query, context) {
        return query ? getNodes(query, context) : null;     // Return a list of nodes 
    }
    
    //internal function to select all nodes in the DOM that satifies the parameters
    //Return value will have a nodes list with all the raw DOM nodes
    //.length can be used how many nodes are found
    //_Chainable functions use the nodes and allow chaining.
    //_Properties can get or set a value on a the first node or when possible on all nodes.
    function getNodes (selector, context) {
        var i, l, nodeType;
        var root = context || document;
        var o = Object.create(_Chainables, _Properties);

        if (selector === window)
            o.nodes = [window];
        else if (selector.nodeType)
            o.nodes = [selector];
        else if (selector instanceof Array) {  // An array with nodes
            o.nodes = [];
            for (i = 0, l = selector.length; i < l; i++) {
                o.nodes[i] = selector[i];
            }
        }
        else if (
                !(!selector || typeof selector !== "string") ||
	            !(((nodeType = root.nodeType) !== 1 && nodeType !== 9))
            )
            o.nodes = root.querySelectorAll(selector);
        return o;
    }

    //This allows to extend THORN with extra functions and attributes
    THORN.extend = function (targetname, obj) {						// Merge to objects
        var prop, target;
        switch (targetname) {
            case 'CHAINABLEMETHODS': //Falls through as there is no separate methods object
            case 'CHAINABLES': target = _Chainables; break;
            case 'CONSTANTS':
            case 'PROPERTIES': target = _Properties; break;
            case 'THORN': target = THORN; break;
            case 'PLUGINS': target = _Plugins; break;
            default: target = typeof targetname === 'string' ? Object.create(null) : targetname; break;
        }
        for (prop in obj) {
            if (obj.hasOwnProperty(prop)) {
                target[prop] = obj[prop];
            }
        }
        return target;
    };

    //THORN collects all onready functions
    var onLoadFunctions = []

    //First attaches all Plugins to their nodes and then calls the
    //registered onready functions one by one.
    function DOMReady() {
        var i, l;

        for (i = 0, l = onLoadFunctions.length; i < l; i++) {
            onLoadFunctions[i]();
        }
        onLoadFunctions = null;
        document.removeEventListener('DOMContentLoaded', DOMReady, false);
    };

    THORN.onready = function (fn) {
        //register the onready function
        onLoadFunctions.push(fn);
    }

    document.addEventListener('DOMContentLoaded', DOMReady, false);


    window.THORN = window.þ = THORN; // Expose þ to the world
})();


@@ -84,12 +84,24 @@
 15-0
-1-28
+2-02 21:15+07:00
 %0A Ve
@@ -128,17 +128,16 @@
 trict%22;%0A
-!
 (functio
@@ -139,24 +139,24 @@
 nction () %7B%0A
+
     //Object
@@ -453,24 +453,25 @@
 nodes %0A    %7D
+;
 %0A    %0A    //
@@ -825,24 +825,19 @@
 es.%0A    
-function
+var
  getNode
@@ -838,16 +838,27 @@
 etNodes 
+= function 
 (selecto
@@ -1719,33 +1719,8 @@
 j) %7B
-%09%09%09%09%09%09// Merge to objects
 %0A   
@@ -1736,24 +1736,32 @@
 prop, target
+, sealed
 ;%0A        sw
@@ -2307,24 +2307,119 @@
 ty(prop)) %7B%0A
+                sealed = target%5Bprop%5D && target%5Bprop%5D.sealed;%0A                if (!sealed)%0A    
             
@@ -2448,16 +2448,16 @@
 %5Bprop%5D;%0A
-
         
@@ -2574,16 +2574,17 @@
 ons = %5B%5D
+;
 %0A%0A    //
@@ -3067,21 +3067,22 @@
 sh(fn);%0A
-
     %7D
+;
 %0A%0A    do

clean up the syntax and add support for sealed extensions