$(document).ready(function(){
   var checkboxIndex;
   var autoIndex;
   
   // раставляем индексы   
   $("div.item-auto").each(function(i){
      $(this).attr("id", "item-auto-" + i);
   });
   
   // раставляем индексы в чекбоксах
   $("div.item-auto input.checkbox-compare").each(function(i){
      $(this).attr("rel", + i);
   });
  
  // Обработчик при клике на чекбокс
   $("input.checkbox-compare").click(function(){
     checkboxIndex = $(this).attr("rel");
     autoIndex = "#item-auto-" + checkboxIndex;
     if ($(this).is(":checked")){
       $(autoIndex).css("background-color","#d0cfcf");

     } else {
       $(autoIndex).css("background-color","#e9e9e9");
     }
   });


	// Добавляем всплывающую подсказку
	/*
	$("input.checkbox-compare").before('<div class="compare_tooltip"><a class="button-compare" href="#">Сравнить автомобили</a></div>');

	// Обработчик при наведении на чекбокс
	$("input.checkbox-compare").bind("mouseenter", function(){
		$(this).prev("div.compare_tooltip").show();
	});
	$("input.checkbox-compare").bind("mouseleave", function(){
		$("div.compare_tooltip").hide();	
	});
	$("div.checkbox-hover-area").bind("mouseleave", function(){
		$("div.compare_tooltip").hide();		
	});
	$("div.compare_tooltip").bind("mouseleave", function(){
		$(this).hide();		
	});
	*/

	// Наведение на кнопку сравнения
	$("div.div-button-compare a").hover(function(){
//		$(this).css('background-image', );
	});


   // Обработчик при клике ссылку Сравнить
   $("a.button-compare").click(function(){
     var elems = $(".checkbox-compare:checked"); // Выбираем выбранные checkbox`ы
     var carsID = new Array(); // пустой массив
     var url;
     var width;
     var height;

     if (elems.size() > 1){
       $("div.loading").show(); // Показываем процесс загрузки
       
       // Устанавливаем ширину окна сравнения результатов 
       if (elems.size() == 2) {
         width = 860;
       } else if (elems.size() == 3) {
         width = 1080;
       } else {
         width = $(window).width() - 100; 
       } 
       
       height = $(window).height() - 50; // Устанавливаем высоту окна сравнения результатов
       
       // Формируем массив выбранных ID автомобилей
       elems.each(function(i){ 
         carsID[i] = $(this).attr("value");
       });

       url = template_folder + "/compare_load.php?id=" + carsID; // Задаем параметры в ссылке на документ
       
       // Делаем запрос на сравнение автомобилей
       $.ajax({
         type: "GET",
         url: url,
         dataType: "html",
         success: function(data) {
            if (data.length > 0)
            {
              $("#compare-window").empty();
              $("#compare-window").append(data);  
            }

            $("div.loading").hide(); // Убираем процесс загрузки
            
            // Показываем окно  
            $("#compare-window").dialog("destroy");
            $("#compare-window").dialog({
              width: width,
              minWidth: 800,
              height: height,
              minHeight: 400,
              modal: true,
              title: "Сравнение автомобилей",
              beforeclose: function() {
                elems.removeAttr("checked");
                $("div.item-auto").css("background-color","#e9e9e9"); 
              }
            });
         }
       });
        
     } else {
       
       // Показываем сообщение об ошибке
       $("#error-message").dialog("destroy");
       $("#error-message").dialog({
         modal: true,
         resizable: false,
         title: "&nbsp;",
         buttons: {
           Ок: function() {
             $(this).dialog('close');
           }
         }
       });
     }
                  
   return false; 
   
   }); 
}); 