 $(document).ready(function(){
     // remove all nowrap attributes within menu
     var normalCSS = {
        'white-space' : 'normal',
        'background-color' : '#D9D7D1',
        'border' : '1px solid #CFCCC6',
        //'border-left' : '1px solid #B8B6B1',
        //'border-top' : '0px',
        'border-collapse' : 'separate',
        'padding' : '3px 2px 3px 9px',
        'font-size' : '12px',
        'width' : '190px'
      }
      
     var parentCSS = {
        'background-color' : '#8c8b88'

     }
     var mouseoverCSS = {
         'background-color' : '#ffffff',
         'border' : '1px solid #CFCCC6',
         //'border-left' : '1px solid #B8B6B1',
        // 'border-top' : '0px',
         'border-collapse' : 'separate',
         'padding' : '3px 2px 3px 9px',
         'font-size' : '12px',
         'width' : '190px'
     }
     var hiddenCSS = {
         'visible' : 'none'
     }

     var linkCSS = {
         'color' : '#333'
     }

     var currentPageItem = {
        /* 'background-color' : '#8c8b88', */
         'font-weight' : 'bold'
         /*'border-bottom' : '1px solid #8c8b88',
         'border-top' : '0px'*/
     }

     var currentPageItemNav1 = {
         'background-color' : '#8c8b88',
         'font-weight' : 'bold'
         /*'border-bottom' : '1px solid #8c8b88',
         'border-top' : '0px'*/
     }

     var incidentLinkOverCSS = {
         /*'color' : '#000000'*/
     }

     var incidentLinkCSS = {
         'color' : '#ffffff'
     }

     var incidentPageItem = {
         'background-color' : '#e40812',
         'border-bottom' : '1px solid #e40812',
         'border-top' : '0px',
         'font-weight' : 'normal',
         'padding-left' : '20px'
     }
     var incidentPageOverItem = {
         'background-color' : '#ff0000',
         'border-bottom' : '1px solid #ff0000',
         'border-top' : '0px',
         'padding-left' : '20px'
     }

     var tableCSS = {
         'border' : '0px',
         'width' : '190px',
         'display' : 'inline-block'
     }

     // second level links
     var nav2 = {
        'white-space' : 'normal',
        'border' : '1px solid #CFCCC6',
        //'border-left' : '1px solid #B8B6B1',
        //'border-top' : '0px',
        'border-collapse' : 'separate',
        'padding' : '3px 2px 3px 15px',
        'font-size' : '12px',
        'width' : '190px',

         'background-color' : '#C4C1BC'
     }

     var nav2Over = {
         'background-color' : '#ffffff',
         'border' : '1px solid #CFCCC6',
         //'border-left' : '1px solid #B8B6B1',
        // 'border-top' : '0px',
         'border-collapse' : 'separate',
         'padding' : '3px 2px 3px 15px',
         'font-size' : '12px',
         'width' : '190px'
     }

     // third level links
     var nav3 = {
         'background-color' : '#f0eee8',
         'border' : '1px solid #CFCCC6',

        'white-space' : 'normal',

        'padding' : '3px 2px 3px 20px',
        'font-size' : '12px',
        'width' : '190px'
     }
     var nav3Over = {
         'background-color' : '#ffffff',
         'border' : '1px solid #CFCCC6',
         //'border-left' : '1px solid #B8B6B1',
        // 'border-top' : '0px',
         'border-collapse' : 'separate',
         'padding' : '3px 2px 3px 20px',
         'font-size' : '12px',
         'width' : '190px'

     }

     var uplinkOver = {
         'background' : 'url(images/up-dark.gif) center center no-repeat'
     }

     var uplink = {
         'background' : 'url(images/up-light.gif) center center no-repeat'
     }

     var flame = {
        'background' : 'url(images/flame.gif) center left no-repeat',
        'float' : 'left',
        'height' : '20px',
        'left' : '-10px',
        'position' : 'relative',
        'top' : '4px',
        'width' : '20px'
     }

     var flameover = {
        'background' : 'url(images/flame-over.gif) center left no-repeat',
        'float' : 'left',
        'height' : '20px',
        'left' : '-10px',
        'position' : 'relative',
        'top' : '4px',
        'width' : '20px'
     }


   $("#submenu-wrapper table").css(tableCSS);

   $("#submenu-wrapper table td").each(function() {
       if (jQuery(this).text() == '') {
           jQuery(this).remove();
       }
   });
   // empty cells have been removed, now remove empty rows
   $("#submenu-wrapper table tr").each(function() {
       if (jQuery(this).text() == '') {
           jQuery(this).remove();
       }
   });



   $("#submenu-wrapper table td a").css(linkCSS);

   $(".nav1").each(function() {
       jQuery(this).parent().css(normalCSS);
       jQuery(this).parent().mouseover(function() {
           jQuery(this).css(mouseoverCSS);
       });
       jQuery(this).parent().mouseout(function() {
           jQuery(this).css(normalCSS);
       });
   });

   $(".nav2").each(function() {
       jQuery(this).parent().css(nav2);
       jQuery(this).parent().mouseover(function() {
           jQuery(this).css(nav2Over);
       });
       jQuery(this).parent().mouseout(function() {
           jQuery(this).css(nav2);
       });
   });

   // third level links
   $(".nav").each(function() { // don't have a 3 on the end for some reason
       jQuery(this).parent().css(nav3);
       jQuery(this).parent().mouseover(function() {
           jQuery(this).css(nav3Over);
       });
       jQuery(this).parent().mouseout(function() {
           jQuery(this).css(nav3);
       });
   });

   
   // loop over again now that all the blanks have been removed
   $("#submenu-wrapper table td").each(function() {

       // print the class of this link and the class of the next
       var thisLinkClass = "";
       var nextLinkClass = "";
       jQuery(this).find('a').each(function() {
           thisLinkClass = jQuery(this).attr("class");
            jQuery(this).parent().parent().next('tr').find('td').each(function() {
                jQuery(this).find('a').each(function() {
                    nextLinkClass = jQuery(this).attr("class");
                });
            });

       });

       if (thisLinkClass == "nav1" && nextLinkClass == "nav2" ) {
           // this item is a parent level item
           jQuery(this).css(parentCSS);
           jQuery(this).mouseout(function(){
               jQuery(this).css(parentCSS);
           });
       }
   });



   // find the link to the current page and style that
   // in the same loop find the link to the incidents page and style that
   $("#submenu-wrapper table td a").each(function() {


      //jQuery(this).html(jQuery(this).text().replace(/^\s+|\s+$/g, '')); // remove &nbsp;
      jQuery(this).html(jQuery(this).text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '')); // remove &nbsp;
      var paramList = jQuery(this).attr("href").split("?")[1].split("&");
      for (i = 0; i < paramList.length; i++) {
          // current page
          if (paramList[i].split("=").length > 0 && paramList[i].split("=")[0] == 'pageId' ) {
              if (paramList[i].split("=")[1] == pageId) {

                  jQuery(this).parent().css(currentPageItem);
                  jQuery(this).parent().mouseout(function() {
                      jQuery(this).css(currentPageItem);
                  });

                  /*
                   * Is this item:
                   *    1. a nav1 with no children -
                   *    2. a nav1 with children -
                   *    3. a nav2
                   *    4. a nav
                   *    5. colCurrentBushfires
                   *
                   */

                  // add in the arrow to the right
                  var pageMarker = document.createElement('div');
                  if (jQuery(this).attr("class") == "nav1") {
                      jQuery(this).parent().css(currentPageItemNav1);
                      jQuery(this).parent().mouseout(function() {
                          jQuery(this).css(currentPageItemNav1);
                      });
                      nextLinkClass = '';
                      jQuery(this).parent().parent().next('tr').find('td').each(function() {
                          jQuery(this).find('a').each(function() {
                              nextLinkClass = jQuery(this).attr("class");
                          });
                      });
                      if (nextLinkClass == 'nav2') {
                        pageMarker.className = 'page-marker nav1Parent';
                      }
                      else {
                        pageMarker.className = 'page-marker nav1';
                      }
                  }
                  else if (jQuery(this).attr("class") == "nav2") {
                      pageMarker.className = 'page-marker nav2';
                  }
                  else if (jQuery(this).attr("class") == "nav") {
                      pageMarker.className = 'page-marker nav';
                  }
                  else
                    pageMarker.className = 'page-marker';

                  if (paramList[i].split("=")[1] == "colCurrentBushfires") {
                      pageMarker.className = 'page-marker red';
                  }
                  

                  jQuery(this).parent().prepend(pageMarker);
              }
              if (paramList[i].split("=")[1] == "colCurrentBushfires") {
                  jQuery(this).css(incidentLinkCSS);
                  jQuery(this).mouseover(function() {
                      jQuery(this).css(incidentLinkOverCSS);

                  });
                  jQuery(this).mouseout(function() {
                      jQuery(this).css(incidentLinkCSS);
                  });
                  jQuery(this).parent().css(incidentPageItem);
                  jQuery(this).parent().mouseover(function() {
                      jQuery(this).css(incidentPageOverItem);
                      $(".incident-marker").each(function() {
                        jQuery(this).css(flameover);
                      });
                  });
                  jQuery(this).parent().mouseout(function() {
                      jQuery(this).css(incidentPageItem);
                      $(".incident-marker").each(function() {
                        jQuery(this).css(flame);
                      });
                  });

                  var incidentMarker = document.createElement('div');
                  incidentMarker.className = 'incident-marker';

                  jQuery(this).parent().prepend(incidentMarker);


                  
              }
          }
      }


   });

   // inject div into submenu-wrapper for up link
 //  var uplinkDiv = document.createElement('div');
 //  uplinkDiv.className = "uplink";

//   $("#submenu-wrapper").prepend(uplinkDiv);

  /* $("#submenu-wrapper table tr").each(function() {
       var frontCell = document.createElement('td');
       frontCell.className = "uplink";

       jQuery(this).prepend(frontCell);

   });
*/
    if ($("#up-level-link").length > 0) {
       $(".uplink").each(function() {
          jQuery(this).css(uplink);
          jQuery(this).click(function() {
                window.location = $("#up-level-link").attr("href");
           });
          jQuery(this).mouseover(function() {
                jQuery(this).css(uplinkOver);
           });
          jQuery(this).mouseout(function() {
                jQuery(this).css(uplink);
           });
       });
    }


 });