/*
 * Debug logging facility - uses logger() for Firefox
 * using a debug-output div for others
 *
 * @str - debug text string
 * ****/
function logger(str) {
   try {
     console.log(str);
   } catch(e) {
     $('#debugWindow').append(str+'<br />')
   }
}
    
//create a dropdown containing Nokia devices    
var select = $('#NokiaSelect');
$('#nokia_table td a').each(function(i){

  //device name
  var dev = $(this).text();
  //add an option containing the device name index = the index number of the table 
  //row containing the device features
  select.append('<option index="'+(i+1)+'" value="'+dev+'">'+dev+"</option>");
});

//create a dropdown containing the other devices    
var other_select = $('#OtherSelect');
$('#other_table tr').each(function(i){
  
  //brand name
  var brand = $('th.brand', this).text();
  //device name
  var dev = $('td.device', this).text();

  //add an option containing the device name index = the index number of the table 
  //row containing the device features
  if (brand!='') {other_select.append('<option class="brand" index="'+(i+1)+'" value="brand">'+brand+"</option>");}
  if (dev!=''){other_select.append('<option class="device" index="'+(i+1)+'" value="'+dev+'">'+dev+"</option>");}
});

/**
 * Displays the list of supported features
 *
 * @param sel - selector object
 * @param type - Specifies whether the selector lists Nokia or other brand devices
 * *****/
function fetchContent(sel,type){
  
  if (type==='nokia'){
    var container = $('#container_features');
    var devicesTable = $('table#nokia_table');
    var product_link = $('#product_link');
	var empty_container = $('#container_other_features');
    
	//empty the other container
	empty_container.empty();
	
    //empty the link box
    product_link.empty();
	
	//Unselecting the other selector
	var other_select = $('#OtherSelect');
    if (other_select[0] != undefined) {
      other_select[0].selectedIndex=0;
    }
  } else {
    var container = $('#container_other_features');
    var devicesTable = $('table#other_table');
	var product_link = $('#product_link');
	var empty_container = $('#container_features');
	
	//empty the other container
	empty_container.empty();
	
    //empty the link box
    product_link.empty();
	
	//Unselecting the other selector
	var nokia_select = $('#NokiaSelect');
  if(nokia_select[0] != undefined ) {
  	nokia_select[0].selectedIndex=0;
  }
  }

  //if a brand if clicked, moving the selector to the first device
  if(sel.value==='brand'){
    sel.selectedIndex += 1;
     if(document.createEvent) {
        var evt = document.createEvent("Events");
        evt.initEvent("change", true, false);
        sel.dispatchEvent(evt);
      } else if (document.createEventObject) {
        var evt = document.createEventObject();
        sel.fireEvent("onchange", evt);
      }
  }
  
  //empty the features list if the empty option is selected
  container.empty();


  
  //header row of the tables, containing the feature names
  var featureNames = $('th:not(.brand)', devicesTable);
  //logger(featureNames);
  featureNames.each(function() {
    //logger($(this).text());
  
  });  
  //value of the "index" attribute of a currently selected option
  var sIndex = sel.options[sel.selectedIndex].index;

  $('tr:nth-child('+(sIndex+1)+') td', devicesTable).each(function(index) {
    //logger("Cell contains: " + $(this).text());
    if($(this).text().toLowerCase() == 'x') {
      //logger("Index: " + index);
      //logger($(featureNames[index]).text());
      container.append("<li>"+$(featureNames[index]).text()+"</li>");
    }
    
  });

    if (type==='nokia' && sIndex!=0){
      //fetching the product link
      var url = $('tr:nth-child('+(sIndex+1)+') td a', devicesTable).attr("href");
      
      //add the product link
      product_link.append("<a href=\""+url+"\">"+sel.value+" product page</a>");
    }
  
}

