var timer_arr = Array(); // assc array to just hold timers, just to make it easier to maintain..
timer_arr["main_nav"] = false; // means no timers are running around for the main nav animations
$( document ).ready( function() {
  // SHOULD ONLY HAVE ONE WINDOW.RESIZE EVENT-HANDLER SO ITS HERE AT THE TOP
  $( window ).resize( function() {
    if ( $( "#project_modal" ).css( "display" ) == "block" ) {
      gpp_modal_rePos();
    }
  } );

  $( "#logo" ).click( function() {
    window.location = "/";
  } );

  // SHARE BUTTON
  $( "#share" ).addClass( "addthis_toolbox" ).addClass( "addthis_default_style" );
  // END ADD THIS CODE

  //MAIN HOME SLIDE NAV

  // on/rollover states for main slide nav items
  $( ".main_slide_nav_item" ).hover( function() {
    $( this ).addClass( "rollover" );
    $( this ).children( ".slide_arrow" ).addClass( "on" );
  }, function() {
    $( this ).removeClass( "rollover" );
    if ( !$( this ).hasClass( "on" ) ) {
      $( this ).children( ".slide_arrow" ).removeClass( "on" );
    }
  } );
  /*var main_slider_nav_width = 0;
  $( ".main_slide_nav_item" ).each( function() {
      main_slider_nav_width += parseInt( total_width( $( this ) ) );
  } );
  $( "#main_slide_nav" ).css( "padding-left", ( parseInt( $( "#main_slide_nav" ).parent().width() ) - main_slider_nav_width ) / 2 + "px" );
  $( window ).load( function() { // webkit fix
    var main_slider_nav_width_again = 0;
    $( ".main_slide_nav_item" ).each( function() {
        main_slider_nav_width_again += parseInt( total_width( $( this ) ) );
    } );
    $( "#main_slide_nav" ).css( "padding-left", ( parseInt( $( "#main_slide_nav" ).parent().width() ) - main_slider_nav_width_again ) / 2 + "px" );
  } );*/

  var numDots = $('.nav_dot').length;
  $('#main_slide_nav').css('width', parseInt((numDots * 24) + 26) + 'px');

  $( ".nav_dot" ).hover( function() {
    if ( !$( this ).hasClass( "on" ) ) {
      $( this ).addClass( "rollover" );
    }
  }, function() {
    $( this ).removeClass( "rollover" );
  } );

  // SEARCH BOX
  $('#search_box').focus(function(){
      if ($(this).val() == 'Search Green Riverside') {
          $(this).val('');
      }
  });
    $('#search_box').blur(function(){
      if ($(this).val() == '') {
          $(this).val('Search Green Riverside');
      }
  });

  // REBATE DROPDOWN CAROUSEL & MINI-CAROUSEL ONSTATES & MODAL NAV
  $('#header_carousel_left_nav, #header_carousel_right_nav, .mini-carousel_left_nav, .mini-carousel_right_nav, #modal_right_nav, #modal_left_nav').hover(
      function(){
          $(this).addClass('on');
      },
      function(){
          $(this).removeClass('on');
      }
  );

  /* NEWS AND EVENTS BOXES */

  if ($('.news_box_items').size()) {
      $('.box .news_box_items').customScroller({
          width:      462,
          height:     201,
          horizontal: 1,
          vertical:   1,
          speed:      4
      });
      $('.box_small .news_box_items').customScroller({
          width:      218,
          height:     293,
          horizontal: 1,
          vertical:   1,
          speed:      4
      });
  }


  /* VSS (Vertical Slide Show)
  $('.vss_selector_wrapper').hover(
      function() {
          if (!$(this).hasClass('on')) {
              $(this).addClass('on');
              $(this).children('.vss_selector').children('img').toggleClass('on');
          }
      },
      function() {
          if ($(this).hasClass('on')) {
              $(this).removeClass('on');
              $(this).children('.vss_selector').children('img').toggleClass('on');
          }
      }
  );*/
  $( "#vss" ).height( $( ".vss_selector" ).size() * $( ".vss_selector" ).height() );

  /*$('#vss .slide_img').height(  $('.vss_selector').size() * $('.vss_selector').height()  );*/

  /*subnav item vertical alignment adjustment due to text wrapping */
  $('.subnav_label').each(function(){
      var pad = 30 - ($(this).height() / 2) + 'px';
      $(this).css('padding-top', pad);
      $(this).css('padding-bottom', pad);
  });

    /* Map Filter Accordio. */
    $('.has_fold .filter_category_title, .has_fold .filter_accordion_button').click(function () {
        $(this).parent('.has_fold').siblings('.fold').slideToggle(200);
        $(this).parent('.has_fold').children('.filter_accordion_button').toggleClass('on');
    });




    /* External Links
    *
    *  example: <a href="http://www.example.com/" rel="external">Click Here</a>
    *
    * */
    $( "a[rel=external]" ).click( function() {
      this.target = "_blank";
    } );

  gsl_slider_init( $( ".slider_gsl_2" ).find( ".spotlight_item_slider" ), 2, 2 );
  gsl_slider_init( $( ".slider_gsl_4" ).find( ".spotlight_item_slider" ), 2, 4 );
  rebate_carousel( $( "#header_carousel" ), 5, 2, ($( ".slider_rb_3" ).length > 0 ? $( ".slider_rb_3" ) : null), 3, 1 );

  $( "#search_box" ).live( "keypress", function( e ) {
    if ( e.keyCode == 13 || e.charCode == 13 ) {
      return search_form_submit();
    }
    return true;
  } );
  $( "#search_button" ).live( "click", function() {
    search_form_submit();
  } );

  gpp_modal_init(); // green power project page setup
  news_init(); // news and events (and press) setup
  contact_us_init();
  init_header_nav();
} );

function self_trim( obj ) {
  $( obj ).val( $.trim( $( obj ).val() ) );
}

function gpp_modal_rePos() {
  $( "#project_modal_box" ).css( {
    "margin-top" : $( window ).height() / 2 - $( "#project_modal_mid" ).height() / 2 - 43 + "px"
  } );
}

function gpp_modal_init() { /* Green Power Projects */
  var gpp_count = 0;
  var gpp1 = 0; // height, SideA
  var gpp2 = 0; // height, SideB
  $( ".project" ).each( function() {
    if ( gpp_count % 2 == 0 ) {
      gpp1 = total_height( $( ".project_img" )[ gpp_count ] ) + total_height( $( ".project_title" )[ gpp_count ] ) + total_height( $( ".project_link" )[ gpp_count ] );
    } else {
      gpp2 = total_height( $( ".project_img" )[ gpp_count ] ) + total_height( $( ".project_title" )[ gpp_count ] ) + total_height( $( ".project_link" )[ gpp_count ] );
      if ( gpp1 != gpp2 ) {
        $( $( ".project" )[ gpp_count - 1 ] ).css( {
          "height" : (gpp1 > gpp2 ? gpp1 : gpp2) + 17 + "px"
        } );
        $( $( ".project" )[ gpp_count ] ).css( {
          "height" : (gpp1 > gpp2 ? gpp1 : gpp2) + 17 + "px"
        } );
      }
    }
    ++gpp_count;
  } );

  var active_modal_idx = 1; // Not zero based -- upon showing the modal, this is reset to some int..
  var active_thumb_idx = 0; // current thumbnail used...
  var gpp_modal_width = 790; // px    //844
  var ani_time = 500; // miliseconds
  var real_modal_length = $( ".modal_content" ).length;
  var is_animating = false;
  var is_fading = false;
  var fade_time = 500; // ms
  var thumb_idx_arr = Array(); // thumb_idx_arr[ active_modal_idx ] to access active_thumb_idx

  $( "#project_modal" ).height( $( document ).height() );

  // close the modal
  $( ".modal_close" ).click( function() {
    $( "#project_modal" ).css( {
      "display" : "none"
    } );
  } );

  $( ".project_img" ).live( "click", function() {
    $( this ).closest( ".project" ).find( ".project_link" ).click();
  } );

  // open a project modal
  $( ".project_link" ).click( function() {
    active_modal_idx = 1 + parseInt( $( this ).attr( "class" ).substr( $( this ).attr( "class" ).indexOf( "gpp_pop_idx_" ) + 12 ) );
    $( "#project_modal" ).css( {
      "display" : "block"
    } );
    $( "#project_modal_mid, #gpp_caro_box" ).height( $( $( ".modal_content" )[ active_modal_idx - 1 ] ).height() + 26 + "px" );
    $( "#gpp_carousel" ).css( {
      "left" : 0 - gpp_modal_width - gpp_modal_width * (active_modal_idx - 1) + "px"
    } );
    $( "#project_modal_box" ).css( {
      "height" : $( "#modal_fixed" ).height() + "px"
    } );
    ++active_modal_idx;
    $( "#modal_fixed" ).css( {
      "position" : "static"
    } );
    $( "#modal_fixed" ).css( {
      "position" : "fixed"
    } );
    gpp_modal_rePos();
  } );

  // align the thumbnails (css/js trickery) -- setting up the layout
  $( ".modal_thumbs" ).each( function() { // all are grey, active means color.
    var mt_idx = 0;
    $( this ).find( ".modal_thumb" ).each( function() { // B&W, then color
      $( this ).css( {
        "left" : parseInt( mt_idx / 2 ) * 60 + "px",
        "display" : mt_idx % 2 == 1 ? "none" : "block"
      } );
      ++mt_idx;
    } );
    $( this ).find( ".modal_thumb:first" ).css( { // turn the first thumb to "on" (hide the black and white, then show the color)
      "display" : "none"
    } ).next().css( { // turn it's color equiv to "off"
      "display" : "block"
    } );
  } );

  $( ".modal_left" ).each( function() { // set up the fading effect, start off with the rest of the main img's as invisible
    $( this ).children( "img:not(:first)" ).css( { // leaves the first main img of each content modal in-tact
      "display" : "none"
    } );
  } );

  $( "#project_modal" ).css( { // chrome bug-fix
    "position" : "fixed",
    "height" : "100%"
  } );

  // no more layout additions beyound this point
  // the code below, relies on the layout being in a certain structure
  var fake_first = $( ".modal_content:last" );
  var fake_last = $( ".modal_content:first" );
  $( "#gpp_carousel div:last" ).remove(); // the clear
  $( "#gpp_carousel" ).prepend( '<div id="fake_first_pannel">' + $( fake_first ).html() + "</div>" );
  $( "#gpp_carousel" ).append( '<div id="fake_last_pannel">' + $( fake_last ).html() + "</div>" );
  $( "#gpp_carousel" ).append( '<div class="clear"></div>' );

  // change the photo on the project modal
  $( ".modal_thumb" ).live( "click", function() {
    if ( thumb_idx_arr[ active_modal_idx ] == undefined ) {
      thumb_idx_arr[ active_modal_idx ] = 0;
    }
    if ( !is_fading ) {
      var my_idx = parseInt( $( this ).attr( "class" ).substr( $( this ).attr( "class" ).indexOf( "thumb_idx_") + 10 ) );
      if ( thumb_idx_arr[ active_modal_idx ] != my_idx ) {
        // the black & white thumbnail, and the color one, have the same idx value.
        is_fading = true;
        // turn on "clicked on" thumb
        $( this ).css( { // click on B&W, turn color on
          "display" : "none"
        } ).next().css( { // turn black and white thumb to "on"
          "display" : "block"
        } );
        // change bg photo
        // fade out old idx,
        // fade in new idx
        $( this ).closest( ".modal_left" ).find( ".mod_img_idx_" + thumb_idx_arr[ active_modal_idx ] ).fadeOut( fade_time );
        $( this ).closest( ".modal_left" ).find( ".mod_img_idx_" + my_idx ).fadeIn( fade_time, function() {
          is_fading = false;
        } );
        // turn off previous thumb
        $( this ).parent().find( ".thumb_idx_" + thumb_idx_arr[ active_modal_idx ] + ":first" ).css( {
          "display" : "block"
        } ).next().css( {
          "display" : "none"
        } );
        //active_thumb_idx = my_idx;
        thumb_idx_arr[ active_modal_idx ] = my_idx;
      }
    }
  } );

  $( "#modal_left_nav" ).live( "click", function() {
    if ( is_animating == false ) {
      is_animating = true;
      --active_modal_idx; // if less than zero, we see a cloned copy of what used to be the last content pannel..
      $( "#gpp_carousel" ).animate( {
        "left" : 0 - gpp_modal_width * (active_modal_idx - 1) + "px"
      }, ani_time, function() {
        if ( active_modal_idx == 1 ) { // if we're sitting on a cloned pannel
          active_modal_idx = real_modal_length + 1; // then move the user off to the "real" one (simulate wrap-around)
          $( "#gpp_carousel" ).css( {
            "left" : 0 - gpp_modal_width * (active_modal_idx - 1) + "px"
          } );
        }
        is_animating = false;
      } );
      var the_new_height = $( $( ".modal_content" )[ (active_modal_idx - 2 + real_modal_length) % real_modal_length ] ).height() + 26;
      $( "#project_modal_mid, #gpp_caro_box" ).animate( {
        "height" : the_new_height + "px"
      }, ani_time );
      $( "#project_modal_box" ).animate( {
        "margin-top" : $( window ).height() / 2 - the_new_height / 2 - 43 + "px"
      }, ani_time );
    }
  } );

  $( "#modal_right_nav" ).live( "click", function() {
    if ( is_animating == false ) {
      is_animating = true;
      ++active_modal_idx;
      $( "#gpp_carousel" ).animate( {
        "left" : 0 - gpp_modal_width * (active_modal_idx - 1) + "px"
      }, ani_time, function() {
        if ( active_modal_idx - 1 > real_modal_length ) {
          active_modal_idx = 2;
          $( "#gpp_carousel" ).css( {
            "left" : 0 - gpp_modal_width + "px"
          } );
        }
        is_animating = false;
      } );
      var the_new_height = $( $( ".modal_content" )[ (active_modal_idx - 2 + real_modal_length) % real_modal_length ] ).height() + 26;
      $( "#project_modal_mid, #gpp_caro_box" ).animate( {
        "height" : the_new_height + "px"
      }, ani_time );
      $( "#project_modal_box" ).animate( {
        "margin-top" : $( window ).height() / 2 - the_new_height / 2 - 43 + "px"
      }, ani_time );
    }
  } );
  $( "#gpp_proj_" + $( ".hidden_data" ).text() ).click();

  // img preloads
  if ( $( "#modal_fixed" ).length > 0 ) { // remember that these init functions launch, always, on every page..
    // so don't burden pages, that don't use these img's...
    pl_img( "/images/modal_top.png" );
    pl_img( "/images/modal_mid.png" );
    pl_img( "/images/modal_base.png" );
    $( ".modal_thumbs img:odd" ).each( function() {
      pl_img( "/includes/img_scaler.php?data=0|450|450|0|" + $( $( this ).attr( "src" ).split( "|" ) ).last()[0] );
    } );
  }
}

function str_arr_unique( arr ) {
  arr.sort();
  var arr2 = Array();
  arr2.push( arr[0] );
  for ( var x = 1; x < arr.length; ++x ) {
    if ( arr[x] != arr[x - 1] ) {
      arr2.push( arr[x] );
    }
  }
  return arr2;
}

function build_img_loader() {
  if ( $( "#img_loader" ).length < 1 ) {
    $( "body" ).append( '<div id="img_loader"></div>' );
  }
  $( "#img_loader" ).css( {
    "position" : "absolute",
    "height" : "1px",
    "width" : "1px",
    "top" : "-1000px",
    "left" : "-1000px",
    "overflow" : "hidden",
    "display" : "block"
  } );
}

var pl_img_count = 0;
function pl_img( img ) { // preload an img
  if ( $( "#img_loader" ).length < 1 ) {
    build_img_loader();
  }
  $( "#img_loader" ).append( '<img alt="" id="img_pl_' + pl_img_count++ + '" src="' + img + '" />' );
}

function news_init() {
  var ani_time = 250; // ms
  var is_animating = false;
  var active_thumb_idx = 0;
  $( ".nv_image_thumbs" ).each( function() { // all are grey, active means color.
    var mt_idx = 0;
    $( this ).find( ".nv_image_thumb" ).each( function() { // B&W, then color
      $( this ).css( {
        "left" : parseInt( mt_idx / 2 ) * 60 + "px",
        "display" : mt_idx % 2 == 1 ? "none" : "block"
      } );
      ++mt_idx;
    } );
    $( this ).find( ".nv_image_thumb:first" ).css( { // turn the first thumb to "on"
      "display" : "none"
    } ).next().css( { // turn it's color equiv to "off"
      "display" : "block"
    } );
  } );

  $( ".nv_image_thumbs img:odd" ).each( function() {
    pl_img( $( $( this ).attr( "src" ).split( "|" ) ).last() );
  } );

  $( ".nv_image_thumb" ).live( "click", function() {
    var this_thumb_img = $( this )
    var my_idx = parseInt( $( this ).attr( "class" ).substr( $( this ).attr( "class" ).indexOf( "thumb_idx_") + 10 ) );
    if ( !is_animating && thumb_idx_arr[ active_modal_idx ] != my_idx ) { // the black & white thumbnail, and the color one, have the same idx value.
      is_animating = true;
      // turn on "clicked on" thumb
      $( this ).css( { // click on B&W, turn color on
        "display" : "none"
      } ).next().css( { // turn black and white thumb to "on"
        "display" : "block"
      } );
      // change bg photo
// need to re-write this so it can cross fade instead of vanish & come back
      var new_img_src = $( this_thumb_img ).find( "img" ).attr( "src" ).split( "|" );
      new_img_src = $( new_img_src )[ $( new_img_src ).length - 1 ];
      var the_img = $( this_thumb_img ).parent().parent().find( ".nv_image" ).find( "img" );
      $( the_img ).fadeTo( ani_time, 0.1, function() {
        $( the_img ).attr( "src", new_img_src );
        // turn off previous thumb
        $( this_thumb_img ).parent().find( ".thumb_idx_" + thumb_idx_arr[ active_modal_idx ] + ":first" ).css( {
          "display" : "block"
        } ).next().css( {
          "display" : "none"
        } );
        thumb_idx_arr[ active_modal_idx ] = my_idx;
        $( the_img ).fadeTo( ani_time, 1 );
        is_animating = false;
      } );
    }
  } );
  $( ".nv_image_thumbs img:odd" ).each( function() {
    pl_img( $( $( this ).attr( "src" ).split( "|" ) ).last()[0] );
  } );
}

function search_form_submit() {
  $( "#search_box" ).val( $.trim( $( "#search_box" ).val() ) );
  $( "#search_box" ).blur();
  if ( $( "#search_box" ).val() != "" && $( "#search_box" ).val() != "Search Green Riverside" ) {
    $( "#search_wrapper" ).find( "form" )[0].submit();
  } else {
    return false;
  }
  return true;
}

function total_width( obj ) { // clone of jquery's outerWidth() but IE can actually use this one one..
  return parseInt( $( obj ).css( "margin-left" ) ) + parseInt( $( obj ).css( "margin-right" ) ) + parseInt( $( obj ).css( "padding-left" ) ) + parseInt( $( obj ).css( "padding-right" ) ) + parseInt( $( obj ).width() );
}

function total_height( obj ) {
  return parseInt( $( obj ).css( "margin-top" ) ) + parseInt( $( obj ).css( "margin-bottom" ) ) + parseInt( $( obj ).css( "padding-top" ) ) + parseInt( $( obj ).css( "padding-bottom" ) ) + parseInt( $( obj ).height() );
}

function gsl_slider_init( obj, slide_by, amount_required ) { // obj => the slider, the large div that holds all the items.
  var gsl_items = $( obj ).find( ".spotlight_item" );
  if ( gsl_items.length < amount_required ) {
    // change css to show the carasoul is not usable.
    return;
  }
  // set up the carousal to have (at least) twice as many item
  $( $( obj ).find( "div[class='clear']" ) ).remove(); // the div-clear at the very end...
  $( gsl_items ).each( function() { // clone the items.
    $( obj ).append( '<div class="spotlight_item">' + $( this ).html() + '</div>', '<div class="spotlight_separator"></div>' );
  } );
  $( gsl_items ).each( function() { // clone the items again.
    $( obj ).append( '<div class="spotlight_item">' + $( this ).html() + '</div>', '<div class="spotlight_separator"></div>' );
  } ); // do not use .append( this ) because it will "move", not clone.   "this" returns a reference, not a value
  $( obj ).append( '<div class="clear"></div>' ); // done cloning
  var unit_width = total_width( $( obj ).find( ".spotlight_separator" )[0] ) + total_width( $( gsl_items )[0] );
  var gsl_idx = 0;
  var gsl_size = gsl_items.length * 2; // this gives us room to play with. since we actually have 3x and not 2x
  var is_animating = false;
  var ani_speed = 1000; // mili sec's
  // Nav UI
  $( obj ).parent().prev().find( ".spotlight_nav_right" ).click( function() {
    if ( is_animating != true ) {
      is_animating = true;
      if ( gsl_idx + slide_by + amount_required - 1 > gsl_size - 1 ) { // if about to overflow
        gsl_idx = gsl_idx - gsl_size / 2; // the math revolves around the "x2" type of setup, but we really have x3. (as intended)
        $( obj ).css( { // don't animate this
          "left" : 0 - unit_width * gsl_idx + "px"
        } );
      }
      gsl_idx += slide_by;
      $( obj ).animate( { // animate this
        "left" : 0 - unit_width * gsl_idx + "px"
      }, ani_speed, function() {
        is_animating = false;
      } );
    }
  } );

  $( obj ).parent().prev().find( ".spotlight_nav_left" ).click( function() {
    if ( is_animating != true ) {
      is_animating = true;
      if ( gsl_idx - slide_by - amount_required < 0 ) { // if about to underflow
        gsl_idx = gsl_idx + gsl_size / 2;
        $( obj ).css( { // don't animate this
          "left" : 0 - unit_width * gsl_idx + "px"
        } );
      }
      gsl_idx -= slide_by;
      $( obj ).animate( { // animate this
        "left" : 0 - unit_width * gsl_idx + "px"
      }, ani_speed, function() {
        is_animating = false;
      } );
    }
  } );
}


// you must call this like:  $( "some_id_or_class" ).length == 0 ? null : $( "some_id_or_class" )[0]
// because the defaults can't be specifieid:  obj_mini = null, param = 0, ...
function rebate_carousel( obj_nav, req_nav_amount, nav_slide_by, obj_mini, req_mini_amount, mini_slide_by ) {
/// ### currently not set up to handle multiple-mini-nav's on the same page (as an array) ### ///
// also, this ins't realllyyy only for "rebates" it's just for the mini carousel really..
  var rb_nav_idx_res = 0; // nav residential index
  var rb_nav_idx_com = 0; // commerical index
  var rb_mini_idx_res = 0; // mini carousel residential index
  var rb_mini_idx_com = 0; // mini com index
  var is_animating = false; // for when we are watching left and right navigation occur
  var ani_time = 500; // milisec's to move the carousels...  ..currently fading is set to 750 ms
  var is_fading = false; // for when we are watching commercial and residential fade in and out.
  var fade_time = 300;
  var com_res_mode = true; // true for residential, false for commerical mode.
  var mini_res_btn = null;
  var mini_com_btn = null; // these will be the references to the mini carousel's residential and commerical fitlers...if they're around..

  var nav_is_animating = false; // if the nav carousel is moving....
  var nav_res_slider = $( obj_nav ).find( ".rb_slider_residential" ); // residential container to move around
  var nav_res_items = $( nav_res_slider ).find( ".header_carousel_item" ); // the data in the slider to duplicate, if we have enough...
  var active_nav_res = nav_res_items.length >= req_nav_amount; // if the residential has enough items to become a carousel
  var nav_res_idx_max = nav_res_items.length * 2;
  var nav_com_slider = $( obj_nav ).find( ".rb_slider_commercial" ); // commercial slider
  var nav_com_items = $( nav_com_slider ).find( ".header_carousel_item" ); // com items
  var active_nav_com = nav_com_items.length >= req_nav_amount; // if the commercial slider has enough to be a carousel
  var nav_com_idx_max = nav_com_items.length * 2;
  var nav_item_width = active_nav_res ? parseInt( $( $( nav_res_items )[0] ).css( "width" ) ) : active_nav_com ? parseInt( $( $( nav_com_items )[0] ).css( "width" ) ) : 186;
  var hidden_res_sep = null;
  var hidden_com_sep = null; // the dotted seperator between items..  tracking who to hide and unhide, etc..

// code for the mini carousel
  if ( obj_mini != null && obj_mini != undefined ) { // if on the home page..

    // the rebate mini-carousel residential slider
    var mini_res_slider = $( obj_mini ).find( ".rb_slider_residential" );

    // the mini res items
    var mini_res_items = $( mini_res_slider ).find( ".mini-carousel_item" );

    // the rebate mini-carousel commercial slider
    var mini_com_slider = $( obj_mini ).find( ".rb_slider_commercial" );

    // the mini comm items
    var mini_com_items = $( mini_com_slider ).find( ".mini-carousel_item" );

    // if res has enough items to move and wrap..
    var active_mini_res = mini_res_items.length >= req_mini_amount;

    // if comm has enough items to move and wrap
    var active_mini_com = mini_com_items.length >= req_mini_amount; // same with commercial..

    // width of each item for the math... (must be the same for both sliders for the mini rb)
    var rb_mini_item_width = active_mini_res ? $( $( mini_res_items )[0] ).width() : active_mini_com ? $( $( mini_com_items )[0] ).width() : 0;

    // the reported lengths of the arrays to meddle with...
    var mini_res_idx_max = mini_res_items.length * 2; // we're actually gonna put x3 as many items in..
    var mini_com_idx_max = mini_com_items.length * 2; // so the animation work when we hit the edges of the 2nd "set"

    // the buttons that move the mini carousel
    var mini_left = $( obj_mini ).parent().find( ".mini-carousel_left_nav" );
    var mini_right = $( obj_mini ).parent().find( ".mini-carousel_right_nav" );

    mini_res_btn = $( obj_mini ).parent().prev().find( ".mini-carousel_filter_residential" );
    mini_com_btn = $( obj_mini ).parent().prev().find( ".mini-carousel_filter_commercial" )

    // replicate the items to have enough to play with.
    if ( active_mini_res ) { // only replicate if we have enough items.. or it'll look horrible
      $( mini_res_slider ).find( 'div[class="clear"]' ).remove();
      $( mini_res_items ).each( function() {
        $( mini_res_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_res_items ).each( function() {
        $( mini_res_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_res_items ).each( function() {
        $( mini_res_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_res_slider ).append( '<div class="clear"></div>' );
    }

    // do it for commercial now
    if ( active_mini_com ) {
      $( mini_com_slider ).find( 'div[class="clear"]' ).remove();
      $( mini_com_items ).each( function() {
        $( mini_com_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_com_items ).each( function() {
        $( mini_com_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_com_items ).each( function() {
        $( mini_com_slider ).append( '<div class="mini-carousel_item">' + $( this ).html() + '</div>' );
      } );
      $( mini_com_slider ).append( '<div class="clear"></div>' );
    }

    // now we have x3 as many items as there would of been.
    // but we use math as though we had x2 as many, so that certain animations,
    // under certain conditions, don't make it show things disapear of vanish suddenly.. then re-appear. etc..

    // slide the mini-carousel
    $( mini_right ).click( function() {
      if ( is_animating != true ) {
        if ( active_mini_res && com_res_mode ) { // use residential idx
          is_animating = true;
          if ( rb_mini_idx_res + mini_slide_by + req_mini_amount - 1 > mini_res_idx_max - 1 ) { // if about to overflow
            rb_mini_idx_res -= mini_res_idx_max / 2; // move the slider near the begining, sit it on an identical icon.
            $( mini_res_slider ).css( { // don't animate this
              "left" : 0 - rb_mini_item_width * rb_mini_idx_res + "px"
            } );
          }
          rb_mini_idx_res += mini_slide_by;
          //animate the mini carousel
          $( mini_res_slider ).animate( {
            "left" : 0 - rb_mini_item_width * rb_mini_idx_res + "px"
          }, ani_time, function() {
            is_animating = false;
          } );
        } else if ( active_mini_com && !com_res_mode ) { // uses the commercial index if we have enough
          is_animating = true;
          // do the math
          if ( rb_mini_idx_com + mini_slide_by + req_mini_amount - 1 > mini_com_idx_max - 1 ) { // if about to overflow
            rb_mini_idx_com -= mini_com_idx_max / 2; // move slider to a visually identical spot that's near the begining.
            $( mini_com_slider ).css( { // don't animate this
              "left" : 0 - rb_mini_item_width * rb_mini_idx_com + "px"
            } );
          }
          rb_mini_idx_com += mini_slide_by;
          // move the mini carousel
          $( mini_com_slider ).animate( {
            "left" : 0 - rb_mini_item_width * rb_mini_idx_com + "px"
          }, ani_time, function() {
            is_animating = false;
          } );
        }
      }
    } );

    // slide the mini-carousel
    $( mini_left ).click( function() {
      if ( is_animating != true ) {
        if ( active_mini_res && com_res_mode ) { // use residential idx
          is_animating = true;
          if ( rb_mini_idx_res - mini_slide_by - req_mini_amount < 0 ) { // if about to underflow
            rb_mini_idx_res += mini_res_idx_max / 2; // move the slider up to a visually identical point.
            $( mini_res_slider ).css( { // don't animate this
              "left" : 0 - rb_mini_item_width * rb_mini_idx_res + "px"
            } );
          }
          rb_mini_idx_res -= mini_slide_by;
          //animate the mini carousel
          $( mini_res_slider ).animate( {
            "left" : 0 - rb_mini_item_width * rb_mini_idx_res + "px"
          }, ani_time, function() {
            is_animating = false;
          } );
        } else if ( active_mini_com && !com_res_mode ) { // uses commercial index instead
          is_animating = true;
          if ( rb_mini_idx_com - mini_slide_by - req_mini_amount < 0 ) { // if about to underflow
            rb_mini_idx_com += mini_com_idx_max / 2; // move the slider up to a visually identical point.
            $( mini_com_slider ).css( { // don't animate this
              "left" : 0 - rb_mini_item_width * rb_mini_idx_com + "px"
            } );
          }
          rb_mini_idx_com -= mini_slide_by;
          $( mini_com_slider ).animate( {
            "left" : 0 - rb_mini_item_width * rb_mini_idx_com + "px"
          }, ani_time, function() {
            is_animating = false;
          } );
        }
      }
    } );

    // click on res, change the state to residential. also fades in and out
    $( mini_res_btn ).click( function() {
      if ( !is_fading && !com_res_mode ) { // if not animating, and not already on residential
        is_fading = true;
        com_res_mode = true;
        $( this ).addClass( "on" );
        $( this ).parent().find( ".mini-carousel_filter_commercial" ).removeClass( "on" );
        $( "#rebates_drop_hideout" ).find( ".rebate_header_residential" ).addClass( "on" );
        $( "#rebates_drop_hideout" ).find( ".rebate_header_commercial" ).removeClass( "on" );
        $( "#header_carousel_left_nav, #header_carousel_right_nav" ).css( {
          "visibility" : active_nav_res ? "visible" : "hidden"
        } );
        $( obj_mini ).parent().find( ".mini-carousel_left_nav, .mini-carousel_right_nav" ).css( {
          "visibility" : active_mini_res ? "visible" : "hidden"
        } );
        setTimeout( function() {
          $( ".rb_slider_residential, .rb_mini_listings_res" ).fadeIn( fade_time );
          setTimeout( function() {
            is_fading = false;
          }, fade_time );
        }, parseInt( fade_time / 2 ) );
        $( ".rb_slider_commercial" ).fadeOut( fade_time, function() {
          $( this ).css( { // if the parent/ancestor element is not visible, fadeOut does nothing
            "display" : "none"
          } ); // so this is to fix it so that jquery actually listens to me, always..
        } ); // fadeIn does not seem to have this problem though, go figure.
        $( ".rb_mini_listings_com" ).fadeOut( fade_time, function() {
          $( this ).css( {
            "display" : "none"
          } );
        } );
      }
    } );

    // click on com, change the state to commercial.
    $( mini_com_btn ).click( function() {
      if ( !is_fading && com_res_mode ) { // if not animating, and mode was residential, then switch to commerical
        is_fading = true;
        com_res_mode = false;
        $( this ).addClass( "on" );
        $( this ).parent().find( ".mini-carousel_filter_residential" ).removeClass( "on" );
        $( "#rebates_drop_hideout" ).find( ".rebate_header_commercial" ).addClass( "on" );
        $( "#rebates_drop_hideout" ).find( ".rebate_header_residential" ).removeClass( "on" );
        $( "#header_carousel_left_nav, #header_carousel_right_nav" ).css( {
          "visibility" : active_nav_com ? "visible" : "hidden"
        } );
        $( obj_mini ).parent().find( ".mini-carousel_left_nav, .mini-carousel_right_nav" ).css( {
          "visibility" : active_mini_com ? "visible" : "hidden"
        } );
        setTimeout( function() {
          $( ".rb_slider_commercial, .rb_mini_listings_com" ).fadeIn( fade_time );
          setTimeout( function() {
            is_fading = false;
          }, fade_time );
        }, parseInt( fade_time / 2 ) );
        $( ".rb_slider_residential" ).fadeOut( fade_time, function() {
          $( this ).css( { // jquery bugfix, or a DOM bugfix.. i dono which
            "display" : "none"
          } );
        } );
        $( ".rb_mini_listings_res" ).fadeOut( fade_time, function() {
          $( this ).css( {
            "display" : "none"
          } );
        } );
      }
    } );
  } // end if the mini carousel was around

  $( "#srp_items" ).animate( {
    "height" :  $( "#srp_items" ).find( ".rb_large_listing_res" ).height()
  }, fade_time );

  $( ".rebate_header_residential" ).click( function() {
    if ( !is_fading && !com_res_mode ) { // if not animating, and not yet residential mode
      is_fading = true;
      com_res_mode = true;
      $( ".rebate_header_residential" ).addClass( "on" );
      $( ".rebate_header_commercial" ).removeClass( "on" );
      $( mini_res_btn ).addClass( "on" );
      $( mini_com_btn ).removeClass( "on" );
      $( "#srp_items" ).animate( {
        "height" :  $( "#srp_items" ).find( ".rb_large_listing_res" ).height()
      }, fade_time );
      $( "#header_carousel_left_nav, #header_carousel_right_nav" ).css( {
        "visibility" : active_nav_res ? "visible" : "hidden"
      } );
      $( obj_mini ).parent().find( ".mini-carousel_left_nav, .mini-carousel_right_nav" ).css( {
        "visibility" : active_mini_res ? "visible" : "hidden"
      } );
      setTimeout( function() {
        $( ".rb_slider_residential, .rb_mini_listings_res, .rb_large_listing_res" ).fadeIn( fade_time );
        setTimeout( function() {
          is_fading = false;
        }, fade_time );
      }, parseInt( fade_time / 2 ) );
      $( ".rb_slider_commercial" ).fadeOut( fade_time, function() {
        $( this ).css( { // because jquery won't change the css to display:none on anything who'se ancestor is already display:none
          "display" : "none"
        } );
      } );
      $( ".rb_mini_listings_com" ).fadeOut( fade_time, function() {
        $( this ).css( {
          "display" : "none"
        } );
      } );
      $( ".rb_large_listing_com" ).fadeOut( fade_time, function() {
        $( this ).css( {
          "display" : "none"
        } );
      } );
      $("#vss_r").fadeIn(1000);
      $("#vss_c").fadeOut(1000);
      $('#vss_r_c_wrapper').height($('#vss_r').height())
    }
  } );

  $( ".rebate_header_commercial" ).click( function() {
    if ( !is_fading && com_res_mode ) { // if not animating, and not yet commercial mode
      is_fading = true;
      com_res_mode = false;
      $( ".rebate_header_commercial" ).addClass( "on" );
      $( ".rebate_header_residential" ).removeClass( "on" );
      if ( obj_mini != null && obj_mini != undefined ) {
        $( mini_com_btn ).addClass( "on" );
        $( mini_res_btn ).removeClass( "on" );
      }
      $( "#srp_items" ).animate( {
        "height" :  $( "#srp_items" ).find( ".rb_large_listing_com" ).height()
      }, fade_time );
      $( "#header_carousel_left_nav, #header_carousel_right_nav" ).css( {
        "visibility" : active_nav_com ? "visible" : "hidden"
      } );
      $( obj_mini ).parent().find( ".mini-carousel_left_nav, .mini-carousel_right_nav" ).css( {
        "visibility" : active_mini_com ? "visible" : "hidden"
      } );
      setTimeout( function() {
        $( ".rb_slider_commercial, .rb_mini_listings_com, .rb_large_listing_com" ).fadeIn( fade_time );
        setTimeout( function() {
          is_fading = false;
        }, fade_time );
      }, parseInt( fade_time / 2 ) );
      $( ".rb_slider_residential" ).fadeOut( fade_time, function() {
        $( this ).css( {
          "display" : "none"
        } );
      } );
      $( ".rb_mini_listings_res" ).fadeOut( fade_time, function() {
        $( this ).css( {
          "display" : "none"
        } );
      } );
      $( ".rb_large_listing_res" ).fadeOut( fade_time, function() {
        $( this ).css( {
          "display" : "none"
        } );
      } );
            $("#vss_c").fadeIn(1000);
      $("#vss_r").fadeOut(1000);
      $('#vss_r_c_wrapper').height($('#vss_c').height())
    }
  } );

  if ( active_nav_res ) { // if there are enough items, then duplicate it enough times, etc...
    $( nav_res_slider ).find( 'div[class="clear"]' ).remove(); // we need to move it around..
    $( nav_res_items ).each( function() { //reconssturct the html
      $( nav_res_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_res_items ).each( function() {
      $( nav_res_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_res_items ).each( function() {
      $( nav_res_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_res_slider ).append( '<div class="clear"></div>' );
    hidden_res_sep = $( nav_res_slider ).find( ".header_carousel_separator")[4];
    $( hidden_res_sep ).css( {
      "visibility" : "hidden"
    } );
  } else {
    $( nav_res_slider ).find( ".header_carousel_separator:last" ).css( {
      "visibility" : "hidden"
    } );
    $( nav_res_slider ).css( {
      "padding-left" : (parseInt( $( "#header_carousel_items" ).css( "width" ) ) - $( nav_res_slider ).find( ".header_carousel_item" ).length * nav_item_width) / 2 + "px"
    } );
  }

  if ( active_nav_com ) {
    $( nav_com_slider ).find( 'div[class="clear"]' ).remove();
    $( nav_com_items ).each( function() {
      $( nav_com_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_com_items ).each( function() {
      $( nav_com_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_com_items ).each( function() {
      $( nav_com_slider ).append( '<div class="header_carousel_item">' + $( this ).html() + '</div>' );
    } );
    $( nav_com_slider ).append( '<div class="clear"></div>' );
    hidden_com_sep = $( nav_com_slider ).find( ".header_carousel_separator")[4];
    $( hidden_com_sep ).css( {
      "visibility" : "hidden"
    } );
  } else {
    $( nav_com_slider ).find( ".header_carousel_separator:last" ).css( {
      "visibility" : "hidden"
    } );
    $( nav_com_slider ).css( {
      "padding-left" : (parseInt( $( "#header_carousel_items" ).css( "width" ) ) - $( nav_com_slider ).find( ".header_carousel_item" ).length * nav_item_width) / 2 + "px"
    } );
  }

  $( "#header_carousel_left_nav" ).click( function() {
    if ( !nav_is_animating ) {
      if ( active_nav_res && com_res_mode ) { // if the res carousel is working, and we're in residential mode.
        nav_is_animating = true;
        if ( rb_nav_idx_res - nav_slide_by - req_nav_amount < 0 ) { // if about to underflow
          rb_nav_idx_res += nav_res_idx_max / 2; // instantly move you to an identical spot up the carousel..
          $( hidden_res_sep ).css( {
            "visibility" : "visible"
          } );
          hidden_res_sep = $( nav_res_slider ).find( ".header_carousel_separator" )[ rb_nav_idx_res + 4 ];
          $( hidden_res_sep ).css( {
            "visibility" : "hidden"
          } );
          $( nav_res_slider ).css( {
            "left" : 0 - nav_item_width * rb_nav_idx_res + "px"
          } );
        }
        rb_nav_idx_res -= nav_slide_by; // move down by "slide_by" down the slider...
        $( nav_res_slider ).animate( {
          "left" : 0 - nav_item_width * rb_nav_idx_res + "px"
        }, ani_time, function() {
          $( hidden_res_sep ).css( {
            "visibility" : "visible"
          } );
          hidden_res_sep = $( nav_res_slider ).find( ".header_carousel_separator" )[ rb_nav_idx_res + 4 ];
          $( hidden_res_sep ).css( {
            "visibility" : "hidden"
          } );
          nav_is_animating = false;
        } );
      } else if ( active_nav_com && !com_res_mode ) { // if the com carousel is working, and we are in com mode.
        nav_is_animating = true;
        $( hidden_com_sep ).css( {
          "visibility" : "visible"
        } );
        if ( rb_nav_idx_com - nav_slide_by - req_nav_amount < 0 ) { // if about to underflow
          rb_nav_idx_com += nav_com_idx_max / 2; // instantly move you to an identical spot up the carousel..
          $( nav_com_slider ).css( {
            "left" : 0 - nav_item_width * rb_nav_idx_com + "px"
          } );
        }
        rb_nav_idx_com -= nav_slide_by; // move down by "slide_by" down the slider...
        $( nav_com_slider ).animate( {
          "left" : 0 - nav_item_width * rb_nav_idx_com + "px"
        }, ani_time, function() {
          hidden_com_sep = $( nav_com_slider ).find( ".header_carousel_separator" )[ rb_nav_idx_com + 4 ];
          $( hidden_com_sep ).css( {
            "visibility" : "hidden"
          } );
          nav_is_animating = false;
        } );
      }
    }
  } );

  $( "#header_carousel_right_nav" ).click( function() {
    if ( nav_is_animating != true ) {
      if ( active_nav_res && com_res_mode ) {
        nav_is_animating = true;
        $( hidden_res_sep ).css( {
          "visibility" : "visible"
        } );
        if ( rb_nav_idx_res + nav_slide_by + req_nav_amount - 1 > nav_res_idx_max - 1 ) { // if about to overflow
          rb_nav_idx_res -= nav_res_idx_max /2;
          $( nav_res_slider ).css( {
            "left" : 0 - nav_item_width * rb_nav_idx_res + "px"
          } );
        }
        rb_nav_idx_res += nav_slide_by;
        hidden_res_sep = $( nav_res_slider ).find( ".header_carousel_separator" )[ rb_nav_idx_res + 4 ];
        $( hidden_res_sep ).css( {
          "visibility" : "hidden"
        } );
        $( nav_res_slider ).animate( {
          "left" : 0 - nav_item_width * rb_nav_idx_res + "px"
        }, ani_time, function() {
          nav_is_animating = false;
        } );
      } else if ( active_nav_com && !com_res_mode ) {
        nav_is_animating = true;
        $( hidden_com_sep ).css( {
          "visibility" : "visible"
        } );
        if ( rb_nav_idx_com + nav_slide_by + req_nav_amount - 1 > nav_com_idx_max - 1 ) { // if about to overflow
          rb_nav_idx_com -= nav_com_idx_max /2;
          $( nav_com_slider ).css( {
            "left" : 0 - nav_item_width * rb_nav_idx_com + "px"
          } );
        }
        rb_nav_idx_com += nav_slide_by;
        hidden_com_sep = $( nav_com_slider ).find( ".header_carousel_separator" )[ rb_nav_idx_com + 4 ];
        $( hidden_com_sep ).css( {
          "visibility" : "hidden"
        } );
        $( nav_com_slider ).animate( {
          "left" : 0 - nav_item_width * rb_nav_idx_com + "px"
        }, ani_time, function() {
          nav_is_animating = false;
        } );
      }
    }
  } );

  $( ".selectors .thumb, .selectors .list" ).click( function() {
    if ( !is_fading ) {
      is_fading = true;
      var is_off = $( ".mini-carousel_wrapper.off, .mini-carousel_list_view.off" );
      setTimeout( function() {
        $( is_off ).fadeIn( fade_time );
      }, fade_time / 2 );
      $( ".mini-carousel_wrapper:not(.off), .mini-carousel_list_view:not(.off)" ).fadeOut( fade_time, function() {
        $( ".mini-carousel_wrapper, .mini-carousel_list_view" ).toggleClass( "off" );
        is_fading = false;
      } );
    }
  } );
}

function contact_us_init() {
  var curr_item_picked = 0;
  $( "#contact_subject" ).focus( function() {
    if ( $( this ).val() == "Please Select" ) {
      $( this ).val( "" );
    }
  } );

  $( "#contact_subject" ).blur( function() {
    self_trim( this );
    if ( $( this ).val() == "" ) {
      $( this ).val( "Please Select" );
    } else {
      $( this ).closest( ".contact_form_row" ).find( ".contact_form_required" ).css( {
        "color" : "#CCC"
      } );
    }
    var was_in_arr = false;
    $( ".subject_dropdown li" ).each( function() {
      if ( $( this ).text() == $( "#contact_subject" ).val() ) {
        was_in_arr = true;
      }
    } );
    if ( !was_in_arr ) {
      curr_item_picked = 0;
    }
  } );
  $( "#contact_subject" ).click( function() {
    $( ".subject_dropdown" ).show(); //slideDown(100);
  } );
  $( ".subject_dropdown ul li" ).click( function() {
    curr_item_picked = parseInt( $( this ).attr( "id" ).substr( 14 ) );
    $( "#contact_subject" ).val( $( this ).html() );
    $( ".subject_dropdown" ).hide(); //slideUp(25);
    $( "#contact_subject" ).closest( ".contact_form_row" ).find( ".contact_form_required" ).css( {
      "color" : "#CCC"
    } );
  } );
  $( ".drop_trigger" ).mouseleave( function() {
    $( ".subject_dropdown" ).hide(); //slideUp(25);
    $( "#contact_subject" ).blur();
  } );
  $( ".subject_dropdown ul li" ).hover( function() {
    $( this ).toggleClass( "on" );
  }, function () {
    $( this ).toggleClass( "on" );
  } );

  function contact_input_validate( obj ) {
    self_trim( obj );
    if ( $( obj ).val() == "" ) {
      $( obj ).closest( ".contact_form_row" ).find( ".contact_form_required" ).css( {
        "color" : "#F00"
      } );
      if ( timer_arr["contact_us"] == true ) {
        clearTimeout( timer_arr["contact_us_t"] );
        timer_arr["contact_us"] = false;
        $( "#contact_err_msg" ).stop().text( "" ).css( {
          "opacity" : "1"
        } );
      }
      $( "#contact_err_msg" ).text( "Please fill in all required fields." );
      timer_arr["contact_us"] = true;
      timer_arr["contact_us_t"] = setTimeout( function () {
        $( "#contact_err_msg" ).stop().css( {
          "opacity" : "1"
        } ).animate( {
          "opacity" : "0"
        }, 1000, function() {
          $( "#contact_err_msg" ).text( "" ).css( {
            "opacity" : "1"
          } );
        } );
        timer_arr["contact_us"] = false;
      }, 4000 );
    } else {
      $( obj ).closest( ".contact_form_row" ).find( ".contact_form_required" ).css( {
        "color" : "#CCC"
      } );
    }
  }

  $( "#contact_fname, #contact_lname, #contact_email_1, #contact_email_2, #contact_comment" ).blur( function() {
    contact_input_validate( this );
  } );

  $( "#contact_street_address, #contact_city, #contact_phone" ).blur( function() {
    self_trim( this );
  } );

  $( "#contact_zip" ).blur( function() {
    $( this ).val( $( this ).val().replace( /[^\d]/g, '' ) );
    self_trim( this );
  } );

  var locked = false;
  $( "#contact_submit" ).click( function() {
    if ( !locked ) {
      locked = true;
      $( "#contact_fname, #contact_lname, #contact_email_1, #contact_email_2, #contact_comment" ).blur();
      if ( $( "#contact_subject" ).val() == "Please Select" ) {
        $( "#contact_err_msg" ).text( "Please fill in all required fields." );
        $( "#contact_subject" ).closest( ".contact_form_row" ).find( ".contact_form_required" ).css( {
          "color" : "#F00"
        } );
      } else if ( $( "#contact_fname" ).val() == "" || $( "#contact_lname" ).val() == "" || $( "#contact_email_1" ).val() == "" || $( "#contact_email_2" ).val() == "" || $( "#contact_comment" ).val() == "" ) {
        $( "#contact_err_msg" ).text( "Please fill in all required fields." );
      } else if ( $( "#contact_email_1" ).val() != $( "#contact_email_2" ).val() ) {
        $( "#contact_err_msg" ).text( "Email values must match." );
      } else {
        $( "#contact_err_msg" ).text( "" );
        var theData = "";
        $( "#contact_form input, textarea" ).each( function() {
          theData += $( this ).attr( "id" ) + "=" + escape( $( this ).val() ) + "&";
        } );
        $.ajax( {
          "cache" : false,
          "data" : theData + "picked=" + curr_item_picked,
          "dataType" : "html",
          "type" : "post",
          "url" : "/ajax/contact_us.php",
          "error" : function( xhr, stat, err ) {
            locked = false;
          },
          "success" : function( dat, stat, xhr ) {
            locked = false;
            switch ( parseInt( $.trim( dat ).substr( 0, 1 ) ) ) {
              case 0 : { // fields empty
                $( "#contact_err_msg" ).text( "Please fill in all required fields." );
                break;
              }
              case 1 : { // success
                $( "#contact_err_msg" ).text( "Your comments were successfully sent." );
                $( "#contact_form input, textarea" ).val( "" );
                $( ".contact_form_required" ).css( {
                  "color" : "#CCC"
                } );
                $( "#contact_subject" ).val( "Please Select" );
                break;
              }
              case 2 : { // email empty
                $( "#contact_err_msg" ).text( "Email values is not valid." );
                break;
              }
              default :
              case 3 : {
                $( "#contact_err_msg" ).text( "An error occured. Please try again later." );
                break;
              }
            }
          }
        } );
      }
    }
  } );
}


function navRollovers( obj ) {
  $( obj ).hover( function() {
    if ( !$( this ).hasClass( "on" ) ) {
      $( this ).addClass( "rollover" ); // dark grey "on" state
      $( this ).children( ".slide_arrow" ).addClass( "on" );
      $( "#main_nav_about:not( .on )" ).css( {
        "background-position" : "left -95px"
      } );
      $( "#main_nav_go:not( .on )" ).css( {
        "background-position" : "left -122px"
      } );
      $( "#main_nav_rebate:not( .on )" ).css( {
        "background-position" : "left -149px"
      } );
    }
  }, function () {
    $( this ).removeClass( "rollover" ); // return to light "off" state
    $( this ).children( ".slide_arrow" ).removeClass( "on" );
  } );
}

function init_header_nav() {
  navRollovers( "#main_nav_map" );
  navRollovers( "#main_nav_news" );
  navRollovers( "#main_nav_links" );
  var main_nav_wait_time = 200; // milisec
  var fade_time = 300; // ms
  var height_ani_time = 250; // time it takes to adjust the height of the dropdown to fully extend

  // MAIN NAV DROPDOWNS

//// ToDo: merge this to the css file:
$( ".drop_hideout" ).css( {
  "position" : "absolute", // no more static
  "top" : "0px", // new css
  "margin-top" : "0px", // no more margin-top
  "display" : "none" // new css
} );

// peeking at the height of elements
  $( ".main_nav_dropdown" ).css( {
    "height" : "1px",
    "display" : "block",
    "overflow" : "hidden",
    "padding" : "0px",
    "margin" : "0px"
  } );
  $( ".drop_hideout" ).css( {
    "display" : "block"
  } );
  var about_drop_height = $( "#about_drop_hideout" ).height() + 47;
  var go_drop_height = $( "#go_drop_hideout" ).height() + 47;
  var rebates_drop_height = $( "#rebates_drop_hideout" ).height() + 47;
  $( ".drop_hideout" ).css( {
    "display" : "none"
  } );
  $( ".main_nav_dropdown" ).css( {
    "height" : "",
    "display" : "none",
    "overflow" : "",
    "padding" : "",
    "margin" : ""
  } );

//// END ToDo

  timer_arr["nav_pop_who"] = "";
  timer_arr["init_nav_pop"] = false;
  var time_left = 2000; // time needed before the nav will pop open for the very first time.

  // About Green Riverside
  $( "#main_nav_about" ).mouseenter( function() {
    $( "#main_nav_about:not( .on )" ).css( { // change to it's dark state, if not on
      "background-position" : "-280px -95px"
    } );
    $( "#main_nav_go:not( .on )" ).css( { // all :not( .on ) allows the green color to persist, whoever has it.
      "background-position" : "left -122px"
    } );
    $( "#main_nav_rebate:not( .on )" ).css( { // the rest change to it's white "off" state
      "background-position" : "left -149px"
    } );
    if ( timer_arr["init_nav_pop"] ) {
      if ( timer_arr["main_nav"] == true && timer_arr["main_nav_who"] != 1 ) {
        clearTimeout( timer_arr["main_nav_t"] );
        timer_arr["main_nav"] = false;
      }
      if ( timer_arr["main_nav"] == false && timer_arr["main_nav_who"] != 1 ) { // if nobody is que'd to go
        timer_arr["main_nav"] = true; // allows only one transition to another menu, at a time.
        timer_arr["main_nav_who"] = 1; // About
        timer_arr["main_nav_t"] = setTimeout( function() {
          // fade out all, fade in AboutGreen
  //      hide all content divs
          $( ".drop_hideout:not( #about_drop_hideout )" ).stop().animate( {
            "opacity" : 0
          }, fade_time, function() {
            $( this ).css( {
              "display" : "none"
            } );
          } );
  //      bring in the only correct content div
          $( "#about_drop_hideout" ).css( {
            "display" : "block"
          } );
          if ( $( ".main_nav_dropdown" ).hasClass( "open" ) ) {
            $( "#about_drop_hideout" ).stop().animate( { // fadeIn, but force display:block (fadeIn fails on children who's parents are display:none
              "opacity" : 1
            }, fade_time, function() {
              timer_arr["main_nav"] = false;
            } );
          } else { // else first time opening, don't cross fade it with nothing
            $( "#about_drop_hideout" ).css( {
              "opacity" : 1
            } );
            timer_arr["main_nav"] = false;
          }
  //      if nav open
          if ( true || !$( ".main_nav_dropdown" ).hasClass( "open" ) ) {
  //        adjust/fix the height (cuz thigns are pos:abs now)
            $( ".main_nav_dropdown" ).css( {
              "display" : "block"
            } );
            $( ".main_nav_dropdown_inner_wrapper" ).css( {
              "height" : about_drop_height + "px"
            });
          $( ".main_nav_dropdown" ).stop().animate( {
              "height" : about_drop_height + "px"
            }, height_ani_time, function() {
              var h1 = $( "#about_drop .top_drop_left" ).height();
              var h2 = $( "#about_drop .top_drop_center" ).height();
              $( "#about_drop .top_vertical_separator" ).css( {
                "height" : (h1 > h2 ? h1 : h2)
              } );
            } );
            $( ".main_nav_dropdown" ).addClass( "open" );
          }
        }, main_nav_wait_time );
      }
    } else {
      setTimeout( function() {
        timer_arr["init_nav_pop"] = true;
        if ( timer_arr["nav_pop_who"] != null && timer_arr["nav_pop_who"] != undefined ) {
          $( timer_arr["nav_pop_who"] ).trigger( "mouseenter" );
        }
      }, time_left );
      timer_arr["nav_pop_who"] = "#main_nav_about";
    }
  } );

  $( "#main_nav_about" ).mouseleave( function() {
    timer_arr["nav_pop_who"] = "";
  } );


//  Go Green
  $( "#main_nav_go" ).mouseenter( function() {
    $( "#main_nav_about:not( .on )" ).css( { // set to "off" (light) state
      "background-position" : "left -95px"
    } );
    $( "#main_nav_go:not( .on )" ).css( { // change to dark
      "background-position" : "-122px -122px"
    } );
    $( "#main_nav_rebate:not( .on )" ).css( { // change to light
      "background-position" : "left -149px"
    } );
    if ( timer_arr["init_nav_pop"] ) {
      if ( timer_arr["main_nav"] == true && timer_arr["main_nav_who"] != 2 ) {
        clearTimeout( timer_arr["main_nav_t"] );
        timer_arr["main_nav"] = false; // clear the que
      }
      if ( timer_arr["main_nav"] == false && timer_arr["main_nav_who"] != 2 ) {
        timer_arr["main_nav"] = true;
        timer_arr["main_nav_who"] = 2; // Go Green
        timer_arr["main_nav_t"] = setTimeout( function() {
  //      hide all content divs
          $( ".drop_hideout:not( #go_drop_hideout )" ).stop().animate( {
            "opacity" : 0
          }, fade_time, function() {
            $( this ).css( {
              "display" : "none"
            } );
          } );
  //      bring in the only correct content div
          $( "#go_drop_hideout" ).css( {
            "display" : "block"
          } );
          if ( $( ".main_nav_dropdown" ).hasClass( "open" ) ) {
            $( "#go_drop_hideout" ).stop().animate( {
              "opacity" : 1
            }, fade_time, function() {
              timer_arr["main_nav"] = false;
            } );
          } else {
            $( "#go_drop_hideout" ).css( {
              "opacity" : 1
            } );
            timer_arr["main_nav"] = false;
          }
          //if ( !$( ".main_nav_dropdown" ).hasClass( "open" ) ) { // i forget why this was here..
          $( ".main_nav_dropdown" ).css( {
            "display" : "block"
          } );

                    $( ".main_nav_dropdown_inner_wrapper" ).css( {
              "height" : go_drop_height + "px"
            });
          $( ".main_nav_dropdown" ).stop().animate( {
            "height" : go_drop_height + "px"
          }, height_ani_time, function() {
            var h1 = $( "#go_drop_hideout .top_drop_left" ).height();
            var h2 = $( "#go_drop_hideout .top_drop_center" ).height();
            $( "#go_drop_hideout .top_vertical_separator" ).height( (h1 > h2 ? h1  : h2) );
          } );
          $( ".main_nav_dropdown" ).addClass( "open" );
          //}
        }, main_nav_wait_time );
      }
    } else {
      setTimeout( function() {
        timer_arr["init_nav_pop"] = true;
        if ( timer_arr["nav_pop_who"] != null && timer_arr["nav_pop_who"] != undefined ) {
          $( timer_arr["nav_pop_who"] ).trigger( "mouseenter" );
        }
      }, time_left );
      timer_arr["nav_pop_who"] = $( "#main_nav_go" );
    }
  } );

//  Green Rebate Programs
  $( "#main_nav_rebate" ).mouseenter( function() {
    $( "#main_nav_rebate:not( .on )" ).css( {
      "background-position" : "-304px -149px"
    } );
    $( "#main_nav_about:not( .on )" ).css( {
      "background-position" : "left -95px"
    } );
    $( "#main_nav_go:not( .on )" ).css( {
      "background-position" : "left -122px"
    } );
    if ( timer_arr["init_nav_pop"] ) {
      if ( timer_arr["main_nav"] == true && timer_arr["main_nav_who"] != 3 ) {
        clearTimeout( timer_arr["main_nav_t"] );
        timer_arr["main_nav"] = false;
      }
      if ( timer_arr["main_nav"] == false && timer_arr["main_nav_who"] != 3 ) {
        timer_arr["main_nav"] = true;
        timer_arr["main_nav_who"] = 3; // Go Green
        timer_arr["main_nav_t"] = setTimeout( function() {
  //      hide all content divs
          $( ".drop_hideout:not( #rebates_drop_hideout )" ).stop().animate( {
            "opacity" : 0
          }, fade_time, function() {
            $( this ).css( {
              "display" : "none"
            } );
          } );
  //      bring in the only correct content div
          $( "#rebates_drop_hideout" ).css( {
            "display" : "block"
          } );
          if ( $( ".main_nav_dropdown" ).hasClass( "open" ) ) {
            $( "#rebates_drop_hideout" ).stop().animate( {
              "opacity" : 1
            }, fade_time, function() {
              timer_arr["main_nav"] = false;
            } );
          } else {
            $( "#rebates_drop_hideout" ).css( {
              "opacity" : 1
            } );
            timer_arr["main_nav"] = false;
          }
  //      if nav open, adjust the height of the drop down.
          if ( true || !$( ".main_nav_dropdown" ).hasClass( "open" ) ) {
                    $( ".main_nav_dropdown_inner_wrapper" ).css( {
              "height" : rebates_drop_height + "px"
            });
          $( ".main_nav_dropdown" ).stop().animate( {
              "height" : rebates_drop_height + "px"
            }, height_ani_time );
            $( ".main_nav_dropdown" ).addClass( "open" );
          }
        }, main_nav_wait_time );
      }
    } else {
      setTimeout( function() {
        timer_arr["init_nav_pop"] = true;
        if ( timer_arr["nav_pop_who"] != null && timer_arr["nav_pop_who"] != undefined ) {
          $( timer_arr["nav_pop_who"] ).trigger( "mouseenter" );
        }
      }, time_left );
      timer_arr["nav_pop_who"] = $( "#main_nav_rebate" );
    }
  } );

  setTimeout( function() {
    timer_arr["init_nav_pop"] = true;
  }, time_left );

// Reset Dropdowns
  $( "#header_wrapper" ).mouseenter( function() {
    timer_arr["init_nav_pop"] = true;
    timer_arr["nav_pop_who"] = null;
    if ( $( ".main_nav_dropdown" ).hasClass( "open" ) ) {
      clearTimeout( timer_arr["main_nav_t"] );
      timer_arr["main_nav"] = false;
      timer_arr["main_nav_who"] = 0;
      $( ".main_nav_dropdown" ).removeClass( "open" );
      $( ".main_nav_dropdown" ).animate( {
        "height" : "0px"
      }, height_ani_time, function() {
        $( ".drop_hideout" ).css( {
          "display" : "none",
          "opacity" : 0
        } );
        $( ".main_nav_dropdown" ).css( {
          "display" : "none"
        } );
      } );
      $( "#main_nav_about:not( .on )" ).css( {
        "background-position" : "left -95px"
      } );
      $( "#main_nav_go:not( .on )" ).css( {
        "background-position" : "left -122px"
      } );
      $( "#main_nav_rebate:not( .on )" ).css( {
        "background-position" : "left -149px"
      } );
    }
  } );
}
