//////////
GW_Home = function( )
{
  var faderInt = null;
  var faderCnt = 0;

  return {
    //////////
    ListingsTabClick: function( which )
    {
      var tab      = new Array( )
      tab['new']   = 'New Listings';
      tab['sold']  = 'Recent Sales';
      tab['price'] = 'Price Reductions';

      for( var i in tab )
      {
        if( i == which )
        {
          $('#listings_' + i).show( );
          $('#listings_tab_' + i).addClass( 'tabon' );
          $('#listings_tab_' + i).html( tab[i] );
          $('#listings_view').html( 'View All ' + tab[i] + '&nbsp;&raquo;' );
          $('#listings_view').attr( 'href', '/search/' + i + '.html' );
        }
        else
        {
          $('#listings_' + i).hide( );
          $('#listings_tab_' + i).removeClass( 'tabon' );
          $('#listings_tab_' + i).html( '<a href="#" onclick="GW_Home.ListingsTabClick( ' + "'" + i + "'" + ' );return false;" class="alt">' + tab[i] + '</a>' );
        }
      }
    },

    //////////
    FadeTestimonials: function( )
    {
      if( GW_Home.faderInt == null )
      {
        GW_Home.faderInt = setInterval( 'GW_Home.FadeTestimonials( )', 7000 );
        GW_Home.faderCnt = 1;

        $('#testimonial' + GW_Home.faderCnt).fadeTo( 0, 0.99 );
      }
      else
      {
        var last         =   GW_Home.faderCnt;
        GW_Home.faderCnt = ( GW_Home.faderCnt + 1 > 2 ) ? 1 :
         ( GW_Home.faderCnt + 1 );

        $('#testimonial' + last).fadeTo( 1500, 0.01 );
        $('#testimonial' + GW_Home.faderCnt).fadeTo( 1500, 0.99 );
      }
    }
  }
}( );

//////////
GW_Search = function( )
{
  return {
    //////////
    Restore: function( )
    {
      if( $("input[name='s.section']").val( ) != '' )
      {
        GW_Search.OptionsTabClick( $("input[name='s.section']").val( ) );
      }
    },

    //////////
    OptionsTabClick: function( which )
    {
      var tab         = new Array( )
      tab['advanced'] = 'Advanced Search';
      tab['basic']    = 'Basic Search';
      tab['mls']      = 'MLS Search';

      for( var i in tab )
      {
        if( i == which )
        {
          $("input[name='s.section']").val( i );
          $('#search_' + i).show( );
          $('#search_tab_' + i).html( tab[i] );
        }
        else
        {
          $('#search_' + i).hide( );
          $('#search_tab_' + i).html( '<a href="#" onclick="GW_Search.OptionsTabClick( ' + "'" + i + "'" + ' );return false;" class="alt">' + tab[i] + '</a>' );
        }
      }

      if( which == 'advanced' )
      {
        $('#search_basic').show( );
      }
    }
  }
}( );

//////////
GW_KauaiMap = function( )
{
  var selected = new Array( );

  return {
    //////////
    Restore: function( )
    {
      if( typeof window.parent != 'undefined' && window.parent != null )
      {
        var str = $("input[name='map']", window.parent.document).val( );
        $("input[name='map']", window.parent.document).val( '' );

        if( str )
        {
          for( var sub in str.split( ',' ) )
          {
            GW_KauaiMap.Click( sub );
          }
        }
      }
    },

    //////////
    Show: function( el )
    {
      document.getElementById( el ).style.display = 'block';

      return false;
    },

    //////////
    Hide: function( el )
    {
      if( ! selected[ el ] )
      {
        document.getElementById( el ).style.display = 'none';
      }

      return false;
    },

    //////////
    Click: function( el )
    {
      if( ! selected[ el ] )
      {
        selected[ el ] = true;
        document.getElementById( el ).style.display = 'block';
      }
      else
      {
        selected[ el ] = false;
        document.getElementById( el ).style.display = 'none';
      }

      if( typeof window.parent != 'undefined' && window.parent != null )
      {
        var str = new Array( );

        for( var x in selected )
        {
          if( selected[x] == true )
          {
            str.push( x );
          }
        }

        window.parent.focus( );
        $("input[name='map']", window.parent.document).val( str.join( ',' ) );
      }

      return false;
    }
  }
}( );

//////////
GW_ContactForm = function( )
{
  var submitted = false;
  var responded = false;

  return {
    //////////
    Reset: function( )
    {
      $('#contactform_form')  .show( );
      $('#contactform_wait')  .hide( );
      $('#contactform_result').hide( );

      $("textarea[name='message']").val( '' );
      $("textarea[name='message']").focus( );

      return false;
    },

    //////////
    isInvalidName: function( val )
    {
      if( val == '' )
      {
        alert( "Please enter a contact name" );
        return 1;
      }

      return 0;
    },

    //////////
    isInvalidEmail: function( val )
    {
      if( val == '' )
      {
        alert( "Please enter a contact email" );
        return 1;
      }

      var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

      if( reg.test( val ) == false )
      {
        alert( "Please enter a valid email address" )
        return 1;
      }

      return 0;
    },

    //////////
    isInvalidPhone: function( val )
    {
      if( val == '' )
      {
        alert( 'Please enter a contact phone number' );
        return 1;
      }

      return 0;
    },

    //////////
    isInvalidMessage: function( val )
    {
      if( val == '' )
      {
        alert( 'Please enter the message you wish to send to Greg' );
        return 1;
      }

      return 0;
    },

    //////////
    onSubmit: function( )
    {
      if( GW_ContactForm.isInvalidName( $("input[name='name']").val( ) )
       || GW_ContactForm.isInvalidEmail( $("input[name='email']").val( ) )
       || GW_ContactForm.isInvalidPhone( $("input[name='phone']").val( ) )
       || GW_ContactForm.isInvalidMessage( $("textarea[name='message']").val( ) ) )
      {
        return false;
      }

      GW_ContactForm.submitted = new Date( );

      $('#contactform_form')  .hide( );
      $('#contactform_wait')  .show( );
      $('#contactform_result').hide( );

      $('#contactform_result').load( '/asset/ajax/contactgreg.html'
       , {   name:    $("input[name='name']")      .val( )
           , email:   $("input[name='email']")     .val( )
           , phone:   $("input[name='phone']")     .val( )
           , message: $("textarea[name='message']").val( )
         }
       , GW_ContactForm.onAjaxResult
       );

      return false;
    },

    //////////
    onAjaxResult: function( responseText, textStatus, XMLHttpRequest )
    {
      GW_ContactForm.responded = new Date( );

      if( textStatus == 'success' )
      {
        if( GW_ContactForm.responded - GW_ContactForm.submitted > 0    &&
            GW_ContactForm.responded - GW_ContactForm.submitted < 3000 )
        {
          setTimeout( GW_ContactForm.ShowResult,
           2000 - ( GW_ContactForm.responded - GW_ContactForm.submitted ) );
        }
        else
        {
          GW_ContactForm.ShowResult( );
        }
      }
    },

    //////////
    ShowResult: function( )
    {
      $('#contactform_form')  .hide( );
      $('#contactform_wait')  .hide( );
      $('#contactform_result').show( );
    }
  }
}( );

