var changemade = false;

$(document).ready(function() { 
        
    $('a[rel=external]').attr('onclick','window.open( this.href );return false');
    $('.news_content a[rel=bookmark], .news_feed a[rel=bookmark]').attr('onclick','window.open( this.href );return false');  
        
    $('form.monitor input, form.monitor select, form.monitor checkbox, form.monitor radio, form.monitor textarea').change( function( ) {
            if( changemade == false )
            {
                $('a').click( function( ) {
                    return confirm('There are unsaved changes to the form on this page! Click Ok to leave anyway, or cancel to stay.');
                });
                changemade = true;
            }            
    });        
        
         
    $('#search_what').tooltip({ 
        tip: '#search_what_tip',
        position: "bottom center", 
        opacity: 0.9,
        effect: 'fade',
        delay: 400,   
        predelay: 0,
        offset: [10,0],
        show: true,
        fadeInSpeed:200,
        fadeOutSpeed:200,
        events: { 
            def: "null,mouseout",                // default show/hide events for an element 
            input: "null,focus",                        // for all input elements 
            tooltip: "null,click"            // the tooltip element 
        }
    });
    
    $('#search_where').tooltip({ 
        tip: '#search_where_tip',
        position: "bottom center", 
        opacity: 0.9,
        effect: 'fade',
        delay: 400,
        predelay: 0,
        offset: [10,0],
        fadeInSpeed:200,
        fadeOutSpeed:200,        
        events: { 
            def: "null,mouseout",                // default show/hide events for an element 
            input: "null,focus",                        // for all input elements 
            tooltip: "null,click"            // the tooltip element 
        }
    });
    
    $('#search_what').tooltip(0).show( );
    $('#search_where').tooltip(0).show( );
  
    $('#search_what').autocomplete({ 
        serviceUrl:'http://www.listcars4free.com/lc4fajax/autocomplete_find_make',
        minChars:2, 
        delimiter: /(,|;)\s*/, // regex or character
        maxHeight:200,
        width:220,
        zIndex: 9999,
        deferRequestBy: 0
      });
    
    $('#search_where').autocomplete({ 
        serviceUrl:'http://www.listcars4free.com/lc4fajax/autocomplete_find_loc',
        minChars:2, 
        maxHeight:200,
        width:220,
        zIndex: 9999,
        deferRequestBy: 0
      });
        
    $('#new_location_dialog').dialog( { modal:true, title:"Choose a location", resizable:false, height:450 } );
    
    
    $("#browse_categories").accordion( );
    
    $('#current_province').mouseenter( function( ) { $( '#province_selector_dialog' ).show( ); } );
    $('#province_selector_dialog').mouseleave( function( ) { $( '#province_selector_dialog' ).fadeOut( 300 ); } );    
    
});

function do_location_search( )
{
    var query = $('#new_loc').val( );
    
    $.ajax({
          url: "http://www.listcars4free.com/lc4fajax/find_possible_locations",
          type: "GET",
          data: ({query : query}),
          dataType: "html",
          success: function(msg)
          {
              $('.location_results').html( msg );
          }
       }
    );   
}

function set_new_location( city, province, postal_code )
{
    $.ajax({
           type: "POST",
           url: "http://www.listcars4free.com/lc4fajax/set_new_location",
           data: ({city : city, province : province, postal_code : postal_code }),
           async: true, 
           success: function( msg )
           {                       
                location.href = location.href;
           }
     }
     );    
}

function get_vehicle_models( make )
{
    $.ajax({
           type: "POST",
           url: "http://www.listcars4free.com/lc4fajax/get_vehicle_models",
           data: ({vehicle_make : make }),
           async: true, 
           dataType: "html",
           success: function( msg )
           {                       
              $('#vehicle_model').html( msg );
           }
     }
     );    
}


