/*	
	Updated @ 2008-11-11
	License: GNU General Public License v3
	Developer: Ehsun Behravesh
	email: ehsun7b@gmail.com
	Tested on Firefox 3.0.3, Opera 9.52, Safari 3.1.2, Internet Explorer 7
*/
var AjaxCSSJS = Class.create();

AjaxCSSJS.prototype = {
  initialize: function(url, onLoadFunction) {
    try {
      
      this.url			      = url;
      this.onLoadFunction = onLoadFunction;
      
      this.id = 'id-'+this.url.substring(this.url.lastIndexOf('/')+1, this.url.indexOf('.'+this.type));
      /**
             * Alteração na verificação de extensão das URL's, não é mais necessário passar o tipo por parâmetros.
             * @author: Diego Lopes
             * @date: 05/12/2008
             * @OS: 6537-009
             * @Inspeção: Tuane em 07/01/2009
             */
      if (this.url.substr(this.url.length -3) == 'css') {
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", this.url);
      } else if (this.url.substr(this.url.length -2) == 'js') {
        var fileref = document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", this.url);
        fileref.onload = this.onLoadFunction;
        fileref.onreadystatechange = this.onReadyChange.bind(this, fileref);
      }
      
      if (typeof fileref != "undefined") {
        fileref.setAttribute("id", this.id);
        document.getElementsByTagName("head")[0].appendChild(fileref);
      }

    } catch (e) {
      alert("error:" + e.name + " - " + e.message);
    }
  },

  onReadyChange: function(fr) {
    if (fr.readyState == 'complete' || fr.readyState == 'loaded')
      this.onLoadFunction();
  }
};

/**
 * Classe para carregar vários arquivos JS dinamicamente.
 * OS - 0010006623
 * Autor: Jean . Data 13/03/2009. 
 * parâmetros:
 * array_urls - array simples contendo as urls dos javascripts a serem carregados.
 * indexFile   - index corrente do array_urls para poder percorrer o mesmo durante a recursão.
 * mainOnLoadFunction - funcão a ser executada após carregar os arquivos JSs.
 */
var AjaxMultiJS = Class.create();

AjaxMultiJS.prototype = {
  initialize: function(array_urls, indexFile, mainOnLoadFunction) {
    try {
                  
			if(indexFile == (array_urls.length-1)) // quando for o último índice, será executado a funcão mainOnLoadFunction.
			  this.onLoadFunction = mainOnLoadFunction;
		  else {
			  this.onLoadFunction = function(){ //recursão
				  new AjaxMultiJS(array_urls, (indexFile+1), mainOnLoadFunction);
				}
			}
      
      
      this.url			      = array_urls[indexFile];
      this.id = 'id-'+this.url.substring(this.url.lastIndexOf('/')+1, this.url.indexOf('.'+this.type));
			
			var fileref = document.createElement('script');
      
			fileref.setAttribute("type","text/javascript");
      
			fileref.setAttribute("src", array_urls[indexFile]);      
			fileref.onload = this.onLoadFunction;      
			fileref.onreadystatechange = this.onReadyChange.bind(this, fileref);
      
      if (typeof fileref != "undefined") {        
        fileref.setAttribute("id", this.id + indexFile);
        
        document.getElementsByTagName("head")[0].appendChild(fileref);
      }

    } catch (e) {      
      alert("error:" + e.name + " - " + e.message);
    }
  },

  onReadyChange: function(fr) {
    if (fr.readyState == 'complete' || fr.readyState == 'loaded')
      this.onLoadFunction();
  }
};
