Free javascript Hosting


js-override.js

Uploaded on Dec 21 2021 05:38 by efijewp

$(document).ready(function () {

String.prototype.trim = function() {
return this.replace(/^\s+/, '').replace(/\s+$/, '');
};

if (!('ontouchstart' in document)) {
$('body').addClass('no-touch');
}

$.fn.exists = function () {
return this.length > 0;
};

(function() {
var storage = {};

var pageContext = window['pageContext'];

var lazyLoadDefOptions = {
effect: 'fadeIn',
placeholder: 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=',
threshold: 200
};

var listNonCachableBlocks = {
'list_videos_my_favourite_videos': 1,
'list_videos_my_uploaded_videos': 1,
'list_albums_my_favourite_albums': 1,
'list_albums_my_created_albums': 1,
'list_playlists_my_created_playlists': 1,
'list_dvds_my_created_channels': 1,
'list_videos_my_channel_videos': 1,
'list_members_subscriptions_my_subscriptions': 1,
'list_videos_videos_from_my_subscriptions': 1,
'list_albums_albums_from_my_subscriptions': 1,
'list_videos_my_purchased_videos': 1,
'list_albums_my_purchased_albums': 1,
'list_members_my_conversations': 1,
'list_members_my_friends': 1,
'list_messages_my_conversation_messages': 1
};

var isMobileDevice = (/iphone|ipad|Android|webOS|iPod|BlackBerry|Windows Phone/gi).test(navigator.appVersion);

var utilitiesMergeOptions = function(def, contextKey) {
if (pageContext && pageContext[contextKey]) {
for (var prop in pageContext[contextKey]) {
if (pageContext[contextKey].hasOwnProperty(prop)) {
def[prop] = pageContext[contextKey][prop];
}
}
}
return def;
};

var utilitiesLoadSmileys = function($container) {
$container.find('img[data-src]').each(function () {
var originalSrc = $(this).attr('data-src');
if (originalSrc) {
this.src = originalSrc;
$(this).removeAttr('data-src');
}
});
};

var utilitiesScrollTo = function($obj, speed) {
if (typeof speed == 'undefined') {
speed = 400;
}
if ($obj.exists()) {
var windowTop = $(document).scrollTop();
var windowBottom = windowTop + $(window).height();
var objectTop = $obj.offset().top;
if (objectTop > windowTop && objectTop < windowBottom) {
return;
}
}
$.scrollTo($obj, speed, {offset: -100});
};

var utilitiesParseParameters = function(str) {
var result = {};
if (str) {
var params = str.split(';');
for (var i = 0; i < params.length; i++) {
var pair = params[i].split(':');
if (pair.length == 2) {
var paramNames = pair[0].split('+');
for (var j = 0; j < paramNames.length; j++) {
result[paramNames[j]] = decodeURIComponent(pair[1]).replace(/[+]/g,' ');
}
}
}
}
return result;
};

var utilitiesGetBlock = function (blockId, sender, args, params) {
var url = (args.url ? args.url : window.location.href);
if (url.indexOf('#') > 0) {
url = url.substring(0, url.indexOf('#'));
}
$.ajax({
url: url + (url.indexOf('?') >= 0 ? '&' : '?') + 'mode=async&function=get_block&block_id=' + blockId + (params ? '&' + $.param(params) : ''),
type: 'GET',
cache: false,
beforeSend: function () {
$(sender).block({message: null});
if (args.beforeSend) {
args.beforeSend(sender);
}
},
complete: function () {
$(sender).unblock();
if (args.complete) {
args.complete(sender);
}
console.log(1221)
},
success: function (html) {
storage[blockId] = params;
if (args.success) {
args.success(sender, html);
}
},
error: function () {
if (args.error) {
args.error(sender);
}
}
});
};

var utilitiesAjaxRequest = function(sender, params, successCallback) {
var url = window.location.href;
if (url.indexOf('#') > 0) {
url = url.substring(0, url.indexOf('#'));
}
$.ajax({
url: url + (url.indexOf('?') >= 0 ? '&' : '?') + 'mode=async&format=json&' + $.param(params),
type: 'GET',
beforeSend: function () {
$(sender).block({message: null});
},
complete: function () {
$(sender).unblock();
},
success: function (json) {
if (typeof json != 'object') {
json = JSON.parse(json);
}
if (json && successCallback) {
successCallback(json);
}
}
});
};

var utilitiesRecaptcha = function($container) {
if (!$container) {
$container = $(document);
}
if (typeof grecaptcha == 'object') {
$container.find('[data-recaptcha-key]').each(function() {
var $outer = $(this);
if (!$outer.attr('data-recaptcha-id')) {
$outer.html('');
var recaptchaId = grecaptcha.render($outer.get(0), {
sitekey: $outer.attr('data-recaptcha-key'),
theme: $outer.attr('data-recaptcha-theme') || 'light',
size: $outer.attr('data-recaptcha-size') || 'normal',
callback: function() {
var $errorContainer = $outer.parent().find('.field-error');
$errorContainer.fadeOut();
$outer.parent().find('.error').removeClass('error');
}
});
$outer.attr('data-recaptcha-id', recaptchaId);
}
});
}
};

var utilitiesAjaxForm = function($form, callbacks, withCredentials) {
var considerFormBlocking = function($form, isBlock) {
var $popupParent = $form.parents('.popup-holder');
if ($popupParent.exists()) {
$form = $popupParent;
}
isBlock ? $form.block({ message: null }) : $form.unblock();
};

var defaultErrorMessage = 'Unexpected server response received. Please contact support.';
if (pageContext && pageContext['server_error']) {
defaultErrorMessage = pageContext['server_error'];
}

$form.ajaxForm({
data: {
format: 'json',
mode: 'async'
},

xhrFields: {
withCredentials: !!withCredentials
},

beforeSerialize: function () {
var $autoPopulates = $form.find('[data-form-populate-from]');
$autoPopulates.each(function() {
var populateFromName = $(this).attr('data-form-populate-from');
if (populateFromName) {
var $populateFrom = $form.find('[name="' + populateFromName + '"]');
if ($populateFrom.exists()) {
$(this).val($populateFrom.val());
}
}
});
if (callbacks && callbacks['beforeSerialize']) {
callbacks['beforeSerialize']($form);
}
},

beforeSubmit: function (data) {
var confirmText = $form.attr('data-confirm') || '';
if (confirmText && !confirm(confirmText)) {
return false;
}

var result = true;
if (callbacks && callbacks['beforeSubmit']) {
result = callbacks['beforeSubmit']($form, data);
}
considerFormBlocking($form, result);
return result;
},

uploadProgress: function(event, position, total, percent) {
if (callbacks && callbacks['uploadProgress']) {
callbacks['uploadProgress']($form, percent);
}
},

success: function (response, statusText, xhr) {
$form.find('.generic-error').empty().hide();
considerFormBlocking($form, false);

if (xhr.getResponseHeader('Content-Type').indexOf('application/json') >= 0) {
if (typeof response != 'object') {
response = JSON.parse(response);
}

if (response['status'] == 'failure') {
for (var i = 0; i < response['errors'].length; i++) {
var error = response['errors'][i];

var fieldName = error['field'];
var errorCode = error['code'];
var errorMessage = error['message'];

var $errorContainer = null;
if (fieldName) {
var $field = $form.find('[name="' + fieldName + '"]');
if (!$field.exists()) {
$field = $form.find('[data-name="' + fieldName + '"] [type="text"]');
}
if (!$field.exists()) {
$field = $form.find('[data-name="' + fieldName + '"] select');
}
if (!$field.exists()) {
$field = $form.find('[data-name="' + fieldName + '"]');
}
if ($field.exists()) {
$field.addClass('error');
$field.closest('.row').addClass('error');
$field.parents('.file-control').find('[type="text"]').addClass('error');
$errorContainer = $field.parent().find('.field-error');
if (!$errorContainer.exists()) {
var fieldTitle = $field.parent().find('label').text();
if (fieldTitle) {
errorMessage += ' (' + fieldTitle + ')';
}
}
if (i==0) {
$field.focus();
}
} else {
errorMessage += ' (' + fieldName + ')';
}
}
if (!$errorContainer || !$errorContainer.exists()) {
$errorContainer = $form.find('.generic-error');
}

$errorContainer.empty().html(errorMessage).fadeIn();

if (fieldName == 'code' && errorCode != 'required') {
var $captcha = $form.find('.captcha-control img');
if ($captcha.exists()) {
$captcha.attr('src', $captcha.attr('src').replace(new RegExp('rand=\\d+'),'rand=' + new Date().getTime()));
$form.find('.captcha-control .textfield').val('');
}
}
}

if (typeof grecaptcha == 'object') {
$form.find('[data-recaptcha-key]').each(function() {
var recaptchaId = $(this).attr('data-recaptcha-id');
if (recaptchaId) {
if (grecaptcha.getResponse(recaptchaId)) {
grecaptcha.reset(recaptchaId);
}
}
});
}

if (callbacks && callbacks['error']) {
callbacks['error']($form);
}
utilitiesScrollTo($form, 0);
} else if (response['status'] == 'success') {
if (callbacks && callbacks['success']) {
callbacks['success']($form, response['data']);
} else if (response['redirect']) {
window.location = response['redirect'];
} else {
var $reloader = $('[data-reload-to]');
if ($reloader.exists()) {
window.location = $reloader.attr('data-reload-to');
} else {
if ($form.hasClass('ajax-form')) {
$form.closest('.popup-holder').html($form.attr('data-success'));
} else if ($form.hasClass('ajax-form-message')) {
$form.closest('.block-massage').addClass('me-block');
$form.closest('.item').addClass('me');
$form.closest('.message-holder').removeClass('form-message');
if ($form.hasClass('reject')) {
$form.closest('.text').html($form.attr('data-reject'));
} else {
$form.closest('.text').html($form.attr('data-success'));
}

} else {
window.location.reload();
}
}
}
} else {
$form.find('.generic-error').html(defaultErrorMessage).show();
utilitiesScrollTo($form, 0);
if (callbacks && callbacks['error']) {
callbacks['error']($form);
}
}

} else if (xhr.getResponseHeader('Content-Type').indexOf('text/html') >= 0) {
if (callbacks && callbacks['success']) {
callbacks['success']($form, response);
} else {
if ($(response).attr('data-fancybox') == 'message' || $(response).find('[data-fancybox="message"]').exists()) {
$.fancybox($(response), {
topRatio: 0.3,

beforeClose: function() {
var $redirectTo = this.inner.find('[data-fancybox-redirect-to]');
if ($redirectTo.exists()) {
window.location = $redirectTo.attr('data-fancybox-redirect-to');
} else {
window.location.reload();
}
return true;
}
});
} else {
$form.empty().append(response);
}
}
} else {
$form.find('.generic-error').html(defaultErrorMessage).show();
utilitiesScrollTo($form, 0);
if (callbacks && callbacks['error']) {
callbacks['error']($form);
}
}
},

error: function () {
considerFormBlocking($form, false);
$form.find('.generic-error').html(defaultErrorMessage).show();
utilitiesScrollTo($form, 0);
if (callbacks && callbacks['error']) {
callbacks['error']($form);
}
},

complete: function() {
if (callbacks && callbacks['complete']) {
callbacks['complete']($form);
}
}
});

$form.find('input, select, textarea').each(function() {
var $field = $(this);

var hideErrorFunction = function() {
var $errorContainer = $field.parent().find('.field-error');
$errorContainer.fadeOut();
$field.removeClass('error');
$field.closest('.row').removeClass('error');
$field.parents('fieldset').removeClass('error');
$field.parents('.file-control').find('[type="text"]').removeClass('error');
};

$field.change(hideErrorFunction);
if ($field.get(0).tagName.toLowerCase() == 'textarea' || $field.get(0).type == 'text' || $field.get(0).type == 'password') {
$field.keypress(hideErrorFunction);
}
});

$form.find('.file-control [type="file"]').change(function() {
var $input = $(this);
var value = $input.val();
if (value.lastIndexOf('/') >= 0) {
value = value.substring(value.lastIndexOf('/') + 1);
}
if (value.lastIndexOf('\\') >= 0) {
value = value.substring(value.lastIndexOf('\\') + 1);
}
var files = $input.prop("files");
if (files && files.length > 1) {
value = '';
for (var i = 0; i < files.length; i++) {
if (value) {
value += ', ';
}
if (i >= 3) {
value += '...';
break;
}
value += files[i].name;
}
}

var $container = $input.parents('.file-control');
if ($input.attr('multiple') && (!files || files.length == 1)) {
var $clone = $container.clone(true, true);
$clone.wrap('<form>').parent('form').trigger('reset');
$clone.unwrap();
// $container.parent().append($clone);
}
$container.find('[type="text"]').val($container.find('.button').html().trim() + ' ' + value);
});

$form.find('.list-selector').each(function() {
var $container = $(this);

$container.find('[type="text"]').focus(function() {
var $input = $(this);
var url = $container.attr('data-selector');
var name = $container.attr('data-name');
var selected = ($container.attr('data-selected') || '').split(',');

if (!$container.find('.list-selector-popup').exists()) {
if (url && name) {
$.ajax({
url: url,
type: 'GET',
beforeSend: function () {
$input.block({message: null});
$input.css({cursor: 'wait'});
},
complete: function () {
$input.unblock();
$input.css({cursor: 'text'});
},
success: function (html) {
var selectedIds = [];
var selectedLabels = [];
var filterValue = '';
var $popupContent = $('<div class="list-selector-popup">' + html + '</div>');

var $filterInput = $popupContent.find('[name="filter"]');
for (var i = 0; i < selected.length; i++) {
var id = selected[i].trim();
if (id) {
var $checkbox = $popupContent.find('input[type="checkbox"][value="' + id + '"],input[type="radio"][value="' + id + '"]');
var $label = $popupContent.find('label[for="' + $checkbox.attr('id') + '"]');
$checkbox.prop('checked', true);
selectedIds.push(id);
selectedLabels.push($label.html());
}
}

$container.append($popupContent);
$popupContent.find('input[type="checkbox"]').click(function() {
var $checkbox = $(this);
var i = 0;
var $label = $popupContent.find('label[for="' + $checkbox.attr('id') + '"]');
if ($label.html()) {
var value = $checkbox.prop('value');
if ($checkbox.prop('checked')) {
selectedIds.push(value);
selectedLabels.push($label.html());
$container.append($('<input type="hidden" name="' + name + '[]" value="' + value + '"/>'))
} else {
for (i = 0; i < selectedIds.length; i++) {
if (selectedIds[i] == value) {
selectedIds.splice(i, 1);
selectedLabels.splice(i, 1);
break;
}
}
$container.find('input[type="hidden"][value="' + value + '"]').remove();
}
var selectedText = '';
for (i = 0; i < selectedLabels.length; i++) {
if (selectedText != '') {
selectedText += ', ';
}
selectedText += selectedLabels[i];
}
$input.prop('value', selectedText);
$input.change();
}
}).focus(function() {
$filterInput.focus();
});

$popupContent.find('input[type="radio"]').change(function() {
var $checkbox = $(this);
var i = 0;
var $label = $popupContent.find('label[for="' + $checkbox.attr('id') + '"]');
if ($label.html()) {
var value = $checkbox.prop('value');
if ($checkbox.prop('checked')) {
$container.find('input[type="hidden"][name="' + name + '[]"]').remove();
selectedIds[0] = value;
selectedLabels[0] = $label.html();
$container.append($('<input type="hidden" name="' + name + '[]" value="' + value + '"/>'))
}
var selectedText = selectedLabels[0];
$input.prop('value', selectedText);
$input.change();
}
}).focus(function() {
$filterInput.focus();
});

$filterInput.focus().keyup(function() {
if ($filterInput.val() == filterValue) {
return;
}
filterValue = this.value.toLowerCase();

$container.find('.item').each(function() {
var $item = $(this);
if (filterValue == '') {
$item.show();
} else {
$item.toggle($item.find('label').html().toLowerCase().indexOf(filterValue) >= 0);
}
});
});

$(document).mouseup(function(e) {
if (!$container.is(e.target) && $container.has(e.target).length === 0)
{
$popupContent.hide();
}
});
}
});
}
} else {
$container.find('.list-selector-popup').show();
$container.find('[name="filter"]').focus();
}
});
});

$form.find('input[placeholder],textarea[placeholder]').placeholder();

$form.find('.smileys-support img').each(function() {
$(this).attr('title', $(this).attr('alt'));
$(this).click(function() {
var $textarea = $(this).parents('.smileys-support').find('textarea');
if (!$textarea.exists()) {
return;
}
var textarea = $textarea.get(0);
var smiley = $(this).attr('alt');

if (document.selection) {
textarea.focus();
var sel = document.selection.createRange();
sel.text = smiley;
textarea.focus();
} else if (textarea.selectionStart || textarea.selectionStart == '0') {

var startPos = textarea.selectionStart;
var endPos = textarea.selectionEnd;
var scrollTop = textarea.scrollTop;

if (!$(this).parents('.smileys-support').hasClass('comment-smileys')) {
textarea.value = textarea.value.substring(0, startPos) + smiley + textarea.value.substring(endPos, textarea.value.length);
}

textarea.focus();
textarea.selectionStart = startPos + smiley.length;
textarea.selectionEnd = startPos + smiley.length;
textarea.scrollTop = scrollTop;
} else {
textarea.value += smiley;
textarea.focus();
}
});
});

$form.find('[data-expand-id]').click(function() {
var $button = $(this);
var contentId = $button.attr('data-expand-id');
if (contentId) {
var $content = $('#' + contentId);
if ($button.hasClass('expand')) {
$content.slideDown(400, function () {
$(window).trigger('scroll');
});
$button.removeClass('expand').addClass('collapse');
} else {
$content.slideUp(400, function () {
});
$button.removeClass('collapse').addClass('expand');
}
}
});

$form.find('[data-action="choose"]').each(function() {
$(this).click(function() {
if ($(this).hasClass('disabled')) {
return;
}

var $form = $(this).parents('form');
var $radio = $(this).find('input');
$form.find('[data-action="choose"] [type="radio"]').prop('checked', false);
$radio.prop('checked', true);

if ($radio.prop('checked')) {
$form.find('[data-action="choose"]').removeClass('active');
$(this).addClass('active');

var $captchaControl = $form.find('.captcha-control');
if ($captchaControl.exists()) {
if ($radio.attr('name') == 'payment_option') {
$captchaControl.append($captchaControl.parent().find('[type="submit"]'));
$captchaControl.parent().find('label').removeClass('hidden');
$captchaControl.removeClass('hidden');
} else if ($radio.attr('name') == 'card_package_id') {
$captchaControl.parent().append($captchaControl.find('[type="submit"]'));
$captchaControl.parent().find('label').addClass('hidden');
$captchaControl.addClass('hidden');
}
}
}
});
});

utilitiesRecaptcha($form);
};

var utilitiesAjaxFancyBox = function ($sender, url, afterShowCallback) {
$.fancybox([{href: url, type: 'ajax'}], {
afterShow: function() {
if (!afterShowCallback) {
this.inner.find('[data-form="ajax"]').each(function () {
utilitiesAjaxForm($(this));
});
this.inner.find('[data-fancybox="ajax"]').each(function() {
$(this).click(function(e) {
e.preventDefault();
utilitiesAjaxFancyBox($(this), this.href || $(this).attr('data-href'));
});
});
if ($.fn.select2) {
this.inner.find('.selectbox').select2();
}
$(".presents-form .nano").nanoScroller({
// scroll: 'top'
iOSNativeScrolling: true,
alwaysVisible: true

});
}
utilitiesLoadSmileys(this.inner);
if (afterShowCallback) {
afterShowCallback.call(this);
}
},

beforeClose: function() {
if ($('body').hasClass('reload')) {
window.location.reload();
}

if (this.inner.find('[data-fancybox="refresh"]').exists()) {
if ($sender && $sender.attr('data-fancybox-refresh-id')) {
utilitiesReloadBlock($sender.attr('data-fancybox-refresh-id'), $sender, false, true);
} else {
window.location.reload();
}
} else if ($sender && $sender.attr('data-fancybox-refresh-id')) {
utilitiesReloadBlock($sender.attr('data-fancybox-refresh-id'), $sender, false, true);
}
return true;
},

helpers: {
overlay: {closeClick: false}
},

type: 'ajax',
topRatio: 0.3
});
};

var utilitiesReloadBlock = function(blockId, sender, scroll, animate) {
if (!blockId) {
window.location.reload();
return;
}
var params = null;
if (storage[blockId]) {
params = storage[blockId];
}

var args = {};
args.success = function (sender, html) {
storage[blockId] = params;
var animationOpacity = 0.1;
if (!animate) {
animationOpacity = 1;
}
if (scroll) {
utilitiesScrollTo($('#' + blockId));
}
$('#' + blockId).animate({opacity: animationOpacity}, 400, function () {
var div = document.createElement('div');
div.innerHTML = html;

var content = $(div).children().first();
$(content).css('opacity', animationOpacity);
$(this).replaceWith(content);
$('#' + blockId).animate({opacity: 1}, 400);
initAjaxLists($('#' + blockId));

if (typeof(Storage) !== 'undefined') {
var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}
sessionStorage.setItem(userId + location.href + '#' + blockId, $('#' + blockId).html());
sessionStorage.setItem(userId + location.href + '#' + blockId + ':params', JSON.stringify(params));

if (typeof(window.initNewChat) !== 'undefined' && $('#js__chat-id').length) {
window.initNewChat($('#js__chat-id').val());
}
}
});
};

var errorTries = 0;
args.error = function () {
errorTries++;
var hasFromParameter = false;
for (var paramName in params) {
if (params.hasOwnProperty(paramName)) {
if (paramName.indexOf('from') == 0 && parseInt(params[paramName])>1) {
params[paramName] = parseInt(params[paramName]) - 1;
if (errorTries > 1) {
params[paramName] = 1;
}
hasFromParameter = true;
}
}
}
if (!hasFromParameter) {
params = null;
}

if (errorTries > 1) {
delete args.error;
}

utilitiesGetBlock(blockId, sender, args, params);
};

utilitiesGetBlock(blockId, sender, args, params);
};

var initMenu = function () {
$('.navigation button').click(function() {
$(this).parents('.navigation').toggleClass('open');
});
};

var initTabs = function () {
var $tabLinks = $('.tabs-menu li a');
if ($tabLinks.exists()) {
var tabLinks = {};
var tabQuery = '';
var currentActiveTab = '';
$tabLinks.each(function() {
if ($(this).attr('href').indexOf('#') == 0) {
var tabId = $(this).attr('href');
if (!currentActiveTab || tabId == window.location.hash) {
currentActiveTab = tabId;
}
tabLinks[tabId] = $(this);
tabQuery += ',' + tabId;
}
});

var $tabs = $(tabQuery ? tabQuery.substr(1) : '');
$tabs.hide();

if (currentActiveTab) {
tabLinks[currentActiveTab].addClass('active');
$(currentActiveTab).show();
}

$tabLinks.click(function(e) {
if ($(this).attr('href').indexOf('#') == 0) {
e.preventDefault();
var tabId = $(this).attr('href');
$tabLinks.removeClass('active');
$tabs.hide();
$(tabId).show();
if (tabLinks[tabId]) {
tabLinks[tabId].addClass('active');
}

$(window).trigger('scroll');
}
});
}
};

var initFancyBox = function () {
$('[data-fancybox="ajax"]').each(function() {
$(this).click(function(e) {
e.preventDefault();
utilitiesAjaxFancyBox($(this), this.href || $(this).attr('data-href'));
});
});

$('.block-album .images a.item, .block-screenshots a.item, .list-albums-images a, .list-videos-screenshots a, .img-holder-wall a, .post_link_img a').fancybox({
openEffect: 'none',
closeEffect: 'none',
prevEffect: 'none',
nextEffect: 'none',
helpers: {
title: {
type: 'inside'
},
buttons: {
position: 'bottom'
}
}
});

if (window.location.href.indexOf('?login') > 0) {
$('#login').click();
}
};

var initSelectBox = function () {
if ($.fn.select2) {
$('.selectbox').select2();
}
};

var initRating = function () {
var $ratingContainer = $('.rating-container');
var $links = $ratingContainer.find('.rate-like, .rate-dislike');
$links.click(function(e) {
e.preventDefault();
var $link = $(this);
if ($link.hasClass('disabled') || $link.hasClass('voted')) {
return;
}

var vote = parseInt($link.attr('data-vote')) || 0;
var videoId = $link.attr('data-video-id');
var albumId = $link.attr('data-album-id');
var playlistId = $link.attr('data-playlist-id');
var postId = $link.attr('data-post-id');
var modelId = $link.attr('data-model-id');
var csId = $link.attr('data-cs-id');
var dvdId = $link.attr('data-dvd-id');
var flagId = $link.attr('data-flag-id');
if (videoId || albumId || playlistId || modelId || csId || postId || dvdId) {
utilitiesAjaxRequest($link, {action: 'rate', video_id: videoId, album_id: albumId, playlist_id: playlistId, model_id: modelId, cs_id: csId, post_id: postId, dvd_id: dvdId, vote: vote}, function(json) {
if (json['status']=='success') {
$links.addClass('disabled');
$link.removeClass('disabled').addClass('voted');
$link.find('.quantity').html(+$link.find('.quantity').html()+1);
$ratingContainer.find('.voters').html($ratingContainer.find('.voters').attr('data-success'));

var $scale = $ratingContainer.find('.scale');
var oldRating = parseFloat($scale.attr('data-rating'));
var oldRatingVotes = parseInt($scale.attr('data-votes'));
if (oldRatingVotes > 0) {
if (oldRating == 0) {
oldRatingVotes = 0;
}
var newRating = (oldRating * oldRatingVotes + vote) / (oldRatingVotes + 1) / 5 * 100;
if (newRating > 100) {
newRating = 100;
}
$ratingContainer.find('.scale').css({width: newRating + '%'});
}
} else {
$links.addClass('disabled');
$ratingContainer.find('.voters').html($ratingContainer.find('.voters').attr('data-error'));
}
});
if (flagId) {
utilitiesAjaxRequest($link, {action: 'flag', video_id: videoId, album_id: albumId, playlist_id: playlistId, postId: postId, dvdId: dvdId, flag_id: flagId}, function() {});
}
}
});
};

var initAddToFavourites = function () {
$('.btn-favourites li a').click(function(e) {
var $link = $(this);
var videoId = $link.attr('data-video-id');
var albumId = $link.attr('data-album-id');
var favType = $link.attr('data-fav-type') || 0;
var createPlaylistUrl = $link.attr('data-create-playlist-url');
var playlistId = $link.attr('data-playlist-id') || 0;
if ((videoId || albumId)) {
e.preventDefault();
if ($link.hasClass('delete')) {
utilitiesAjaxRequest($link.parents('li'), {action: 'delete_from_favourites', video_id: videoId, album_id: albumId, fav_type: favType, playlist_id: playlistId}, function(json) {
if (json['status']=='success') {
if (playlistId > 0) {
$link.parents('li').addClass('hidden');
$link.parents('ul').find('#add_playlist_' + playlistId).removeClass('hidden');
} else {
$link.parents('li').addClass('hidden');
$link.parents('ul').find('#add_fav_' + favType).removeClass('hidden');
var count = $('.header').find('.js-count').html();
var total = +count - 1;
$('.header').find('.js-count').html(total);
}
}
});
} else {
if (favType == 10 && !playlistId) {
if (createPlaylistUrl) {
utilitiesAjaxFancyBox($link, createPlaylistUrl, function () {
var $form = this.inner.find('form');
utilitiesAjaxForm($form, {
success: function($form, newPlaylistData) {
$.fancybox.close();

newPlaylistData = $(newPlaylistData);
playlistId = newPlaylistData.attr('data-playlist-id');
var playlistTitle = newPlaylistData.attr('data-playlist-title');

if (playlistId) {
utilitiesAjaxRequest($link.parents('li'), {action: 'add_to_favourites', video_id: videoId, album_id: albumId, fav_type: favType, playlist_id: playlistId}, function(json) {
if (json['status']=='success') {
var $newItem = $link.parents('ul').find('#add_playlist_').clone(true, true);
$newItem.find('a').each(function() {
if ($(this).attr('data-playlist-id')) {
$(this).attr('data-playlist-id', $(this).attr('data-playlist-id').replace('%ID%', playlistId));
}
if ($(this).attr('href')) {
$(this).attr('href', $(this).attr('href').replace('%ID%', playlistId));
}
$(this).html($(this).html().replace('%1%', playlistTitle));
});
$newItem.attr('id', 'add_playlist_' + playlistId);
$newItem.insertBefore($link.parents('li'));
$newItem = $link.parents('ul').find('#delete_playlist_').clone(true, true);
$newItem.find('a').each(function() {
if ($(this).attr('data-playlist-id')) {
$(this).attr('data-playlist-id', $(this).attr('data-playlist-id').replace('%ID%', playlistId));
}
if ($(this).attr('href')) {
$(this).attr('href', $(this).attr('href').replace('%ID%', playlistId));
}
$(this).html($(this).html().replace('%1%', playlistTitle));
});
$newItem.attr('id', 'delete_playlist_' + playlistId);
$newItem.removeClass('hidden');
$newItem.insertBefore($link.parents('li'));
}
});
}
}
});
});
}
} else {
utilitiesAjaxRequest($link.parents('li'), {action: 'add_to_favourites', video_id: videoId, album_id: albumId, fav_type: favType, playlist_id: playlistId}, function(json) {
if (json['status']=='success') {
if (playlistId > 0) {
$link.parents('li').addClass('hidden');
$link.parents('ul').find('#delete_playlist_' + playlistId).removeClass('hidden');
} else {
$link.parents('li').addClass('hidden');
$link.parents('ul').find('#delete_fav_' + favType).removeClass('hidden');
var count = $('.header').find('.js-count').html();
var total = +count + 1;
$('.header').find('.js-count').html(total);
}
}
});
}
}
}
});
};

var initErrorFlaggingForm = function () {
var $blockFlagging = $('.block-flagging');
if ($blockFlagging.exists()) {
var $flaggingForm = $blockFlagging.find('form');
if ($flaggingForm.exists()) {
utilitiesAjaxForm($flaggingForm, {
success: function($form) {
$form.find('.success').show();
$form.find('.block-radios, .block-textarea').hide();
}
});
}
}
};

var initShareForm = function () {
var $blockShare = $('.block-share');
if ($blockShare.exists()) {
if (typeof window['getEmbed'] == 'function') {
$blockShare.find('.embed-code').val(window['getEmbed']());
}
$blockShare.find('.embed-button').click(function (e) {
e.preventDefault();
if (typeof window['getEmbed'] == 'function') {
var embedSize = $(this).attr('data-embed-size');
if (embedSize && embedSize.indexOf('x') > 0) {
var embedWidth = parseInt(embedSize.substr(0, embedSize.indexOf('x')));
var embedHeight = parseInt(embedSize.substr(embedSize.indexOf('x') + 1));
$blockShare.find('.embed-code').val(window['getEmbed'](embedWidth, embedHeight));
}
}
});
$blockShare.find('.embed-code-custom-width, .embed-code-custom-height').on('keyup blur', function () {
var $widthField = $blockShare.find('.embed-code-custom-width');
var $heightField = $blockShare.find('.embed-code-custom-height');
if (parseInt($widthField.val()) && parseInt($heightField.val())) {
$blockShare.find('.embed-code-custom-width-label').html(parseInt($widthField.val()));
$blockShare.find('.embed-code-custom-height-label').html(parseInt($heightField.val()));
$blockShare.find('.embed-button-custom').attr('data-embed-size', parseInt($widthField.val()) + 'x' + parseInt($heightField.val()));
}
});
}
};

var initCommentForm = function () {
$('body').on('click', '.block-new-comment .submit', function(){
var $this = $(this);
$this.addClass('active');
});
var $blockComments = $('.block-comments');
var $blockNewComment = $blockComments.find('.block-new-comment');
if ($blockComments.exists() && $blockNewComment.exists()) {
$blockComments.find('.toggle-button').click(function (e) {
e.preventDefault();
$(this).toggleClass('active');
$blockNewComment.toggle();
$blockComments.find('.success').hide();
if ($blockNewComment.is(':visible')) {
var $captcha = $blockNewComment.find('.captcha-control img');
if ($captcha.exists()) {
if ($captcha.attr('data-src')) {
$captcha.attr('src', $captcha.attr('data-src').replace(new RegExp('rand=\\d+'),'rand=' + new Date().getTime()));
} else {
$captcha.attr('src', $captcha.attr('src').replace(new RegExp('rand=\\d+'),'rand=' + new Date().getTime()));
}
}
}

utilitiesLoadSmileys($blockNewComment);

});

var $commentsForm = $blockComments.find('form');
if ($commentsForm.exists()) {
utilitiesAjaxForm($commentsForm, {
success: function($form, newCommentData) {
var $anonymousUsernameField = $form.find('[name="anonymous_username"]');
var anonymousUsername = $anonymousUsernameField.val();
if (anonymousUsername) {
$.cookie('kt_anonymous_username', anonymousUsername, {path: '/'});
}

$form.get(0).reset();
$anonymousUsernameField.val(anonymousUsername || '');

var $captcha = $form.find('.captcha-control img');
if ($captcha.exists()) {
$captcha.attr('src', $captcha.attr('src').replace(new RegExp('rand=\\d+'),'rand=' + new Date().getTime()));
}

if (typeof grecaptcha == 'object') {
$form.find('[data-recaptcha-key]').each(function() {
var recaptchaId = $(this).attr('data-recaptcha-id');
if (recaptchaId) {
grecaptcha.reset(recaptchaId);
}
});
}

if ($blockComments.find('.toggle-button').exists())
{
// $blockNewComment.hide();
$blockComments.find('.toggle-button').removeClass('active');
}

var commentsBlockId = $blockComments.attr('data-block-id');
var $commentsList = $('.list-comments');
if (newCommentData && newCommentData['approved'] && commentsBlockId && $commentsList.exists()) {
var args = {
success: function(sender, html) {
if (typeof(Storage) !== 'undefined') {
var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}
sessionStorage.removeItem(userId + location.href + '#' + commentsBlockId);
sessionStorage.removeItem(userId + location.href + '#' + commentsBlockId + ':params');
}

var resultElement = document.createElement('DIV');
resultElement.innerHTML = html;

var $newItem = $(resultElement).find('.comment-item[data-comment-id="' + (newCommentData['comment_id'] || newCommentData['entry_id']) + '"]').addClass('hidden');
if ($newItem.hasClass('item-reply')) {
var $commentParentId =$newItem.attr('data-id-parent');
$commentsList.find('.comment-item[data-comment-id="' + $commentParentId + '"] .reply-holder').prepend($newItem);
$commentsList.find('.comment-item[data-comment-id="' + $commentParentId + '"] .block-new-comment').hide();
$commentsList.find('.comment-item[data-comment-id="' + $commentParentId + '"] .success').show();
} else {
$('.main-comment .block-new-comment').hide();
$('.main-comment .success').show();
$commentsList.find('#' + commentsBlockId + '_items').prepend($newItem);

console.log($newItem)
}

setTimeout(function() {
$commentsList.show();
$newItem.fadeIn();
}, 200);
}
};

utilitiesGetBlock(commentsBlockId, null, args);
} else {
var $activeForm= $('.submit.active').closest('.form-holder');
$activeForm.find('.block-new-comment').hide();
$activeForm.find('.success').show();
}
}
});
}

$commentsForm.find('[name="anonymous_username"]').val($.cookie('kt_anonymous_username') || '');
}
};

var initCommentActions = function () {
$('.list-comments').on('click', '.comment-options a', function(e) {
var $link = $(this);
var $item = $(this).parents('.comment-item');
var $container = $(this).parents('.comment-options');
var $rating = $container.find('.comment-rating');
var $ratingLinks = $container.find('.comment-like, .comment-dislike');
var commentId = $item.attr('data-comment-id');

if (($link.hasClass('comment-like') || $link.hasClass('comment-dislike'))) {
e.preventDefault();
if ($link.hasClass('disabled')) {
return;
}
var increment = ($link.hasClass('comment-dislike') ? -1 : 1);
utilitiesAjaxRequest($link, {action: 'vote_comment', vote: increment, comment_id: commentId}, function(json) {
if (json['status'] == 'success') {
$ratingLinks.fadeOut();
if ($rating.exists()) {
var ratingValue = parseInt($rating.html());
if (!isNaN(ratingValue)) {
ratingValue += increment;
$rating.html(ratingValue);
if (ratingValue > 0) {
$rating.addClass('positive');
} else if (ratingValue < 0) {
$rating.addClass('negative');
$item.addClass('dim-comment');
} else if (ratingValue == 0) {
$rating.removeClass('positive').removeClass('negative');
$item.removeClass('dim-comment');
}
}
}
} else {
$ratingLinks.addClass('disabled');
}
});
} else if ($link.hasClass('comment-edit')) {
e.preventDefault();
$.fancybox($('.popup-edit-comment').clone(true, true), {
afterShow: function () {
var $form = this.inner.find('form');
$form.find('[name="comment_id"], [name="entry_id"]').val(commentId);

var originalText = $item.find('.original-text').html() || '';
originalText = originalText.replace(/<br>/gi, '\n').replace(/&gt;/gi, '>').replace(/&lt;/gi, '<').replace(/&#34;/gi, '"');
originalText = originalText.replace(/<img.*?alt=['"](.*?)['"].*?>/gi, '$1');
$form.find('[name="comment"], [name="entry"]').val(originalText);

utilitiesAjaxForm($form, {
success: function() {
$.fancybox.close();
var $blockComments = $('.block-comments');
var commentsBlockId = $blockComments.attr('data-block-id');
if (commentsBlockId) {
var args = {
success: function(sender, html) {
if (typeof(Storage) !== 'undefined') {
var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}
sessionStorage.removeItem(userId + location.href + '#' + commentsBlockId);
sessionStorage.removeItem(userId + location.href + '#' + commentsBlockId + ':params');
}

var resultElement = document.createElement('DIV');
resultElement.innerHTML = html;

var newCommentText = $(resultElement).find('.comment-item[data-comment-id="' + commentId + '"] .comment-text').html() || '';
$item.find('.comment-text').html(newCommentText);
}
};
utilitiesGetBlock(commentsBlockId, null, args);
}
}
});
utilitiesLoadSmileys(this.inner);
},

helpers: {
overlay: {closeClick: false}
},

topRatio: 0.3
});
}
});
};

var initAjaxLists = function ($container) {
if (!$container) {
$container = $(document);

if (typeof(Storage) !== 'undefined') {
var ajaxIds = {};
$container.find('[data-action="ajax"]').each(function() {
var id = $(this).attr('data-block-id');
if (!ajaxIds[id]) {
ajaxIds[id] = true;
}
});

var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}

for (var blockId in ajaxIds) {
if (ajaxIds.hasOwnProperty(blockId)) {
var html = sessionStorage.getItem(userId + location.href + '#' + blockId);
if (!html) {
html = sessionStorage.getItem(location.href + '#' + blockId);
}
if (html) {
$('#' + blockId).html(html).find('[data-fancybox="ajax"]').each(function() {
$(this).click(function(e) {
e.preventDefault();
utilitiesAjaxFancyBox($(this), this.href || $(this).attr('data-href'));
});
});
if ($.fn.select2) {
$('#' + blockId).find('.selectbox').select2();
}
}
var params = sessionStorage.getItem(userId + location.href + '#' + blockId + ':params');
if (!params) {
params = sessionStorage.getItem(location.href + '#' + blockId + ':params');
}
if (params) {
try {
storage[blockId] = JSON.parse(params);
} catch (e) {}

if (listNonCachableBlocks[blockId]) {
$('#' + blockId).find('img.lazy-load').removeClass('lazy-load');
utilitiesReloadBlock(blockId, blockId, false, false);
}
}
}
}
}
} else {
$container.find('[data-fancybox="ajax"]').each(function() {
$(this).click(function(e) {
e.preventDefault();
utilitiesAjaxFancyBox($(this), this.href || $(this).attr('data-href'));
});
});
if ($.fn.select2) {
$container.find('.selectbox').select2();
}
}

if ($.fn.lazyload) {
if (isMobileDevice) {
$container.find('img.lazy-load').each(function() {
var originalSrc = $(this).attr('data-original');
if (originalSrc) {
this.src = originalSrc;
}
});
} else {
$container.find('img.lazy-load').lazyload(utilitiesMergeOptions(lazyLoadDefOptions, 'lazyload'));
}
}

if ($.fn.thumbs) {
if (!isMobileDevice) {
$container.find('img[data-cnt]').thumbs();
}
}
if ($.fn.videopreview) {
$container.find('img[data-preview]').videopreview();
}

$container.find('[data-action="ajax"]').click(function (e) {
e.preventDefault();

var args = {};
var $sender = $(this);
var appendTo = $sender.attr('data-append-items-to');
var maxQueries = parseInt($sender.attr('data-max-queries')) || 0;

var blockId = $sender.attr('data-block-id');
if (!blockId) {
return;
}
var containerId = $sender.attr('data-container-id');

if (maxQueries && appendTo) {
var currentQueries = parseInt($('#' + appendTo).attr('data-current-queries')) || 0;
if (currentQueries < maxQueries) {
currentQueries++;
if (currentQueries == maxQueries) {
$('#' + containerId).remove();
} else {
$('#' + appendTo).attr('data-current-queries', currentQueries);
}
} else {
return;
}
}

var params = utilitiesParseParameters($sender.attr('data-parameters'));

var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}

args.success = function (sender, html) {
if (appendTo) {
var resultElement = document.createElement('DIV');
resultElement.innerHTML = html;

if (containerId) {
var $newContainer = $(resultElement).find('#' + containerId);
if ($newContainer.exists()) {
$('#' + containerId).replaceWith($newContainer);
initAjaxLists($newContainer);
} else {
$('#' + containerId).remove();
}
}

var $itemsToAppend = $(resultElement).find('#' + appendTo + ' .item');
$itemsToAppend.css({display: 'none'});
if ($('#' + appendTo).attr('data-append-to-beginning') == 'true') {
$itemsToAppend.insertBefore($('#' + appendTo).find('.item').first());
} else {
$itemsToAppend.insertAfter($('#' + appendTo).find('.item').last());
}
$itemsToAppend.fadeIn().promise().done(function () {
for (var paramName in params) {
if (params.hasOwnProperty(paramName)) {
if (paramName.indexOf('from') == 0 && parseInt(params[paramName])>1) {
delete params[paramName];
break;
}
}
}
if (!listNonCachableBlocks[blockId]) {
if (typeof(Storage) !== 'undefined') {
sessionStorage.setItem(userId + location.href + '#' + blockId, $('#' + blockId).html());
sessionStorage.setItem(userId + location.href + '#' + blockId + ':params', JSON.stringify(params));
}
}
});
initAjaxLists($itemsToAppend);

} else {
utilitiesScrollTo($('#' + blockId));
$('#' + blockId).animate({opacity: 0.1}, 400, function () {
var div = document.createElement('div');
div.innerHTML = html;

var content = $(div).find('#' + blockId);
$(content).css('opacity', '0.1');
$(this).replaceWith(content);
$('#' + blockId).animate({opacity: 1}, 400);
initAjaxLists($('#' + blockId));

if (typeof(Storage) !== 'undefined') {
sessionStorage.setItem(userId + location.href + '#' + blockId, $('#' + blockId).html());
sessionStorage.setItem(userId + location.href + '#' + blockId + ':params', JSON.stringify(params));
}
});
}
};

utilitiesGetBlock(blockId, containerId ? $('#' + containerId) : $sender, args, params);
});

$container.find('[data-rt]').mousedown(function() {
var rotatorParams = $(this).attr('data-rt');
if (rotatorParams) {
var url = window.location.href;
if (url.indexOf('#') > 0) {
url = url.substring(0, url.indexOf('#'));
}
var img = new Image();
img.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'mode=async&action=rotator_videos&pqr=' + rotatorParams;
$(this).attr('data-rt', '');
}
});

$container.find('[data-playlist-item]').click(function(e) {
e.preventDefault();

$container.find('[data-playlist-item]').removeClass('selected');

var $item = $(this);
$item.addClass('selected');

var playlistItemUrl = $item.attr('data-playlist-item');
if (playlistItemUrl) {
storage['playlist_state'] = playlistItemUrl;

var args = {};
args.url = playlistItemUrl;
args.success = function (sender, html) {
$('.player').html('').append($(html.trim()).find('.player-holder'));
$('.player-holder').find('[data-fancybox="ajax"]').each(function() {
$(this).click(function(e) {
e.preventDefault();
utilitiesAjaxFancyBox($(this), this.href || $(this).attr('data-href'));
});
});
$('.player-holder').find('[data-form="ajax"]').each(function () {
utilitiesAjaxForm($(this));
});
setTimeout(function() {
utilitiesScrollTo($('.player'), 0);
}, 0);
};
utilitiesGetBlock('video_view_video_view', $item, args);
}
});
if (!storage['playlist_state']) {
var $defaultItem = $container.find('[data-playlist-item]').first();
if ($defaultItem.exists()) {
$defaultItem.addClass('selected').click();
}
} else {
$container.find('[data-playlist-item]').each(function() {
if ($(this).attr('data-playlist-item') == storage['playlist_state']) {
$(this).addClass('selected');
}
});
}

$container.find('[data-fav-video-id]').click(function(e) {
e.preventDefault();
e.stopPropagation();

var $link = $(this);
var videoId = $link.attr('data-fav-video-id');
var favType = $link.attr('data-fav-type') || 0;
utilitiesAjaxRequest($link, {action: 'add_to_favourites', video_id: videoId, video_ids: [videoId], fav_type: favType, playlist_id: 0}, function(json) {
if (json['status'] == 'failure' && json['errors'] && json['errors'][0] && json['errors'][0]['code'] == 'not_logged_in') {
if (pageContext && pageContext['loginUrl']) {
utilitiesAjaxFancyBox($link, pageContext['loginUrl']);
}
} else {
$link.addClass('fixed');
}
});
});
$container.find('[data-fav-album-id]').click(function(e) {
e.preventDefault();
e.stopPropagation();

var $link = $(this);
var albumId = $link.attr('data-fav-album-id');
var favType = $link.attr('data-fav-type') || 0;
utilitiesAjaxRequest($link, {action: 'add_to_favourites', album_id: albumId, album_ids: [albumId], fav_type: favType}, function(json) {
if (json['status'] == 'failure' && json['errors'] && json['errors'][0] && json['errors'][0]['code'] == 'not_logged_in') {
if (pageContext && pageContext['loginUrl']) {
utilitiesAjaxFancyBox($link, pageContext['loginUrl']);
}
} else {
$link.addClass('fixed');
}
});
});

function createDeleteCallback($form, $sender, blockId) {
return function(json) {
if (json['status'] == 'success') {
utilitiesReloadBlock(blockId, $sender, true, true);
if ($form.attr('data-refresh-block-ids')) {
var blockIds = $form.attr('data-refresh-block-ids').split(',');
for (var j = 0; j < blockIds.length; j++) {
utilitiesReloadBlock(blockIds[j], $sender, false, true);
}
} else if ($sender.attr('data-redirect-url')) {
window.location = $sender.attr('data-redirect-url');
}
location.reload();

} else {
for (var i = 0; i < json['errors'].length; i++) {
var error = json['errors'][i];
var errorMessage = error['message'];
if (errorMessage) {
$form.find('.generic-error').empty().html(errorMessage).fadeIn();
}
}
utilitiesScrollTo($('#' + blockId), 0);
}
};
}

$container.find('[data-action="select"]').each(function() {
$(this).click(function(e) {
if ($(this).hasClass('disabled')) {
return;
}

var $form = $(this).parents('form');
var $checkbox = $(this).find('input');
if (!$(e.target).is($checkbox)) {
$checkbox.prop('checked', !$checkbox.prop('checked'));
}

var selectedNumber = parseInt($form.attr('data-selected-cnt')) || 0;
if ($checkbox.prop('checked')) {
$(this).addClass('active');
selectedNumber++;
} else {
$(this).removeClass('active');
selectedNumber = Math.max(selectedNumber - 1, 0);
}
$form.find('input[data-mode="selection"]').prop('disabled', selectedNumber == 0);
$form.find('input[data-action="select_all"]').toggleClass('active', selectedNumber == $form.find('input[type=checkbox]').length - $form.find('input[type=checkbox][disabled]').length);
$form.attr('data-selected-cnt', selectedNumber);
});
});

$container.find('[data-action="choose"]').each(function() {
$(this).click(function() {
if ($(this).hasClass('disabled')) {
return;
}

var $form = $(this).parents('form');
var $radio = $(this).find('input');
$radio.prop('checked', true);

if ($radio.prop('checked')) {
$form.find('[data-action="choose"]').removeClass('active');
$(this).addClass('active');
}
});
});

$container.find('[data-action="delete"]').each(function() {
$(this).click(function(e) {
e.preventDefault();

if ($(this).hasClass('disabled')) {
return;
}

var $form = $(this).parents('form');
var $button = $(this);
var confirmText = $button.attr('data-confirm') || '';
if (!confirmText || confirm(confirmText)) {
var objectId = $button.attr('data-id');
if (!objectId) {
return;
}

var blockId = $form.attr('data-block-id');
var params = utilitiesParseParameters($form.attr('data-parameters'));
params['function'] = 'get_block';
params['block_id'] = blockId;
params['delete'] = [objectId];
utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
}
});
});

$container.find('[data-action="delete_mixed"]').each(function() {
$(this).click(function(e) {
e.preventDefault();

if ($(this).hasClass('disabled')) {
return;
}

var $form = $(this).parents('form');
var $button = $(this);
var confirmText = $button.attr('data-confirm') || '';
if (!confirmText || confirm(confirmText)) {
var videoId = $button.attr('data-video-id');
var albumId = $button.attr('data-album-id');
if (!videoId && !albumId) {
return;
}

var blockId = $form.attr('data-block-id');
var params = utilitiesParseParameters($form.attr('data-parameters'));
params['function'] = 'get_block';
params['block_id'] = blockId;
if (videoId) {
params['delete_video_ids'] = [videoId];
} else if (albumId) {
params['delete_album_ids'] = [albumId];
}
utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
}
});
});

$container.find('form[data-controls]').each(function () {
var $form = $(this);
var blockId = $form.attr('data-block-id');

$form.find('input[type="button"]').each(function() {
$(this).click(function(e) {
e.preventDefault();

var $button = $(this);
var confirmText = $button.attr('data-confirm') || '';
if (confirmText) {
var selectedNumber = parseInt($form.attr('data-selected-cnt')) || 0;
confirmText = confirmText.replace(/\[count\](.*)\[\/count\]/gi, function(match, p1) {
var defaultValue = '';
var values = p1.split('||');
for (var i = 0; i < values.length; i++) {
var temp = values[i].split(':', 2);
if (temp.length == 1) {
defaultValue = temp[0].trim();
} else {
var compareExamples = temp[0].split(',');
for (var j = 0; j < compareExamples.length; j++) {
var compareExample = compareExamples[j].trim();
if (compareExample.indexOf('//') == 0) {
if (selectedNumber % 100 == parseInt(compareExample.substring(2))) {
return temp[1].trim().replace('%1%', '' + selectedNumber);
}
} else if (compareExample.indexOf('/') == 0) {
if (selectedNumber % 10 == parseInt(compareExample.substring(1))) {
return temp[1].trim().replace('%1%', '' + selectedNumber);
}
} else if (selectedNumber == parseInt(temp[0].trim())) {
return temp[1].trim().replace('%1%', '' + selectedNumber);
}
}
}
}
return defaultValue;
}).replace('%1%', '' + selectedNumber);
}
var params = {};
if (!confirmText || confirm(confirmText)) {
if ($button.attr('data-action') == 'select_all') {
if ($button.hasClass('active')) {
$form.find('input[type=checkbox]').each(function() {
if (this.checked) {
$(this).click();
}
});
} else {
$form.find('input[type=checkbox]').each(function() {
if (!this.checked) {
$(this).click();
}
});
}
} else if ($button.attr('data-action') == 'delete_multi') {
params = utilitiesParseParameters($form.attr('data-parameters'));
params['function'] = 'get_block';
params['block_id'] = blockId;
params['delete'] = [];
$form.find('input[type=checkbox]').each(function() {
if (this.checked) {
params['delete'].push(this.value);
}
});

utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
} else if ($button.attr('data-action') == 'move_multi') {
var playlistSelectorUrl = $button.attr('data-href');
if (playlistSelectorUrl) {
utilitiesAjaxFancyBox($button, playlistSelectorUrl, function () {
var $inner_form = this.inner.find('form');
utilitiesAjaxForm($inner_form, {
beforeSubmit: function($inner_form) {
$.fancybox.close();

var playlistId = parseInt($inner_form.find('[name="playlist_id"]:checked').val());
if (playlistId) {
params = utilitiesParseParameters($form.attr('data-parameters'));
params['function'] = 'get_block';
params['block_id'] = blockId;
params['move_to_playlist_id'] = playlistId;
params['delete'] = [];
$form.find('input[type=checkbox]').each(function() {
if (this.checked) {
params['delete'].push(this.value);
}
});

utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
} else {
var createPlaylistUrl = $inner_form.attr('data-create-playlist-url');
if (createPlaylistUrl) {
utilitiesAjaxFancyBox($button, createPlaylistUrl, function () {
var $inner_form = this.inner.find('form');
utilitiesAjaxForm($inner_form, {
success: function($inner_form, newPlaylistData) {
$.fancybox.close();

newPlaylistData = $(newPlaylistData);
playlistId = newPlaylistData.attr('data-playlist-id');

if (playlistId) {
params = utilitiesParseParameters($form.attr('data-parameters'));
params['function'] = 'get_block';
params['block_id'] = blockId;
params['move_to_playlist_id'] = playlistId;
params['delete'] = [];
$form.find('input[type=checkbox]').each(function() {
if (this.checked) {
params['delete'].push(this.value);
}
});

utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
}
}
});
});
}
}
return false;
}
});
});
}
} else if ($button.attr('data-action') == 'redirect') {
var redirectUrl = $button.attr('data-redirect-url');
if (!redirectUrl) {
return;
}

window.location = redirectUrl;
} else if ($button.attr('data-action') == 'delete_playlist') {
var playlistId = $button.attr('data-id');
if (!playlistId) {
return;
}

params['action'] = 'delete_playlists';
params['delete'] = [playlistId];
utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
} else if ($button.attr('data-action') == 'delete_dvd') {
var dvdId = $button.attr('data-id');
if (!dvdId) {
return;
}

params['action'] = 'delete_dvds';
params['delete'] = [dvdId];
utilitiesAjaxRequest($button, params, createDeleteCallback($form, $button, blockId));
}
}
});
})
});

if ($('.box.show').hasClass('dialogues-list')) {
initMessages();
$('[data-form="ajax"]').each(function () {
utilitiesAjaxForm($(this));
});

if (navigator.platform == 'MacIntel') {
$('.platform-text').text('cmd+enter')
} else {
$('.platform-text').text('ctrl+enter')
}

$("#send_message_message").keydown(function(event) {
if((event.ctrlKey || event.metaKey) && event.which == 13) {
$(this).closest('form').find('.submit').click();
return false;
}
}
);

var widthHeader = $('.header').width();
if (widthHeader > 767) {
$(".dialogues-holder .dialogue-holder .nano").nanoScroller({
// scroll: 'top'
scroll: 'bottom',
iOSNativeScrolling: true,
alwaysVisible: true
});

setTimeout(function() {
$(".dialogues-holder .dialogue-holder .nano").nanoScroller({
// scroll: 'top'
scroll: 'bottom',
iOSNativeScrolling: true,
alwaysVisible: true
});

}, 500);
setTimeout(function() {
$(".dialogues-holder .dialogue-holder .nano").nanoScroller({
// scroll: 'top'
scroll: 'bottom',
iOSNativeScrolling: true,
alwaysVisible: true
});

},1000);

setTimeout(function() {
$(".dialogues-holder .dialogue-holder .nano").nanoScroller({
// scroll: 'top'
scroll: 'bottom',
iOSNativeScrolling: true,
alwaysVisible: true
});

},1500);

$("#about").on("update", function(event, vals){
if (vals.position < 500) {
$('.js-load-more').click();
}
});
} else {
var $btn_more = $('.js-load-more');
$(window).scroll(function() {
if($(window).scrollTop() < 800) {
$btn_more.trigger('click');
}
});
}
}

rangeSlider();
userSearch();
initLoadСity();
};


var initAjaxForms = function () {
$('[data-form="ajax"]').each(function () {
utilitiesAjaxForm($(this));
});
};

var initSubscriptions = function () {
var $btnSubscriptions = $('[data-subscribe-to], [data-unsubscribe-to]');
$btnSubscriptions.click(function(e) {
e.preventDefault();

var $btn = $(this);
if ($btn.hasClass('done')) {
return;
}
var subscriptionTo = $btn.attr('data-subscribe-to') || $btn.attr('data-unsubscribe-to');
var subscriptionId = $btn.attr('data-id');
if (subscriptionTo && subscriptionId) {
var params = {action: 'subscribe'};
if (!$btn.attr('data-subscribe-to')) {
params['action'] = 'unsubscribe';
}
if (subscriptionTo == 'category') {
params[params['action'] + '_category_id'] = subscriptionId;
} else if (subscriptionTo == 'model') {
params[params['action'] + '_model_id'] = subscriptionId;
} else if (subscriptionTo == 'content_source') {
params[params['action'] + '_cs_id'] = subscriptionId;
} else if (subscriptionTo == 'user') {
params[params['action'] + '_user_id'] = subscriptionId;
} else if (subscriptionTo == 'playlist') {
params[params['action'] + '_playlist_id'] = subscriptionId;
} else if (subscriptionTo == 'dvd') {
params[params['action'] + '_dvd_id'] = subscriptionId;
}
utilitiesAjaxRequest($btn, params, function(json) {
if (json['status'] == 'success') {
$btn.addClass('done');
var $buttonInfo = $btn.parents().first().find('.button-info');
if ($buttonInfo.exists()) {
if (params['action'] == 'subscribe') {
$buttonInfo.html(parseInt($buttonInfo.html()) + 1);
} else {
$buttonInfo.html(parseInt($buttonInfo.html()) - 1);
}
}
}
});
}
});
};

var initSearch = function () {
if (typeof(Storage) !== 'undefined') {
var userId = '';
if (pageContext && pageContext['userId']) {
userId = pageContext['userId'] + ':';
}

var selectedOptions = sessionStorage.getItem(userId + location.href + '#search_filter');
if (!selectedOptions) {
selectedOptions = sessionStorage.getItem(location.href + '#search_filter');
}
if (selectedOptions) {
selectedOptions = JSON.parse(selectedOptions);
for (var id in selectedOptions) {
$('#' + id).prop('checked', true);
}
if (!selectedOptions.search_string_filter) {
$('#search_string_filter').prop('checked', false);
}
}
}

$('#search_form').submit(function(e) {
try {
if (this['q'].value == '') {
this['q'].focus();
e.preventDefault();
return;
}
if ($(this).attr('data-url')) {
var value = this['q'].value.replace(/[ ]+/g, '-').replace(/[?]/g, '').replace(/[&]/g, '%26').replace(/[?]/g, '%3F').replace(/[/]/g, '%2F');
window.location = $(this).attr('data-url').replace('%QUERY%', encodeURIComponent(value));
e.preventDefault();
}
} catch (e) {}
});

$('#search_form .search-button').click(function() {
$(this).submit();
});


// $('[data-search-filter-ids] .checkbox').click(function() {
// setTimeout(searchFilter, 500)
// });

// $( "[data-search-filter-ids] .select" ).change(function () {
// setTimeout(searchFilter, 500)
// });


$('body').on('click', '.js-user-filter', function() {
searchFilter();
return false;
});

$('body').on('click', '.js-user-filter-clear', function() {
var $container = $('[data-search-filter-ids]');
var blockIds = $container.attr('data-search-filter-ids').split(',');
var params = {};

storage[blockIds] = params;
utilitiesReloadBlock(blockIds, $container, false, true);
return false;
});
};

function searchFilter() {
var $container = $('[data-search-filter-ids]');
var blockIds = $container.attr('data-search-filter-ids').split(',');
var params = {};
$container.find('.radio').each(function () {
var $input = $(this);
if ($input.prop('type') == 'hidden' || ($input.prop('type') == 'radio' && $input.prop('checked'))) {
params[$input.prop('name')] = $input.prop('value');
}
});

$container.find('input').each(function () {
var $input = $(this);
if ($input.prop('type') == 'hidden' || ($input.prop('type') == 'checkbox' && $input.prop('checked'))) {
params[$input.prop('name')] = $input.prop('value');
}
});

$container.find('.selectbox').each(function() {
var $select = $(this);
if ($select.attr('id') != 'country_id') {
params[$select.prop('name')] = $select.prop('value');
}
});

$container.find('.number').each(function() {
var $number = $(this);
params[$number.prop('name')] = $number.prop('value');
});

var $name = $('body').find('.headline .selected.active').attr('data-name');
var $value = $('body').find('.headline .selected.active').attr('data-value');
console.log($name);
console.log($value);

params[$name] = $value;

var user = $container.find('.user');
params[user.prop('name')] = user.prop('value');



if (user.prop('value')!='')
{
params = {};
params[user.prop('name')] = user.prop('value');
}
console.log(params);

storage[blockIds] = params;
utilitiesReloadBlock(blockIds, $container, false, true);
};

function searchFilterInput() {
var $container = $('[data-search-filter-ids]');
var blockIds = $container.attr('data-search-filter-ids').split(',');
var params = {};

var user = $container.find('.user');
params[user.prop('name')] = user.prop('value');

console.log(params);

storage[blockIds] = params;
utilitiesReloadBlock(blockIds, $container, false, true);
};


var userSearch = function () {
// $("[data-search-filter-ids] .user").keyup(function(e) {
// clearTimeout($.data(this, 'timer'));
// if (e.keyCode == 13){
// searchFilterInput();
// }else
// $(this).data('timer', setTimeout(searchFilterInput, 500));
// });
};

var rangeSlider = function () {
$("[data-search-filter-ids] .js-range-slider1").ionRangeSlider({
onChange: function (data) {
$('.js-from1').val(data.from);
$('.js-to1').val(data.to);
},
onFinish: function () {
// searchFilter();
},
});

$("[data-search-filter-ids] .js-range-slider2").ionRangeSlider({
onChange: function (data) {
$('.js-from2').val(data.from);
$('.js-to2').val(data.to);
},
onFinish: function () {
// searchFilter();
},
});

$("[data-search-filter-ids] .js-range-slider3").ionRangeSlider({
onChange: function (data) {
$('.js-from3').val(data.from);
$('.js-to3').val(data.to);
},
onFinish: function () {
// searchFilter();
},
});
};

function initLoadСity() {
$("#country_id").change(function(){
var country = $(this).val();
var url = $(this).attr('data-url');
$.ajax({
type: 'GET',
url: url,
data: 'country_id='+country,
success: function(data) {
$('#edit_profile_city').html(data);
}
});

var val = $("body #country_id").find("[value='" + country + "']").text();
$('#edit_profile_country_id').attr("value", val);
});
}

var initVideoUploadForm = function() {
$('[data-form="ajax-upload"]').each(function () {
var $form = $(this);
var redirectUrl = $form.attr('data-redirect-url');
var progressUrl = $form.attr('data-progress-url');
var continueForm = $form.attr('data-continue-form');
var lastPercent = 0;
var timeoutId = null;

var progressFunction = function(percent) {
percent = Math.min(percent || 0, 100);
if (percent > lastPercent) {
$form.find('.progressbar .progress').stop(true, true).animate({width: percent + '%'});
$form.find('.progressbar .text').html('ОБРАБОТАНО ' + percent + '%');
lastPercent = percent;
}
};

utilitiesAjaxForm($form, {
success: function($form, uploadData) {
$form.find('.progressbar .progress').css({width: '100%'});

if (uploadData && uploadData['filename'] && redirectUrl) {
window.location = redirectUrl.replace('%HASH%', uploadData['filename']);
} else if (uploadData && uploadData['filename'] && continueForm) {
$form.remove();
$('#' + continueForm).find('input[type="submit"]').enable(true);
$('#' + continueForm).find('input[name="file"]').val(uploadData['filename'] + '.mp4');
$('#' + continueForm).find('input[name="file_hash"]').val(uploadData['filename']);
}
},

beforeSerialize: function($form) {
var md5filename = '';
if ($form.find('[name="url"]').val() || $form.find('[name="content"]').val() || $form.find('[name="content[]"]').val() || $form.find('[name="embed"]').val()) {
for (var i = 0; i < 32; i++) {
md5filename += '' + Math.floor((Math.random() * 10));
}
$form.find('[name="filename"]').val(md5filename);
} else {
$form.find('[name="filename"]').val(md5filename);
}
},

beforeSubmit: function($form) {
lastPercent = 0;
$form.find('.progressbar-holder').append($('<div class="progressbar"><div class="progress"></div><div class="text"></div></div>'));
if ($form.find('[name="upload_option"]:checked').val() == 'url' && progressUrl) {
var md5filename = $form.find('[name="filename"]').val();
if (md5filename) {
var checkProgressFunction = function() {
$.ajax({
url: progressUrl.replace('%HASH%', md5filename),
type: 'GET',
timeout: 10000,
cache: false,

success: function(xml) {
if (xml && xml.documentElement) {
var loaded = $(xml.documentElement).find('loaded').text() || 0;
var total = $(xml.documentElement).find('total').text() || 1;
progressFunction(Math.floor(loaded / total * 100));
}
},

complete: function() {
timeoutId = setTimeout(checkProgressFunction, 1000);
}
});
};
timeoutId = setTimeout(checkProgressFunction, 1000);
}
}
return true;
},

uploadProgress: function($form, percent) {
if ($form.find('[name="upload_option"]:checked').val() != 'url') {
progressFunction(percent);
}
},

complete: function($form) {
$form.find('.progressbar').hide().remove();
if (timeoutId) {
clearTimeout(timeoutId);
}
}
}, true);

$form.find('[name="upload_option"]').change(function() {
var $radio = $(this);
if ($radio.prop('checked')) {
var disabledProp = 'disabled';
if ($radio.val() == 'file') {
$form.find('[name="content"]').parents('.file-control').find('input').removeAttr(disabledProp).click();
$form.find('[name="url"]').attr(disabledProp, disabledProp).val('').change();
$form.find('[name="embed"]').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
$form.find('[name="duration"]').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
$form.find('[name="screenshot"]').parents('.file-control').find('input').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
} else if ($radio.val() == 'url') {
$form.find('[name="content"]').parents('.file-control').find('input').attr(disabledProp, disabledProp).val('').change();
$form.find('[name="url"]').removeAttr(disabledProp).focus();
$form.find('[name="embed"]').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
$form.find('[name="duration"]').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
$form.find('[name="screenshot"]').parents('.file-control').find('input').attr(disabledProp, disabledProp).val('').change().parents('.row').find('label').removeClass('required');
} else if ($radio.val() == 'embed') {
$form.find('[name="url"]').attr(disabledProp, disabledProp).val('').change();
$form.find('[name="content"]').parents('.file-control').find('input').attr(disabledProp, disabledProp).val('').change();
$form.find('[name="embed"]').removeAttr(disabledProp).focus().parents('.row').find('label').addClass('required');
$form.find('[name="duration"]').removeAttr(disabledProp).parents('.row').find('label').addClass('required');
$form.find('[name="screenshot"]').parents('.file-control').find('input').removeAttr(disabledProp).parents('.row').find('label').addClass('required');
}
}
});

var params = {mode: 'async', format: 'json', action: $form.find('[name="action"]').val()};
$form.attr('action', ($form.attr('action') || '') + (($form.attr('action') || '').indexOf('?') >=0 ? '&' : '?') + $.param(params));
});
};

var initProfile = function() {
$('[data-action="message"],[data-action="add_to_friends"]').click(function(e) {
e.preventDefault();

var $btn = $(this);
if ($btn.hasClass('done')) {
return;
}

var popupClass = '.popup-send-message';
if ($btn.attr('data-action') == 'add_to_friends') {
popupClass = '.popup-add-to-friends';
}
$.fancybox($(popupClass).clone(true, true), {
afterShow: function () {
var $form = this.inner.find('form');
utilitiesAjaxForm($form, {
success: function() {
$btn.addClass('done');
$.fancybox.close();
if ($btn.attr('data-action') == 'add_to_friends') {
window.location.reload();
}
}
});
utilitiesLoadSmileys(this.inner);
},

helpers: {
overlay: {closeClick: false}
},

topRatio: 0.3
});
});
};

var initMessages = function () {
var $messageForm = $('#send_message_form');
if ($messageForm.exists()) {
utilitiesLoadSmileys($messageForm);
utilitiesAjaxForm($messageForm, {
success: function($form, newMessageData) {
var editing = false;
if ($form.find('[name="message_id"]').val() == newMessageData['message_id']) {
editing = true;
}
$form.get(0).reset();
$form.find('[name="message_id"]').val('');

var params = {};
params['conversation_user_id'] = $form.find('[name="conversation_user_id"]').val();

var messagesBlockId = $form.attr('data-block-id');
var $messagesList = $('.list-messages');
if (newMessageData && messagesBlockId && $messagesList.exists()) {
var args = {
success: function(sender, html) {
var resultElement = document.createElement('DIV');
resultElement.innerHTML = html;
if (editing) {
$messagesList.find('.item[data-message-id="' + newMessageData['message_id'] + '"]').replaceWith($(resultElement).find('.item[data-message-id="' + newMessageData['message_id'] + '"]'));
} else {
// var $newItem = $(resultElement).find('.item[data-message-id="' + newMessageData['message_id'] + '"]').addClass('hidden');

// if(newMessageData['attach_id']){
// var attach = '<img class="message-img" src="' + newMessageData['attach_file'] + '" alt="image">';
// $newItem.find('.text').append(attach);
// }

if ($('#' + messagesBlockId + '_items .block-massage').last().hasClass('me-block')) {
// $('#' + messagesBlockId + '_items .block-massage').last().append($newItem);
} else {
$('#' + messagesBlockId + '_items').append('<div class="block-massage me-block"></div>');
// $('#' + messagesBlockId + '_items .block-massage').last().append($newItem);
}

var widthHeader = $('.header').width();
if (widthHeader > 767) {
var height = 0;
$('.block-massage').each(function(i,elem) {
height = height + $(this).height()
});
$(".dialogues-holder .dialogue-holder .nano").nanoScroller({scrollTop: height});
} else {
$('body, html').scrollTop($(document).height());
}

setTimeout(function() {
console.log(11)
// $messagesList.show();
// $newItem.fadeIn();

}, 200);

$('.attach-container').html('');
$('#js-imageID').val('0');
}
}
};
// utilitiesGetBlock(messagesBlockId, null, args, params);

$('#js-imageID').val('0');
$('.attach-container').html('');
$('.attach-container-image').html();

}
}
});
}



$(document).on('click', '.list-messages [data-edit-message-id]', function(e) {
e.preventDefault();

var $button = $(this);
var messageId = $button.attr('data-edit-message-id');
if (!messageId) {
return;
}

var $form = $('#send_message_form');
if ($form.find('[name="message_id"]').val()) {
return;
}
$form.find('[name="message_id"]').val(messageId);
utilitiesScrollTo($form);

var $item = $button.parents('.item[data-message-id="' + messageId + '"]');
$item.addClass('editing');

var originalText = $item.find('.original-text').html() || '';
originalText = originalText.replace(/<br>/gi, '\n').replace(/&gt;/gi, '>').replace(/&lt;/gi, '<').replace(/&#34;/gi, '"');
originalText = originalText.replace(/<img.*?alt=['"](.*?)['"].*?>/gi, '$1');
originalText = originalText.trim();
$form.find('[name="message"]').val(originalText).focus();
});
};

$('body').on('click', '.js-delete-this', function() {
var $this = $(this);
var $form = $this.closest('#about');
var params = $form.find('input[type="hidden"]').serializeArray();
var str = $.param(params);

var this_params = $.param($this.parent().find('input'));

$.ajax({
url: '?mode=async&format=json&' + str + '&' + this_params,
success: function(data) {
if (data.status == 'failure') {
$form.find('.generic-error').html(data.errors[0].message).fadeIn();
console.log();

} else {
$this.closest('.item').remove();
}
}
});

return false;
});

$('body').on('click', '.js-delet-all', function() {
var $this = $(this);
$this.removeClass('show');
var $form_big = $('#about');

$form_big.find('.js-mobile_delete input:checkbox').each(function() {
if ($(this).is(":checked")) {
var params = $(this).attr('data-id');
$('[data-message-id~=' + params + ']').remove();
$.ajax({
url: '?mode=async&format=json&action=delete&=on&delete[]=' + params,
success: function(data) {
if (data.status == 'failure') {
$form.find('.generic-error').html(data.errors[0].message).fadeIn();
console.log(12);
} else {
console.log(params)
$('[data-message-id~=' + params + ']').remove();
}
}
});
}
});



return false;
});

$('body').on('click', '.js-mobile_delete', function() {
var $form = $(this).closest('#custom_list_messages_my_conversation_messages_items');
setTimeout(function(){
$('.delet-all').removeClass('show');
$form.find('.js-mobile_delete input:checkbox').each(function() {
if ($(this).is(":checked")) {
if (!$('.delet-all').hasClass('show')) {
$('.delet-all').addClass('show');
}
}
});

}, 100);
});

$(document).on('click', '[data-action="ignore_conversation"], [data-action="not_ignore_conversation"]', function(e) {

e.preventDefault();

var $button = $(this);
var confirmText = $button.attr('data-confirm') || '';
if (!confirmText || confirm(confirmText)) {
var userId = $button.attr('data-user-id');
if (!userId) {
return;
}

var blockId = $button.attr('data-block-id');
var params = {};
params['function'] = 'get_block';
params['block_id'] = blockId;
params['action'] = $button.attr('data-action');
params['conversation_user_id'] = userId;
utilitiesAjaxRequest($button, params, function(json) {
if (json['status'] == 'success') {
window.location.reload();
}
});
}
});

$(document).on('click', '.js-show-comments', function(e) {


if ($(this).hasClass('active')) {
$(".block-comments").fadeOut();
$(this).removeClass('active');
} else {
$(".block-comments").fadeIn()
$(this).addClass('active');
}

});

$(document).on('click', '[data-action="delete_conversation"]', function(e) {
e.preventDefault();
var $button = $(this);
var confirmText = $button.attr('data-confirm') || '';
if (!confirmText || confirm(confirmText)) {
var userId = $button.attr('data-user-id');
if (!userId) {
return;
}

var blockId = $button.attr('data-block-id');
var params = {};
params['function'] = 'get_block';
params['block_id'] = blockId;
params['action'] = $button.attr('data-action');
params['conversation_user_id'] = userId;
utilitiesAjaxRequest($button, params, function(json) {
if (json['status'] == 'success') {
window.location.reload();
}
});
}
});

var initStats = function() {
var sendStatsReq = function(action) {
var statsUrl = window.location.href;
if (statsUrl.indexOf('#') > 0) {
statsUrl = statsUrl.substring(0, statsUrl.indexOf('#'));
}
if (statsUrl.indexOf('?') >= 0) {
statsUrl += '&';
} else {
statsUrl += '?';
}

if (action == 'js_stats' && pageContext) {
if (pageContext['disableStats']) {
return;
}
if (pageContext['videoId']) {
statsUrl += 'video_id=' + pageContext['videoId'] + '&';
}
if (pageContext['albumId']) {
statsUrl += 'album_id=' + pageContext['albumId'] + '&';
}
}

var img = new Image();
img.src = statsUrl + 'mode=async&action=' + action + '&rand=' + new Date().getTime();
};

$.cookie('kt_tcookie', '1', {expires: 7, path: '/'});
if ($.cookie('kt_tcookie') == '1') {
sendStatsReq('js_stats');
}

if (pageContext && pageContext['userId']) {
var reporter = function() {
sendStatsReq('js_online_status');
};
reporter();
setInterval(reporter, 60 * 1000);
}
};

var initAutoscroll = function() {
var $autoscroll = $('[data-autoscroll="true"]');
if ($autoscroll.exists()) {
utilitiesScrollTo($autoscroll.first());
}
};

var initRecaptcha = function() {
$(document).on('recaptchaloaded', function() {
utilitiesRecaptcha();
});
};

var initKVSPlayTrailerOnHover = function initKVSPlayTrailerOnHover() {
var timeout1;
var timeout2;
var interval;
var count = 0;
function trailerPlay(el) {
var $this = el;
var $video = $this.find('video');
var $image = $this.find('img');
if ($video.length) {
$video.get(0).play();
$image.hide();
} else {
$('.js-swipe').hide();
var $loader = $('<div class="preview-progress"></div>');
$this.append($loader);
setTimeout(function () {
$loader.addClass('is-full');
});

timeout1 = setTimeout(function () {
//avoid downloading video with quick hover
var video_url = $this.attr('data-preview');
var video_subtitles = $this.attr('data-subtitles');
// var $new_video = $('<video autoplay loop muted playsinline src="' + video_url + '"><track default kind="subtitles" src="' + video_subtitles + '" srclang="ru"></video>');
var $new_video = $('<video autoplay loop muted playsinline src="' + video_url + '"></video>');

function playVideo() {
$this.append($new_video);
$new_video.get(0).play();
$image.hide();
$loader.remove();
}
timeout2 = setTimeout(function () {
if ($new_video.get(0).readyState > 0) {
//play video if already loaded in 1000 ms
playVideo();
} else {
interval = setInterval(function () {
//wait and play once loaded
if ($new_video.get(0).readyState > 0) {
playVideo();
clearInterval(interval);
}
}, 100);
}
}, 100);
}, 200);
}
}

if (isMobileDevice) {
window.initKVSPlayTrailerOnHover = initKVSPlayTrailerOnHover;

$('body').find('[data-preview]').swipe({
swipeLeft: function swipeLeft(event, direction, distance, duration, fingerCount) {
clearTimeout(timeout1);
clearTimeout(timeout2);
clearInterval(interval);
var $this = $(this);
var $image = $this.find('img');
count++;
$('[data-preview]').each(function (idx) {
var $this = $(this);
var $video = $this.find('video');
if ($video.length) {
$video.get(0).remove();
$('img').show();
}
$this.find('.preview-progress').remove();
});
if (count > 1) {
if ($(this).find('video').eq(0).length) {
$(this).find('video').get(0).remove();
$('img').show();
}
count = 0;
} else {
trailerPlay($(this));
}
},
swipeRight: function swipeRight(event, direction, distance, duration, fingerCount) {
clearTimeout(timeout1);
clearTimeout(timeout2);
clearInterval(interval);
var $this = $(this);
var $image = $this.find('img');
count++;
$('[data-preview]').each(function (idx) {
var $this = $(this);
var $video = $this.find('video');
if ($video.length) {
$video.get(0).remove();
$('img').show();
}
$this.find('.preview-progress').remove();
});
if (count > 1) {
if ($(this).find('video').eq(0).length) {
$(this).find('video').get(0).remove();
$('img').show();
}
count = 0;
} else {
trailerPlay($(this));
}
},
preventDefaultEvents: false,
// fix
threshold: 80
});
} else {
$('body').on('mouseenter', '[data-preview]', function () {
trailerPlay($(this));
}).on('mouseleave', '[data-preview]', function () {
clearTimeout(timeout1);
clearTimeout(timeout2);
clearInterval(interval);
var $this = $(this);
var $image = $this.find('img');
$image.show();
var $video = $this.find('video');
if ($video.length) {
$video.get(0).remove();
}
$this.find('.preview-progress').remove();
});
}
};

var myMessagesLoad = function () {
function messagesLoad(data_user) {
var blockIds = 'custom_list_messages_my_conversation_messages';
var params = {};
params['conversation_user_id'] = data_user;
storage[blockIds] = params;
utilitiesReloadBlock(blockIds, false, true);

setTimeout(function(){
var widthHeader = $('.header').width();
if (widthHeader < 767) {
$('.wrapper').addClass('mobile-dialodues');
$('.dialogues-holder').removeClass('empty-holder');
$('.wrapper').removeClass('ajax');
$('body, html').scrollTop($(document).height());
}
}, 1500);
};

$('body').on('click', '.js-ajax', function () {
$('.js-ajax').removeClass('active');
$('.item').removeClass('active');
$(this).addClass('active');
$(this).parent().addClass('active');
$(this).find('.label .new').addClass('hidden');
$(this).find('.label .new').html('0');
var data_user = $(this).attr('data-user');
messagesLoad(data_user)
$('.wrapper').addClass('ajax');
return false;
});

$('body').on('click', '.js-backwards-users', function(){
$('.wrapper').removeClass('mobile-dialodues');
$('.header').removeClass('nav-up');
$('.dialogues-holder').addClass('empty-holder');
$('body, html').scrollTop(0);
return false;
});

$('body').on('click', '.js-reject', function(){
$(this).closest('form').addClass('reject');
});
};

var initMethods = [
initMenu,
initTabs,
initFancyBox,
initSelectBox,
initRating,
initAddToFavourites,
initErrorFlaggingForm,
initShareForm,
initCommentForm,
initCommentActions,
initAjaxLists,
initAjaxForms,
initSubscriptions,
initSearch,
rangeSlider,
initLoadСity,
initVideoUploadForm,
initProfile,
initMessages,
initStats,
initAutoscroll,
initRecaptcha,
initKVSPlayTrailerOnHover,
myMessagesLoad,
userSearch
];

for (var i = 0; i < initMethods.length; i++) {
if (typeof initMethods[i] == 'function') {
try {
initMethods[i].call(this);
} catch (e) {
if (console && console.error) {
console.error(e);
}
}
}
}
})();
});

Back to list