/**
 * @author Pavlo Chubatyy <pchubatyy@malkosua.com>
 */
(function($){
    $(document).ready(function(){
        // We are subscribing to all ajax calls on the page
        $('body').ajaxSuccess(function(e, xhr, settings){
            var stop = false;
            var jsonFound = false;
            // this is not json call we just go away
            if (settings.hasOwnProperty('dataType') && settings.dataType.toLowerCase() !== 'json')
                stop = true;

            if (settings.hasOwnProperty('dataTypes')) {
                $.each(settings.dataTypes, function(index, element){
                    if (element == 'json') {
                        jsonFound = true;
                        return;
                    }
                });
            }
            if (!jsonFound || stop)
                return;
            try {
                // parsing the response into a variable
                var response = $.parseJSON(xhr.responseText);
                // coremetrics instructions found?
                if (!response.hasOwnProperty('coremetrics'))
                    return;
                $.each(response.coremetrics, function(index, el){
                    // Executing all of them
                    eval(el);
                });
            } catch (e) {
                // nothing to do with exception, but stop everything here
            }
        });
    });
})(jQuery);

