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/140290-KASTANIENHOF-Das-Haus-fuer-den-Reitsport-GmbH-Hamburg","stars":4,"name":"KASTANIENHOF - Das Haus f\u00fcr den Reitsport - GmbH"},{"url":"http://www.qype.com/place/4627-Schweinske-Wandsbek-Hamburg","stars":3,"name":"Schweinske Wandsbek"},{"url":"http://www.qype.com/place/1712135-Croque-Haus-Ludwigslust","stars":5,"name":"Croque-Haus"},{"url":"http://www.qype.com/place/1020163-Sanitaetshaus-Warmbier-Ludwigslust","stars":5,"name":"Sanit\u00e4tshaus Warmbier"},{"url":"http://www.qype.com/place/1712132-Autohaus-M-M-Ludwigslust","stars":5,"name":"Autohaus M \u0026 M"},{"url":"http://www.qype.com/place/1712130-Dipl-Med-Joachim-Herrmann-Ludwigslust","stars":5,"name":"Dipl. Med. Joachim Herrmann"},{"url":"http://www.qype.com/place/759234-Dr-med-dent-B-Manstetten-Hamburg","stars":5,"name":"Dr. med. dent. B. Manstetten"},{"url":"http://www.qype.com/place/46521-Imbissbude-am-Bahnhof-Eidelstedt-Hamburg","stars":4,"name":"Imbissbude am Bahnhof Eidelstedt"},{"url":"http://www.qype.com/place/748587-Corpus-Coaching-Thomas-Folger-Hamburg","stars":5,"name":"Corpus Coaching Thomas Folger"},{"url":"http://www.qype.com/place/470192-Friseursalon-Roger-Ortmann-Ludwigslust","stars":3,"name":"Friseursalon Roger Ortmann"},{"url":"http://www.qype.com/place/470158-Stift-Bethlehem-Diakonissenhaus-und-Krankenhaus-Ludwigslust","stars":4,"name":"Stift Bethlehem Diakonissenhaus und Krankenhaus"},{"url":"http://www.qype.com/place/470101-Sporthalle-Ludwigslust","stars":5,"name":"Sporthalle"},{"url":"http://www.qype.com/place/59388-Le-CroBag-im-Hauptbahnhof-Nordseite--Hamburg","stars":5,"name":"Le CroBag im Hauptbahnhof (Nordseite)"},{"url":"http://www.qype.com/place/442694-Bezirksamt-Hamburg-Mitte-Kundenzentrum-Wilhelmsburg-Hamburg","stars":3,"name":"Bezirksamt Hamburg-Mitte, Kundenzentrum Wilhelmsburg"},{"url":"http://www.qype.com/place/434280-Baeckerei-Horst-Millahn-Elmshorn","stars":3,"name":"B\u00e4ckerei Horst Millahn"},{"url":"http://www.qype.com/place/434267-Deichmann-Schuhe-GmbH-Co-KG-Elmshorn","stars":5,"name":"Deichmann-Schuhe GmbH \u0026 Co. KG"},{"url":"http://www.qype.com/place/220941-Scheune-Cafe-Cocktailbar-Restaurant-Elmshorn","stars":3,"name":"Scheune. C\u00e0fe-Cocktailbar-Restaurant"},{"url":"http://www.qype.com/place/229346-Cineplex-Elmshorn-Filmtheater-Betriebsgesellschaft-mbH-Elmshorn","stars":3,"name":"Cineplex Elmshorn Filmtheater-Betriebsgesellschaft mbH"},{"url":"http://www.qype.com/place/253499-Marktkauf-SB-Warenhaus-Hamburg","stars":2,"name":"Marktkauf SB-Warenhaus"},{"url":"http://www.qype.com/place/252967-Deutsche-Eiche-Getraenkeindustrie-Inh-Jens-Schmidt-Burg","stars":5,"name":"Deutsche Eiche Getr\u00e4nkeindustrie Inh. Jens Schmidt"},{"url":"http://www.qype.com/place/252951-STAR-Tankstelle-Burg","stars":5,"name":"STAR Tankstelle"},{"url":"http://www.qype.com/place/252937-Eiscafe-Italia-Burg","stars":5,"name":"Eiscaf\u00e9 Italia"},{"url":"http://www.qype.com/place/252926-Arzt-Detlef-Meyer-Burg","stars":5,"name":"Arzt Detlef Meyer"},{"url":"http://www.qype.com/place/252903-Burger-Faehrhaus-Burg","stars":5,"name":"Burger F\u00e4hrhaus"}]
  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()
