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/108249-Litho-Koeln","stars":4,"name":"Litho"},{"url":"http://www.qype.com/place/1643788-Goldengruen-Duisburg","stars":3,"name":"Goldengr\u00fcn"},{"url":"http://www.qype.com/place/1871469-Slotsk-lderen-hos-Gitte-Kik-Kopenhagen","stars":5,"name":"Slotsk\u00e6lderen hos Gitte Kik"},{"url":"http://www.qype.com/place/1493406-Cafe-FINO-fatti-vivo-Duisburg","stars":5,"name":"Caf\u00e9 FINO fatti vivo"},{"url":"http://www.qype.com/place/187222-Viva-Vinum-Duisburg","stars":5,"name":"Viva Vinum"},{"url":"http://www.qype.com/place/15989-toertchen-toertchen-Koeln","stars":4,"name":"t\u00f6rtchen t\u00f6rtchen"},{"url":"http://www.qype.com/place/4335-Kap-am-Suedkai-Koeln","stars":3,"name":"Kap am S\u00fcdkai"},{"url":"http://www.qype.com/place/38462-La-Cana-Koeln","stars":2,"name":"La Cana"},{"url":"http://www.qype.com/place/191703-Mandarin-Oriental-Bangkok","stars":5,"name":"Mandarin Oriental"},{"url":"http://www.qype.com/place/237841-Movies-Cafe-Bistro-Duisburg","stars":3,"name":"Movies Caf\u00e9-Bistro"},{"url":"http://www.qype.com/place/146646-Immonet-GmbH-Hamburg","stars":1,"name":"Immonet GmbH"},{"url":"http://www.qype.com/place/1551857-Absolut-Ice-Bar-Copenhagen-Kopenhagen","stars":4,"name":"Absolut Ice Bar Copenhagen"},{"url":"http://www.qype.com/place/732709-BERTRAMS-Hotel-Guldsmeden-Copenhagen","stars":4,"name":"BERTRAMS Hotel Guldsmeden"},{"url":"http://www.qype.com/place/37843-MongoS-Restaurant-Duisburg","stars":5,"name":"Mongo'S Restaurant"},{"url":"http://www.qype.com/place/14961-Starbucks-Coffee-Deutschland-GmbH-Koeln","stars":3,"name":"Starbucks Coffee Deutschland  GmbH"},{"url":"http://www.qype.com/place/39958-Erikas-Braterei-Muelheim","stars":4,"name":"Erikas Braterei"},{"url":"http://www.qype.com/place/36021-Brutzelhuette-Muelheim-a-d-Ruhr","stars":5,"name":"Brutzelh\u00fctte"},{"url":"http://www.qype.com/place/32177-dm-drogerie-markt-Duisburg","stars":4,"name":"dm drogerie markt"},{"url":"http://www.qype.com/place/473-Muellersches-Volksbad-Muenchen","stars":4,"name":"Mueller'sches Volksbad"},{"url":"http://www.qype.com/place/1505-Bar-Centrale-Muenchen","stars":4,"name":"Bar Centrale"},{"url":"http://www.qype.com/place/6421-Riva-Muenchen","stars":4,"name":"Riva"},{"url":"http://www.qype.com/place/30618-Golden-Leaf-Hotel-Altmuenchen-Muenchen","stars":4,"name":"Golden Leaf Hotel Altm\u00fcnchen"},{"url":"http://www.qype.com/place/7337-Deutsches-Museum-Muenchen","stars":5,"name":"Deutsches Museum"},{"url":"http://www.qype.com/place/24842-Weststadthalle-GmbH-Essen","stars":4,"name":"Weststadthalle GmbH"}]
  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()
