/* Javascript */
tilaa=function(id) {
  d = doSimpleXMLHttpRequest('/kauppa/kori/lisaaX/'+id);
  d.addBoth(function(res) {lataaKori();});
  d.addCallback(partial(function(id,res) {Highlight(''+id);},id));
  return false;
}

naytaKori = function(res) {
  var newBody = TBODY(null,map(function(row){return TR(null,map(partial(TD,null),islice(row,1,4)));},res.rows));
  var e = getElement('ostoskori');
  var body = e.getElementsByTagName('tbody')[0];
  newBody = swapDOM(body,newBody);
  if (firstSkipped) { Highlight(newBody); }
  else { firstSkipped=true; }

  var newTotal = TH(null,res.total);
  var foot = e.getElementsByTagName('tfoot')[0];
  total = foot.getElementsByTagName('th')[1];
  swapDOM(total,newTotal);

  map(function(row) {
    var e = getElement('notice_'+row[0]);
    if (e) {
      var eo = getFirstElementByTagAndClassName('span','kori',e);
      if (eo) { removeElement(eo); }
      appendChildNodes(e,SPAN({'class':'info kori'},row[2]+' kpl'));
    }
  },res.rows);
}
lataaKori = function() {
  d = loadJSONDoc('/kauppa/kori/naytaX/');
  d.addBoth(naytaKori);
}


toggleInfo=function(id) {
  toggleElementClass('invisible','info_'+id);
  toggleElementClass('invisible','noinfo_'+id);
  toggleElementClass('invisible','yesinfo_'+id);
}
var kellukepos;
var kellukeproc=-1;
naytaKuva=function(id,event) {
  if (kellukeproc==-1) { showElement('kelluke'); }
  kellukepos=event.screenX
  kellukeproc=1;
  d = loadJSONDoc('/kauppa/tuotteet/kuvatiedotX/'+id);
  d.addBoth(function(res) {
    var felem = DIV(null,IMG({'src':res.url}));
    if (res.text) {
       appendChildNodes(felem,BR(null),SPAN(null,res.text));
    }
    if (res.source_url) {
       appendChildNodes(felem,BR(null), SPAN(null,'Kuvan lähde: '+res.source_url));
    }
    if (kellukeproc==1) {
      replaceChildNodes('kelluke',felem);
      setElementPosition(felem,new Coordinates(kellukepos+50,10));
      appear('kelluke',options={'queue':'break'});
    } else {
      kellukeproc=2;
    }
  });
}
piilotaKuva=function(id) {
  kellukeproc=0;
  fade('kelluke',options={'queue':'break'});
}
var uutiset;
function lataaUutiset(json) {
  var ul = document.createElement('ul');

  for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    var alturl;

    for (var k = 0; k < entry.link.length; k++) {
      if (entry.link[k].rel == 'alternate') {
        alturl = entry.link[k].href;
        break;
      }
    }

    var li = document.createElement('li');
    var a = document.createElement('a');
    a.href = alturl;
    a.target = '_blank';

    var paiva = entry.updated.$t;
    var tpaiva =  paiva.substring(8,10) + '.' + paiva.substring(5,7) + '.' + paiva.substring(0,4);
    var txt = document.createTextNode(tpaiva + ': ' + entry.title.$t);
    a.appendChild(txt);
    li.appendChild(a);

    ul.appendChild(li);
  }

  // Install the bullet list of blog posts.
  uutiset=ul;
}

