function addQypeStyles() {
  styles = 'div#qypeWidget { width: auto; border: 1px solid #3FC8F4; padding: 3px 5px; margin: 0; background: #FFF; -moz-border-radius: 0; -webkit-border-radius: 0; }\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 5px; padding: 5px; border: none; border-top: 1px solid #DDD; background: none; }\n' +
           'div#qypeWidget ul.qypeList li a { display: inline; margin: 0; padding: 0; text-decoration: none; color: #444; }\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', 'Mein 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 = [{"url":"http://www.qype.com/place/154840-SCHLECKER-Drogeriemarkt-Giessen","stars":3,"name":"SCHLECKER Drogeriemarkt"},{"url":"http://www.qype.com/place/39834-Subway-Giessen","stars":3,"name":"Subway"},{"url":"http://www.qype.com/place/1986188-Heladeria-Pasteleria-La-Barca-Arona","stars":5,"name":"Heladeria Pasteleria La Barca"},{"url":"http://www.qype.com/place/227835-Kopierstudio-Giessen","stars":4,"name":"Kopierstudio"},{"url":"http://www.qype.com/place/1876281-Chau-Vy-Asia-Imbiss-Giessen","stars":1,"name":"Chau Vy Asia Imbiss"},{"url":"http://www.qype.com/place/1857915-Evangelische-Stadtkirche-Laubach","stars":5,"name":"Evangelische Stadtkirche"},{"url":"http://www.qype.com/place/1646721-Kuchen-So-Giessen","stars":4,"name":"Kuchen \u0026 So"},{"url":"http://www.qype.com/place/567594-Cafe-Inti-Goettingen","stars":3,"name":"Caf\u00e8 Inti"},{"url":"http://www.qype.com/place/1033724-Lahn-Dill-Bergland-Therme-Bad-Endbach","stars":5,"name":"Lahn Dill Bergland Therme"},{"url":"http://www.qype.com/place/1776591-Theodor-Burk-KG-Bad-Endbach","stars":4,"name":"Theodor Burk KG"},{"url":"http://www.qype.com/place/1705541-Salads-Giessen","stars":3,"name":"Salad's"},{"url":"http://www.qype.com/place/152871-Woscht-Anna-Wurst-Anna--Giessen","stars":5,"name":"Woscht-Anna (Wurst-Anna)"},{"url":"http://www.qype.com/place/1659271-Giessen-Giessen","stars":5,"name":"Gie\u00dfen"},{"url":"http://www.qype.com/place/1627091-Lebkuchen-Schmidt-GmbH-Co-KG-Giessen","stars":4,"name":"Lebkuchen-Schmidt GmbH \u0026 Co. KG"},{"url":"http://www.qype.com/place/1606060-Familiencafe-Kruemelkiste-Giessen","stars":4,"name":"Familiencaf\u00e9 Kr\u00fcmelkiste"},{"url":"http://www.qype.com/place/1308793-Sonnenstudio-speed-SUN-Giessen","stars":4,"name":"Sonnenstudio speed SUN"},{"url":"http://www.qype.com/place/1561140-Dachcafe-Giessen","stars":4,"name":"Dachcaf\u00e9"},{"url":"http://www.qype.com/place/1542144-asia-snack-Giessen","stars":3,"name":"asia snack"},{"url":"http://www.qype.com/place/1439160-Antiquariat-Dichtung-und-Kunst-Giessen","stars":4,"name":"Antiquariat Dichtung und Kunst"},{"url":"http://www.qype.com/place/1390104-Restaurant-Badwirt-Strandbad-Tettenhausen-Waging","stars":5,"name":"Restaurant Badwirt - Strandbad Tettenhausen"},{"url":"http://www.qype.com/place/1344615-Manfreds-Soul-Cafe-El-Medano","stars":5,"name":"Manfred's Soul Cafe"},{"url":"http://www.qype.com/place/1329789-Gundi-Kebab-Lounge-Giessen","stars":5,"name":"Gundi Kebab Lounge"},{"url":"http://www.qype.com/place/1320663-Bar-Restaurant-San-Jose-San-Isidro","stars":4,"name":"Bar Restaurant San Jose"},{"url":"http://www.qype.com/place/1315057-City-Grill-Giessen","stars":4,"name":"City Grill"}]
  fobj.write('<div id="qypeWidget">\n')
  fobj.write('    <a href="http://www.qype.com"><img src="http://assets3.qypecdn.net/images/qype_widget_logo.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.qypecdn.net/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.com/people/'+ qypetoolConfig.username + '">' + qypetoolConfig.username + '</a>'
    fobj.write('<p>Ich bin ' + qypetoolUserLink + ' auf ')
    fobj.write('<a href="http://www.qype.com">Qype</a></p>')
  }
  fobj.write(affilinetTrackingImage())
  fobj.write('</div>')

  e.innerHTML = fobj.string
}

addQypeStyles()
writeQypeTool()
