/**
 * This is a helper class/object for the SXOOP.template package
 *  
 */
var SXOOP = SXOOP || {};

SXOOP.json = {};

SXOOP.json.bind = function(request,callback){
    request.onreadystatechange = function(){
       if (request.readyState == 4){
           if (request.status == 200 ||
               request.status == 304){
               var json = false;
               try{
                   json = eval('(' + request.responseText + ')');
               } catch (e) {
                   alert("ERROR EVALUATING RESPONSE\n" + e + "\n" + request.responseText);
               }
               callback(json);
           }else{
               alert(request.statusText + ":" + request.responseText);
           }
       }
	};
};

SXOOP.ajax = {};

SXOOP.ajax.createRequest = function(){
    if (typeof XMLHttpRequest != 'undefined'){
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject ("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
};
    
SXOOP.template.helper = {};

/**
 * Called on page load to process all elements which 
 * have a class of 'placeholder'
 */
SXOOP.template.helper.process = function()
{
    var allElements = document.getElementsByTagName("*");
    for (var i = 0;i < allElements.length; i++){
        var el = allElements[i];
        var cn = el.className;
        if (cn.match(/placeholder/)){
            SXOOP.template.helper.processElement(el);
        }
    }
};
/**
 * Apply a template to an element on the page
 * The element must have the following attributes...
 * format, src & template
 * src is the URL to the remote data
 * format can be 'JSON' or 'XML'
 * template is the ID of the template element on the page.
 * 
 */
SXOOP.template.helper.processElement = function(element)
{
    var format = element.getAttribute("format");
    var src = element.getAttribute("src");
    var template = element.getAttribute("template");
    if (format == "JSON"){
        SXOOP.template.helper.processJSON(src,template,element);
    }else if (format == "XML"){
        SXOOP.template.helper.processXML(src,template,element);
    }else {
        alert("invalid format attribute '" + format + "'");
        return;
    }
};
/**
 * process a template from the JSON returned from a URL
 */
SXOOP.template.helper.processJSON = function(url,template,placeHolder)
{
    if (typeof placeHolder == "string"){
        placeHolder = document.getElementById(placeHolder);
    }
    var req = SXOOP.ajax.createRequest();
    SXOOP.json.bind(req,function(response){
        var templateData = {};
        templateData[template] = response;
        var templateText = document.getElementById(template).innerHTML;
        var result = SXOOP.template.parse(templateText,templateData);
        placeHolder.innerHTML = result;
    });
    var url = SXOOP.template.proxy + "?url=" + escape(url);
    req.open("GET",url,true);
    req.send(null);
};
/**
 * process a template from the XML returned from a URL
 */
SXOOP.template.helper.processXML = function(url,template,placeHolder)
{
    if (typeof placeHolder == "string"){
        placeHolder = document.getElementById(placeHolder);
    }
    var url = SXOOP.template.proxy + "?url=" + escape(url);
    var xmlData = new JKL.ParseXML( url );
    xmlData.async(function(data){
        var templateText = document.getElementById(template).innerHTML;
        var result = SXOOP.template.parse(templateText,data);
        placeHolder.innerHTML = result;
    });
    xmlData.parse();
};

SXOOP.template.helper.addLoadEvent = function(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
            if (oldonload){
                oldonload();
            }
            func();
        };
    }
};
SXOOP.template.helper.addLoadEvent(SXOOP.template.helper.process);
