var RW_ConfigDefault = {
    host: 'https://s1.revenuewell.com',
    id: '',
    rv_page_size: 5,
    rv_iframe_hight: 1000
};

var load_jquery_callback = function () {
    (function ($) {
        $(document).ready(function () {
            if (typeof window.RW_Config != "undefined") $.extend(RW_ConfigDefault, window.RW_Config);
            window.RW_Config = RW_ConfigDefault;

            initRequestAppointmentWidgets();
            initReviewsWidgets();
        });

        function initRequestAppointmentWidgets() {
            var isInitializedString = "isinitialized";
            var appRequestElements = $("#RwRequestAppointment, #RwRequestApppointment, .rw-request-appointment");
            for (var i = 0; i < appRequestElements.length; i++) {
                var appRequest = appRequestElements.eq(i);
                if (!appRequest.data(isInitializedString) && appRequest.find("iframe").length == 0) {
                    appRequest.data(isInitializedString, true);
                    var idData = appRequest.data("secondid");
                    var config = getConfig(idData, false);
                    var configString = configToString(config);

                    $('<iframe frameborder=0 id="RW_Iframe_RequestAppointment" name="RW_Iframe_RequestAppointment" width="410px" height="950px" src="' +
                        config.host + '/OldComponents/Widget/RequestAppointment?id=' + config.id + "&healthgrades=" + config.healthgrades + configString + '"/>').appendTo(appRequest);
                }
            }
        }

        function initReviewsWidgets() {
            var isInitializedString = "isinitialized";
            var $reviewsElements = $("#RwReview, .rw-review");
            for (var i = 0; i < $reviewsElements.length; i++) {
                var $review = $reviewsElements.eq(i);
                if (!$review.data(isInitializedString) && $review.find("iframe").length == 0) {
                    var idData = $review.data("secondid");
                    var config = getConfig(idData, true);
                    var configString = configToString(config);

                    $('<iframe frameborder=0 id="RW_Iframe_Review" name="RW_Iframe_Review" width="550px" height="' +
                        config.rv_iframe_hight + 'px" src="' +
                        config.host + '/Widget/Review?id=' + config.id + '&limit=' + config.rv_page_size + configString + '"/>').appendTo($review);
                }
            }
        }

        function configToString(config) {
            var data = "";
            $.each(config, function (i, n) {
                data = data + "&" + i + "=" + n.toString().replace("#", "~");
            });
            return data;
        }

        function getConfig(id, isReviewWidget) {
            if (id === undefined || typeof RW_Config_List === 'undefined' || RW_Config_List == null || !Array.isArray(RW_Config_List)) {
                return window.RW_Config;
            }

            var data = RW_Config_List.filter(function (element) {
                return element.id == id && (isReviewWidget ? (typeof element.rv_header_color !== 'undefined') : (typeof element.ra_header_color !== 'undefined'));
            })[0];

            return data === undefined ? window.RW_Config : data;
        }
    })(jQuery);
};


if (typeof jQuery != 'undefined') {
    load_jquery_callback();
    _addLoadEventRW(load_jquery_callback);
} else {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    var host = typeof window.RW_Config.host == 'undefined' ? window.RW_ConfigDefault.host : window.RW_Config.host;
    script.src = host + '/Scripts/jquery-1.9.1.js';

    //calling a function after the js is loaded (IE)
    var loadFunction = function () {
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            script.onload = script.onreadystatechange = null;
            load_jquery_callback();
        }
    };
    script.onreadystatechange = loadFunction;

    //calling a function after the js is loaded (Firefox)  
    script.onload = load_jquery_callback;
    head.appendChild(script);
}

function _addLoadEventRW(_function) {
    var _onload = window.onload;
    if (typeof window.onload != 'function') {
        if (window.onload) {
            window.onload = _function;
        } else {
            var _addEventListener = window.addEventListener || document.addEventListener;
            var _attachEvent = window.attachEvent || document.attachEvent;
            if (_addEventListener) {
                _addEventListener('load', _function, true);
                return true;
            } else if (_attachEvent) {
                var _result = _attachEvent('onload', _function);
                return _result;
            } else {
                //todo: preloading fix for ie5.2 on mac os
                return false;
            }
        }
    } else {
        window.onload = function () {
            _onload();
            _function();
        }
    }
}