/* global pm, wpcom_reblog */
var jetpackLikesWidgetQueue = [];
var jetpackLikesWidgetBatch = [];
var jetpackLikesMasterReady = false;
function JetpackLikespostMessage( message, target ) {
if ( 'string' === typeof message ){
try {
message = JSON.parse( message );
} catch(e) {
return;
}
}
pm( {
target: target,
type: 'likesMessage',
data: message,
origin: '*'
} );
}
function JetpackLikesBatchHandler() {
var requests = [];
jQuery( 'div.jetpack-likes-widget-unloaded' ).each( function() {
if ( jetpackLikesWidgetBatch.indexOf( this.id ) > -1 ) {
return;
}
jetpackLikesWidgetBatch.push( this.id );
var regex = /like-(post|comment)-wrapper-(\d+)-(\d+)-(\w+)/,
match = regex.exec( this.id ),
info;
if ( ! match || match.length !== 5 ) {
return;
}
info = {
blog_id: match[2],
width: this.width
};
if ( 'post' === match[1] ) {
info.post_id = match[3];
} else if ( 'comment' === match[1] ) {
info.comment_id = match[3];
}
info.obj_id = match[4];
requests.push( info );
});
if ( requests.length > 0 ) {
JetpackLikespostMessage( { event: 'initialBatch', requests: requests }, window.frames['likes-master'] );
}
}
function JetpackLikesMessageListener( event, message ) {
var allowedOrigin, $container, $list, offset, rowLength, height, scrollbarWidth;
if ( 'undefined' === typeof event.event ) {
return;
}
// We only allow messages from one origin
allowedOrigin = window.location.protocol + '//widgets.wp.com';
if ( allowedOrigin !== message.origin ) {
return;
}
if ( 'masterReady' === event.event ) {
jQuery( document ).ready( function() {
jetpackLikesMasterReady = true;
var stylesData = {
event: 'injectStyles'
},
$sdTextColor = jQuery( '.sd-text-color' ),
$sdLinkColor = jQuery( '.sd-link-color' );
if ( jQuery( 'iframe.admin-bar-likes-widget' ).length > 0 ) {
JetpackLikespostMessage( { event: 'adminBarEnabled' }, window.frames[ 'likes-master' ] );
stylesData.adminBarStyles = {
background: jQuery( '#wpadminbar .quicklinks li#wp-admin-bar-wpl-like > a' ).css( 'background' ),
isRtl: ( 'rtl' === jQuery( '#wpadminbar' ).css( 'direction' ) )
};
}
if ( ! window.addEventListener ) {
jQuery( '#wp-admin-bar-admin-bar-likes-widget' ).hide();
}
stylesData.textStyles = {
color: $sdTextColor.css( 'color' ),
fontFamily: $sdTextColor.css( 'font-family' ),
fontSize: $sdTextColor.css( 'font-size' ),
direction: $sdTextColor.css( 'direction' ),
fontWeight: $sdTextColor.css( 'font-weight' ),
fontStyle: $sdTextColor.css( 'font-style' ),
textDecoration: $sdTextColor.css('text-decoration')
};
stylesData.linkStyles = {
color: $sdLinkColor.css('color'),
fontFamily: $sdLinkColor.css('font-family'),
fontSize: $sdLinkColor.css('font-size'),
textDecoration: $sdLinkColor.css('text-decoration'),
fontWeight: $sdLinkColor.css( 'font-weight' ),
fontStyle: $sdLinkColor.css( 'font-style' )
};
JetpackLikespostMessage( stylesData, window.frames[ 'likes-master' ] );
JetpackLikesBatchHandler();
jQuery( document ).on( 'inview', 'div.jetpack-likes-widget-unloaded', function() {
jetpackLikesWidgetQueue.push( this.id );
});
});
}
if ( 'showLikeWidget' === event.event ) {
jQuery( '#' + event.id + ' .post-likes-widget-placeholder' ).fadeOut( 'fast', function() {
jQuery( '#' + event.id + ' .post-likes-widget' ).fadeIn( 'fast', function() {
JetpackLikespostMessage( { event: 'likeWidgetDisplayed', blog_id: event.blog_id, post_id: event.post_id, obj_id: event.obj_id }, window.frames['likes-master'] );
});
});
}
if ( 'clickReblogFlair' === event.event ) {
wpcom_reblog.toggle_reblog_box_flair( event.obj_id );
}
if ( 'showOtherGravatars' === event.event ) {
$container = jQuery( '#likes-other-gravatars' );
$list = $container.find( 'ul' );
$container.hide();
$list.html( '' );
$container.find( '.likes-text span' ).text( event.total );
jQuery.each( event.likers, function( i, liker ) {
var element = jQuery( '
' );
element.addClass( liker.css_class );
element.find( 'a' ).
attr({
href: liker.profile_URL,
rel: 'nofollow',
target: '_parent'
}).
addClass( 'wpl-liker' );
element.find( 'img' ).
attr({
src: liker.avatar_URL,
alt: liker.name
}).
css({
width: '30px',
height: '30px',
paddingRight: '3px'
});
$list.append( element );
} );
offset = jQuery( '[name=\'' + event.parent + '\']' ).offset();
$container.css( 'left', offset.left + event.position.left - 10 + 'px' );
$container.css( 'top', offset.top + event.position.top - 33 + 'px' );
rowLength = Math.floor( event.width / 37 );
height = ( Math.ceil( event.likers.length / rowLength ) * 37 ) + 13;
if ( height > 204 ) {
height = 204;
}
$container.css( 'height', height + 'px' );
$container.css( 'width', rowLength * 37 - 7 + 'px' );
$list.css( 'width', rowLength * 37 + 'px' );
$container.fadeIn( 'slow' );
scrollbarWidth = $list[0].offsetWidth - $list[0].clientWidth;
if ( scrollbarWidth > 0 ) {
$container.width( $container.width() + scrollbarWidth );
$list.width( $list.width() + scrollbarWidth );
}
}
}
pm.bind( 'likesMessage', JetpackLikesMessageListener );
jQuery( document ).click( function( e ) {
var $container = jQuery( '#likes-other-gravatars' );
if ( $container.has( e.target ).length === 0 ) {
$container.fadeOut( 'slow' );
}
});
function JetpackLikesWidgetQueueHandler() {
var $wrapper, wrapperID, found;
if ( ! jetpackLikesMasterReady ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
if ( jetpackLikesWidgetQueue.length > 0 ) {
// We may have a widget that needs creating now
found = false;
while( jetpackLikesWidgetQueue.length > 0 ) {
// Grab the first member of the queue that isn't already loading.
wrapperID = jetpackLikesWidgetQueue.splice( 0, 1 )[0];
if ( jQuery( '#' + wrapperID ).hasClass( 'jetpack-likes-widget-unloaded' ) ) {
found = true;
break;
}
}
if ( ! found ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
} else if ( jQuery( 'div.jetpack-likes-widget-unloaded' ).length > 0 ) {
// Grab any unloaded widgets for a batch request
JetpackLikesBatchHandler();
// Get the next unloaded widget
wrapperID = jQuery( 'div.jetpack-likes-widget-unloaded' ).first()[0].id;
if ( ! wrapperID ) {
// Everything is currently loaded
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
}
if ( 'undefined' === typeof wrapperID ) {
setTimeout( JetpackLikesWidgetQueueHandler, 500 );
return;
}
$wrapper = jQuery( '#' + wrapperID );
$wrapper.find( 'iframe' ).remove();
if ( $wrapper.hasClass( 'slim-likes-widget' ) ) {
$wrapper.find( '.post-likes-widget-placeholder' ).after( '' );
} else {
$wrapper.find( '.post-likes-widget-placeholder' ).after( '' );
}
$wrapper.removeClass( 'jetpack-likes-widget-unloaded' ).addClass( 'jetpack-likes-widget-loading' );
$wrapper.find( 'iframe' ).load( function( e ) {
var $iframe = jQuery( e.target );
$wrapper.removeClass( 'jetpack-likes-widget-loading' ).addClass( 'jetpack-likes-widget-loaded' );
JetpackLikespostMessage( { event: 'loadLikeWidget', name: $iframe.attr( 'name' ), width: $iframe.width() }, window.frames[ 'likes-master' ] );
if ( $wrapper.hasClass( 'slim-likes-widget' ) ) {
$wrapper.find( 'iframe' ).Jetpack( 'resizeable' );
}
});
setTimeout( JetpackLikesWidgetQueueHandler, 250 );
}
JetpackLikesWidgetQueueHandler();
;
function plusone_vote(){omniTrackEv("google+")}jQuery(document).ready(function(a){a(".entry-tools .share > a").click(function(b){b.preventDefault();var c=a(this).closest("li");c.hasClass("selected")||omniTrackEv("share"),c.toggleClass("selected")}),a("[data-omniture-event]").click(function(){void 0!=typeof omniTrackEv&&omniTrackEv(a(this).attr("data-omniture-event"))});var b="ontouchstart"in document.documentElement;b||(a(".read-later > a").on("mouseenter",function(){a(".read-later > ul").show()}),a(".read-later").on("mouseleave",function(){a(".read-later > ul").hide()})),a(".more-photos").length&&a(".more-photos .categories li a").click(function(b){b.preventDefault();var c=a(this).attr("data-site");a(".more-photos .categories li").removeClass("current"),a(".more-photos .gallery-thumbs").addClass("loading"),a(this).parent().addClass("current"),a.ajax({url:ajaxurl,data:{action:"time_get_remote_galleries",site:c,_ajax_nonce:ajax_nonce},dataType:"json",success:function(b){b.html&&(a(".more-photos .gallery-thumbs").html(b.html),a(".more-photos .gallery-thumbs").removeClass("loading"))}})}),a(".single").length&&Modernizr.load({load:["http://widgets.outbrain.com/outbrain.js","http://cdn.taboolasyndication.com/libtrc/timeinc-time/loader.js"]}),a(".entry-sharing, .widget_time_follow, .widget_time_connect").length&&Modernizr.load({load:["https://platform.twitter.com/widgets.js","http://connect.facebook.net/en_US/all.js#xfbml=1","https://apis.google.com/js/plusone.js","http://platform.linkedin.com/in.js","http://assets.pinterest.com/js/pinit.js"],complete:function(){FB.Event.subscribe("edge.create",function(b){omniTrackEv("fb-like"),a(".double-like").length&&"desktop"===ad_mode&&(a(".double-like").show(),omniTrackEv("double-like-display")),"http://www.facebook.com/time#dbl"==b&&omniTrackEv("double-like-click")}),twttr.events.bind("tweet",function(){omniTrackEv("tweet")})}}),a(".share-fb-button").length&&a(".share-fb-button .fb-share").click(function(){window.open("http://www.facebook.com/sharer.php?u="+encodeURIComponent(window.location.href),"Share","toolbar=0,status=0,width=600,height=400")})}),navigator.mozApps&&window.locationbar.visible===!1&&$("a[href^='http']:not([href*='time.com'])").attr("target","_blank");;