/*
Site:      shape5.com
Email:     contact@shape5.com
@license:  Copyrighted Commercial Software
@copyright (C) 2016 Shape 5 LLC
*/


;(function($) {
  'use strict';
  $.fn.likeMe = function(options) {
    var settings = {
      fadeDuration: 700,
      pageUrl: document.URL,
      showOnHover: false,
      ignore: null
    }, positions = [
      {position: 'absolute', top: 10, left: 10},
      {position: 'absolute', top: 10, right: 10},
      {position: 'absolute', bottom: 10, left: 10},
      {position: 'absolute', bottom: 10, right: 10}
    ], icons = [];
    icons.push('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="20" height="20" data-icon="heart" data-container-transform="translate(0 1)" viewBox="-1 -1 10 10"><path d="M2 1c-.55 0-1.046.224-1.406.594-.37.36-.594.856-.594 1.406 0 .55.224 1.046.594 1.406l3.406 3.438 3.406-3.438c.37-.37.594-.856.594-1.406 0-.55-.224-1.046-.594-1.406-.36-.37-.856-.594-1.406-.594-.55 0-1.046.224-1.406.594-.37.36-.594.856-.594 1.406 0-.55-.224-1.046-.594-1.406-.36-.37-.856-.594-1.406-.594z" /></svg>');
    icons.push('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="20" height="20" data-icon="thumb-up" viewBox="-1 -1 10 10"><path d="M4.438 0c-.19.021-.34.149-.438.344-.13.26-1.101 2.185-1.281 2.375-.19.18-.439.281-.719.281v4.001h3.5c.21 0 .389-.133.469-.313 0 0 1.031-2.908 1.031-3.188 0-.28-.22-.5-.5-.5h-1.5c-.28 0-.5-.25-.5-.5s.389-1.574.469-1.844c.08-.27-.053-.545-.313-.625l-.219-.031zm-4.438 3v4h1v-4h-1z" /></svg>');
    if (options) {
      $.extend(settings, options);
    }
    var collect = [];
    console.log(settings);
    this.each(function() {
      var selectors = ['img'];
      var ignoreList = '.no-like';
      if (settings.ignore) {
        ignoreList = ignoreList + ',' + settings.ignore;
      }
      var images = $(this).find(selectors.join(','));
      if ($(this).is('img')) images = $(this);
      images.each(function() {
        var _this = $(this);
        if (_this.is(ignoreList)) {
          return;
        }
        var pinUrl = settings.pageUrl;
        var pinMedia = _this[0].src;
        var pinDesc = _this.attr('alt') ? _this.attr('alt') : _this.attr('title');
        var pinIsVideo = 'false';
        var wrap = '<div class="like-me"></div>';
        _this.wrap(wrap).parent('.like-me').css({position: 'relative'});
        _this.after('<span class="like-me-button"><a style="display:block" href="#" data-media="' + encodeURI(pinMedia) + '">' + icons[settings.icon] + '<div></div></a></span>').next('.like-me-button').css(positions[settings.position]);
        collect.push(pinMedia);
        if (settings.votes && settings.votes[pinMedia]) {
          if (_this.next().length) {
            _this.next().find('div').text(settings.votes[pinMedia]);
          }
        }
        _this.parent().addClass('like-me-wrap');
        if (settings.showOnHover == true) {
          $('.like-me-button').css('display', 'none');
          $('.like-me').hover(function() {
            $( this ).find('.like-me-button').stop().fadeIn(settings.fadeDuration);
          }, function() {
            $( this ).find('.like-me-button').stop().fadeOut(settings.fadeDuration);
          });
        }
        _this.parent().find('a').on('click', function (e) {
          e.preventDefault();
          var _this = $(this);
          $.post(this.href, {like_me: 1, media: _this.attr('data-media')}, function(res) {
            try {
              res = JSON.parse(res);
              if (res.likes) {
                _this.find('div').text(res.likes);
              } else {
                _this.find('div').text('');
              }
            } catch (e) {
              throw e;
            }
          }).fail(function(){
            console.log('fail');
          });
        });
      });
    });
    $.post(this.href, {collect: collect}, function(res){
      try {
        res = JSON.parse(res);
        if (res.likes) {
          for (var i in res.likes) {
            if (res.likes.hasOwnProperty(i)) {
              $('img[src$="'+i+'"]').parent().find('div').text(res.likes[i]);
            }
          }
        }
      } catch (e) {
        throw e;
      }
    }).fail(function(){
      console.log('fail');
    });
    return this;
  };
})(jQuery);