//PCSUITE FEATURE TABLE SEARCH SCRIPT
//pcsuite-common.js must be included prior to this file

function findModels() {
   
  var maxResults = 99;
  var minChars = 3;
  var m = $('#model').val().toLowerCase(); //matching is done in lowercase
  m.replace('nokia',''); // ^-^
  if(m.length < minChars) {$('#model').focus();return false;}
  var found = false;
  var msg = {notfound: "The device model you're searching for may not exist, or may be available in another region" , toomany:'More than one model was found. Please refine your search term.'}; 
  var fCount = 0;
  var foundRows = new Array();
  //do some cleanup
  clearResults();
  
  $('tr').find('td:first').each(function() {
    var text = $(this).text().toLowerCase();
    if(text.indexOf(m) !== -1 ) {
  	  $(this).parent().addClass('found');
      foundRows.push($(this).parent().clone(true)); //"true" added for tooltips
      fCount++;
    } 
  });

  fCount < 1 ? $('#msg').text(msg.notfound) : 
    fCount > maxResults ? $('#msg').text(msg.toomany) : $('#msg').text('');

  if(fCount > 0 && fCount <= maxResults) {

  showRow(foundRows);
  $('#clear').show();
    }
  return false;
}

function showRow (foundRows) {

//build table
$('#searchBox').append('<table id="resultTable" border="1"><tbody></tbody></table>');
//add header
$('#pcsuiteTable tr:lt(2)').clone(true).appendTo('#resultTable tbody');
$('#hideResults').click(function(){$('#resultTable').remove();})
//add results
  for(i=0;i<foundRows.length;i++) {
    foundRows[i].appendTo('#resultTable tbody');
  }
$('#resultTable').find('tr').removeClass('on').end().find('tr:odd').addClass('on');


}

function highlightThis() {
//hide previous selection
$('#found').attr('id','');
$(this).parent().attr('id','found');
$('#msg').text('');
}




$(function() {

 addForm();
 $('#finder').submit(function () {
  findModels(); return false; 
})
$('#model').focus(function(){$(this).select();}).click(function(){this.select();}).focus();
$('#findModel').click(findModels);
// $('.acr').hover(
//  function() {$('#titleContainer').text($(this).attr('title'));},
//  function() {$('#titleContainer').html("&nbsp;") ;}
//);

$('#pcsuiteTable td').click(highlightThis);
//rivitys
$('#pcsuiteTable tr:odd').addClass('on');

});
