function show_aircrafttype(event) {
   $("#products").hide();
   $("#manufacturer").hide();   
   if($("#aircrafttype").css("display") == "block"){
     $("#aircrafttype").hide();
   }else{
     $("#aircrafttype").show();
   }
   
   if(event && event.stopPropagation){
		 event.stopPropagation();	// для DOM-совместимых браузеров
   }else{
		 window.event.cancelBubble = true; //для IE
   }
    $(document).click(function(e){
      var target = $(e.target);
      if (target.is('#aircrafttype') || target.parents('#aircrafttype').length) return;
      $(document).unbind('click', arguments.callee);
      $("#aircrafttype").hide();
  });
 }
 
function show_products(event) {
   $("#aircrafttype").hide();
   $("#manufacturer").hide();
   if($("#products").css("display") == "block"){
     $("#products").hide();
   }else{
     $("#products").show();
     $("#products").expose({color: '#000', opacity: '0.3'});
   }   
   
   if(event && event.stopPropagation){
		 event.stopPropagation();	// для DOM-совместимых браузеров
   }else{
		 window.event.cancelBubble = true; //для IE
   }
    $(document).click(function(e){
      var target = $(e.target);
      if (target.is('#products') || target.parents('#products').length) return;
      $(document).unbind('click', arguments.callee);
      $("#products").hide();
  });
 }
 
 function show_manufacturer(event) {
   $("#aircrafttype").hide();
   $("#products").hide();
   if($("#manufacturer").css("display") == "block"){
     $("#manufacturer").hide();
   }else{
     $("#manufacturer").show();
     $("#manufacturer").expose({color: '#000', opacity: '0.3'});
   }   
   if(event && event.stopPropagation){
		 event.stopPropagation();	// для DOM-совместимых браузеров
   }else{
		 window.event.cancelBubble = true; //для IE
   }
    $(document).click(function(e){
      var target = $(e.target);
      if (target.is('#manufacturer') || target.parents('#manufacturer').length) return;
      $(document).unbind('click', arguments.callee);
      $("#manufacturer").hide();
  });
 }
   
   
$().ready(function(){
  /*aircrafttype*/  
  $("#aircrafttypecheckall").click(function(){
    $val = "";
    $("#aircrafttype input:checkbox").each(function(index) {
      if($(this).next("label").children().get(0)){
        $tagname = $(this).next("label").children().get(0).tagName;
      }else{
        $tagname = "";
      }
      if($tagname=="IMG"){
        $val += $(this).next("label").children().attr("alt")+" ";
      }else{
        if($tagname=="SPAN"){
          $val += $(this).next("label").children().html()+" ";
        }else{
          $val += $(this).next("label").html()+" ";
        }
      }
      $(this).attr("checked", "checked");
    });
    $("#aircrafttype-sel div").html("");
    $("#aircrafttype-sel div").html($val);
  });
  
  $("#aircrafttypeuncheckall").click(function(){
    $("#aircrafttype input:checkbox").each(function(index) {
      $(this).attr("checked", ""); 
    });
    $("#aircrafttype-sel div").html("");
  });  
  
  $(".aircrafttypecheckforsub").change(function(){
    $id = $(this).attr("id").substr(9);
    $this_checked = $(this).attr("checked");
    if($this_checked){
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#aircrafttype-sel div").html();
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html()+" "
        $ischecked = $(this).attr("checked");
        if(!$ischecked){
          $checkforsubval += $val;
          $divhtml = $divhtml.replace($val+" ", "");
          $("#aircrafttype-sel div").html($divhtml);
        }
      });
      $("#aircrafttype-sel div").html($checkforsubval+" "+$("#aircrafttype-sel div").html());
      $("#allchildul"+$id+" input").attr("checked", "checked");
    }else{
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#aircrafttype-sel div").html();
      $divhtml = $divhtml.replace($checkforsubval, "");
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html();
        $divhtml = $divhtml.replace($val+" ", "");
      });
      $("#aircrafttype-sel div").html($divhtml);      
      $("#allchildul"+$id+" input").attr("checked", "");
    }
  });
  
  $(".aircrafttypecheckcheck").change(function(){
    $val = $(this).next("label").html();
    $this_checked = $(this).attr("checked");
    $parenttag = $(this).parents("ul").parents("li").children("input").next("label").children().get(0).tagName;
    if($parenttag=="IMG"){
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().attr("alt");
    }else{
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().html();
    }
    $parentid = $(this).parents("ul").parents("li").children("input").attr("id").substr(9);
    if($this_checked){
      $checked = 1;
      $("#aircrafttype-sel div").html($val+" "+$("#aircrafttype-sel div").html());
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if($checked){
          $checked = $(this).attr("checked");          
        }
      });
      if($checked){
        $("#aircrafttype-sel div").html($parentval+" "+$("#aircrafttype-sel div").html());
        $("#allparent"+$parentid).attr("checked", "checked")
      }
    }else{
      $checked = 0;
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if(!$checked){
          $checked = $(this).attr("checked");          
        }
      });
      
      if(!$checked){
        $divhtml = $("#aircrafttype-sel div").html();
        $divhtml = $divhtml.replace($parentval+" ", "");
        $("#aircrafttype-sel div").html($divhtml)
        $("#allparent"+$parentid).attr("checked", "");
      }
      
      $divhtml = $("#aircrafttype-sel div").html();
      $divhtml = $divhtml.replace($val+" ", "");
      $("#aircrafttype-sel div").html($divhtml);
    }
  })
  /*//aircrafttype*/
  
  
  
  /*products*/  
  $("#productscheckall").click(function(){
    $val = "";
    $("#products input:checkbox").each(function(index) {
      if($(this).next("label").children().get(0)){
        $tagname = $(this).next("label").children().get(0).tagName;
      }else{
        $tagname = "";
      }
      if($tagname=="IMG"){
        $val += $(this).next("label").children().attr("alt")+" ";
      }else{
        if($tagname=="SPAN"){
          $val += $(this).next("label").children().html()+" ";
        }else{
          $val += $(this).next("label").html()+" ";
        }
      }
      $(this).attr("checked", "checked");
    });
    $("#products-sel div").html("");
    $("#products-sel div").html($val);
  });
  
  $("#productsuncheckall").click(function(){
    $("#products input:checkbox").each(function(index) {
      $(this).attr("checked", ""); 
    });
    $("#products-sel div").html("");
  });  
  
  $(".productscheckforsub").change(function(){
    $id = $(this).attr("id").substr(9);
    $this_checked = $(this).attr("checked");
    if($this_checked){
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#products-sel div").html();
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html()+" "
        $ischecked = $(this).attr("checked");
        if(!$ischecked){
          $checkforsubval += $val;
          $divhtml = $divhtml.replace($val+" ", "");
          $("#products-sel div").html($divhtml);
        }
      });
      $("#products-sel div").html($checkforsubval+" "+$("#products-sel div").html());
      $("#allchildul"+$id+" input").attr("checked", "checked");
    }else{
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#products-sel div").html();
      $divhtml = $divhtml.replace($checkforsubval, "");
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html();
        $divhtml = $divhtml.replace($val+" ", "");
      });
      $("#products-sel div").html($divhtml);      
      $("#allchildul"+$id+" input").attr("checked", "");
    }
  });
  
  $(".productscheckcheck").change(function(){
    $val = $(this).next("label").html();
    $this_checked = $(this).attr("checked");
    $parenttag = $(this).parents("ul").parents("li").children("input").next("label").children().get(0).tagName;
    if($parenttag=="IMG"){
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().attr("alt");
    }else{
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().html();
    }
    $parentid = $(this).parents("ul").parents("li").children("input").attr("id").substr(9);
    if($this_checked){
      $checked = 1;
      $("#products-sel div").html($val+" "+$("#products-sel div").html());
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if($checked){
          $checked = $(this).attr("checked");          
        }
      });
      if($checked){
        $("#products-sel div").html($parentval+" "+$("#products-sel div").html());
        $("#allparent"+$parentid).attr("checked", "checked")
      }
    }else{
      $checked = 0;
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if(!$checked){
          $checked = $(this).attr("checked");          
        }
      });
      
      if(!$checked){
        $divhtml = $("#products-sel div").html();
        $divhtml = $divhtml.replace($parentval+" ", "");
        $("#products-sel div").html($divhtml)
        $("#allparent"+$parentid).attr("checked", "");
      }
      
      $divhtml = $("#products-sel div").html();
      $divhtml = $divhtml.replace($val+" ", "");
      $("#products-sel div").html($divhtml);
    }
  })
  /*//products*/
  
  
  /*manufacturer*/  
  $("#manufacturercheckall").click(function(){
    $val = "";
    $("#manufacturer input:checkbox").each(function(index) {
      if($(this).next("label").children().get(0)){
        $tagname = $(this).next("label").children().get(0).tagName;
      }else{
        $tagname = "";
      }
      if($tagname=="IMG"){
        $val += $(this).next("label").children().attr("alt")+" ";
      }else{
        if($tagname=="SPAN"){
          $val += $(this).next("label").children().html()+" ";
        }else{
          $val += $(this).next("label").html()+" ";
        }
      }
      $(this).attr("checked", "checked");
    });
    $("#manufacturer-sel div").html("");
    $("#manufacturer-sel div").html($val);
  });
  
  $("#manufactureruncheckall").click(function(){
    $("#manufacturer input:checkbox").each(function(index) {
      $(this).attr("checked", ""); 
    });
    $("#manufacturer-sel div").html("");
  });  
  
  $(".manufacturercheckforsub").change(function(){
    $id = $(this).attr("id").substr(9);
    $this_checked = $(this).attr("checked");
    if($this_checked){
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#manufacturer-sel div").html();
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html()+" "
        $ischecked = $(this).attr("checked");
        if(!$ischecked){
          $checkforsubval += $val;
          $divhtml = $divhtml.replace($val+" ", "");
          $("#manufacturer-sel div").html($divhtml);
        }
      });
      $("#manufacturer-sel div").html($checkforsubval+" "+$("#manufacturer-sel div").html());
      $("#allchildul"+$id+" input").attr("checked", "checked");
    }else{
      $checkforsub = $(this).next("label").children().get(0).tagName;
      if($checkforsub=="IMG"){
        $checkforsubval = $(this).next("label").children().attr("alt")+" ";
      }else{
        $checkforsubval = $(this).next("label").children().html()+" ";
      }
      $divhtml = $("#manufacturer-sel div").html();
      $divhtml = $divhtml.replace($checkforsubval, "");
      $("#allchildul"+$id+" input:checkbox").each(function(index) {
        $val = $(this).next("label").html();
        $divhtml = $divhtml.replace($val+" ", "");
      });
      $("#manufacturer-sel div").html($divhtml);      
      $("#allchildul"+$id+" input").attr("checked", "");
    }
  });
  
  $(".manufacturercheckcheck").change(function(){
    $val = $(this).next("label").html();
    $this_checked = $(this).attr("checked");
    $parenttag = $(this).parents("ul").parents("li").children("input").next("label").children().get(0).tagName;
    if($parenttag=="IMG"){
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().attr("alt");
    }else{
      $parentval = $(this).parents("ul").parents("li").children("input").next("label").children().html();
    }
    $parentid = $(this).parents("ul").parents("li").children("input").attr("id").substr(9);
    if($this_checked){
      $checked = 1;
      $("#manufacturer-sel div").html($val+" "+$("#manufacturer-sel div").html());
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if($checked){
          $checked = $(this).attr("checked");          
        }
      });
      if($checked){
        $("#manufacturer-sel div").html($parentval+" "+$("#manufacturer-sel div").html());
        $("#allparent"+$parentid).attr("checked", "checked")
      }
    }else{
      $checked = 0;
      $("#allchildul"+$parentid+" input:checkbox").each(function(index) {
        if(!$checked){
          $checked = $(this).attr("checked");          
        }
      });
      
      if(!$checked){
        $divhtml = $("#manufacturer-sel div").html();
        $divhtml = $divhtml.replace($parentval+" ", "");
        $("#manufacturer-sel div").html($divhtml)
        $("#allparent"+$parentid).attr("checked", "");
      }
      
      $divhtml = $("#manufacturer-sel div").html();
      $divhtml = $divhtml.replace($val+" ", "");
      $("#manufacturer-sel div").html($divhtml);
    }
  })
  /*//manufacturer*/  
  
  
$(window).keyup(function(event) {
  if(event.keyCode==27){
    $('#manufacturer').hide();
    $('#products').hide();
  }
});  
  
})   
