function addQypeStyles() {
  styles = 'div#qypeWidget { width: auto; border: 1px solid #BACA80; padding: 3px 5px; margin: 0; background: #FFF; }\n' +
           'div#qypeWidget img { border: 0; }\n' +
           'div#qypeWidget img.qypeStars { padding-top: 3px; }\n' +
           'div#qypeWidget h3.qypeHeadline { padding: 5px 3px; margin: 0; }\n' +
           'div#qypeWidget ul.qypeList { vertical-align: top; height: auto; margin: 0; padding: 0; list-style-type: none; }\n' +
           'div#qypeWidget ul li.qypeListItem { display: block; margin: 0; padding: 5px 10px; border: none; background: none; }\n' +
           'div#qypeWidget ul.qypeList li a { display: inline; margin: 0; padding: 0; text-decoration: none; }\n' +
           'p.qypeLinkToQype { margin: 5px; padding: 0; }\n'


  stylesTag = document.createElement('style')
  stylesTag.type = "text/css"

  if(stylesTag.styleSheet){ //IE
    stylesTag.styleSheet.cssText = styles
  } else { //w3c
    var stylesText = document.createTextNode(styles)
    stylesTag.appendChild(stylesText)
  }

  document.getElementsByTagName("head")[0].appendChild(stylesTag)
}

// setting defaults
function qypetoolMergeDefaults(cfg) {
  cfg.merge = function(key, defaultValue) {
    if(typeof(this[key]) == 'undefined') this[key] = defaultValue
  }
  cfg.merge('headline', 'My Qype')
  cfg.merge('reviewCount', 5)
  cfg.merge('showUsername', true)
  cfg.merge('showStars', true)
  cfg.merge('eTracker_cid', '7')
  cfg.merge('eTracker_lid', '158667')
  cfg.merge('affilinet_site', '')
  cfg.merge('affilinet_tnb', '')
  cfg.merge('affilinet_ref', '')
  return cfg
}

function addParamToUrl(url, param, value) {
  return url + ((url.indexOf('?') > -1) ? '&' : '?') + param + '=' + value
}

function affilinetTrackingImage() {
  if (qypetoolConfig.affilinet_site && qypetoolConfig.affilinet_tnb && qypetoolConfig.affilinet_ref) {
    return '<img src="http://banners.webmasterplan.com/view.asp?ref=' + 
           qypetoolConfig.affilinet_ref + '&site=' + 
           qypetoolConfig.affilinet_site + '&type=text&tnb=' + 
           qypetoolConfig.affilinet_tnb + '" width="0" height="0" style="display:none;"/>'
  }
  return ""
}

function addAffilinetTracking(url) {
  if (qypetoolConfig.affilinet_site && qypetoolConfig.affilinet_tnb && qypetoolConfig.affilinet_ref) {
    url = "http://partners.webmasterplan.com/click.asp?ref=" + qypetoolConfig.affilinet_ref +
    "&site=" + qypetoolConfig.affilinet_site + 
    "&type=text" + 
    "&tnb=" + qypetoolConfig.affilinet_tnb + 
    "&diurl=" + encodeURIComponent(url)
  }
  return url
}

// main
function writeQypeTool() {
  var fobj = {
    string: '',
    write: function(text) {
      this.string += text
    }
  }

  if (typeof(qypetoolConfig) == 'undefined') qypetoolConfig = {}
  qypetoolConfig = qypetoolMergeDefaults(qypetoolConfig)

  var e

  if (typeof(qypetoolConfig.targetId) != undefined) {
    e = qypetoolConfig.targetId
    if(typeof(e) == 'string') {
      e = document.getElementById(e)
    }
  } 

  if(typeof(e) == 'undefined') {
    e = document.getElementsByTagName('*');
    var pos = e[e.length-1];
    e = document.createElement('div')
    pos.parentNode.appendChild(e);
  }

  var qypetoolReviews = [{"name": "The Wheatsheaf", "stars": 5, "url": "http://www.qype.co.uk/review/328787"}, {"name": "Borough Market", "stars": 5, "url": "http://www.qype.co.uk/review/328759"}, {"name": "Hayward Executive Airport", "stars": 5, "url": "http://www.qype.co.uk/review/328723"}, {"name": "Great America", "stars": 5, "url": "http://www.qype.co.uk/review/327646"}, {"name": "Heathrow Express", "stars": 2, "url": "http://www.qype.co.uk/review/282664"}, {"name": "The Virgin Clubhouse", "stars": 5, "url": "http://www.qype.co.uk/review/282649"}, {"name": "Autozone", "stars": 5, "url": "http://www.qype.co.uk/review/269768"}, {"name": "Holiday World", "stars": 4, "url": "http://www.qype.co.uk/review/269748"}, {"name": "Elstree Film Studios", "stars": 4, "url": "http://www.qype.co.uk/review/269671"}, {"name": "Petticoat Lane Market", "stars": 5, "url": "http://www.qype.co.uk/review/266714"}, {"name": "Citi Hostels", "stars": 4, "url": "http://www.qype.co.uk/review/266678"}, {"name": "Guinness Storehouse", "stars": 4, "url": "http://www.qype.co.uk/review/266655"}, {"name": "Camden Markets", "stars": 5, "url": "http://www.qype.co.uk/review/266626"}, {"name": "The London Dungeon", "stars": 3, "url": "http://www.qype.co.uk/review/266597"}, {"name": "Evans Cycles", "stars": 3, "url": "http://www.qype.co.uk/review/266536"}, {"name": "Stansted Airport", "stars": 2, "url": "http://www.qype.co.uk/review/248890"}, {"name": "Flamingo Hostel", "stars": 5, "url": "http://www.qype.co.uk/review/248404"}, {"name": "Nintendo World", "stars": 5, "url": "http://www.qype.co.uk/review/220510"}, {"name": "Argos", "stars": 4, "url": "http://www.qype.co.uk/review/217675"}, {"name": "Fulham Broadway Station", "stars": 5, "url": "http://www.qype.co.uk/review/217665"}, {"name": "Gloucester Road Tube Station", "stars": 4, "url": "http://www.qype.co.uk/review/217650"}, {"name": "Ikea Ltd", "stars": 3, "url": "http://www.qype.co.uk/review/217643"}, {"name": "Parsons Green Tube Station", "stars": 4, "url": "http://www.qype.co.uk/review/217629"}, {"name": "Greggs", "stars": 5, "url": "http://www.qype.co.uk/review/185549"}]
  fobj.write('<div id="qypeWidget">\n')
  fobj.write('    <a href="http://www.qype.co.uk"><img src="http://assets3.qype.com/images/qype_widget_logo.en.gif"></a>\n')
  fobj.write('    <h3 class="qypeHeadline">' + qypetoolConfig.headline + '</h3>\n')
  fobj.write('    <ul class="qypeList">\n')
  var qypetoolReviewsLength = Math.min(qypetoolReviews.length, qypetoolConfig.reviewCount)
  for(i=0;i<qypetoolReviewsLength;i++) {
    fobj.write('      <li class="qypeListItem">\n')
    fobj.write('        <a href="' + addAffilinetTracking(qypetoolReviews[i]['url']) + '">' + qypetoolReviews[i]['name'] + '</a><br />\n')
    if (qypetoolConfig.showStars) fobj.write('        <img src="http://assets3.qype.com/images/rating_small_' + qypetoolReviews[i]['stars'] + '.png" class="qypeStars">\n')
    fobj.write("      </li>\n")
  }
  fobj.write('    </ul>\n')
  if (typeof(qypetoolConfig.username) != 'undefined' && qypetoolConfig.showUsername) {
    var qypetoolUserLink = '<a href="' + 'http://www.qype.co.uk/people/'+ qypetoolConfig.username + '">' + qypetoolConfig.username + '</a>'
    fobj.write('<p>I am ' + qypetoolUserLink + ' on ')
    fobj.write('<a href="http://www.qype.co.uk">Qype</a></p>')
  }
  fobj.write(affilinetTrackingImage())
  fobj.write('</div>')

  e.innerHTML = fobj.string
}

addQypeStyles()
writeQypeTool()
