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 = [{"stars":5,"name":"ARTificial Flavours AF66","url":"http://www.qype.com/place/2038867-ARTificial-Flavours-AF66-Frankfurt-am-Main"},{"stars":5,"name":"Die Genussverst\u00e4rker","url":"http://www.qype.com/place/1667653-Die-Genussverstaerker-Offenbach"},{"stars":5,"name":"3f8h.net / electronic publishing","url":"http://www.qype.com/place/3215-3f8h-net-electronic-publishing-Kuessaberg"},{"stars":5,"name":"N\u00e4gel + K\u00f6pfe, IT - Konzepte, Inhaber: Christian Mais e. K.","url":"http://www.qype.com/place/1642275-Naegel-Koepfe-IT-Konzepte-Inhaber-Christian-Mais-e-K--Frankfurt"},{"stars":5,"name":"OSXpert Limited","url":"http://www.qype.com/place/1848576-OSXpert-Limited-Bendorf"},{"stars":5,"name":"Bar Sein-Lounge","url":"http://www.qype.com/place/1306249-Bar-Sein-Lounge-Wil-SG"},{"stars":5,"name":"Hotel Mama Mia Garni","url":"http://www.qype.com/place/967050-Hotel-Mama-Mia-Garni-Ahrbergen"},{"stars":5,"name":"Schramme 10","url":"http://www.qype.com/place/47676-Schramme-10-Hamburg"},{"stars":4,"name":"Ristorante Mondi","url":"http://www.qype.com/place/98444-Ristorante-Mondi-Hamburg"},{"stars":5,"name":"LOMO","url":"http://www.qype.com/place/72249-LOMO-Hamburg"},{"stars":5,"name":"fifty fifty","url":"http://www.qype.com/place/47928-fifty-fifty-Frankfurt-am-Main"},{"stars":5,"name":"Harmonie Kinos","url":"http://www.qype.com/place/447-Harmonie-Kinos-Frankfurt"},{"stars":5,"name":"Caf\u00e9s Bernd Raffalzik Kaffee und Bar No Name","url":"http://www.qype.com/place/53580-Cafes-Bernd-Raffalzik-Kaffee-und-Bar-No-Name-Darmstadt"},{"stars":4,"name":"total.R\u00f6delheim","url":"http://www.qype.com/place/45442-total-Roedelheim-Frankfurt-am-Main"},{"stars":5,"name":"amazon.de","url":"http://www.qype.com/place/531189-amazon-de-Muenchen"},{"stars":5,"name":"Hessisches Landesmuseum Darmstadt","url":"http://www.qype.com/place/27926-Hessisches-Landesmuseum-Darmstadt-Darmstadt"},{"stars":5,"name":"Bar Taunus","url":"http://www.qype.com/place/4260-Bar-Taunus-Darmstadt"},{"stars":5,"name":"Leo Burnett GmbH","url":"http://www.qype.com/place/16804-Leo-Burnett-GmbH-Frankfurt-am-Main"},{"stars":4,"name":"Qype GmbH","url":"http://www.qype.com/place/64500-Qype-GmbH-Hamburg"},{"stars":3,"name":"Pillhuhn","url":"http://www.qype.com/place/7083-Pillhuhn-Darmstadt"},{"stars":4,"name":"Bambus Restaurant \u0026 Bar","url":"http://www.qype.com/place/12623-Bambus-Restaurant-Bar-Darmstadt"},{"stars":5,"name":"Bucerius Kunst Forum GmbH","url":"http://www.qype.com/place/12142-Bucerius-Kunst-Forum-GmbH-Hamburg"},{"stars":5,"name":"Restaurant Adega Alentejana","url":"http://www.qype.com/place/8982-Restaurant-Adega-Alentejana-Darmstadt"},{"stars":4,"name":"Havana Cantina y Bar","url":"http://www.qype.com/place/7088-Havana-Cantina-y-Bar-Darmstadt"}]
  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()
