// ==UserScript==
// @name          Amazon go Int!
// @namespace     http://gerd-saurer.com/amazon
// @description	  Amazon Go Int 
// @include       http://amazon.com/*
// @include       http://www.amazon.com/*
// ==/UserScript==


var debug = false;

window.addEventListener('load', function() {
  try
  {
    var asin = document.getElementById('ASIN').value;
    
    //alert("Amazon go Int! [" + asin +"]");
    
    var toAddBase = document.getElementById('primaryUsedAndNew');
    
    var toAdd = document.createElement('div')
    toAdd.setAttribute('class', 'buying');
    
    var headline =  document.createElement('span');
    headline.setAttribute('class', 'availGreen')
    headline.textContent = "Other Locations:";
    toAddBase.appendChild(headline);
    toAddBase.appendChild(document.createElement('br'));
    
    var requestLink = "http://ecs.amazonaws.de/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=1N7F0YQFCBH54BZNN3R2&AsociateTag=gerdsaurer-20&Operation=ItemLookup&ItemId=" +asin +"&ResponseGroup=Small,Offers";
    var pictureLink = 'http://www.gerd-saurer.com/amazon/goInt/de.gif';
    
    getIntSiteInformation(requestLink, pictureLink, toAddBase);
    
    toAddBase.appendChild(toAdd);
  }
  catch(err)
  {
    log(err);
  }
  
}, true);

//Function to generate the information box
function getIntSiteInformation(requestLink, pictureLink, toAdd) {

  GM_xmlhttpRequest({
    method: "GET", 
    url: requestLink, 
    onload: function( responseDetails ) {
      try {
      
        var parser = new DOMParser();
        var doc = parser.parseFromString(responseDetails.responseText, "text/xml");

        var itemLink = evaluateXPath(doc, "//*[name()='DetailPageURL']");
        if(itemLink.length > 0){
          
          itemLink = itemLink[0].textContent;
          
          var intNode = document.createElement('span');
          var intLinkNode =  document.createElement('a');
          intLinkNode.setAttribute('href', itemLink);
          
          var intPicNode =  document.createElement('img');
          intPicNode.setAttribute('src', pictureLink);
          intPicNode.setAttribute('alt', 'German');
          intPicNode.setAttribute('border', '0');
          intLinkNode.appendChild(intPicNode);
          intNode.appendChild(intLinkNode);
          
          intNode.appendChild(document.createElement('br'));
          
          var itemPrice = evaluateXPath(doc, "//*[name()='LowestNewPrice']/*[name()='FormattedPrice']");
          if(itemPrice.length > 0){
          
            itemPrice = itemPrice[0].textContent;
            
            var priceLable = document.createElement('span')
            priceLable.setAttribute('class', 'priceBlockLabelPrice');
            priceLable.textContent = "Price:";
            intNode.appendChild(priceLable);
            
            var priceText = document.createElement('span')
            priceText.setAttribute('class', 'priceLarge');
            priceText.textContent = itemPrice;
            intNode.appendChild(priceText);
            
          }
          
          toAdd.appendChild(intNode)
        }
      } catch( e ) {
        log(e);
        GM_log( e );
      }
    }
  });

}

// Evaluate an XPath expression aExpression against a given DOM node
// or Document object (aNode), returning the results as an array
// thanks wanderingstan at morethanwarm dot mail dot com for the
// initial work.
function evaluateXPath(doc, aExpr) {

  var result = doc.evaluate(aExpr, doc, null, XPathResult.ANY_TYPE, null);
  var found = [];
  var res;
  while (res = result.iterateNext())
    found.push(res);
  return found;
}


function log(e){
  if(debug == true){
    alert(e);
  }
}
