jQuery.fn.table2CSV = function() {

  var csvData = new Array();

  $(this).find('tr').each(function() {
    var tmpRow = new Array();
    $(this).find('th, td').each(function() {
      if ($(this).css('display') != 'none' && !$(this).hasClass('hidden'))
        tmpRow.push(to_html($.trim($(this).text())));
    });
    var tmp = tmpRow.join('') // to remove any blank rows
    if (tmpRow.length > 0 && tmp != '')
      csvData.push(tmpRow.join(';.;'));
  });

  return csvData.join("\n");

};



$(document).ready(function() {

  $('div.navi a.disabled').click(function(e){
    e.preventDefault();
    return false;
  });

  $('div.buttons a.clearform').click(function(e){
    e.preventDefault();
    $('#filter form').clearForm();
    return false;
  });

  $('div.tabs a.filter').click(function(e){
    e.preventDefault();
    $('#filter').slideToggle('normal', function(){
      $('div.tabs a.filter').parent().toggleClass("tab_up");
      $('div.tabs a.filter').parent().toggleClass("tab_down");
    });
  });

  $('a.navishowall').click(function(e){
    var c = parseInt(gup($(this).attr('href'), 'showall'));
    if (c > 3000) {
      alert("Üle " + c + " kirje korraga laadimist teostada ei saa\nkuna see võib ületada serveri koormuse limiidi\n ning kindlasti ületab brauseri vastuvõtu võime piiri.")
      e.preventDefault();
    } else {
      return (c && c > 300)? confirm("Oled kindel?\n" + c + " kirje korraga laadimine võib võtta kaua aega ja arvutit märgatavalt aeglustada.") : true;
    }
  });
  var base = $('head base').attr('href');

  var loglistclick = function(menuid, id, navigation) {

    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
      xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
      xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlHttpReq) {
      xmlHttpReq.open('POST', base + 'json.php?type=loglistclick&menuID=' + menuid + '&id=' + id, true);
      xmlHttpReq.send();
      xmlHttpReq.onreadystatechange = function() {
        navigation();
      }
    } else {
      navigation();
    }
  }



  if ($('.def table.tbl').length > 0) {

    $('.def table.tbl tbody tr:not(.no_over) td a').click(function(e){
      e.stopPropagation()
      if ($(this).hasClass('delete') || $(this).hasClass('bind'))
        return confirm($(this).find('span').text());
    });

    $('.def table.tbl tbody tr:not(.no_over) td').click(function(event){
        $.loadingIndicator.init();
      var a = $(this).parent().find('a.delete, a.viewlink'), menuID, id, link, visited = $(this).parent().parent().hasClass('visited');
        if (a.length > 0) {
          menuID = gup($(a).attr('href'), 'menuID');
          id = gup($(a).attr('href'), 'id');
          link = $(a).attr('href').replace(/delete/gi, "view") + ((event.shiftKey)? '&edit=1' : '');
        } else {
          menuID = gup(document.location.href, 'menuID');
          if (!menuID)
            menuID = gup(document.location.href, 'menu');
          id = $(this).parent().find('td:first').text();
          link = '?menuID=' + menuID + '&action=view&id=' + id + ((event.shiftKey)? '&edit=1' : '');
        }
        if (menuID && id && !visited) {
          loglistclick(menuID, id, function(){
            document.location = link;
          });
        } else {
          document.location = link;
        }
    });

    $('#content_head a.export').show().click(function(e){
      e.preventDefault();
      var tabledata = $('.def table.tbl').clone();
      $(tabledata).find('a.delete').remove();
      $(tabledata).find('a.viewlink').remove();
      $('<form action="excel.php?type=general" target="blank" method="post"><input type="hidden" name="exportdata" value="'+ $(tabledata).table2CSV() +'" /></form>')
      .appendTo('body').submit().remove();
      $(tabledata).remove();

      return false;

    });
  }


});

function parseParams(url) {
  var params=url;
  if (url.match(/\?(.+)$/)) {
    // in case it is a full query string with ?, only take everything  after the ?
    params = RegExp.$1;
  }
  // split the params
  var pArray = params.split("&");
  // hash to store result
  var pHash = {};
  // parse each param in the array and put it in the hash
  for(var i=0;i<pArray.length;i++) {
    var temp = pArray[i].split("=");
    pHash[temp[0]] = unescape(temp[1]);
  }
  return pHash;
}
