
/* KUbbe VIew Proccesor */
var KUVIP = {
    RE_VAR: new RegExp("\\${[\\w.]+}","g"),
    RE_IF_INIT: new RegExp("\\$if{[\'!<>=\\w.]+}","g"),
    RE_IF_END: new RegExp("\\$if\\$","g"),    
    RE_FOR_INIT: new RegExp("\\$for{[\\w]+,[\\w.]+}","g"),
    RE_FOR_END:new RegExp("\\$for\\$","g"),
    RE_VAR_COND: new RegExp("\\${[\'!<>=\\w.]+\\?[\'\"\\w.\\s:]+::[\'\"\\w.\\s:]+}","g"),
    RE_INCLUDE: new RegExp("\\$include{[\\w/.]+}"),
    RE_I18N: new RegExp("\\$i18n{[\\w.]+}","g"),
    templates: {}
}
/***************** TEMPLATE PARSER ***************************************/
KUVIP.loadTemplate = function(url) {
    var template = this.templates[url];
    if (!template) {
        $.ajax(
           {async: false,
            contentType: "application/x-www-form-urlencoded",
            dataType: "html",
            type: "GET",
            url: url,
            success: function(html){
                template = html;
                KUVIP.templates[url]=template;
            }                      
           }
          );
    }
    return template;
}
KUVIP.parse = function(url, data) {
    var html = this.loadTemplate(url);
    html = this.parseInclude(html);
    html = this.parseFor(html, data);    
    html = this.parseIf(html, data);     
    html = this.parseVars(html, data);   
    html = this.parseVarsCond(html, data);    
    html = this.parseI18N(html);
    return html;
}

KUVIP.parseInclude = function(html) {    
    var matchs = html.match(this.RE_INCLUDE);
    if (matchs != null) {
        for (var i=0; i<matchs.length; i++) {        
            var loadInclude = this.loadTemplate(URL_APL+matchs[i].substring(9,matchs[i].length-1));
            if (loadInclude) {
                loadInclude = this.parseInclude(loadInclude);
                html = html.replace(matchs[i],loadInclude);
            }
        }
    }    
    return html;
}

KUVIP.parseI18N = function(html) {
    var matchs = html.match(this.RE_I18N); 
    if (matchs!=null) {
        var name = null;    
        for (var i=0; i<matchs.length;i++) {
            name = matchs[i].substring(6, matchs[i].length-1);
            html = html.replace(new RegExp("\\$i18n{"+name+"}","g"), KUBBEI18N.keyOnly(name));;                
        }    
    }
    return html;
}

KUVIP.parseVars = function (html, data) {
    var matchs = html.match(this.RE_VAR);
    if (matchs!=null) {        
        var name = null;    
        for (var i=0; i<matchs.length;i++) {
            name = matchs[i].substring(2, matchs[i].length-1);
            html = html.replace(new RegExp("\\${"+name+"}","g"), this.getData(name, data));                
        }
    }
    return html;
}


KUVIP.parseVarsCond = function (html, data) {
    var matchs = html.match(this.RE_VAR_COND);
  
    if (matchs!=null) {
        for (i=0; i<matchs.length; i++) {
            var iCond = matchs[i].indexOf("?");            
            var iSep = matchs[i].indexOf("::");
            var condition = matchs[i].substring(2, iCond);
            var var1 = matchs[i].substring(iCond+1, iSep);
            var var2 = matchs[i].substring(iSep+2, matchs[i].length-1);     

//            if (this.evalue(condition,data)) alert(condition+"-->"+var1+"("+this.getData(var1, data)+"):"+var2+"("+this.getData(var2, data)+")("+this.evalue(condition,data)+")");            
            html = html.replace(matchs[i],
                                this.evalue(condition,data)?this.getData(var1, data):
                                                            this.getData(var2, data));
       
       
        }
    }
    return html;
}

KUVIP.parseIf = function(html, data) {
    var htmlOriginal = html;
    var ifs = [];
    var ifIndex = [];
    var ifendIndex = [];    
    do {        
        var result = this.RE_IF_INIT.exec(html);
        if (result!=null) ifIndex[ifIndex.length]=result.index;
    }while (result!=null);            
    do {        
        var result = this.RE_IF_END.exec(html);        
        if (result!=null) ifendIndex[ifendIndex.length]=result.index;
    }while (result!=null);      
   
    if (ifIndex.length>0) {                         
        var i=0; var j=0;
        var newIf=ifIndex[i];
        while (i<ifIndex.length){
            if (i<ifIndex.length && ifIndex[i+1]<ifendIndex[j]){
                i++;
                j++;
            } else {
                i++;    
                ifs[ifs.length] = {init:newIf, end:ifendIndex[j]};
           
                newIf = ifIndex[i];
                j++;    
            }
        } 

        for (var i=0; i<ifs.length; i++) {
     
            var htmlIf = htmlOriginal.substring(ifs[i].init, ifs[i].end+4);
       
            var iCond = htmlIf.indexOf("}");
            var cond = htmlIf.substring(4,iCond);
            if (this.evalue(cond, data)) {
                html = html.replace(htmlIf, htmlIf.substring(iCond+1, htmlIf.length-4));
            } else {
                html = html.replace(htmlIf, "");
            }                        
        }
        
        return this.parseIf(html, data);
    }
    return html;
}

KUVIP.parseFor = function(html, data) {           
    var htmlOriginal=html;
    var fors = [];
    var forIndex = [];
    var fendIndex = [];    
    do {        
        var result = this.RE_FOR_INIT.exec(html);
        if (result!=null) forIndex[forIndex.length]=result.index;
    }while (result!=null);            
    do {        
        var result = this.RE_FOR_END.exec(html);
        if (result!=null) fendIndex[fendIndex.length]=result.index;
    }while (result!=null);       
    
    if (forIndex.length>0) {                         
        var i=0; var j=0;
        var newFor=forIndex[i];    
        while (i<forIndex.length){
            if (i<forIndex.length && forIndex[i+1]<fendIndex[j]){
                i++;
                j++;
            } else {
                i++;    
                fors[fors.length] = {init:newFor, end:fendIndex[j]};
                newFor = forIndex[i];
                j++;    
            }
        }      
                               
      for (var i=0; i<fors.length;i++) {
          fors[i].htmlFor = html.substring(fors[i].init, fors[i].end+5);
          fors[i].htmlContent = fors[i].htmlFor.substring(fors[i].htmlFor.indexOf("}")+1,fors[i].htmlFor.length-5);
          fors[i].name = fors[i].htmlFor.substring(5,fors[i].htmlFor.indexOf("}"));          
      }
     
      for (var i=0; i<fors.length;i++) {                    
        var names = fors[i].name.split(",");        
        var loop = this.getData(names[1], data);
        var htmlTemp = "";
        for (var j=0; j<loop.length;j++) {
            data[names[0]]=loop[j];            
            var htmlLoopTemp = this.parseFor(fors[i].htmlContent, data)
            htmlLoopTemp = this.parseVars(htmlLoopTemp, data);
            htmlLoopTemp = this.parseVarsCond(htmlLoopTemp, data);    
            htmlTemp+=htmlLoopTemp;            
        }
                
        htmlOriginal = htmlOriginal.replace(fors[i].htmlFor, htmlTemp);                          
      }   
    }
    return htmlOriginal;
}

KUVIP.getData = function(name, data, bool, str) {    
    if (!name && bool) return false;
    if (name.match("'[\\w\\W]+'")) return str?name:name.substring(1,name.length-1);
    if (!isNaN(name)) return name;
    var path = name.split(".");
    var value = data;
    for (var i=0; i<path.length; i++) {
        if (!value || value==null) break;
        value = value[path[i]];
    }
        
    if (bool) {
        value = value?true:false; 
    } else {
        if (!value) value="";
    }
    if (str && typeof value == "string") return "'"+value+"'";
    return value;
}

KUVIP.evalue = function(condition, data) {        
    var conditionTemp = condition;
    var re = new RegExp("[\'\\w.]+","g");
    var matchs = condition.match(re);
    if (matchs==null) return false;
    
    for (var i=0; i<matchs.length;i++) {
        conditionTemp = conditionTemp.replace(matchs[i], this.getData(matchs[i],data, false, true))
    }     
    try {       
        conditionTemp = eval("("+conditionTemp+")");
    } catch (exception) {   
        conditionTemp = condition.replace(matchs[i], this.getData(matchs[i],data, true, true))      
        return conditionTemp?true:false;
    }
    return conditionTemp;
}
/************************************************************************/

