/*
 * Module commun functions
 * part of the GigiJS library
 *
 * Author : GIRON Ronan
 * (c) VIGI Corporation
 */

GigiJS.prototype.common =
{
 initModule: function()
              {
               this.prototype.Array();
               this.prototype.String();
               this.prototype.Math();
               this.prototype.Window();
               this.prototype.general();
               
               GigiJS.modulesIncluded.push( "common" );
              }
};

GigiJS.prototype.common.prototype =
{
 // New functions for Array class
 Array: function()
         {
          // Find an value in the array
          Array.prototype.find = function( value )
                                 {
                                  var bReturn = false;
                                  for( var i in this )
                                   if( !isNaN( i ) && typeof this[i] != "undefined" && this[i] != null && this[i] == value )
                                   {
                                    bReturn = i;
                                    break;
                                   }
                                  return bReturn;
                                 };
          
          // Return the number of elements
          Array.prototype.count = function()
                                  {
                                   var nb = 0;
                                   for( var i in this )
                                    if( !isNaN( i ) ) nb++;
                                   return nb;
                                  };
          
          // Return an array without elements null and undefined
          Array.prototype.compact = function()
          {
           var newArray =  new Array;
           
           for( var i in this )
            if( !isNaN( i ) && typeof this[i] != "undefined" && this[i] != null )
             newArray.push( this[i] );
           
           return newArray;
          };
         },
 
 // New functions for String class
 String: function()
         {
          // Trim string
          String.prototype.trim = function(){ return this.replace( /^\s+/, "" ).replace( /\s+$/, "" ); };
          String.prototype.ltrim = function(){ return this.replace( /^\s+/, "" ); };
          String.prototype.rtrim = function(){ return this.replace( /\s+$/, "" ); };
          
          // Return if a string is find into other
          String.prototype.isFind = function( stringSought )
                                    {
                                     if( this.indexOf( stringSought ) == -1 )
                                      return false;
                                     else
                                      return true;
                                    };
          
          // Return true if the string is empty
          String.prototype.isEmpty = function()
                                     {
                                      if( this == null || this == '' )
                                       return true;
                                      else
                                       return false;
                                     };
         },
 
 // New functions for Math class
 Math: function()
       {
        // Interval function
        Math.interval = function( iStart, iEnd )
                        {
                         var nbReturn = 0;
                         
                         nbReturn = parseInt( ( Math.random() * ( iEnd - iStart ) ) + iStart );
                         
                         return nbReturn;
                        };
       },
 
 // New functions for Window class
 Window: function()
         {
          // Stop fade
          window.stopFade = function()
                            {
                             if( $('window_fade') )
                             {
                              
                             }
                            };
          
          // Fade in of the body
          window.fadeIn = function( color, timeToFade, functionAfter )
                          {
                           if( GigiJS.modulesIncluded.find( "effects" ) === false )
                           {
                            alert( "GigiJS Library - Module 'Effects' must be loaded !" );
                           }
                           else
                           {
                            var myBody = null;
                            
                            if( myBody = document.getElementsByTagName( "body" ).item(0) )
                            {
                             if( !$('window_fade') )
                             {
                              var myDiv = document.createElement( "div" );
                                  myDiv.style.backgroundColor = ( color == null ? "#000" : color );
                                  myDiv.setAttribute( "id", "window_fade" );
                                  myDiv.style.position = "absolute";
                                  myDiv.style.left = "0";
                                  myDiv.style.top = "0";
                                  myDiv.style.zIndex = "1000";
                                  if( window.browser.prototype.isLteIe6() )
                                  {
                                   myDiv.style.width = document.body.clientWidth + "px";
                                   myDiv.style.height = document.body.clientHeight + "px";
                                  }
                                  else
                                  {
                                   myDiv.style.width = "100%";
                                   myDiv.style.height = "100%";
                                  }
                                  document.documentElement.style.overflow = "hidden";
                              
                              myBody.insertBefore( myDiv, myBody.firstChild );
                              $('window_fade').setOpacity( 0 );
                             }
                             window.scrollTo( 0, 0 );
                             $('window_fade').fadeTo( ( timeToFade == null ? 1000 : parseInt( timeToFade ) ), 60, functionAfter );
                             
                             //$('window_fade').onclick = function(){ window.fadeOut( timeToFade ); };
                            }
                           }
                          };
          
          // Fade out of the body
          window.fadeOut = function( timeToFade, functionAfter )
                           {
                            if( GigiJS.modulesIncluded.find( "effects" ) === false )
                            {
                             alert( "GigiJS Library - Module 'Effects' must be loaded !" );
                            }
                            else
                            {
                             if( $('window_fade') )
                             {
                              var myBody = null;
                              
                              if( myBody = document.getElementsByTagName( "body" ).item(0) )
                              {
                               $('window_fade').fadeOut(
                                                        ( timeToFade == null ? 1000 : parseInt( timeToFade ) ),
                                                        function()
                                                        {
                                                         myBody.removeChild( $('window_fade') );
                                                         if( window.browser.prototype.isLteIe6() )
                                                          document.documentElement.style.overflow = "";
                                                         else
                                                          document.documentElement.style.overflow = "auto";
                                                         
                                                         if( functionAfter != null )
                                                          functionAfter();
                                                        }
                                                       );
                              }
                             }
                            }
                           };
         },
 
 general: function()
          {
           // Return the number of elements
           window.isArray = function( array )
                            {
                             if( array instanceof Array )
                              return true;
                             else
                              return false;
                            };
          }
};