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/1247531-Panteha-Filiale-Mainz-Mainz","stars":1,"name":"Panteha Filiale Mainz"},{"url":"http://www.qype.com/place/933907-AQUA-DOME-Tirol-Therme-Laengenfeld-Laengenfeld","stars":5,"name":"AQUA DOME - Tirol Therme L\u00e4ngenfeld"},{"url":"http://www.qype.com/place/587471-Seehotel-Feldberg-Feldberger-Seenlandschaft","stars":3,"name":"Seehotel Feldberg"},{"url":"http://www.qype.com/place/1329871-Ekbatan-Mainz","stars":4,"name":"Ekbatan"},{"url":"http://www.qype.com/place/34291-Cubo-Negro-Mainz","stars":4,"name":"Cubo Negro"},{"url":"http://www.qype.com/place/405928-Starbucks-Coffee-House-Frankfurt-am-Main","stars":3,"name":"Starbucks Coffee House"},{"url":"http://www.qype.com/place/1370425-Gasthof-Pension-Neue-Post-Soelden","stars":1,"name":"Gasthof-Pension Neue Post"},{"url":"http://www.qype.com/place/937328-Muenchen-Airport-Marriott-Hotel-Freising","stars":3,"name":"M\u00fcnchen Airport Marriott Hotel"},{"url":"http://www.qype.com/place/19880-Kakadu-Inh-Jaruial-Multani-Frankfurt","stars":3,"name":"Kakadu Inh. Jaruial Multani"},{"url":"http://www.qype.com/place/143017-Bocado-Mainz","stars":4,"name":"Bocado"},{"url":"http://www.qype.com/place/37551-Haddocks-Mainz","stars":4,"name":"Haddocks"},{"url":"http://www.qype.com/place/124029-Downtown-Diner-Mainz","stars":1,"name":"Downtown Diner"},{"url":"http://www.qype.com/place/113181-Zur-Kanzel-Mainz","stars":5,"name":"Zur Kanzel"},{"url":"http://www.qype.com/place/1222867-Sea-World-Brisbane","stars":4,"name":"Sea World"},{"url":"http://www.qype.com/place/1222304-Cascade-Cafe-Sydney","stars":4,"name":"Cascade Caf\u00e9"},{"url":"http://www.qype.com/place/1222238-Hard-Rock-Cafe-Brisbane","stars":4,"name":"Hard Rock Caf\u00e9"},{"url":"http://www.qype.com/place/146370-Harbour-Bridge-Sydney","stars":5,"name":"Harbour Bridge"},{"url":"http://www.qype.com/place/128418-Dubai-International-Airport-Dubai","stars":4,"name":"Dubai International Airport"},{"url":"http://www.qype.com/place/1221780-Sine-Ausruestung-u-Bekleidung-Mainz-GmbH-Mainz","stars":5,"name":"Sine Ausr\u00fcstung u. Bekleidung Mainz GmbH"},{"url":"http://www.qype.com/place/38984-Messina-Pizzeria-Ristorante-Frankfurt-am-Main","stars":3,"name":"Messina Pizzeria Ristorante"},{"url":"http://www.qype.com/place/958205-Courtyard-By-Marriott-At-North-Ryde-Sydney","stars":4,"name":"Courtyard By Marriott At North Ryde"},{"url":"http://www.qype.com/place/1217344-Qantas-Airways-Limited-Sydney","stars":5,"name":"Qantas Airways Limited"},{"url":"http://www.qype.com/place/1201918-Bungalow-8-Sydney","stars":4,"name":"Bungalow 8"},{"url":"http://www.qype.com/place/1191489-Kiama-Ocean-View-Motor-Inn-Sydney","stars":2,"name":"Kiama Ocean View Motor Inn"}]
  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()
