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 = [{"name":"monte mare","url":"http://www.qype.com/review/2730686","stars":5},{"name":"Dr. med. Klaus Trinkaus Theo Trinkaus","url":"http://www.qype.com/review/2730262","stars":5},{"name":"Spinnr\u00e4dl","url":"http://www.qype.com/review/2729699","stars":4},{"name":"SecretEmotion.de","url":"http://www.qype.com/review/2729151","stars":5},{"name":"Autohaus Euler Kaiserslautern GmbH","url":"http://www.qype.com/review/2722906","stars":1},{"name":"China Wok","url":"http://www.qype.com/review/2721524","stars":5},{"name":"Restaurant Pizzeria Milano","url":"http://www.qype.com/review/2718666","stars":4},{"name":"Sam Kullman's Diner","url":"http://www.qype.com/review/2718414","stars":4},{"name":"Papasote Mexican Restaurant","url":"http://www.qype.com/review/2718392","stars":1},{"name":"Cantina Mexicana","url":"http://www.qype.com/review/2718356","stars":5},{"name":"Restaurant Asia","url":"http://www.qype.com/review/2718205","stars":1},{"name":"Joey's Pizza Service","url":"http://www.qype.com/review/1194959","stars":3},{"name":"Eko's Big D\u00f6ner","url":"http://www.qype.com/review/2708426","stars":1},{"name":"Tamarillo","url":"http://www.qype.com/review/2706551","stars":3},{"name":"Pension H\u00f6hmann","url":"http://www.qype.com/review/2701458","stars":1},{"name":"Dr. Praxis Uta Westermilies","url":"http://www.qype.com/review/2700047","stars":1},{"name":"Burgsch\u00e4nke","url":"http://www.qype.com/review/2697907","stars":5},{"name":"Immobilien Schramm","url":"http://www.qype.com/review/2697365","stars":1},{"name":"Brauhaus am Markt","url":"http://www.qype.com/review/2695536","stars":5},{"name":"Heimservice Pizza Attack","url":"http://www.qype.com/review/2684398","stars":3},{"name":"Art Hotel Lauterbach","url":"http://www.qype.com/review/2683820","stars":5},{"name":"Hardrock-Caf\u00e9","url":"http://www.qype.com/review/2683457","stars":5},{"name":"Caf\u00e9-Bar No. 15 Ihn. Wolfgang Sch\u00e4fer","url":"http://www.qype.com/review/2678084","stars":4},{"name":"Haarschneiderei Eric Vogel","url":"http://www.qype.com/review/2674826","stars":5}]
  fobj.write('<div id="qypeWidget">\n')
  fobj.write('    <a href="http://www.qype.com"><img src="http://assets1.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://assets1.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()
