﻿
$(document).ready(function() {

function createTmpDiv() { //вспомогательная - создает скрытый контейнер
  $("<div id='tmp-div'></div>").appendTo("body").hide();
}

$("<div id='load' style='position:absolute; display:none;'><img class='load' src='/images/1/ajax-loader.gif' width='25' height='25'></div>").appendTo("body");
//$("<div id='tmp-div'></div>").appendTo("body").hide();

var cur = {};
$("*",document.body).live('click', function (e) { //назначаем координаты, ф-я должна быть вначале!
  cur.x = e.pageX;
  cur.y = e.pageY; //alert("x: " + cur.x);
});

$("#load").ajaxStart(function(){ //показ индикатора на любой запрос
  $(this).css({ top:cur.y-12, left:cur.x+20 }).show();
});

$("#load").ajaxStop(function(){ //спрятать индикатор по окончанию всех ajax-запросов
  $(this).hide();
}); 

$(".makerselect > select").change(function() { 
  //var param = $("#searchform").serialize();
  $(".modelselect > select").attr("disabled","disabled");
  $(".engineselect > select").attr("disabled","disabled");
  $(".frameselect > select").attr("disabled","disabled");
  var ajaxurl = $("#searchform").attr("action")+'?isNaked=1'; //alert(ajaxurl);
	//var loc = window.location; //alert(loc);
  $.ajax({
    url: ajaxurl,          
    type: "GET",
    dataType : "html",                   
    data: $("#searchform").serialize(),
    error: function () { 
      return true;
    },
    success: function (xml) { 
      createTmpDiv();
      var tmp = $("#tmp-div").html(xml); //alert(tmp.html());
      //alert($(".modelselect:last").html());
      $(".modelselect:first").html($(".modelselect:last").html());
      $(".engineselect:first").html($(".engineselect:last").html());
      $(".modelselect > select").unbind("change", changeModel).bind("change", changeModel);
      $("#tmp-div").remove();
    }
  });  
  return false;
});

function changeModel() {
  $(".frameselect > select").attr("disabled","disabled");
  var ajaxurl = $("#searchform").attr("action")+'?isNaked=1';
  $.ajax({
    url: ajaxurl,
    type: "GET",
    dataType : "html",
    data: $("#searchform").serialize(),
    error: function () { 
      return true;
    },
    success: function (xml) { 
      createTmpDiv();
      var tmp = $("#tmp-div").html(xml);
      $(".frameselect:first").html($(".frameselect:last").html());
      $("#tmp-div").remove();
    }
  });  
  return false;
};

$(".modelselect > select").bind("change", changeModel);

});
