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":"Caf\u00e9 Sacher","url":"http://www.qype.com/review/2726752","stars":3},{"name":"meinKauf.at","url":"http://www.qype.com/review/2726559","stars":5},{"name":"ADEG Frischetreff","url":"http://www.qype.com/review/2726555","stars":2},{"name":"Il Tempo Bistro","url":"http://www.qype.com/review/2726536","stars":3},{"name":"Faschingsprinz","url":"http://www.qype.com/review/2726471","stars":1},{"name":"Thalia Buch u. Medien GmbH","url":"http://www.qype.com/review/2726413","stars":1},{"name":"ORF","url":"http://www.qype.com/review/2725974","stars":1},{"name":"Franz-Josefs-Bahnhof","url":"http://www.qype.com/review/2725867","stars":3},{"name":"Wiener Theaterkunst-Werkst\u00e4tte, Lambert Hofer Ges.mbH","url":"http://www.qype.com/review/2725551","stars":5},{"name":"Indus","url":"http://www.qype.com/review/2725440","stars":3},{"name":"Weinhaus Dkfm Thekla Hochmayer, Dkfm","url":"http://www.qype.com/review/2725402","stars":5},{"name":"Gr\u00fcnauer","url":"http://www.qype.com/review/2725348","stars":2},{"name":"Pizza Mari'","url":"http://www.qype.com/review/2725284","stars":4},{"name":"Restaurant Yoo","url":"http://www.qype.com/review/2725211","stars":5},{"name":"Gasthaus Winkler","url":"http://www.qype.com/review/2725179","stars":5},{"name":"Kunsthistorisches Museum","url":"http://www.qype.com/review/2724446","stars":5},{"name":"Spanische Hofreitschule","url":"http://www.qype.com/review/2724441","stars":5},{"name":"Dr. Thomas Kralik","url":"http://www.qype.com/review/2724201","stars":5},{"name":"Derag Hotel, Kaiser Franz Joseph","url":"http://www.qype.com/review/2724144","stars":3},{"name":"Cafe Wunderer, Inh. Anton Wunderer's Erben","url":"http://www.qype.com/review/2723905","stars":4},{"name":"Waldgrill Cobenzl","url":"http://www.qype.com/review/1690964","stars":2},{"name":"yohm","url":"http://www.qype.com/review/2723292","stars":4},{"name":"12 Munchies","url":"http://www.qype.com/review/2722581","stars":5},{"name":"Manga Bar Restaurant","url":"http://www.qype.com/review/2722481","stars":1}]
  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()
