/*!
 * TemplateLayout Wef plugin
 * Copyright (c) 2011 Pablo Escalada
 * MIT Licensed
 */

//requires: cssParser
var parser = wef.fn.cssParser; //TODO: loader
//exports: templateLayout


(function () {
    var templateLayout = {
        name:"templateLayout",
        version:"0.0.1",
        description:"W3C CSS Template Layout Module",
        authors:["Pablo Escalada <uo1398@uniovies>"],
        licenses:["MIT"], //TODO: Licenses
        constants:{
            DISPLAY:"display-model",
            SITUATION:"situated"
        },

        init:function () {
            root = new Template("", "", "");
            document.addEventListener(parser.events.PROPERTY_FOUND, function (e) {
                console.log(e.data.selectorText, e.data.declaration);
                lastEvent = e;
                //TODO store them
                //buffer.add(e.data.selectorText, e.data.declaration);
                //TODO populate TemplateDOM
                //var model = e.data.declaration.property == this.constants.DISPLAY ? e.data.declaration.valueText : "";
                //var situation = e.data.declaration.property == this.constants.SITUATION ? e.data.declaration.valueText : "";
                //this.add(e.data.selectorText, model, situation);

            }, false);
            return templateLayout;
        },

        transform:function (cssFile) {

            function readFile(url) {
                function ajaxReadFile() {
                    var request = new XMLHttpRequest();
                    request.open("get", url, false);
                    request.send("");
                    return request.responseText;
                }

                try {
                    return ajaxReadFile(url);
                } catch (e) {
                    //TODO: chrome workaround
                    throw "OperationNotSupportedException";
                }

            }

            parser.parse(readFile(cssFile));

        },

        //testing purposes
        getLastEvent:function () {
            return lastEvent;
        }
    };

    var lastEvent = null;

    wef.plugins.register("templateLayout", templateLayout);
})();

function Template(selectorText, model, situated) {
    this.selectorText = selectorText;
    this.model = model;
    this.situated = situated;
}
Template.prototype = {
    model:"",
    selectorText:"",
    situated:""
};
@@ -1395,24 +1395,57 @@
 File(url) %7B%0A
+                //TODO: refactor%0A
             
@@ -1903,33 +1903,32 @@
               %7D%0A
-%0A
             %7D%0A%0A 
@@ -1971,17 +1971,16 @@
 File));%0A
-%0A
         
@@ -2128,71 +2128,8 @@
     
-wef.plugins.register(%22templateLayout%22, templateLayout);%0A%7D)();%0A%0A
 func
@@ -2171,24 +2171,28 @@
 situated) %7B%0A
+    
     this.sel
@@ -2213,24 +2213,28 @@
 lectorText;%0A
+    
     this.mod
@@ -2245,24 +2245,28 @@
  model;%0A    
+   
+ 
 this.situate
@@ -2279,18 +2279,27 @@
 tuated;%0A
-%7D%0A
+    %7D%0A%0A    
 Template
@@ -2309,24 +2309,28 @@
 ototype = %7B%0A
+    
     model:%22%22
@@ -2327,24 +2327,28 @@
   model:%22%22,%0A
+    
     selector
@@ -2360,16 +2360,20 @@
 %22%22,%0A    
+   
+ 
 situated
@@ -2376,10 +2376,81 @@
 ated:%22%22%0A
-%7D
+    %7D;%0A%0A    wef.plugins.register(%22templateLayout%22, templateLayout);%0A%7D)()
 ;

Refactor and eliminate globals