var log = function (str) {
  try {
    console.log(str);
  }
  catch (e) {
    // pass
  }
};
String.prototype.startsWith = function (str) {
  return (this.match('^' + str) == str);
};
String.prototype.endsWith = function (str) {
  return (this.match(str + '$') == str);
};
String.prototype.trim = function () {
  return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

ZeroClipboard.setMoviePath('/storage/ZeroClipboard.swf');

(function ($) {
    
    var clip = new ZeroClipboard.Client();
    
    $(document).ready(
      function () {
        
        // fix IE
        try {
          supersleight.init()
        }
        catch (err) {}
        
        // fix /en
        if (document.title == "Inside Cop15 - Video Blog") {
          var jfh = $('#modulePage5018149 .journal-filter-header h2');
          if (jfh.text().trim().startsWith('Entries in English')) {
            $('#modulePage5018149 .journal-filter-header').remove();
          }
        }
        
        // if we're french
        var url = window.location.href;
        var parts = url.split('/');
        if (parts.length > 3 && parts[3].startsWith('fr')) {
          // nav links
          $('#navigationBottom-moduleLink5018149').text(
            'Blog Vidéo'
          ).attr({
              'href': '/fr',
              'title': 'Le Blog Vidéo'
            }
          );
          $('#navigationBottom-moduleLink5018150').text(
            'De quoi s’agit-il?'
          ).attr({
              'href': '/fr/de-quoi-sagit-il',
              'title': 'De quoi s’agit-il?'
            }
          );
          $('#navigationBottom-moduleLink5039435').text(
            'Nos vidéos en partage'
          ).attr({
              'href': '/fr/nos-vidos-en-partage',
              'title': 'Nos vidéos en partage'
            }
          );
          // Share
          $('#sectionContent2306101 .caption').text('Partager');
          $('#share-this-link').html(
            '<img src="/storage/icons/sharethis.png"/>&nbsp; Diffusez'
          ).attr({'title': 'Diffusez'});
          $('#copythis a').html(
            '<img src="/storage/icons/website.png"/>&nbsp; Copier l\'url'
          ).attr({'title': 'Copier l\'url'});
          // Subscribe
          $('#sectionContent2304923 .caption').text('Restez informé');
          $('#rss-link').html(
            '<img src="/storage/icons/rss.png" alt="" />&nbsp; Flux RSS'
          ).attr({
              'title': 'Flux RSS',
              'href': 'http://feeds.feedburner.com/insidecop15-fr'
            }
          );
          $('#email-updates-link').attr({
              'href': 'http://feedburner.google.com/fb/a/mailverify?uri=insidecop15-fr'
            }
          );
          // if we're on the blog
          if (document.title == "Inside Cop15 - Video Blog") {
            $('#content .header h2.title a').text(
              'Live Show quotidien a partir de 19h CET. Les actualites du jour par les experts du WWF tous les jours a 11H et 15H.'
            );
            $('.journal-filter-header h2').html(
              'Le WWF a spécialement conçu pour vous une plate forme de partage de vidéo à la demande. Projet d\'envergure et novateur pour une ONG, notre objectif est de vous faire vivre les négociations du sommet sur le changement climatique comme si vous y étiez en vous fournissant en direct et à la demande des reportages vidéo sur tout ce qui passe pendant la COP 15.'
            );
          }
        }
        
        // hook up the share this link
        var stlink = document.getElementById('share-this-link');
        var stentry = SHARETHIS.addEntry({}, {button:false});
        stentry.attachButton(stlink)
        
        // make the copy this URL icon work
        clip.setText(window.location.href);
        clip.glue('copythis');
        clip.addEventListener(
          'complete', 
          function (client, text) {
            // alert("Copied text to clipboard: " + text );
            $('#copythis').css({'background': 'yellow'});
            window.setTimeout(
              "$('#copythis').css({'background': 'white'});", 
              500
            );
          }
        );
        
        // render the tweets
        jQuery("#user-recent-feeds").tweet({
            'username': ["insidecop15"],
            'count': 5,
            'avatar_size': 48,
            'loading_text': ['Loading tweets ...'],
            'query': ['%23incop']
          }
        );
        jQuery('#user-recent-feeds li').eq(0).css({
            'border-top': 'none',
            'padding-top': '0em'
        });
        
      }
    );
    
    $(window).bind(
      'resize', 
      function () {
        clip.reposition();
      }
    );
    
  
})(jQuery);