$(function() {
  var b = window.location.pathname, d = window.location.search;
  $("#main-menu").find("a:nth-child(" + ("/search,/dev,/reports,/blog,/about".split(",").indexOf(b) + 1) + ") li").addClass("selected");
  b.match(/^(\/|\/index\.php)$/i) && ($("#main-menu").find("a:nth-child(1) li").addClass("selected"), load_mini_map());
  b.match(/^\/home\/dev/i) && ($("#main-menu").find("a:nth-child(1) li").addClass("selected"), load_mini_map());
  b.match(/search/i) && show_tabdata();
  $("#find-form").find(".ui-icon-search").bind("click", function() {
    $("#find-form").submit()
  });
  $("#statistic-canvas .tabs-menu").find("li").click(function() {
    $("#statistic-canvas .tabs-menu .selected").removeClass("selected");
    $(this).addClass("selected");
    $("#statistic-canvas").find(".tabs-panel").hide();
    $("#statistic-" + ($(this).index() + 1)).fadeIn(800);
    $("#statistic-canvas").find(".total-value").hide();
    $("#total-" + $(this).index()).fadeIn(800)
  });
  b.match(/statistic/i) && !d && (load_th_map(), load_graph(1), init_tt());
  $("#graph-canvas .tabs-menu").find("li").click(function() {
    $("#graph-canvas .tabs-menu .selected").removeClass("selected");
    $(this).addClass("selected");
    $("#graph-canvas").find(".tabs-panel").hide();
    $("#graph-" + ($(this).index() + 1)).fadeIn(800);
    b.match(/statistic/i) && load_graph($(this).index() + 1)
  });
  b.match(/internet/i) && init_internet_page();
  $("td.self_expand").click(function() {
    $(this).html($(this).attr("rel"));
    $(this).unbind("click")
  })
});
function social(email){
  $.get('/social/',{
    'q':email
  },function(data){
    if(data){
      $('#load_social_here').html('');
      $.each(data.print,function(k,v){
        $('#load_social_here').append(v);
      })
    }
  },'json');
}
function search_paging() {
  $("#severity-paging a").click(function(b) {
    $(".page_cur").removeClass("page_cur");
    $(this).parent().siblings().addClass("page_tab");
    $(this).parent().addClass("page_cur").removeClass("page_tab");
    i = $(this).parent().index();
    s = $(".pager").children().size();
    i != s / 2 && console.log(i - (s / 2).toFixed());
    b.preventDefault();
    q = window.location.search.split("=")[1];
    t = $(this).attr("href").split(",");
    $.post("/statistic/search", {
      query:q, 
      type:t[0], 
      page:t[1].split("=")[1]
    }, function(b) {
      $("#search-" + t[0].substr(1)).html("");
      $("#search-" + t[0].substr(1)).html(b)
    })
  })
}
function add_paging_element(b) {
  for(i = 0;i < b;i++) {
    $(".pager").index(i + 1)
  }
}
function load_graph(b) {
  $.post("demo/gen_graph", function(d) {
    function f(b, d, f) {
      var h = [];
      $(f.data).each(function(b, d) {
        h[b] = [Date.UTC(d[0], d[1] - 1, d[2]), d[3]]
      });
      window.chart[b] = new Highcharts.StockChart({
        chart:{
          renderTo:"graph-" + b
        }, 
        rangeSelector:{
          enabled:!1
        }, 
        global:{
          useUTC:!1
        }, 
        title:{
          text:f.title
        }, 
        scrollbar:{
          enabled:!1
        }, 
        navigator:{
          enabled:!1
        }, 
        navigation:{
          buttonOptions:{
            enabled:!1
          }
        }, 
        xAxis:{}, 
        yAxis:{
          title:{
            text:"Hits/Days"
          }, 
          min:0, 
          max:f.max
        }, 
        series:[{
          name:d, 
          data:h
        }]
      })
    }
    window.chart = [];
    switch(b) {
      case 1:
        f(b, "webattack", d.webattack);
        break;
      case 2:
        f(b, "webphishing", d.webphishing);
        break;
      case 3:
        f(b, "webmalware", d.webmalware);
        break;
      case 4:
        f(b, "severity", d.severity);
        break;
      case 5:
        f(b, "proxylist", d.proxylist);
        break;
    }
  }, "json")
}
function show_tabdata() {
  $(".tabmenu li").bind("click", function() {
    $(".tabmenu").find("li").removeClass("selected");
    $(this).addClass("selected");
    $(".tabdata").hide();
    $("#" + $(this).attr("d")).show();
    $(this).attr("d") == "ip-geo" && load_ip_geo()
  });
  $(".tabmenu").find("li:nth-child(1)").trigger("click")
}
function load_ip_geo() {
  var b = new google.maps.Map(document.getElementById("ip-geo-canvas"));
  mark_this(b, $("#ip-geo-canvas").attr("l"), $("#ip-geo-canvas").attr("t"), !0, 13)
}
function load_mini_map() {
  map = new google.maps.Map(document.getElementById("mini-map"));
  mark_this(map, $("#mini-map").attr("l"), $("#mini-map").attr("t"), !0, 13)
}
function mark_this(b, d, f, g, j, k) {
  myLatlng = new google.maps.LatLng(d, f);
  var h, l;
  b.setOptions({
    minZoom:2, 
    scrollwheel:!1, 
    zoom:j, 
    center:myLatlng, 
    mapTypeId:google.maps.MapTypeId.ROADMAP
  });
  g && (h = new google.maps.Marker({
    map:b, 
    position:new google.maps.LatLng(d, f), 
    icon:k
  }), typeof g == "string" && (l = new google.maps.InfoWindow({
    content:g, 
    size:new google.maps.Size(50, 50)
  }), google.maps.event.addListener(h, "click", function() {
    l.open(b, h)
  })))
}
function load_th_map() {
  map = new google.maps.Map(document.getElementById("th-map-image"));
  mark_this(map, 13.0376, 101.491373, !0, 5)
}
function init_tt() {
  $(".tip_trigger").hover(function() {
    tip = $(this).find(".tip");
    tip.show()
  }, function() {
    tip.hide()
  }).mousemove(function(b) {
    var d = b.pageX + 20, f = b.pageY + 20, g = tip.width(), j = tip.height(), k = $(window).width() - (d + g), h = $(window).height() - (f + j);
    k < 20 && (d = b.pageX - g - 20);
    h < 20 && (f = b.pageY - j - 20);
    tip.css({
      top:f, 
      left:d
    })
  })
}
function init_internet_page() {
  load_wifi_map();
  show_tabdata();
  trigger_wifi_map_center();
  loadReport();
  load_speedtest_map();
  $(".find-textbox").find_textbox_fill("example: macaddr=68:7F:74:70:24:2B")
}
function load_wifi_map() {
  risk_level = {
    low:"green", 
    medium:"yellow", 
    high:"red"
  };
  map = new google.maps.Map(document.getElementById("wifi-map-canvas"));
  $.each(wifi_result, function(b, d) {
    msg = mlbls("", {
      Date:d.lasttime, 
      "MAC Address":d.macaddr, 
      Vendor:d.vendor, 
      SSID:d.ssid, 
      ISP:d.isp
    });
    mark_this(map, d.latitude, d.longitude, msg, 5, "/lib/img/circle-" + (d.risk ? risk_level[d.risk] : "grey") + ".png")
  });
  map.setCenter(new google.maps.LatLng(13.8662, 100.494), 5)
}
function trigger_wifi_map_center() {
  $("tr.wifi-location-records").hover(function() {
    $(this).find("td").css({
      "background-color":"#ff8080", 
      color:"#fff"
    })
  }, function() {
    $(this).find("td").css({
      "background-color":"", 
      color:""
    })
  });
  $("tr.wifi-location-records").click(function() {
    map.setCenter(new google.maps.LatLng($(this).attr("coor").split(",")[0], $(this).attr("coor").split(",")[1]), 7)
  })
}
function load_speedtest_map() {
  $("li:contains('Speed Test')").click(function() {
    speedtest_map = new google.maps.Map(document.getElementById("speedtest-map-canvas"));
    $.each(speedtest_result, function(b, d) {
      msg = mlbls("", {
        Date:d.timestamp, 
        Bandwidth:d.bandwidth, 
        Address:d.address, 
        ISP:d.isp
      });
      mark_this(speedtest_map, d.latitude, d.longitude, msg, 5)
    })
  })
}
function mlbls(b, d) {
  b = typeof b == "undefined" ? "" : b;
  $.each(d, function(d, g) {
    b += htags("b", [], d) + ": " + g + "</br>"
  });
  return b
}
function htags(b, d, f) {
  a = "";
  typeof d == "object" ? $.each(d, function(b, d) {
    a += " " + b + '="' + d + '" '
  }) : typeof d != "null" && (a = d);
  e = typeof f == "string" ? ">" + f + "</" + b : "/";
  return"<" + b + a + e + ">"
}
function loadReport() {
  function b() {
    $("#report_select_menu_city").find("optgroup").html(report_select_menu_city_default_val);
    $.get("/internet/option_city", function(b) {
      $("#report_select_menu_city").find("optgroup").append(b);
      $("#report_select_menu_district").disable();
      $("#report_select_menu_city").change(function() {
        $("#report_select_menu_district").disable();
        $("#report_select_menu_district").find("optgroup").html(report_select_menu_district_default_val);
        $(this).val() != "null" && $(this).val() != "any" && $.post("/internet/option_dist", {
          c:$(this).val()
        }, function(b) {
          $("#report_select_menu_district").find("optgroup").append(b);
          $("#report_select_menu_district").enable()
        })
      })
    })
  }
  function d(b, g) {
    z = $("#report_select_menu_district").val();
    i = $("#report_select_menu_isp").val();
    t = $("#report_select_menu_type").val();
    c = $("#report_select_menu_city").val();
    $.post("/internet/show_report/", {
      z:z, 
      i:i, 
      t:t, 
      c:c, 
      page:b
    }, function(b) {
      g.show();
      $(".pager", g).remove();
      g.find("tbody").html(b.split("##")[0]);
      g.parent().find("#report_lbl").find("h4").html("Total Average bandwidth: " + b.split("##")[2]);
      g.append(b.split("##")[1]);
      $(".pager a", g).click(function() {
        d($(this).attr("page"), g)
      });
      $(".expand").click(function() {
        $(this).parent().html($(this).parent().attr("rel"))
      })
    })
  }
  report_select_menu_city_default_val = $("#report_select_menu_city").find("optgroup").html();
  report_select_menu_district_default_val = $("#report_select_menu_district").find("optgroup").html();
  (function() {
    $.get("/internet/option_isp", function(d) {
      $("#report_select_menu_isp").find("optgroup").append(d);
      $("#report_select_menu_isp").change(function() {
        $(this).val() == "\u0e40\u0e25\u0e37\u0e2d\u0e01\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14" ? $('#report_select_menu_city option[value="any"]').remove() : b()
      })
    })
  })();
  b();
  (function() {
    $.get("/internet/tt/", function(b) {
      b && ($("#report_counting_table tr:nth-child(1) td:nth-child(2)").html(b.split(",")[0]), $("#report_counting_table tr:nth-child(2) td:nth-child(2)").html(b.split(",")[1]), $("#report_counting_table tr:nth-child(3) td:nth-child(2)").html(b.split(",")[2]), $("#report_counting_table tr:nth-child(4) td:nth-child(2)").html(b.split(",")[3]))
    })
  })();
  $("#report_form").submit(function(b) {
    b.preventDefault();
    $("#report_table_list").hide();
    $("#report_table_unique").hide();
    $("#report_lbl").find("h4").html("");
    $("#report_select_menu_type").val() == "list" ? d(1, $("#report_table_list")) : $("#report_select_menu_type").val() == "unique" && d(1, $("#report_table_unique"))
  })
}
(function(b) {
  b.fn.disable = function() {
    return this.attr("disabled", !0).addClass("disabled")
  };
  b.fn.enable = function() {
    return this.removeClass("disabled").attr("disabled", !1)
  };
  b.fn.find_textbox_fill = function(b) {
    var f = !1, g = this;
    g.val(b);
    g.keydown(function() {
      f = !0
    });
    g.mousedown(function() {
      f || g.val("")
    })
  }
})(jQuery);
