
/* **** GENERAL FUNCTIONS **** */

function getExtraArguments( begin, args )
{
   result	= new Array()
   for ( var i = begin; i < args.length; i++ )
   {
      result.push( args[ i ] );
   }
   return result;
}


/* **** COOKIE FUNCTIONS **** */

function putIndexCookie( cookieName, i )
{
   if ( document.cookie != document.cookie )
   {
      index	= document.cookie.indexOf( cookieName );
   }
   else
   {
      index	= -1;
   }

   if ( index == -1 )
   {
      document.cookie	= cookieName + "=" + i + "; path=/";
   }
}

function getIndexCookie( cookieName, maxValue )
{
   if ( document.cookie )    // Cookies enabled
   {
      index	= document.cookie.indexOf( cookieName );
      if (index != -1)
      {
	 namestart	= ( document.cookie.indexOf( "=", index ) + 1 );
	 nameend	= document.cookie.indexOf( ";", index );

	 if ( nameend == -1 )
	 {
	    nameend	= document.cookie.length;
	 }

	 productIndex	= document.cookie.substring( namestart, nameend );

	 if ( productIndex >= 0 && productIndex < maxValue - 1 )    // Go to next item
	 {
	    return ++productIndex;
	 }
	 if ( productIndex >= maxValue - 1 )    // Go back to first item
	 {
	    return 0;
	 }

      }
   }

   // If cookies disabled or page being loaded for first time, so go to random item
   return Math.floor( Math.random() * maxValue );
}


/* **** OneOfSelector OBJECT **** */

documentSelectors	= new Array();

function setAllSelectors()
{
   for ( iSel = 0; iSel < documentSelectors.length; iSel++ )
   {
      documentSelectors[ iSel ].display();
   }
}

window.addOnload( setAllSelectors );


function OneOfSelector( cookieName )
{
   this.cookieName	= cookieName;
   this.divIds		= getExtraArguments( 1, arguments );	// Set divIds to array of all arguments after cookieName

   this.selection	= new Array();			// Different selections to display
   							// contains array of text for each <div>
   documentSelectors.push( this ) ;


   this.add = function ()
   {
      text	= getExtraArguments( 0, arguments );
      this.selection.push( text );
   }

   this.getSelectionIndex = function ()
   {
      var i	= 0;
      i	= getIndexCookie( this.cookieName, this.selection.length );
      putIndexCookie( this.cookieName, i );

      return i;
   }

   this.display = function ()
   {
      if( this.selection.length )
      {
	 var iSel	= this.getSelectionIndex();

	 for ( var iDiv = 0; iDiv < this.divIds.length; iDiv++ )
	 {
	    if ( element = document.getElementById( this.divIds[ iDiv ] ) )
	    {
	       element.innerHTML	= this.selection[ iSel ][ iDiv ];
	    }
	 }

	 if ( this.container.style.display == "none" )
	 {
	    this.container.style.display	= "";
	 }
      }
   }

   this.reveal = function ( id )
   {
      this.container	= document.getElementById ( id );
   }
}
