//
//
//

jQuery.fn.marque_to_model = function(model_id){
  //-------------------------------------
  var container = this;
  var model     = $('#'+model_id);
	var url       = '';
  //-------------------------------------
  function init(){
    url = '/includes/ajax/jquery.marque_to_model.php';
    if ("https:" == window.location.protocol) {
        if('secure.potn.com' == location.host){
            url = '/~potncom' + url;
        }
    } // if
    url = window.location.protocol + '//' + window.location.host + url;

    //if (container.get(0).options.length > 0) return; // already initialised
    var car_manufacturer = container.get_cookie('car_manufacturer');

    jQuery.post
    (
        url,

        {
            action : 'init'
        },

        function(retval){
            container.html('');
            container.html(retval);
            container.select_index(container, car_manufacturer);

            container.bind("change", container.change);
            container.change();

        } // function(retval)

    ) // $.post

  } // function
  //-------------------------------------
  this.change = function() {
      model.html("<option>loading ...</option>");
      var manufacturer = container.attr('value');
      var car_type     = container.get_cookie('car_type');

      jQuery.post
      (
        url,

        {
          action       : 'car_type',
          manufacturer : manufacturer
        },

        function(retval){
          model.html("");
          model.html(retval);

          container.select_index(model, car_type);
        } // function(retval)

      ) // $.post
  } ;
  //-------------------------------------
  this.get_cookie = function( cookie_name ) {
    var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

    if ( results ) return ( unescape ( results[1] ) );

    return null;
  } // function
  //-------------------------------------
  this.select_index = function(list, look_for){
      //-----------------------------
      var list_js = list.get(0);
      if (0 == list_js.childNodes.length) return;

      var x;
      for(var i=0; i < list_js.childNodes.length; i++){
          x = list_js.childNodes[i];

          if (1 != x.nodeType) continue;
          if (x.text == look_for) {
              //x.selected = true;
              list_js.selectedIndex = x.index;

              return;
          } // if
      } // for

      list_js.selectedIndex = 0;

  } // function select_index(list, look_for)
  //-------------------------------------
  init();

  return this;
  //-------------------------------------
} // jQuery.fn.marque_to_model

