/*
*    Clean AJAX Engine v4.1
*    Copyright (C) 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/** Define clean_environment object */
var clean_environment = { };

/** Define debug mode (true or false) */
clean_environment.debug = false; 

/** Define the hash algorithm used on safe requests (md5 or sha1) */
clean_environment.hashAlgorithm = "md5";

/** Classes */
clean_environment.classes = ["classes.Clean", "classes.Engine", "classes.Message", "classes.Connection", 
				             "classes.MessageQueue", "classes.MessageWrapper", "classes.DomIterator", 
				             "classes.ParserTool", "classes.XsltTool", "classes.FlyProgressBar", 
				             "classes.HashTool", "classes.EmbeddedProgressBar", "classes.Console", 
				             "classes.Base64", "classes.Date", "classes.XmlRpcStream", "classes.SoapStream",
				             "classes.RemoteMethod", "classes.QName", "classes.History"];

/** Libraries */
clean_environment.libraries = ["lib.ajaxslt.misc", "lib.ajaxslt.dom", "lib.ajaxslt.xpath", 
					           "lib.ajaxslt.xslt", "lib.pajhome.md5", "lib.pajhome.sha1"];

/** Clean real path (Assigned dynamically) */
clean_environment.cleanPath = "";

/** User agent */
clean_environment.userAgent = navigator.userAgent.toLowerCase();

/**
* Get script real path
*/
function getPath() {
  var path = "";
  var scripts = document.getElementsByTagName('script');
  for(var i = 0; i < scripts.length; i++ ) {	  
    var src = scripts[i].getAttribute('src');
	if(src) {
	  var loader = src.match(/^(.*?)\/?clean-all.js/);
	  if(loader) {
	    var root = loader[0];
		path = root.substring(0, root.lastIndexOf("/") + 1);
		break;
	  }
    }
  }
  return path;
}

/**
* Import scripts to current page
*/
function importFile(js_file_path) {
  if(clean_environment.userAgent.indexOf('safari') != -1)
	 document.writeln("<script type='text/javascript' src='" + js_file_path + "'></script>");	   
  else {
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = js_file_path;
    head.appendChild(script);
  }
}

/**
* Import all scripts present in a package
*/
function importPackage(path, js_package) {	
  for(var f in js_package) {
	
    var file = js_package[f].replace(/\./g, '/') + '.js';
	importFile(path + file);
  }
}

/**
* Clean bootstrap code 
*/
function bootstrap() {
  clean_environment.cleanPath = getPath();	
  importPackage(clean_environment.cleanPath, clean_environment.classes);
  importPackage(clean_environment.cleanPath, clean_environment.libraries); 	
  main();  
}

/**
* Clean main code 
*/
function main() {	
  if(document.body) { 	  	
    var img_path = clean_environment.cleanPath + "resources/loading_circle.gif";
    var progress_bar = new FlyProgressBar(document, "<p><img src='" + img_path + "'/>&nbsp;&nbsp;</p>");
    Engine.start(clean_environment.debug, progress_bar);
  }
  else
    setTimeout(main, 1000);
}

bootstrap();
