SocialMedia = new function() {

  this.article_headline = '';
  this.article_like = true;
  this.initTimerFallback;
  this.init = false;

  this.addSocialMedia = function(headline, like) {
    Sport1.log( '<SocialMedia.addSocialMedia>' );
    Sport1.log( '  ' + headline);
    this.article_headline = headline;
    this.article_like = like;
    Sport1.log( '  ' + this.article_headline);
    Sport1.addEvent(window, 'load', function(){ Sport1.log('SocialMedia.addSocialMedia --> window.load called by EventListener'); });
    Sport1.addEvent(window, 'load', function(){ SocialMedia.initSocialmedia(); });
    this.initTimerFallback = setTimeout(function(){ 
      SocialMedia.initSocialmedia();
    }, 5000);
    Sport1.log( '  timeout defined --> 5 sek');
    Sport1.log( '</SocialMedia.addSocialMedia>' ); 
  }

  this.initSocialmedia = function() {
    Sport1.log('<SocialMedia.initSocialmedia>');
    //check if initialisation already finished
    if( SocialMedia.init ) {
      Sport1.log('  initialisation already finished --> return');
      return;
    }else{
      Sport1.log('  initialisation started');
    }
    SocialMedia.init = true;
    clearTimeout( SocialMedia.initTimerFallback );
    var reTweet = document.getElementById('retweet');
    var fbLike = document.getElementById('fblike');
    var fbLikebig = document.getElementById('fblikebig');
    if( SocialMedia.article_like ) {
      var buttonType = 'like';
    } else {
      var buttonType = 'recommend';
    }
    SocialMedia.addTwitter( reTweet, document.location.href,undefined, SocialMedia.article_headline, undefined, undefined, 'en' );
    SocialMedia.addFBLike( fbLike, document.location.href, 'button_count', 'false', '450', buttonType, 'light' );
    SocialMedia.addFBLike( fbLikebig, document.location.href, 'standard', 'false', '450', buttonType, 'light' );
    Sport1.log('  we start SocialMedia.initFacebook in 500 millisec');
    setTimeout( SocialMedia.initFacebook, 500 );
    Sport1.log('</SocialMedia.initSocialmedia>');
  }

  this.fetchComments = function() {
    var countComments = document.getElementById('count_comments');
    if(countComments != undefined) {
      var commentText = countComments.innerHTML();
      var count = commentText.split(' ')[0];
      if(count.match(/[0-9]+/)){
        return count;
      }
    }
    return 0;
  }

  this.initFacebook = function() {
    Sport1.log('<SocialMedia.initFacebook>');
    Sport1.log('  generate facebook');
    FB.init( {appId: '124954777542100', status: true, cookie: true, xfbml: true} );
    Sport1.log('</SocialMedia.initFacebook>');
  }

  this.addFacebookMeta = function (title, site_name, type, url, image, description) {
    this.addMetaTag('og:title', title);
    this.addMetaTag('og:site_name', site_name);
    this.addMetaTag('og:type', type);
    this.addMetaTag('og:url', url);
    this.addMetaTag('og:image', image);
    this.addMetaTag('og:description', description);
  }

  this.addMetaTag = function(property, content) {
    var head = document.getElementsByTagName('head')[0];
    var meta = document.createElement('meta');
    meta.setAttribute('property', property);
    meta.setAttribute('content', content);
    head.appendChild(meta);
  }

  this.addFBLike = function( pElement, href, layout, show_faces, width, action, colorscheme) {
    Sport1.log('  <SocialMedia.addFBLike>');
    var fblike = document.createElement('fb:like');
    fblike.setAttribute('href', href);
    fblike.setAttribute('layout', layout);
    fblike.setAttribute('show_faces', show_faces);
    fblike.setAttribute('width', width);
    fblike.setAttribute('action', action);
    fblike.setAttribute('colorscheme', colorscheme);
    try{
      pElement.appendChild(fblike);
    }catch(e){
      Sport1.log('    ERROR: pElement is undefined');
    }
    Sport1.log('  </SocialMedia.addFBLike>');
  }

  this.addFBShare = function(pElement, href, type) {
    Sport1.log('  <SocialMedia.addFBShare>');
    var fbshare = document.createElement('fb:share-button');
    fbshare.setAttribute('href', href);
    fbshare.setAttribute('type', type);
    try{
      pElement.appendChild( fbshare );
    }catch(e){
      Sport1.log('    ERROR: pElement is undefined');
    }
    Sport1.log('  </SocialMedia.addFBLike>');
  }

  /*
  url  URL of the page to share
  via  Screen name of the user to attribute the Tweet to
  text  Default Tweet text
  related  Related accounts
  count  Count box position
  lang  The language for the Tweet Button
  counturl  The URL to which your shared URL resolves to
  */
  this.addTwitter = function(pElement, url, via, text, related, count, lang, counturl) {
    Sport1.log('  <SocialMedia.addTwitter>');
    var params = '?url='+url;
  
    if(via != undefined) {
      params += '&via='+via;
    }
    if(text != undefined) {
      params += '&text='+encodeURIComponent(text);
    }
    if(related != undefined) {
      params += '&related='+related;
    }
    if(count != undefined) {
      params += '&count='+count;
    }
    if(lang != undefined) {
      params += '&lang='+lang;
    }
    if(counturl != undefined) {
      params += '&counturl='+counturl;
    }
    var src = 'http://platform.twitter.com/widgets/tweet_button.html'+params;
    var h = '50';
    var w = '130';
    try{
      this.iframeWriter(pElement, src, h, w, 0, 'no');
    }catch(e){
      Sport1.log('    ERROR: pElement is undefined');
    }
    Sport1.log('  </SocialMedia.addTwitter>');
  }

  this.addRetweet = function(pElement, size) {
    size = 'small';
    if(!url)
    {
     var url=window.location.href;
    }
    if(!size)
    var size="big";
    var height="75";
    var width="54"
    if(size=="small")
    {
      height="22";
      width="120";
    }
    if(!username)
      var username="none";
    url=url.replace('?', '*');
    var src='http://www.retweet.com/widget/button/'+size+'/'+username+'/'+url;
    this.iframeWriter(pElement, src, height, width, 0, 'no');
  }

   this.addTweetmeme = function(pElement, tweetmeme_style) {
    var _url = window.location.href;
    var _url = _url.replace(/((?:\?|&)?fbc_receiver=.+)?(?:#.*)?$/, '');
    var url = escape((typeof tweetmeme_url == 'string') ? tweetmeme_url: ((typeof TWEETMEME_URL == 'string') ? TWEETMEME_URL: _url)).replace(/\+/g, '%2b');
    var source = (typeof tweetmeme_source == 'string') ? escape(tweetmeme_source) : ((typeof TWEETMEME_SOURCE == 'string') ? escape(TWEETMEME_SOURCE) : false);
    var style = (typeof tweetmeme_style == 'string') ? escape(tweetmeme_style) : ((typeof TWEETMEME_STYLE == 'string') ? escape(TWEETMEME_STYLE) : 'normal');
    var service = (typeof tweetmeme_service == 'string') ? escape(tweetmeme_service) : ((typeof TWEETMEME_SERVICE == 'string') ? escape(TWEETMEME_SERVICE) : false);
    var service_api = (typeof tweetmeme_service_api == 'string') ? escape(tweetmeme_service_api) : ((typeof TWEETMEME_SERVICE_API == 'string') ? escape(TWEETMEME_SERVICE_API) : false);
    var alias = (typeof tweetmeme_alias == 'string') ? escape(tweetmeme_alias) : ((typeof TWEETMEME_ALIAS == 'string') ? escape(TWEETMEME_ALIAS) : false);
    var hashtags = (typeof tweetmeme_hashtags == 'string') ? escape(tweetmeme_hashtags) : ((typeof TWEETMEME_HASHTAGS == 'string') ? escape(TWEETMEME_HASHTAGS) : false);
    var space = (typeof tweetmeme_space == 'number') ? escape(tweetmeme_space) : ((typeof TWEETMEME_SPACE == 'number') ? escape(TWEETMEME_SPACE) : false);
    var width = (typeof tweetmeme_width == 'number') ? escape(tweetmeme_width) : ((typeof TWEETMEME_WIDTH == 'number') ? escape(TWEETMEME_WIDTH) : false);
    var src = 'http://pro.tweetmeme.com/button.js';
    switch (style) {
    case 'compact':
      var h = 20;
      var w = 90;
      break;
    default:
      var h = 61;
      var w = 50;
      break
    }
    if (width) {
      w = width
    }
    src += '?url=' + url;
    src += '&style=' + style;
    if (source != false) {
      src += '&source=' + source
    }
    if (service) {
      src += '&service=' + service
    }
    if (service_api) {
      src += '&service_api=' + service_api
    }
    if (alias) {
      src += '&alias=' + alias
    }
    if (hashtags) {
      src += '&hashtags=' + hashtags
    }
    if (space) {
      src += '&space=' + space
    }
    this.iframeWriter(pElement, src, h, w, 0, 'no');
    tweetmeme_url = null;
    TWEETMEME_URL = null;
    tweetmeme_source = null;
    TWEETMEME_SOURCE = null;
    tweetmeme_service = null;
    TWEETMEME_SERVICE = null;
    tweetmeme_service_api = null;
    TWEETMEME_SERVICE_API = null;
    tweetmeme_style = null;
    TWEETMEME_STYLE = null;
    tweetmeme_alias = null;
    TWEETMEME_ALIAS = null;
    tweetmeme_hashtags = null;
    TWEETMEME_HASHTAGS = null;
    tweetmeme_space = null;
    TWEETMEME_SPACE = null
  }

  this.iframeWriter = function(pElement, src, height, width, frameborder, scrolling) {
    Sport1.log('  <SocialMedia.iframeWriter>');
    var iframe  = document.createElement('iframe');
    iframe.setAttribute('src', src);
    iframe.setAttribute('height', height);
    iframe.setAttribute('width', width);
    iframe.setAttribute('frameborder', frameborder);
    iframe.setAttribute('scrolling', scrolling);
    try{
      pElement.appendChild(iframe);
    }catch(e){
      Sport1.log('    ERROR: pElement is undefined');
    }
    Sport1.log('  </SocialMedia.iframeWriter>');
  }

}

