function md5 (str) {
    return hash = CryptoJS.MD5(str);
}

var entityMap = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
};

function escapeHtml(string) {
    return String(string).replace(/[&<>"'\/]/g, function (s) {
      return entityMap[s];
    });
}

function rawurlencode(str) {
  //       discuss at: http://phpjs.org/functions/rawurlencode/
  //      original by: Brett Zamir (http://brett-zamir.me)
  //         input by: travc
  //         input by: Brett Zamir (http://brett-zamir.me)
  //         input by: Michael Grier
  //         input by: Ratheous
  //      bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  //      bugfixed by: Brett Zamir (http://brett-zamir.me)
  //      bugfixed by: Joris
  // reimplemented by: Brett Zamir (http://brett-zamir.me)
  // reimplemented by: Brett Zamir (http://brett-zamir.me)
  //             note: This reflects PHP 5.3/6.0+ behavior
  //             note: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
  //             note: pages served as UTF-8
  //        example 1: rawurlencode('Kevin van Zonneveld!');
  //        returns 1: 'Kevin%20van%20Zonneveld%21'
  //        example 2: rawurlencode('http://kevin.vanzonneveld.net/');
  //        returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
  //        example 3: rawurlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
  //        returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'

  str = (str + '')
    .toString();

  // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
  // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
  return encodeURIComponent(str)
    .replace(/!/g, '%21')
    .replace(/'/g, '%27')
    .replace(/\(/g, '%28')
    .
  replace(/\)/g, '%29')
    .replace(/\*/g, '%2A');
}

function print_r(arr, level) {
    var print_red_text = "";
    if(!level) level = 0;
    var level_padding = "";
    for(var j=0; j<level+1; j++) level_padding += "    ";
    if(typeof(arr) == 'object') {
        for(var item in arr) {
            var value = arr[item];
            if(typeof(value) == 'object') {
                print_red_text += level_padding + "'" + item + "' :\n";
                print_red_text += print_r(value,level+1);
        } 
            else 
                print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    } 

    else  print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
    return print_red_text;
}

/*
 * Переключение раздела в блоке афиши  
 */
function afisha_block_toggle (categoryid) {
    $('.afisha-block-gallery-jcarousel').jcarousel('scroll', 0, false);
    $('.afisha-block-categories-item').each(function(){
        $(this).removeClass('afisha-block-categories-item-active');
    });
    if (categoryid == null) {
        $('.afisha-block-categories-item-all').addClass('afisha-block-categories-item-active');
    } else {
        $('.afisha-block-categories-item-'+categoryid).addClass('afisha-block-categories-item-active');
    }
    display_count = 0;
    $('.afisha-block-gallery li').each(function(){
        categoryid_this = $(this).attr('categoryid');
        //alert(categoryid_this);
        if (categoryid == null) {
            $(this).show();
            display_count++;
        } else {
            if (categoryid_this == categoryid) {
                $(this).show();
                display_count++;
            } else {
                $(this).hide();
            }
        }
    });
    //alert(display_count);
    if (display_count > 4) {
        $('.afisha-block-gallery-jcarousel-control-prev').css('visibility', 'visible');
        $('.afisha-block-gallery-jcarousel-control-next').css('visibility', 'visible');
    } else {
        $('.afisha-block-gallery-jcarousel-control-prev').css('visibility', 'hidden');
        $('.afisha-block-gallery-jcarousel-control-next').css('visibility', 'hidden');
    }
    afisha_block_jcarousel.stopAuto();
    afisha_block_jcarousel.reset();
    $('.afisha-block-gallery-jcarousel')
    .jcarousel({
        // Options go here
        //wrap: 'circular'
    }); 
} 