var oTable;
var rows;
var rows_rendered = false;
var feature_filter = false;
var sort = "sort-price";
 
function postDraw(oSettings) {
  // Update ticket count
  $("#ticket-count").html(oSettings.fnDisplayEnd());
  
  // Set correct sort class
  if (oTable) {
    $("#sort-table a").removeClass('sorting_asc sorting_desc');
    var sortlink = $("#" + sort);
    sortlink.addClass($("#ticket-table thead th:eq(" + sortlink.attr("sort") + ")").attr("class"));
  }

  // Set correct ticket description class
  $(oSettings.aoOpenRows).each( function() {
    $(this.nTr).removeClass("even odd").attr("className", this.nParent.className);
  });
}

$(document).ready(function() {
  // Setup map zoom
  $(".zoomin").click(function() {
    $("#venue-map").panFullSize();
  });
  $(".zoomout").click(function() {
    $("#venue-map").normalView();
  });
  
  rows = $('#ticket-table tbody tr:nth-child(even)').remove();
  oTable = $('#ticket-table').dataTable({
    "bAutoWidth": false,
    "bInfo": false,
    "bLengthChange": false,
    "bPaginate": false,
    "aoColumns": [
      { "bSortable": false },
      null,
      null,
      {"sType": "currency"},
      null,
      null,
      null
    ],
    "oLanguage": {
      "sZeroRecords": '<a href="#" onClick="clearSearch()">Click here to go back</a>'
    },
    "aaSorting": [[6, 'desc'],[3, 'asc']],
    "fnDrawCallback": postDraw,
    "sDom": "rt"
  });
  
  // Add back info rows
  $(oTable.fnGetNodes()).each( function (i) {
    var row = $(this);
    oTable.fnOpen( this, $(rows[i]).children("td").html(), row.attr("class") + " ticket-description");
  });
  $("#ticket-table td.ticket-description").removeClass("even odd");
  
  $("#sort-section").bind("click", function() {
    sort = "sort-section";
    $("#ticket-table thead th:eq(1)").trigger("click");
  }).attr("sort", 1);
  $("#sort-row").bind("click", function() {
    sort = "sort-row";
    $("#ticket-table thead th:eq(2)").trigger("click");
  }).attr("sort", 2);
  $("#sort-price").bind("click", function() {
    sort = "sort-price";
    $("#ticket-table thead th:eq(3)").trigger("click");
  }).attr("sort", 3);
  $("#sort-quantity").bind("click", function() {
    sort = "sort-quantity";
    $("#ticket-table thead th:eq(5)").trigger("click");
  }).attr("sort", 5);
  $("#price-search").bind("click", function() {
    oTable.fnDraw();
  })
  $("#clear-search").bind("click", function() {
    clearSearch();
  })
  $("#featured-filter").bind("click", function() {
    feature_filter = true;
    oTable.fnDraw();
    return false;
  });
});

function clearSearch() {
  $("#price_min").val("");
  $("#price_max").val("");
  feature_filter = false;
  oTable.fnDraw();
}

/* Custom filtering function which will filter data in column three between two values */
$.fn.dataTableExt.afnFiltering.push(
 function( oSettings, aData, iDataIndex ) {
   var iMin = $('#price_min').val()  * 1;
   var iMax = $('#price_max').val()  * 1;
   var iPrice = aData[3].replace(",", "").substring(1) * 1;
   if ( iMin == "" && iMax == "" )
   {
     return true;
   }
   else if ( iMin == "" && iPrice < iMax )
   {
     return true;
   }
   else if ( iMin < iPrice && "" == iMax )
   {
     return true;
   }
   else if ( iMin <= iPrice && iPrice <= iMax )
   {
     return true;
   }
   return false;
 }
);

$.fn.dataTableExt.afnFiltering.push(
  function( oSettings, aData, iDataIndex ) {
    if (feature_filter) {
      if (aData[6] == 1) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  }
)

jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
	/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
	var x = a == "-" ? 0 : a.replace( /,/g, "" );
	var y = b == "-" ? 0 : b.replace( /,/g, "" );
	
	/* Remove the currency sign */
	x = x.substring( 1 );
	y = y.substring( 1 );
	
	/* Parse and return */
	x = parseFloat( x );
	y = parseFloat( y );
	return x - y;
};

jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
	/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
	var x = a == "-" ? 0 : a.replace( /,/g, "" );
	var y = b == "-" ? 0 : b.replace( /,/g, "" );
	
	/* Remove the currency sign */
	x = x.substring( 1 );
	y = y.substring( 1 );
	
	/* Parse and return */
	x = parseFloat( x );
	y = parseFloat( y );
	return y - x;
};
