function addQypeStyles() {
  styles = 'div#qypeWidget { width: auto; border: 1px solid #3FC8F4; padding: 3px 5px; margin: 0; background: #FFF; }\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', 'Mi 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 = [{"stars":4,"name":"Restaurante Navarro","url":"http://www.qype.es/place/1223010-Restaurante-Navarro-Valencia"},{"stars":4,"name":"Wok to walk","url":"http://www.qype.es/place/1222946-Wok-to-walk-Valencia"},{"stars":4,"name":"Restaurante La Llotgeta","url":"http://www.qype.es/place/382991-Restaurante-La-Llotgeta-Godella"},{"stars":3,"name":"Marisqueria Restaurante Civera","url":"http://www.qype.es/place/300224-Marisqueria-Restaurante-Civera-Valencia"},{"stars":4,"name":"Casa Lluiset","url":"http://www.qype.es/place/452107-Casa-Lluiset-Torrent"},{"stars":1,"name":"Muerde la pasta","url":"http://www.qype.es/place/329321-Muerde-la-pasta-Valencia"},{"stars":3,"name":"parque de Gulliver","url":"http://www.qype.es/place/276310-parque-de-Gulliver-Valencia"},{"stars":3,"name":"Restaurante Rausell","url":"http://www.qype.es/place/287572-Restaurante-Rausell-Valencia"},{"stars":4,"name":"Restaurante Wansda","url":"http://www.qype.es/place/264940-Restaurante-Wansda-Torrent"},{"stars":5,"name":"Carmelo Restaurante","url":"http://www.qype.es/place/407302-Carmelo-Restaurante-Elda"},{"stars":4,"name":"las Brasas","url":"http://www.qype.es/place/272956-las-Brasas-Mislata"},{"stars":4,"name":"\u00d1am","url":"http://www.qype.es/place/300286--am-Valencia"},{"stars":2,"name":"Pans \u0026 Company","url":"http://www.qype.es/place/304246-Pans-Company-Valencia"},{"stars":5,"name":"Restaurante Crapa Pelata","url":"http://www.qype.es/place/292950-Restaurante-Crapa-Pelata-Valencia"},{"stars":4,"name":"Arepera Canaria","url":"http://www.qype.es/place/297780-Arepera-Canaria-Catarroja"},{"stars":3,"name":"Meson la Tapa","url":"http://www.qype.es/place/272967-Meson-la-Tapa-Mislata"},{"stars":4,"name":"Farigola i Menta","url":"http://www.qype.es/place/267110-Farigola-i-Menta-Torrent"},{"stars":4,"name":"NH K\u00f6ln","url":"http://www.qype.es/place/15284-NH-Koeln-Koeln"},{"stars":4,"name":"Hotel NH Caransa","url":"http://www.qype.es/place/286406-Hotel-NH-Caransa-Amsterdam"},{"stars":5,"name":"Van Gogh Museum","url":"http://www.qype.es/place/89213-Van-Gogh-Museum-Amsterdam"},{"stars":4,"name":"Parque de Cabecera","url":"http://www.qype.es/place/250470-Parque-de-Cabecera-Valencia"},{"stars":5,"name":"Restaurante Tiki Grill","url":"http://www.qype.es/place/266910-Restaurante-Tiki-Grill-Madrid"},{"stars":3,"name":"Amer Pizza S.L.","url":"http://www.qype.es/place/280796-Amer-Pizza-S-L--Valencia"},{"stars":5,"name":"Kukuxumusu","url":"http://www.qype.es/place/147727-Kukuxumusu-Barcelona"}]
  fobj.write('<div id="qypeWidget">\n')
  fobj.write('    <a href="http://www.qype.es"><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.es/people/'+ qypetoolConfig.username + '">' + qypetoolConfig.username + '</a>'
    fobj.write('<p>Yo soy ' + qypetoolUserLink + ' en ')
    fobj.write('<a href="http://www.qype.es">Qype</a></p>')
  }
  fobj.write(affilinetTrackingImage())
  fobj.write('</div>')

  e.innerHTML = fobj.string
}

addQypeStyles()
writeQypeTool()