$(function() {
  $("#modetab-search").click(function() {
    $("#modetab").attr("class", "search");
    $("#modepanel-search").show();
    $("#modepanel-browse").hide();
  });
  
  $("#modetab-browse").click(function() {
    $("#modetab").attr("class", "browse");
    $("#modepanel-search").hide();
    $("#modepanel-browse").show();
  });
  
  $("#mainnav-car").click(function() {
    $("#modetab-browse").click();
    $("#sJenis option:eq(1)").attr("selected", "selected").change();
  });
  
  $("#mainnav-motor").click(function() {
    $("#modetab-browse").click();
    $("#sJenis option:eq(2)").attr("selected", "selected").change();
  });
  
  $("#mainnav-ackattack").click(function() {
    $("#varT").removeAttr("disabled").val("AA");
    $("#modetab-browse").click();
    $("#sJenis option:eq(2)").attr("selected", "selected").change();
  });
  
  $("#mainnav-rallydakar").click(function() {
    $("#varT").removeAttr("disabled").val("RB");
    $("#modetab-browse").click();
    $("#sJenis option:eq(2)").attr("selected", "selected").change();
  });
  
  $("#mainnav-quadsbike").click(function() {
    $("#varT").removeAttr("disabled").val("QB");
    $("#modetab-browse").click();
    $("#sJenis option:eq(1)").attr("selected", "selected").change();
  });
  
  $("#mainnav-lotuscar").click(function() {
    $("#varT").removeAttr("disabled").val("LC");
    $("#modetab-browse").click();
    $("#sJenis option:eq(1)").attr("selected", "selected").change();
  });
  
  $("#quick-search").submit(function(event) {
    event.preventDefault();
    $.ajax({
      url:"quick_search.php",
      type:"POST",
      data:$(this).serialize(),
      success:function(response) {
        if (response == "") {
          $("#result-list").attr("disabled", true);
        }
        else {
          $("#result-list").html(response).removeAttr("disabled").change(getResult);
        }
      }
    });
    return false;
  });
  
  $("#sJenis").change(function() {
    $("#sMerk,#sType").html("").attr("disabled", true);
    $.ajax({
      url:"merk.php",
      type:"POST",
      data:$("#browse-form").serialize(),
      success:function(response) {
        if (response) {
          var merk = $("#sMerk").html(response).removeAttr("disabled").change(merkChanged);
          if (!$("#varT").is(":disabled"))
            merk.change();
        }
      }
    });
  });
  
  $(".btn-detail").click(function() {
    $("#popup-container").removeClass().addClass($("#sJenis").val()+(Math.floor(Math.random()*3)+1));
    var toppos = ($(window).height()-560)/2;
    var leftpos = ($(window).width()-528)/2;
    var me = $(this);
    $.blockUI({message:$("#popup-container"), css:{border:0,background:'transparent',width:528,height:560,left:leftpos,top:toppos,cursor:'normal'}});
    $.ajax({
      url:"product.php?id=" + $(this).data("pid"),
      type:"GET",
      success:function(response) {
        $("#popup-container").html(response);
        $("#popup-vehicle-name").html($("#vehiclename").html());
        $("#popup-protection").html("<img src='" + me.siblings(".img-protection").attr("src") + "' />");
      }
    })
  });
});

function merkChanged() {
  $.ajax({
    url:$("#sJenis").val() + "_type.php",
    type:"POST",
    data:$("#browse-form").serialize(),
    success:function(response) {
      if (response) {
        var jenis = $("#sType").html(response).removeAttr("disabled").change(getResult);
        if (!$("#varT").is(":disabled")) {
          jenis.change();
          $("#varT").attr("disabled", true);
        }
      }
    }
  });
}

function getResult() {
  $("#extra,#superior,#ultimate").hide();
  if ($(this).val() == "") return;
  var vid = $(this).val();
  var data = $(this).attr("id") == "result-list" ? {quick_id:vid} : ($("#sJenis").val() == "car" ? {car_id:vid} : {motor_id:vid});
  $.ajax({
    url:"result.php",
    type:"POST",
    data:data,
    success:function(result) {
      if (result) {
        eval("result="+result);
        $("#vehiclename").html(result.vehiclename);
        if (result.extraProtection) {
          $("#extra").show().find(".product-title").html(result.extraProtection.name);
          $("#extra").find(".product-image").attr("src", result.extraProtection.image);
          $("#extra").find(".btn-detail").data("pid", result.extraProtection.id);
        }
        if (result.superiorProtection) {
          $("#superior").show().find(".product-title").html(result.superiorProtection.name);
          $("#superior").find(".product-image").attr("src", result.superiorProtection.image);
          $("#superior").find(".btn-detail").data("pid", result.superiorProtection.id);
        }
        if (result.ultimateProtection) {
          $("#ultimate").show().find(".product-title").html(result.ultimateProtection.name);
          $("#ultimate").find(".product-image").attr("src", result.ultimateProtection.image);
          $("#ultimate").find(".btn-detail").data("pid", result.ultimateProtection.id);
        }
        $("#banner").hide();
        $("#result-wrapper").show();
      }
    }
  });
}
