var ELEMENTS_PER_PAGE = 10;

$j(document).ready(function() {
	var table = $j("#searchable");
	
	var childrenNodes = table.children();
	
	var tableHead = childrenNodes[0];
	var tableBody = childrenNodes[1];	
	
	var start_index = document.getElementById("page_index").getAttribute('value');
	
	var first_index = eval(start_index * ELEMENTS_PER_PAGE);
	
	var last_index = eval(first_index + ELEMENTS_PER_PAGE);	
	
	var nr_randuri = ChangeContent(first_index, last_index);
	
	drawPager(eval(nr_randuri/ELEMENTS_PER_PAGE));
	
	var filters = DisplayInputSearchFilters();
	
	//retrieveFilterValues(filters);
});


$j(function() {
		
		$j("#search").click(function() {
			
			var filters = getFilterList();
			
			var search_values = retrieveFilterValues(filters);
			
			var results = filterSearchValues(search_values);
			//alert(results);
			drawPager(eval(results/ELEMENTS_PER_PAGE));																		
			return false;
		});
		
		$j("a.LHeader").click(function() {			
			changeActiveElement($j(this).parent().attr("id"));	
			var new_index = getCurrentIndex($j(this).parent().attr("id"));
			ChangeHeaderContent(new_index);	
			var a_el = $j("div.Element-Active");			
			return false;
		});
});

function white_space(v_text)
{
     v_text = (v_text).replace(/ /g,'');	 
	 return v_text;
}

function displayRow(row_value, search_values)
{
	var display = true;
	
	var i = 0;
	var diff = false;
	
	while (i<search_values.length && ! diff)
	{
		var current_value = row_value.find("td").eq(i).html();
		
		if (search_values[i] != '') {
			//if (current_value != search_values[i]) {
			if (current_value.indexOf(search_values[i]) == -1){
				diff = true;
			}
		}
		i++; 
	}
	if (diff == true)
	{
		display = false;
	}
	
	return display;
}

function filterSearchValues(search_values)
{
	var pindex = document.getElementById("page_index").getAttribute('value');
	var first_index = eval (pindex * ELEMENTS_PER_PAGE);
	var last_index = eval(first_index + 10);
	
	var nr_shown_elements = 0;
	var rowIndex = 0;
	
	$j('table.searchable tbody tr').each(function(index) {
		
   		if ($j('th', this).length) 
   		{	      
	      rowIndex = -1;
   		}
		
		var dis_row = displayRow($j(this), search_values);
		
		//if (dis_row == true && nr_shown_elements < ELEMENTS_PER_PAGE) 
		if (dis_row == true)
		{
			if (nr_shown_elements < last_index && nr_shown_elements >= first_index) {
				$j(this).get(0).style.display = '';
			}
			else
			{
				$j(this).get(0).style.display = 'none';
			}			
			nr_shown_elements++;
			
		}
		else
		{
			$j(this).get(0).style.display = 'none';
		}
		rowIndex++;		
		
 	});
	
	return nr_shown_elements;
}

function getFilterList()
{
	var rowIndex = 0;
	var filters = new Array();
	$j('table.searchable thead th').each(function(index) {		
   		if ($j('th', this).length) 
   		{	      
	      rowIndex = -1;
   		}    		
   		filters[rowIndex] = $j(this).get(0).innerHTML;		
   		rowIndex++;		
 	});
	
	return filters;
}

function retrieveFilterValues(filters)
{
	var filter_values = new Array();
	
	for (var i=0;i<filters.length;i++)
	{
		var id_filter = white_space(filters[i]);
		
		var elem = $j('input#'+id_filter);
		var elem_test = document.getElementById(id_filter);
		if (elem_test) {
			filter_values[i] = elem_test.value;
		}
	}
	
	return filter_values;
}

function DisplayInputSearchFilters()
{
	var filters = new Array();
	var rowIndex = 0;
	var content = '';
	$j('table.searchable thead th').each(function(index) {
		
   		if ($j('th', this).length) 
   		{	      
	      rowIndex = -1;
   		} 
   		
   		filters[rowIndex] = $j(this).get(0).innerHTML;
		var curr_col = $j(this).get(0).innerHTML;
		if (curr_col.indexOf('PDF') == -1) {
			var div_content = document.createElement('div');
			div_content.setAttribute('style', 'width:260px;padding-bottom:4px;');
			
			content = content + '<div style="width:260px;padding-bottom:6px;"><label>' + curr_col + '</label><input style="float:right;padding:2px;" id="' + white_space(curr_col) + '" type="text"  value="" /></div>';
		}
   		rowIndex++;
		
 	});
	content = content + '<div style="width:260px;padding-bottom:4px;"><input id="search" type="button" value="Cauta" style="float:right;" /></div>';
	
	var filter_content = $j('#filter').get(0);
	filter_content.innerHTML = content;
	
	return filters;
}

function ChangeContent(first_index, last_index)
{
	var rowIndex = 0;
	
	var filter_list = getFilterList();
	var search_values = retrieveFilterValues(filter_list);

	var nr_shown_elements = 0;
	
	$j('table.searchable tbody tr').each(function(index) {
		
   		if ($j('th', this).length) 
   		{	      
	      rowIndex = -1;
   		} 
   		
		var dis_row = displayRow($j(this), search_values);
		
		//if (dis_row == true && nr_shown_elements < ELEMENTS_PER_PAGE) 
		if (dis_row == true)
		{
			if (nr_shown_elements < last_index && nr_shown_elements >= first_index) {
				$j(this).get(0).style.display = '';
			}
			else
			{
				$j(this).get(0).style.display = 'none';
			}			
			nr_shown_elements++;
			
		}
		else
		{
			$j(this).get(0).style.display = 'none';
		}
		rowIndex++;			
 	});
	return rowIndex;
}

function displayContent(pindex)
{
	document.getElementById("page_index").setAttribute('value', pindex);
	var first_index = eval(pindex * ELEMENTS_PER_PAGE);
	var last_index = eval(first_index + ELEMENTS_PER_PAGE);
	
	ChangeContent(first_index, last_index);
}

function drawPager(nr_pages)
{	
	var pager = $j('#pager');
	
	if (nr_pages < 1 ) 
	{
		if (!pager.get(0)) {
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id', "pager");
			newdiv.setAttribute('style', 'overflow:hidden;float:right;padding-right:10px;');
			newdiv.innerHTML = '';
		}
		else
		{				
			var newdiv = document.getElementById('pager');
			newdiv.innerHTML = '';
		}
	}
	else 
	{		
		if (!pager.get(0)) {
			var newdiv = document.createElement('div');
			newdiv.setAttribute('id', "pager");
			newdiv.setAttribute('style', 'overflow:hidden;float:right;padding-right:10px;');
			
			//newdiv.innerHTML = concat(close_button_code,  html);
			var content = '';
			
			for (var j = 0; j < nr_pages; j++) {
				var page_i = document.createElement('div');
				page_i.innerHTML = j;
				
				content = content + '<div onclick="displayContent(' + j + ')" id="' + j + '" class="pager_element">' + j + '</div>';
			}
			newdiv.innerHTML = content;
			var big_content = $j('#searchable_content').get(0);
			big_content.appendChild(newdiv);
		}
		else {
			var content = '';
			newdiv = document.getElementById('pager');
			for (var j = 0; j < nr_pages; j++) {
				var page_i = document.createElement('div');
				page_i.innerHTML = j;
				
				content = content + '<div onclick="displayContent(' + j + ')" id="' + j + '" class="pager_element">' + j + '</div>';
			}
			
			newdiv.innerHTML = content;
		}
	}
}


