/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
Af.TemplateLoader = Class.create();

Af.TemplateLoader.prototype = {

	initialize: function(url, targetContainerId, sourceElementId, target)
    {
        this.url = url;
		if (target == null && targetContainerId != null)
        {
			this.target = document.getElementById(targetContainerId);
        }
        else
        {
		    this.target = target;
		}
        this.sourceElementId = sourceElementId;
		this.listener = null;
	},
	
	loadTemplate: function ()
    {
	   if (this.target == null) {
		   // save roundtrip if target element is missing
		   return false;
	   }
	   var req = new Af.DataRequest(
           this.url,
           this.requestCompletedTemplateLoad.bind(this),
           requestFailedCommon,
           null,
           requestTimedoutCommon);
	   ajaxEngine.processRequest(req);
	},
	
	requestCompletedTemplateLoad: function(response)
    {
	   if (this.listener != null && this.listener.makeVisible)
       {
	      this.listener.makeVisible(this);
	   }
	   removeAll(this.target);
	   this.target.innerHTML = response.responseText;
      
	   // the following fix is required for FireFox. FireFox seems to create actual layout element
	   // inside another element (as a result of mainArea.innerHTML = response.responseText). We
	   // extract the actual element and add it to the 'MainArea'
	   if (this.sourceElementId != null)
       {
		   var ec = new Af.ElementCollection(this.target);
		   var e = ec.getFirstElementById(this.sourceElementId);
		   if (e != null)
           {
			   this.element = e;
		   }
	   }
       else
       {
	       this.element = this.target.childNodes[0];
	   }
	  
	   if (this.listener != null && this.listener.templateLoaded)
       {
	      this.listener.templateLoaded(this);
	   }
	},
	
	/*
	   This code shows how to use "loadTemplateUsingService"
	   var tl = new Af.TemplateLoader("hj/calendars/Calendar_Week_Nav_View.html", this.targetContainerId, "monthlyCalendar");
	   tl.loadTemplateUsingService();
	*/
	   
	loadTemplateUsingService: function() {
	   var req = new Af.DataRequest(svcURL,
			this.requestTemplateLoadUsingServiceCompleted.bind(this), requestFailedCommon, null, requestTimedoutCommon);
	   req.addService("TemplateService", "getTemplate");
	   req.addParameter("htmlFile", this.url);
	   req.addParameter("elementId", this.sourceElementId);
	   ajaxEngine.processRequest(req);
	},
	
	
	requestTemplateLoadUsingServiceCompleted: function(response) {
	   debugA(response.responseText);
	},
	
	reAttachElement: function() {
	   if (this.target.childNodes.length > 0 && this.target.childNodes[0] == this.element) {
	       return;
	   }
	   removeAll(this.target);
	   try{
		   this.target.appendChild(this.element);
	   }catch(err){
	   }
	}
}

