//fnFindCellRowIndexes didn't work with server procession. > therefore this new function

// each column will be numbered 0, 1, 2, ... etc from the left to right. 
// tell the function which column to base the string check on via iColumn parameter.
// tableToCheck is the html id of the table you'd like to check, example: '#thetable'
//
// example usage: 
// var accountnumber, rowIndex;
// accountnumber = '1234567890';
// rowIndex = rowfindRowIndexUsingCol(accountnumber, '#comcast', 0);
//
function findRowIndexUsingCol(StringToCheckFor, tableToCheck, iColumn){
	// Initialize variables
	var i, aData, sValue, IndexLoc, oTable, iColumn;
	
	oTable = $(tableToCheck).dataTable();
	aiRows = oTable.fnGetNodes();
	
	for (i=0,c=aiRows.length; i<c; i++) {
		iRow = aiRows[i];	// assign current row to iRow variable
		aData = oTable.fnGetData(iRow);	// Pull the row
		
		sValue = aData[iColumn];	// Pull the value from the corresponding column for that row
		
		if(sValue == StringToCheckFor){
			IndexLoc = i;
			break;
		}
	}
	
	return IndexLoc;
}


$.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
	return this.each(function(){
		var clicks = 0, self = this;
		jQuery(this).click(function(event){
		  clicks++;
		  if (clicks == 1) {
			setTimeout(function(){
				if(clicks == 1) {
					single_click_callback.call(self, event);
				} else {
					double_click_callback.call(self, event);
				}
				clicks = 0;
				}, timeout || 300);
			}
		});
	});
}

/*
 * Function: fnGetDisplayNodes
 * Purpose:  Return an array with the TR nodes used for displaying the table
 * Returns:  array node: TR elements
 *           or
 *           node (if iRow specified)
 * Inputs:   object:oSettings - automatically added by DataTables
 *           int:iRow - optional - if present then the array returned will be the node for
 *             the row with the index 'iRow'
 */

$.fn.dataTableExt.oApi.fnFindCellRowIndexes = function ( oSettings, sSearch, iColumn ){
	var i,iLen, j, jLen,aOut = [], aData;
	for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ ){
		aData = oSettings.aoData[i]._aData;
		if ( typeof iColumn == 'undefined' ){
			for ( j=0, jLen=aData.length ; j<jLen ; j++ ){
				if ( aData[j] == sSearch ){
					aOut.push( i );
				}
			}
		}
		else if ( aData[iColumn] == sSearch ){
			aOut.push( i );
		}
	}
	return aOut;
}


$.fn.dataTableExt.oApi.fnGetDisplayNodes = function ( oSettings, iRow )
{
	var anRows = [];
	if ( oSettings.aiDisplay.length !== 0 ){
		if ( typeof iRow != 'undefined' ){
			return oSettings.aoData[ oSettings.aiDisplay[iRow] ].nTr;
		}
		else{
			for ( var j=oSettings._iDisplayStart ; j<oSettings._iDisplayEnd ; j++ ){
				var nRow = oSettings.aoData[ oSettings.aiDisplay[j] ].nTr;
				anRows.push( nRow );
			}
		}
	}
	return anRows;
};


$.fn.dataTableExt.oApi.fnGetFilteredData = function ( oSettings ) {
	var a = [];
	for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
		a.push(oSettings.aoData[ oSettings.aiDisplay[i] ]._aData);
	}
	return a;
}

$.fn.dataTableExt.oApi.fnGetAdjacentTr  = function ( oSettings, nTr, bNext ) {
	var iCurrent = oSettings.oApi._fnNodeToDataIndex( oSettings, nTr );
	var iDisplayIndex = $.inArray( iCurrent, oSettings.aiDisplay );
	if ( iDisplayIndex == -1 ){
		return null;
	}
	iDisplayIndex += (typeof bNext=='undefined' || bNext) ? 1 : -1;
	if ( iDisplayIndex < 0 || iDisplayIndex >= oSettings.aiDisplay.length ){
		return null;
	}
	return oSettings.aoData[ oSettings.aiDisplay[ iDisplayIndex ] ].nTr;
}

function fnFormatDetails (oTable, nTr ){
	var aData = oTable.fnGetData( nTr );
	var mydata= aData[9].replace(/<!--|-->/gi,'')
	var sOut 	= '<div style="width:640px;margin: 0 0 0 0px;padding:10px 0 10px 100px;"class="bigtype innerDetails">' + mydata + '</div>';
	//sOut 		+=	'<td></tr>';
	//</table>';
	return sOut;
}


var manual='';
var oTable;
var giRedraw = false;
var aPos ='0';
var pos='';
var nNext='';
var nPrev='';
var current_track='';
var explore_on='false';
var lastfilter='';
$(document).ready(function() {
	oTable = $('#example').dataTable( {
		"bProcessing": false,
		"bServerSide": false,
		"sAjaxSource": "_version2/server_processing.php",
		"aoColumns": [ 
			/* ID */    { "sWidth": "25px"},
			/* guest */ { "sWidth": "210px"},
			/* url */  { 	"bVisible":    false, "sWidth": "0px"},
			/* audio lenght */ { "sWidth": "60px"},
			/* info */ { 	"bSearchable":    false ,"bVisible": false , "sWidth": "0px"},
			/* date */    { "sWidth": "80px"},
			/* sort */   { "sWidth": "70px"},
			/* event */    { "sWidth": "130px"},
			/* countall */    { "sWidth": "50px"},
			/* html  */ { 	"bSearchable":    false ,"bVisible":    false , "sWidth": "0px"}
		],
		"bAutoWidth": false,	
		"bInfo": false,
		"bPaginate": false,
		"bJQueryUI": false,
		"fnInitComplete": function() { 
		
			/*
			 * Insert a 'details' column to the table
			 */
			var nCloneTh = document.createElement( 'th' );
			nCloneTh.style.minWidth="29px";
			nCloneTh.style.maxWidth="29px";
			nCloneTh.style.paddingLeft="12px";
			nCloneTh.style.paddingRight="4px";
			var nCloneTd = document.createElement( 'td' );
			nCloneTd.style.minWidth="25px";
			nCloneTd.style.maxWidth="25px";
			nCloneTd.style.paddingLeft="12px";
			nCloneTd.style.paddingRight="4px";
			nCloneTd.innerHTML = '<img src="_version2/_media/_pics/details_open.png" class="open_close">';
			
			$('#example thead tr').each( function (){ this.insertBefore(  nCloneTh, this.childNodes[0] );});
			$('#example tbody tr').each( function (){ this.insertBefore(  nCloneTd.cloneNode( true ), this.childNodes[0] );});
		
			$('#listen_table').fadeIn(100);
			$.getScript('_version2/_javascript/stuff3.js');
			
		}		
	});
});


