$(document).ready(function(){ 
  
  // ------------------------------------------------------------------
  //  Paging handler for news list.
  // ------------------------------------------------------------------
  
  $("a[rel*=page]").click(function(){
    
    // Store id of clicked anchor
    p_id = $(this).attr("id");
    
    // Ajax
    $.ajax({
      type: "GET",
      url: this.href + "/TRUE",
      success: function(html){
        // Append result into div
        $("#news").html(html);
        
        // Force page to top of page
        window.location = '#top';
        
        // Set selected pagination class
        $("a[rel*=page]").each(function(){
          $(this).removeClass("selected");
          
          if($(this).attr("id") == p_id){
            $(this).addClass("selected");
          }
        });
      }
    });
    
    return false;
    
  });
  
  // ------------------------------------------------------------------
  //  Handler for navigating Suara Blogger in Vox Populi page.
  // ------------------------------------------------------------------
  
  $("a[rel=suarablogger_link]").click(function(){
  
    // Set value for later calculation
    calc = (this.id == "suarablogger_link_next") ? 1 : -1;
    
    // Current data position
    p_id = parseInt($("#suarablogger_link_id").val());
    
    // Ajax
    $.ajax({
      type: "POST",
      url: "voxpopuli/suarablogger/" + (p_id + calc),
      success: function(html){
        // Check for data existance
        if(html != '')
        {
          // Append result into div
          $("#suara_blogger").html(html);
        
          // Force page to top of Suara Seleb box
          window.location = '#suarablogger';
          
          // Set position of current data
          $("#suarablogger_link_id").val(p_id + calc);
        }
      }
    });
    
    return false;
    
  });
  
  // ------------------------------------------------------------------
  //  Handler for navigating Suara Seleb in Vox Populi page.
  // ------------------------------------------------------------------
  
  $("a[rel=suaraseleb_link]").click(function(){
  
    // Set value for later calculation
    calc = (this.id == "suaraseleb_link_next") ? 1 : -1;
    
    // Current data position
    p_id = parseInt($("#suaraseleb_link_id").val());
    
    // Ajax
    $.ajax({
      type: "POST",
      url: "voxpopuli/suaraseleb/" + (p_id + calc),
      success: function(html){
        // Check for data existance
        if(html != '')
        {
          // Append result into div
          $("#suara_seleb").html(html);
        
          // Force page to top of Suara Seleb box
          window.location = '#suaraseleb';
          
          // Set position of current data
          $("#suaraseleb_link_id").val(p_id + calc);
        }
      }
    });
    
    return false;
    
  });
  
  // ------------------------------------------------------------------
  //  Handler for navigating Vox Populi.
  // ------------------------------------------------------------------
  
  $("a[rel=voxpopuli_link]").click(function(){
  
    // Set value for later calculation
    calc = (this.id == "voxpopuli_link_next") ? 1 : -1;
    
    // Current data position
    p_id = parseInt($("#voxpopuli_link_id").val());
    
    // Ajax
    $.ajax({
      type: "POST",
      url: "voxpopuli/page/" + (p_id + calc) + "/TRUE",
      success: function(html){
        // Check for data existance
        if(html != '')
        {
          // Append result into div
          $("#vox_populi").html(html);
        
          // Force page to top of voxpopuli box
          window.location = '#top';
          
          // Set position of current data
          $("#voxpopuli_link_id").val(p_id + calc);
        }
      }
    });
    
    return false;
    
  });
  
  // ------------------------------------------------------------------
  //  Handler for quick count 
  // ------------------------------------------------------------------
  
  $("#surveyor").change(function(){
    
    var surveyor_id = $("#surveyor").val();
    
    if(surveyor_id != "") {
      // Ajax
      $.ajax({
        type: "POST",
        url: "quickcount/view/" + surveyor_id,
        success: function(html){
          // Check for data existance
          if(html != '')
          {
            // Append result into div
            $("#candidate").html(html);
            
            // Force page to top of quickcount box
            window.location = '#quickcount';
          }
        }
      });
    }
      
    return false;
    
  });
  
  // ------------------------------------------------------------------
  //  Hehe.. :)
  // ------------------------------------------------------------------
  
  if(window.addEventListener) {
    var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
    window.addEventListener("keydown", function(e){
      kkeys.push(e.keyCode);
      if(kkeys.toString().indexOf( konami ) >= 0)
        window.location = "http://indonesiamemilih.kompas.com/assets/images/em.jpg";
    }, true);
  }
  
}); 
