/**
 * initialize the recent works section of the home page
 */
$(function() {
    if(typeof PageConfig == 'undefined') {
        var errorMsg = "there's a problem loading our recent works gallery!";
        alert(errorMsg);
        throw errorMsg;
    }
    $.getJSON(PageConfig.dataURL, function(data) {
       var items = [];
       var container = $('#recentWorks');
       
       var fbAttr = {
           'scrolling' : 'no',
           'frameborder' : '0',
           'style' : 'border:none; overflow:hidden; width:450px; height:21px;',
           'allowTransparency' : 'true'
       };
       
       $.each(data, function(key, val) {
          var thumbURL = PageConfig.blogURL + '/work/' + val.post_name + '/';
          var article = $(document.createElement('article'));
          var thumb = $(document.createElement('a')).attr({'class': 'thumb', 'href': thumbURL});
          var projectName = $(document.createElement('h4')).text(val.post_title);
          var desc = $(document.createElement('p')).text(val.post_content);
          var social = $(document.createElement('div')).attr('class', 'social');
          
          // prepare social elements
          var loveit = $(document.createElement('a')).attr({'class': 'loveit', 'href': val.post_name});
          var fb = $(document.createElement('div')).attr('class', 'fb');

          var img = $(document.createElement('img')).attr({'src': val.thumbnail, 'width': "228", 'height': "150", 'alt': val.post_title});
          var tint = $(document.createElement('img')).attr({'src': '/wp-content/themes/yolk_1.0/assets/images/thumb-overlay.png', 'width': "228", 'height': "150", 'class': 'tint'});
          img.appendTo(thumb);
          tint.appendTo(thumb);          
          
          // fb like options
          var fbOptions = {
              'app_id': PageConfig.fbAppID,
              'href': thumbURL,
              'send': "false",
              'layout': "button_count",
              'width': "86",
              'show_faces': "false",
              'font': "arial",
              'action' : "like"
          };          
          
          // create the container for the fb like iframe
          var fbDiv = $(document.createElement('div')).attr({'class': 'fb', 'id' : 'fb_' + val.ID});
          
          // create the fb like iframe
          var fb = $(document.createElement('iframe')).attr(fbAttr);
          
          // set the src attribute
          fb.attr('src', PageConfig.fbLikeURL + '?' + $.param(fbOptions));                    
          fb.appendTo(fbDiv);
          
          loveit.html('<span id="counter_' + val.ID + '" class="count">' + val.love_count + '</span>');

            loveit.click(function() {
                // love this work
                $.post('/assets/egg-white/love-it.php', {'id' : val.ID, 'cat' : 'work', 'mode' : PageConfig.mode}, function(data) {
                    $('.loveit').find('#counter_' + val.ID).html(data);
                });

                return false;
            });          
          
          loveit.appendTo(social);

          fbDiv.appendTo(social);

          thumb.appendTo(article);
          projectName.appendTo(article);
          desc.appendTo(article);
          social.appendTo(article);
          
          container.append(article);          
       });
    });
});




