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":4,"url":"http://www.qype.com/place/1634517-Hummel-u-Quiddje-Hamburg","name":"Hummel u. Quiddje"},{"stars":5,"url":"http://www.qype.com/place/1518812-KFZ-REPARATUR-WERKSTATT-Marco-Saal-Hamburg","name":"KFZ-REPARATUR-WERKSTATT Marco Saal"},{"stars":5,"url":"http://www.qype.com/place/1898655-The-Bird-Hamburg","name":"The Bird"},{"stars":1,"url":"http://www.qype.com/place/27028-La-Famille-Hamburg","name":"La Famille"},{"stars":1,"url":"http://www.qype.com/place/1779052-A-T-U-Hamburg-9-Hamburg","name":"A.T.U Hamburg 9"},{"stars":5,"url":"http://www.qype.com/place/1641034-Dieter-Sanchez-the-Imbiss-Hamburg","name":"Dieter Sanchez- the Imbiss"},{"stars":3,"url":"http://www.qype.com/place/5843-The-Locks-bar-and-restaurant-Hamburg","name":"The Locks - bar and restaurant"},{"stars":3,"url":"http://www.qype.com/place/348112-Hotel-Residenz-Oberhausen-Oberhausen","name":"Hotel Residenz Oberhausen"},{"stars":1,"url":"http://www.qype.com/place/357953-Gut-Apeldoer-GmbH-Co-Golfplatz-Hennstedt","name":"Gut Apeld\u00f6r GmbH \u0026Co. Golfplatz"},{"stars":5,"url":"http://www.qype.com/place/12101-KAO-KAO-Muenchen","name":"KAO KAO"},{"stars":5,"url":"http://www.qype.com/place/12462-Restaurant-Brook-Hamburg","name":"Restaurant Brook"},{"stars":2,"url":"http://www.qype.com/place/1055-Omas-Apotheke-Hamburg","name":"Omas Apotheke"},{"stars":5,"url":"http://www.qype.com/place/216273-Howies-London","name":"Howies"},{"stars":3,"url":"http://www.qype.com/place/352586-Best-Western-Shaftesbury-Paddington-Court-London","name":"Best Western Shaftesbury Paddington Court"},{"stars":3,"url":"http://www.qype.com/place/325278-Tandur-Hamburg","name":"Tandur"},{"stars":4,"url":"http://www.qype.com/place/582-Waldhaus-Reinbek-Reinbek","name":"Waldhaus Reinbek"},{"stars":2,"url":"http://www.qype.com/place/664-Cafe-Glockenspiel-Muenchen","name":"Cafe Glockenspiel"},{"stars":3,"url":"http://www.qype.com/place/35597-Ibis-Hotel-Essen","name":"Ibis Hotel"},{"stars":3,"url":"http://www.qype.com/place/78152-Cafe-Cocoa-Muenchen","name":"Caf\u00e9 Cocoa"},{"stars":5,"url":"http://www.qype.com/place/288158-Orthopaedie-Bavariapark-Muenchen","name":"Orthop\u00e4die Bavariapark"},{"stars":2,"url":"http://www.qype.com/place/241153-OXMO-XXL-Hamburg","name":"OXMO XXL"},{"stars":2,"url":"http://www.qype.com/place/146078-Apollo-Apotheke-Hamburg","name":"Apollo Apotheke"},{"stars":4,"url":"http://www.qype.com/place/146073-Restaurant-Zur-Duene--Ostseebad-Boltenhagen","name":"Restaurant \"Zur D\u00fcne\""},{"stars":4,"url":"http://www.qype.com/place/159-Eisenstein-Hamburg","name":"Eisenstein"}]
  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()
