// JavaScript Document
function clog(x){
if (typeof(console) == "object"){console.log(x)};
};
function _communityhelp(){
var self = this;
var cdnPrefix = 'http://i.cdn.turner.com/v5cache/CARTOON/site/';
/* hard coded CMA IDs to know what data to get */
this.init = function(){
var jsonInit = { "items": [
{
"cmaID": "450042",
"divID": "community-sbsu"
},
{
"cmaID": "450121",
"divID": "community-mim"
}
]
}
/* loop json data to make ajax call(s) */
for (var i = 0; i < jsonInit.items.length; i++){
self.getData(jsonInit.items[i].cmaID, jsonInit.items[i].divID);
}
}
/* ajax call to data for community-help section */
this.getData = function(cmaID, divID){
if (typeof cmaID != 'undefined'){
jQuery.ajax({
url: '/cnservice/cartoonsvc/content/json/getContentById.do?contentId=' + cmaID,
dataType: 'json',
success: function(data){
self.createHtml(data, divID);
},
error: function(jqXHR){
}
});
}
}
/* create HTML string from returned JSON from the CMA */
this.createHtml = function(data, divID){
var html = '
'
+ '';
self.insertHtml(html, divID);
}
/* insert HTML string into appropriate div */
this.insertHtml = function(html, divID){
jQuery('#' + divID).html(html);
}
}
function community(){
function numberWithCommas(x) { /*return comma formated numbers*/
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function util(){
/*Object[key] function*/
if (!Object.keys) Object.keys = function(o) {
if (o !== Object(o))
throw new TypeError('Object.keys called on a non-object');
var k=[],p;
for (p in o) if (Object.prototype.hasOwnProperty.call(o,p)) k.push(p);
return k;
};
/*Object[key] function*/
/*array.indexOf function*/
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
/*array.indexOf function*/
/*array.filter function*/
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
/*array.filter function*/
};
/*
jQuery AJAX Call
It will log and return string 'error' with callback;
options are set by _options object when called
requires at least 'url' property from _options.
*/
function ajaxCall(_options, callback) {
var _default = {/*Default Values*/
type: 'GET',
data: 'json',
contentType: 'application/json',
url: '',
dataType:'json'
};
var _setting = jQuery.extend({}, _default, _options);/*Merges default/options values*/
/*clog(_setting);*/
jQuery.ajax({
contentType: _setting.contentType,
type: _setting.type,
data: _setting.data,
url: _setting.url,
dataType:_setting.dataType,
success: function(data){callback(data)},
error: function(xhr, text, error){
clog('ajax error:');
clog(_options);
clog(text);
clog(error);
clog(xhr);
callback('error');
}
});
};
/*Init toon.lib*/
var _toon = new toon_lib;
_toon.init(window.location);
/*Init toon.lib*/
/*Function for You're famous blog
wordpress api call through CN cache layer
www.cartoonnetwork.com/wp-api/ is the cache layer for public-api.wordpress.com/rest/v1/sites/4222494/
*/
function famous(){
var _opts = {
url :'http://www.cartoonnetwork.com/wp-api/posts/?category=youre-famous&number=1',
dataType : 'jsonp'
};
ajaxCall(_opts, function(data){
var html = jQuery('#template_famous').html(); /*html template*/
var wpURL = 'https://public-api.wordpress.com/rest/v1/sites/4222494/'; /*word press api url*/
var cnURL = 'http://www.cartoonnetwork.com/wp-api/'; /*CN cache layer url*/
html = html.replace('{{url}}', 'http://www.cartoonnetwork.com/blog/post/' + data.posts[0].slug);
var reply_opts = {};
reply_opts.url = data.posts[0].meta.links.replies.replace(wpURL, cnURL); /*replace word press address with cn cache layer*/
reply_opts.dataType = 'jsonp';
ajaxCall(reply_opts, function(reply_data){
if (reply_data.found > 0){ /*make sure there is at least one comment*/
var dname = reply_data.comments[0].content.replace('
', '').replace('
', '');/*removing html code*/
if (location.hostname.indexOf("staging")>-1) {dname = 'LANKY JONAS MANTIS'};/*if is on staging replace it w/ know dname*/
html = html.replace('{{dname}}', dname.replace(/\s/g, '
'));
_toon.name_search({ /*retrieve user info through toon.lib*/
name:dname,
exact:'true',
with_dna:'false'
}, function(toon_data){
html = html.replace('{{avatar}}', toon_data[0].avatar)
.replace('{{levelImage}}', toon_data[0].levelInfo.levelImage.lrg);
var famous_ad = jQuery('#div_ce_famouse_ad').html();/*extract famous ad*/
jQuery('#div_ce_famous').html(html);
//jQuery('#div_ce_famouse_ad').html(famous_ad);/*inject famous ad into new famous html*/
});/*_toon.name_search({*/
};/*if (reply_data.found > 0){*/
});/*ajaxCall(url:reply_url, function(reply){*/
});/*ajaxCall(_opts, function(data){*/
};/*functioni famous()*/
/*Function for first 4 blog post
exclude you're famous post
url:'http://www.cartoonnetwork.com/wp-api/posts/?number=5', number = 5 incase there one of them is you're famous post*/
function blog_posts(){
var _opts = {
url:'http://www.cartoonnetwork.com/wp-v1-batch-api/?urls%5B%5D=/sites/cartoonnetworkblog.wordpress.com/posts/?sticky=true&urls%5B%5D=/sites/cartoonnetworkblog.wordpress.com/posts/&pretty=true',
dataType:'jsonp'
};
ajaxCall(_opts, function(data){
if (data == 'error'){
jQuery('#div_blog_error').show();
jQuery('#div_ce_blog_posts').hide();
return false;
}
// Build an array for the keys
var posts = [];
var threadIDs = [];
for (var key in data) {
for (var i=0; i < data[key].posts.length; i++) {
if (!data[key].posts[i].categories["You're Famous!"]){ /*skip if it's you're famous blog*/
var obj = {};
obj.id = data[key].posts[i].ID;
obj.url = 'http://www.cartoonnetwork.com/blog/post/' + data[key].posts[i].slug;
obj.featured_image = data[key].posts[i].featured_image;
obj.title = data[key].posts[i].title;
obj.threadID = thread_id_extractor(data[key].posts[i].content);
/*Assign proper name to the category*/
if (data[key].posts[i].categories['Advertisement']) {
obj.category = 'Advertisement';
} else if (data[key].posts[i].categories['Community']) {
obj.category = 'Community';
} else if (data[key].posts[i].categories['Cool & New']) {
obj.category = 'Cool & New';
} else if (data[key].posts[i].categories['Games']) {
obj.category = 'Games';
} else if (data[key].posts[i].categories['Mobile']) {
obj.category = 'Mobile';
} else if (data[key].posts[i].categories['Video']) {
obj.category = 'Video';
} else {
obj.category = 'Cool & New';
}
/*Assign proper name to the category*/
obj.messageCount = 0;
posts.push(obj);
threadIDs.push(obj.threadID);
if (posts.length == 4) {break};
};/*if (!data.posts[i].categories["You're Famous!"]){*/
};/*for (var i=0; i -1) {posts[indexNo].messageCount = data[i].stats.messageCount};
};/*for (var i=0; i'))
.replace('{{rating}}', numberWithCommas(data.plays[i].rating));
jQuery('#div_ce_tpw_'+(i+1)).html(html);
} else if (i<9){ /*rank 4 to 9*/
var html = template_4to9
.replace('{{avatar}}', data.plays[i].user_info.avatar)
.replace('{{levelImage}}', data.plays[i].user_info.levelInfo.levelImage.sml)
.replace(/{{playerID_link}}/g, data.plays[i].user_info.playerID.replace(/\s/g, '-'))
.replace('{{playerID}}', data.plays[i].user_info.playerID)
.replace('{{rating}}', numberWithCommas(data.plays[i].rating));
if (i<6){
jQuery('#div_ce_tpw_4to6 .ce_tpw_'+(i+1)).html(html);
} else if (i<9){
jQuery('#div_ce_tpw_7to9 .ce_tpw_'+(i+1)).html(html);
};
};
};/*for (i=0; i<3; i++){*/
/*Additional UI effect.*/
jQuery('.ce_tpw_top3_player_info_avatar a').hover(
function(){
jQuery(this).parent('.ce_tpw_top3_player_info_avatar')
.siblings('.ce_tpw_top3_player_info_name')
.css('color', '#0095d0');
},
function(){
jQuery(this).parent('.ce_tpw_top3_player_info_avatar')
.siblings('.ce_tpw_top3_player_info_name')
.css('color', '#000');
}
);
jQuery('.ce_tpw_avatar a').hover(
function(){
jQuery(this).parent('.ce_tpw_avatar')
.siblings('.ce_tpw_player_info')
.children('.ce_tpw_display_name')
.css('text-decoration', 'underline');
},
function(){
jQuery(this).parent('.ce_tpw_avatar')
.siblings('.ce_tpw_player_info')
.children('.ce_tpw_display_name')
.css('text-decoration', 'none');
}
);
});/*_toon.weekly_ranking_community(function(data){*/
};/*function leaderboard(){*/
/*Function to retrieve highlight badge games (same as the badge games on the games home page*/
function badge_games(){
var _opts = {
url:'/feeds/json/games.json',
dataType:'json'
};
ajaxCall(_opts, function(data){
var html = '';
for (var i=0; i 0)?{'ids':ids}:{'ids':[0]};
var _options = {/*Options to send to ajgaXall*/
url: '/forums/tf-api/threads?include_messages=true&message_limit=4&filter=moderated',
type: 'POST',
data: JSON.stringify(threadIds)
/*data: JSON.stringify({'ids':[3,19318]})*/
};
//_options.data = JSON.stringify(threadIds);
ajaxCall(_options, function(data){ /* post ajax call to retrieve threads base on over-write thread ids*/
if (data == 'error') {
jQuery('#div_mpt_error').show();
jQuery('#div_ce_mpt_slider').hide();
return false;
};
for (var i=0; i 4) ? 4:data[i].stats.messageCount,
lastPageLink: (data[i].stats.messageCount > 15) ? thread_last_page_link+(data[i].stats.messageCount-15):thread_last_page_link+'0',
messages: []
};
for (var j=0; j 0)? '|'+objMsg.tegId:objMsg.tegId;
};
};
_self.threads.push(objThread);
_self.treadIds.push(data[i].threadId);
};
callback(_self.threads, _self.stringIds, _self.treadIds);
}); /*ajaxCall(_options, function(data){ /forums/tf-api/threads?include_messages=true&message_limit=4&filter=moderated*/
}); /*ajaxCall(_opts, function(data){ /cnservice/cartoonsvc/content/json/getContentById.do?contentId=274380*/
};
/*Compile thread array with specify thread ids defined in CMA */
/*Compile the rest of thread array with latest threads*/
function getData(aryThreads, stringIds, threadIds, callback){
var _self = this;
_self.threads = aryThreads;
_self.stringIds = stringIds;
_self.threadIds = threadIds;
_self.no_of_msg_to_get = 6 - _self.threads.length;
if (_self.no_of_msg_to_get > 0) {
var _options = {
url: '/forums/tf-api/threads?offset=0&limit='+_self.no_of_msg_to_get+'&include_messages=true&message_limit=4&filter=moderated',
type: 'GET',
dataType: 'text'
};
ajaxCall(_options, function(jsontext){ /*ajax call for list of latest threads */
jsontext = jsontext.replace(/\\\'/g, "'");
var data = JSON.parse(jsontext);
for (var i=0; i 4) ? 4:data[i].stats.messageCount,
lastPageLink: (data[i].stats.messageCount > 15) ? thread_last_page_link+(data[i].stats.messageCount-15):thread_last_page_link+'0',
messages: []
};
for (var j=0; j 0)? '|'+objMsg.tegId:objMsg.tegId;
};
};/*for (var j=0; j -1){
_self.threads[i].forum.primary_color = color[forumId_pos];
} else { /*default color*/
_self.threads[i].forum.primary_color = '#0096ce';
};
};/*for (var i=1; i<_self.threads.lenght; i++){*/
callback(_self.threads, _self.stringIds, _self.treadIds);
});/*getColor(function(forumid, color){*/
});/*ajaxCall(_options, function(jsontext){*/
};/*if (_self.no_of_msg_to_get > 0) {*/
};/*function getData(aryThreads, stringIds, threadIds, callback){*/
/*Compile the rest of thread array with latest threads*/
function getColor(callback){
var _color = this;
_color.forumid = [];
_color.color = [];
var _options = {
url: '/cnservice/cartoonsvc/content/json/getContentsByContentType.do?contentTypeId=595',
type: 'GET'
};
ajaxCall(_options, function(data){ /*ajax call for color retrieval*/
for (var i=0; i -1) {
if (data[pos].levelInfo){
_self.aryThreads[i].messages[j].levelInfo = data[pos].levelInfo;
} else {
_self.aryThreads[i].messages[j].levelInfo = emptyLvlInfo;
};
};
};/*for (var j=0; j<_self.aryThreads[i].messageCount; j++){*/
};/*for (var i=0; i<_self.aryThreads.length; i++){*/
populate_threads(_self.aryThreads);
});/*_toonlib(stringIds, function(data){*/
}); /*getData(aryThreads, stringIds, threadIds, function(aryThreads, stringIds, threadIds){*/
});/*postData(function(aryThreads, stringIds, threadIds){*/
}; /*function threads(){*/
/*Populate html with aryThreads*/
function populate_threads(aryThreads){
/*function to format convet time to human readable*/
function time_formatter(unix_timestamp){
var current_date = new Date().getTime();
var timezone_offset_seconds = 18000;
current_date += timezone_offset_seconds;
var msg_date = new Date(unix_timestamp*1000);
var time_difference = new Date(current_date - msg_date);
if (time_difference < 60000){
return 'less than a minute ago';
} else if (time_difference < 120000){
return 'a minute ago';
} else if (time_difference < 3600000){
return time_difference.getMinutes() + ' minutes ago';
} else {
return msg_date.toLocaleString();
};
};/*function time_formatter(unix_timestamp){*/
/*
Before next command, the slides order from left is 0 1 2 3 4 5
Next command will reverse the order and change slide order to 5 4 3 2 1 0
*/
var thread_arry = aryThreads.reverse();
var template_header_li_html = jQuery('#div_ce_mpt_slider_header_div').html();
var template_content_li_html = jQuery('#div_ce_mpt_slider_content_li').html();
var template_replies_div = jQuery('#div_ce_mpt_slider_replies_div').html();
var forum_header_li_html = '';
var forum_content_li_html = '';
for (var i=0; i