/* coolz toolz */
var youtube_embed = function( find_string, find_video_string ) {
var embed_limit = 5;
var post_selector = '.post';
//var find_string = 'youtube.com';
var find_url_string = 'Click to Play
';
//var video_image_string_end = '/hqdefault.jpg">';
var all_posts = document.querySelectorAll( post_selector );
for ( var i = 0; i < all_posts.length; i++ )
{
var html_content = all_posts[ i ].innerHTML;
var html_content_lowercase = html_content.toLowerCase();
//console.log( 'node ' + i.toString() );
//console.log( html_content_lowercase );
var find_position = 0;
//while ( ( find_position = html_content_lowercase.indexOf( find_string ) ) !== -1 )
for ( var embed_count = 0; embed_count < embed_limit; embed_count++ )
{
//console.log( 'find' + find_position );
//console.log( html_content_lowercase.substr( find_position ) );
if ( find_position === -1 )
{
break;
}
if ( ( find_position = html_content_lowercase.indexOf( find_string, find_position ) ) !== -1 )
{
//console.log( 'found' + find_position.toString() );
//console.log( 'youtube possible in node ' + i.toString() );
//console.log( html_content_lowercase.substr( find_position ) );
//var find_url_position = html_content_lowercase.lastIndexOf( find_url_string, find_position );
//
//if ( find_url_position === -1 )
//{
// find_url_position = html_content_lowercase.lastIndexOf( find_url_string_other, find_position );
//}
var find_url_position = html_content_lowercase.lastIndexOf( find_url_string_other, find_position );
if ( find_url_position === -1 )
{
find_url_position = html_content_lowercase.lastIndexOf( find_url_string, find_position );
}
//console.log( 'find_url_position' + find_url_position.toString() );
if ( find_url_position !== -1 )
{
//console.log( 'find_url_position' );
var find_url_end_position = html_content_lowercase.indexOf( find_url_end_string, find_url_position );
if ( find_url_end_position !== -1 )
{
//console.log( 'find_url_end_position' );
var url_string = html_content.substr( find_url_position, find_url_end_position - find_url_position + find_url_end_string.length );
//console.log( url_string );
var find_link_position = html_content_lowercase.indexOf( find_link_string, find_url_position );
if ( find_link_position !== -1 )
{
//console.log( 'find_link_position' );
var find_link_end_position = html_content_lowercase.indexOf( find_link_end_string, find_link_position + find_link_string.length );
if ( find_link_end_position !== -1 )
{
//console.log( 'find_link_end_position' );
var link_string = html_content.substr( find_link_position, find_link_end_position - find_link_position + find_link_end_string.length );
//console.log( link_string );
var find_video_position = html_content_lowercase.indexOf( find_video_string, find_link_position );
if ( find_video_position !== -1 )
{
//console.log( 'find_video_position' );
var video_string = html_content.substr( find_video_position + find_video_string.length, find_link_end_position - find_video_position + find_link_end_string.length - find_video_string.length - find_link_end_string.length );
//console.log( video_string );
for ( var j = 0; j < video_string_terminators.length; j++ )
{
var video_string_terminator_position = video_string.indexOf( video_string_terminators[ j ] );
if ( video_string_terminator_position !== -1 )
{
video_string = video_string.substr( 0, video_string_terminator_position );
}
}
//console.log( video_string );
if ( video_string.length > 0 )
{
//var embed_html = embed_string + video_string + embed_string_end;
//var video_image_html = video_image_string + video_string + video_image_string_end;
var video_image_html = video_image_string;
while ( ( video_string_placeholder_position = video_image_html.indexOf( video_string_placeholder ) ) !== -1 )
{
video_image_html = video_image_html.replace( video_string_placeholder, video_string );
}
//console.log( video_image_html );
var post_string = html_content.substr( 0, find_url_position );
var post_end_string = html_content.substr( find_url_position, html_content.length );
//var post_html = post_string + embed_html + post_end_string;
var post_html = post_string + video_image_html + post_end_string;
//console.log( post_html );
//console.log( 'inserting youtube embed in node ' + i.toString() );
all_posts[ i ].innerHTML = post_html;
html_content = all_posts[ i ].innerHTML;
html_content_lowercase = html_content.toLowerCase();
find_position = post_string.length + video_image_html.length + url_string.length;
}
}
}
}
}
}
}
}
}
};
var youtube_onclick_override = function( e ) {
e.preventDefault();
//console.log( 'youtube click override' );
var find_video_string = '#';
var video_string_position = this.href.lastIndexOf( find_video_string );
if ( video_string_position !== -1 )
{
var video_string = this.href.substr( video_string_position + find_video_string.length );
var embed_string = '';
var embed_html = embed_string + video_string + embed_string_end;
this.parentNode.innerHTML = embed_html;
}
};
var youtube_embed_attach_handler = function( handler ) {
//console.log( 'attach youtube click handler' );
var embed_link_selector = 'div.youtube_embed div.overlay';
var all_embed_links = document.querySelectorAll( embed_link_selector );
for ( var i = 0; i < all_embed_links.length; i++ )
{
all_embed_links[ i ].parentNode.onclick = handler;
}
};
//console.log( 'LOADING coolz.js' );
//window.onload = function() {
youtube_embed( 'youtube.com', 'v=' );
youtube_embed( 'youtu.be', 'be/' );
youtube_embed_attach_handler( youtube_onclick_override );
//}