﻿/*
 * GigiJS library
 *
 * Author : GIRON Ronan
 * (c) VIGI Corporation
 */
 
GigiJS = function( path )
         {
          this.path = ( typeof path != "undefined" ? path : "/jscripts/GigiJS/" );
          
          this.head = document.getElementsByTagName( "head" ).item(0);
          this.body = null;
          this.nav = { ie:true, version:0 };
          
          // Init common module
          this.common.initModule();
         };

GigiJS.prototype =
{
 // Init the GigiJS Library
 init: function()
       {
        var self = this;
        // Init modules
        for( i=0 ; i<GigiJS.modulesIncluded.count() ; i++ )
        {
         if( GigiJS.modulesIncluded[i] != "common" )
         {
          try
          {
           if( eval( 'self.' + GigiJS.modulesIncluded[i] ) )
            if( eval( 'self.' + GigiJS.modulesIncluded[i] + '.initModule' ) )
             eval( 'self.' + GigiJS.modulesIncluded[i] + '.initModule();' );
          }
          catch(e) {}
          finally {}
         }
        }
        
        // Function $ : Shortcut of document.getElementById()
        window.$ = function( id )
                   {
                    var element = null;
                    
                    if( typeof id == "string" )
                     element = document.getElementById( id );
                    else
                     element = id;
                    
                    if( element )
                    {
                     // Init element with modules
                     for( i=0 ; i<GigiJS.modulesIncluded.count() ; i++ )
                     {
                      try
                      {
                       if( eval( 'self.' + GigiJS.modulesIncluded[i] ) )
                        if( eval( 'self.' + GigiJS.modulesIncluded[i] + '.initElement' ) )
                         eval( 'self.' + GigiJS.modulesIncluded[i] + '.initElement(element);' );
                      }
                      catch(e) {}
                      finally {}
                     }
                     
                     return element;
                    }
                    else
                    {
                     return false;
                    }
                   };
        
        window.include = function( file ){ self.include( file ); };
       },
 
 // Include a GigiJS module
 includeModule: function( moduleName )
                {
                 var self = this;
                 
                 if( this.head && GigiJS.modulesIncluded.find( moduleName ) === false )
                 {
                  if( navigator.appName != "Microsoft Internet Explorer" )
                  {
                   var js = document.createElement( "script" );
                   js.setAttribute( "language", "javascript" );
                   js.setAttribute( "type", "text/javascript" );
                   js.setAttribute( "src", this.path + "GigiJS." + moduleName + ".js" );
                   this.head.appendChild( js );
                  }
                  else
                  {
                   document.write( '<script type="text/javascript" src="' + this.path + "GigiJS." + moduleName + '.js"></script>' );
                  }
                  
                  GigiJS.modulesIncluded.push( moduleName );
                 }
                },
 
 // Include a JavaScript file
 include: function( file )
          {
           if( this.head && GigiJS.filesIncluded.find( file ) === false )
           {
            if( navigator.appName != "Microsoft Internet Explorer" )
            {
             var js = document.createElement( "script" );
             js.setAttribute( "language", "javascript" );
             js.setAttribute( "type", "text/javascript" );
             js.setAttribute( "src", file );
             this.head.appendChild( js );
            }
            else
            {
             document.write( '<script type="text/javascript" src="' + file + '"></script>' );
            }
            
            GigiJS.filesIncluded.push( file );
           }
          },
 
 // Error - common mudule missing
 errorCommon: function(){ alert( 'GigiJS Library - The common module must be loaded with GigiJS Library.' ); }
};

// Array of included files
GigiJS.filesIncluded = new Array();
GigiJS.modulesIncluded = new Array();

// Function $ : Shortcut of document.getElementById() in intern
GigiJS.$ = function( id )
           {
            if( document.getElementById( id ) )
             return document.getElementById( id );
            else
             return false;
           };

// Run GigiJS Library
var myGigiJSLib = null;
GigiJS.run = function( path )
             {
              myGigiJSLib = new GigiJS( path );
             };