
var libhttp = {

    request : function ()
    {
      /** The file toot allows IE 7 to run this code localy
      */
      var file = !! String( document.location ).match( /^file:\/\/\// );
      if ( !file && typeof( XMLHttpRequest  ) != 'undefined' )
      {
        return new XMLHttpRequest();
      }
      if ( typeof( ActiveXObject  ) != 'undefined' )
      {
        try
        {
          return new ActiveXObject( "Msxml2.XMLHTTP" );
        }
        catch ( e )
        {
          return new ActiveXObject( "Microsoft.XMLHTTP" );
        }
      }
      if ( typeof( XMLHttpRequest  ) != 'undefined' )
      {
        return new XMLHttpRequest();
      }
      if ( typeof window.createRequest != "undefined" )
      {
        return window.createRequest();
      }
      return null;
    },

    append_script_url : function ( url, type )
    {
      var ss = document.createElement( "script" );
      ss.setAttribute('type', type ? type :'text/javascript');
      ss.setAttribute( "src", url );
      document.getElementsByTagName("HEAD")[0].appendChild( ss );
    },
      
      
    append_style_url : function ( style, type, media )
    {
      var ss = document.createElement( "link" );
      ss.setAttribute('type', type ? type :'text/css');
      ss.setAttribute('media', media ? media : 'screen');
      ss.setAttribute( "href", style );
      ss.setAttribute( "rel", "stylesheet" );
      document.getElementsByTagName("HEAD")[0].appendChild( ss );
    },  
      
    append_style : function ( style, type )
    {  
      var ss = document.createElement( "style" );
      ss.setAttribute('type', type ? type :'text/css');
      document.getElementsByTagName("HEAD")[0].appendChild( ss );
      ss.innerHTML = style;
    },


    extract_response : function ( text )
    {
      var j = 10;  
      var inbody = false;
      var r = { style : [], script : [], body : "" };
      
      var re_script = (/<script[^>]+src\s*=/gi);
      var re_style = (/<link[^>]+rel\s*=\s*("|')stylesheet\1/gi);
      
      var lines = text.split( '\r\n' );
      for ( var i in lines )
      {
        var line = lines[i];
        if ( inbody )
        {
          if ( line.match( /<\/body[^>]*>/i ) ) return r;
          r.body += "\n" + line;
        }
        else if ( line.match( re_style ) )
        {
          var re = /href\s*=\s*('|")([^>]*)\1/gi;
          var m = re.exec( line );
          if ( m && m.length > 2 ) r.style.push( m[2] );
          
          // reset  re's
          //re_style.test()
          re.test(""); 
        }
        else if ( line.match( re_script ) )  
        {
          var re = /src\s*=\s*('|")([^>]*)\1/gi;
          var m = re.exec( line );
          if ( m && m.length > 2 ) r.script.push( m[2] );
          
          // reset re's
          //re_script.test("");
          re.test("");
        }
        else if ( line.match( /<body[^>]*>/i ) )  
        {
          inbody = true;
        }
      }
      return r;
    },



  
    load_template : function ( template, callback, opt_id )
    {
      var i = template.lastIndexOf( "/" );
      var s = 1 + i ? template.slice( 0, 1 + i ) : "";
      var rel_path = s.replace( /[^.][^\/]*\/\.\.\//g, '/' );
      
      //libutil.log( template, i, s, rel_path );
      
      var http = this.request();

      http.open( "GET", template + "?&time=" + Date().toString(), true );
      
      var fix_path = function( i )
      {
        if ( rel_path && ! i.match( /^(\/|https?:\/\/)/ ) )
        {
          var r = rel_path + i;
        }
        else
        {
          var r = i.replace( /[^.][^\/]*\/\.\.\//g, '/' );
        }
        //libutil.log( i, " =>>> ", r );
        return r;
      };
      
      var that = this;
      http.onreadystatechange = function ()
      {

        if ( http.readyState == 4 )
        {
          //libutil.log_html( http.responseText );
          var r = that.extract_response( http.responseText, rel_path );

          for ( var i in r.style )
          {
            that.append_style_url( fix_path( r.style[ i ] ) );
          }

          var tb = document.getElementById( opt_id ? opt_id : 'template-body' );
          if ( tb ) tb.innerHTML = r.body;
          
          for ( var i in r.script )
          {
            that.append_script_url( fix_path( r.script[ i ] ) );
          }
          
          if ( callback ) callback();
        }
      };
      http.send(null);
    }
    
  };

