/**
 * Public toggle function - toggles state of ul
 *
 * id - id of the ul to toggle
 */
function ocToggle( id ){
  //get toggle state
  if (  dojo.hasClass( id, 'oc_closed' ) )
    ocOpen( id );
  else
    ocClose( id );
}//end function toggle( id )

/**
 * Opens a given ul
 *
 * id - id of ul to open
 */
function ocOpen( id ) {
  _ocDoToggle( id, true );
  _ocUpdateCookie( id, true );
}//end function ocOpen( id )

/**
 * Closes a given ul
 *
 * id - id of ul to close
 */
function ocClose( id ) {
  _ocDoToggle( id, false );
  _ocUpdateCookie( id, false );
}//end function ocClose( id )

/**
 * Expands all of the ul items
 */

function ocOpenAll() {
  dojo.query( '[id^="oc_"]' ).forEach( "ocOpen( dojo.attr( item, 'id' ) );" );
}//end function ocOpenAll()

/**
 * Closes all of the ul items
 */
function ocCloseAll() {
  dojo.query( '[id^="oc_"]' ).forEach( "ocClose( dojo.attr( item, 'id' ) );" );
}//end function expandAll()

/**
 * Updates the default value of the cookie
 *
 * val - true if default to be open
 */
function ocUpdateDefault( val ) {
  var cookie = getCookie( site_code + '_oc' );
  cookie = '{d:' + ( val ? '1' : '0' ) + ',' + cookie.substring( 4 );
  setCookie( site_code + '_oc', cookie  );
}//end function updateDefault()

/**
 * Private function that updates the cookie
 *
 * id - id of ul
 * open - true if ul has been opened
 */
function _ocUpdateCookie( id, open ) {
//alert( site_code + '_oc' );
//alert( dojo.cookie( site_code + '_oc') );
  //get bitstring and index
  var cookie = getCookie( site_code + '_oc' );
  if ( cookie == null ) return;
  var o = dojo.fromJson( cookie );
//alert( o===undefined );
  var bs = o[page_code];
//alert( bs === undefined );
  var index = id.substring( 3 );
//alert( index === undefined );
  //update bitstring and store new cookie
  bs = bs.substring( 0, index ) + ( open ? '1' : '0' ) + bs.substring( ++index );
  o[page_code] = bs;
  setCookie( site_code + '_oc', dojo.toJson( o ) );
}//end function _updateCookie( id, open )

/**
 * Private function that performs the swapping of classes
 *
 * id - id of ul to toggle
 * open - true if opening ul
 */
function _ocDoToggle( id, open ) {
  dojo.removeClass( id, 'oc_' + ( open ? 'closed' : 'open' ) );
  dojo.addClass( id, 'oc_' + ( open ? 'open' : 'closed' ) );
}//end function _doToggle( open )

/**
 * Onload function to open uls that were previously opened
 * Creates default cookie if one doesn't exist
 */
dojo.addOnLoad(
  function() {
    //get nodes
    var nodes = dojo.query( '[id^="oc_"]');

    //if no nodes, quit
    if ( nodes.length == 0 )
      return;

    //get cookie
    var cookie = getCookie( site_code + '_oc' );

    //check for cookie existence
    if ( cookie === undefined || cookie == null ){

      //create default cookie
      cookie = "{d:0}";
      //dojo.cookie( site_code + '_oc', cookie, { path: '/' }  );
    }//end if ( cookie === undefined )

    //eval object and get bitstring
    var o = dojo.fromJson( cookie );
    var bs = o[ page_code ];

    //check that bitstring is defined
    if ( bs === undefined ) {
      bs = '';
      nodes.forEach(
        function( item ) {
          bs += dojo.hasClass( dojo.attr( item, 'id' ), "oc_open" ) ? '1' : '0';
        }//end nodes.forEach( function()
       );//end  nodes.forEach(
      o[ page_code ] = bs;
      setCookie( site_code + '_oc', dojo.toJson( o ) );
    }//end if ( bs === undefined )

    //check for default and all closed
    if ( o['d'] == 1 && bs == 0 ) {
      nodes.removeClass( 'oc_closed' );
      nodes.addClass( 'oc_open' );
    }//end if ( o['d'] == 1 && bs == 0 )
    else {
      //for each node
      nodes.forEach(
        function( item ){
          //get node id and bitstring index
          var id = dojo.attr( item, 'id' );
          var index = id.substring( 3 );

          //if open
          if ( bs.charAt( index ) == '1' )
            _ocDoToggle( id, true );
          else
            _ocDoToggle( id, false );
        }//end function( item )
      );//end nodes.forEach
    }//end } else
  }//end function()
);//end dojo.addOnLoad(
