var fav;
// last row to be added
var last=0;
// do req, get fav and assign.

var FIELD_FILTER='ca.reachable.tv.recorder.filter.FavouriteImpl';

function log(msg) {
//  if (eval(console)!='undefined') 
//    console.log(msg);
}

function onNewFavouriteLine(url,http_request) {
  var table=document.getElementById('dyn');
  var tr=document.createElement('tr');
  tr.setAttribute('id','row'+last);
  table.appendChild(tr);
  tr.innerHTML=http_request.responseText;
  if (last>0)
    document.getElementById('operator'+last).style.display='';
 var e=createLineElement();
 e.setAttribute('type','ca.reachable.tv.recorder.filter.FieldFilterImpl');
 e.setAttribute('field','title');
 e.setAttribute('operator','is');
 e.setAttribute('join-operator','AND');
// fieldChange(last,document.getElementById('field'+last).options[document.getElementById('field'+last).selectedIndex].value); 
/*
  var span=document.getElementById('nextAfter'+last);
if (!span)
  alert('does not exist: nextAfter'+last);
  span.innerHTML=http_request.responseText;
*/
}

function addRow() {
  absorb(last);
  newFavouriteLine(last+1);
  last++;
}

// take values from curr row into dom tree
function absorb(num) {

}

function removeRow() {
  // remove from dom
  var rowe=document.getElementById('row'+last);
  if (rowe==null)
  alert('no elem: row'+last);
  rowe.parentNode.removeChild(rowe);
  var e=getLineElement(last);
  e.parentNode.removeChild(e);
  last--
}

function fieldChange(num,val) {
	var elem=getLineElement(num);
	elem.setAttribute('field',val);
  var e=document.getElementById('field'+num);
  var strOpSel=document.getElementById('strOp'+num);
  var binOpSel=document.getElementById('binOp'+num);
  var channelSel=document.getElementById('channelSel'+num);
  var categorySel=document.getElementById('categorySel'+num);
  var valInput=document.getElementById('value'+num);
  strOpSel.style.display='none';
  binOpSel.style.display='none';
  channelSel.style.display='none';
  categorySel.style.display='none';
  valInput.style.display='none';

  if (val=='title'||val=='subtitle'||val=='description') { 
    strOpSel.style.display='';
    valInput.style.display='';
    elem.setAttribute('operator',strOpSel.options[strOpSel.selectedIndex].value);
    elem.setAttribute('value',val.value);
  }
  
  if (val=='category') { 
    binOpSel.style.display='';
    elem.setAttribute('operator',binOpSel.options[binOpSel.selectedIndex].value);
    setCategory(num,categorySel);
  }
  
  if (val=='channel'||val=='star') { 
    binOpSel.style.display='';
    elem.setAttribute('operator',binOpSel.options[binOpSel.selectedIndex].value);
    setChannel(num,channelSel);
  }
}

function getItemsElement() {
  var items=fav.getElementsByTagName('items');
  var itemsE;
  if (items.length==0) { 
    itemsE=document.createElement("items");
    fav.appendChild(itemsE);
    log('created items');
  } else
    itemsE=items[0];
  return itemsE;    
}

function opChange(num,val) {
  var elem=getLineElement(num);
  elem.setAttribute('operator',val);
}

function valChange(num,val) {
  var elem=getLineElement(num);
  elem.setAttribute('value',val);
}

function joinChange(num,val) {
  var elem=getLineElement(num);
  elem.setAttribute('join-operator',val);
}

function createLineElement() {
	var itemsE=getItemsElement();
  var ie=document.createElement("item");
  itemsE.appendChild(ie);
  log('appended item');
  return ie;
}

function getLineElement(num) {
  var itemsE=getItemsElement();
  var items=itemsE.getElementsByTagName('item');
  if (items.length>num) 
    return items[num]
  else if (items.length==0) {
		var ie=document.createElement("item");
		itemsE.appendChild(ie);
		log('created item');
		return ie;
	}
	var msg="Invalid item position: "+num;
	log(msg+'\n'+DOM_toString(fav));
	throw msg;
}

function addLine(num) {
  table.appendChild(tr);
  tr.setAttribute('id','row'+tr);
  var td=document.createAttribute('td');
  tr.appendChild(td);
//          td.innerHTML='boo';

}

function loadFavourite() {
  alert('loadFavourite');
}

function onGetFavourite(url,http_request) {
  alert(http_request.responseText);
  fav=http_request.responseXML;
  loadFavourite();
}

function initFavourite() {
	fav=document.createElement("favourite");
	fav.setAttribute("class",FIELD_FILTER);
	newFavouriteLine(0);
}

// current row number
var currNum;
/**
 * Loads the channels (if not already there) and assigns to channel selector 
 * in given row
 */
function setChannel(num,chanSel) {
  if (chanSel.options.length==0) {
    var cache=document.getElementById('channelCache');
    if (cache.innerHTML.length>10) {
      chanSel.innerHTML=cache.innerHTML;
    } else {    
      currNum=num;
      getChannels();
    }
  } 
  if (chanSel.options.length>0) {
    var val=chanSel.options[chanSel.selectedIndex].value;
    getLineElement(num).setAttribute('value',val);
    chanSel.style.display=''; 
  }
}

function onGetChannels(url,http_request) {
  log(http_request.responseText);
  var elems=http_request.responseXML.getElementsByTagName('channel');
  var cache=document.getElementById('channelCache');
  for (var i=0; i<elems.length; i++) {
    var elem=elems[i];
    var option=document.createElement('option');
    var chnum=getByPath(elem,'num');
    option.setAttribute('value',chnum);
    var labelStr=chnum+' '+getByPath(elem,'call')+': '+getByPath(elem,'desc');
    option.appendChild(document.createTextNode(labelStr));
    cache.appendChild(option);
  }
  var chanSel=document.getElementById('channelSel'+currNum);
  setChannel(currNum,chanSel);
  
}

function setCategory(num,categorySel) {
  if (categorySel.options.length==0) {
    var cache=document.getElementById('categoryCache');
    if (cache.innerHTML.length>10) {
      categorySel.innerHTML=cache.innerHTML;
    } else {    
      currNum=num;
      getCategories();
    }
  } 
  if (categorySel.options.length>0) {
    var val=categorySel.options[categorySel.selectedIndex].value;
    getLineElement(num).setAttribute('value',val);
    categorySel.style.display=''; 
  }
}

function onGetCategories(url,http_request) {
  log(http_request.responseText);
  var elems=http_request.responseXML.getElementsByTagName('category');
  var cache=document.getElementById('categoryCache');
  for (var i=0; i<elems.length; i++) {
    var elem=elems[i];
    var option=document.createElement('option');
    var name=getByPath(elem,'@name');
    var key=getByPath(elem,'@key');
    option.setAttribute('value',key);
    option.appendChild(document.createTextNode(name));
    cache.appendChild(option);
  }
  var categorySel=document.getElementById('categorySel'+currNum);
  setCategory(currNum,categorySel);
}

