(function () {
    var loadinghtm = "loading...",
    //objecte de configuració
    fxec_config = {
        host: "https://calendar.fxstreet.com",
        statichost: "https://staticcontent.fxstreet.com",
        selector: "#fxst_calendar",
        culture: "en-US",
        rows: 20,
        pastevents: 5,
        hoursbefore: 15,
        timezone: "UTC",
        columns: "date,time,country,event,actual,consensus,previous,volatility,countrycurrency",
        showcountryname: false,
        countrycode: "BE,IN,GR,CH,ZA,HU,RU,CL,BR,ID,FR,ES,IT,IE,EE,RO,QA,JP,EMU,NZ,CA,PL,TR,CN,HK,LT,CZ,KR,DK,IS,CO,FI,AU,PE,AE,PT,LV,NO,DE,AR,MX,US,UK,SE,SK,AT,SG,NL",
        css: 'mini',
        isfree: false,
        isSecure: function () { return window.location.protocol === "https:"; }
    },
    async_loadcss = function (url, callback) {
        var css = document.createElement('link');
        css.type = 'text/css';
        css.rel = 'stylesheet';
        css.href = url;
        if (typeof callback !== "undefined") {
            if (css.addEventListener) {
                css.addEventListener('load', callback, false);
            } else {
                css.onreadystatechange = function () {
                    if (this.readyState === "complete" || this.readyState === "loaded") {
                        callback.call();
                    }
                };
            }
        }
        document.getElementsByTagName('head')[0].appendChild(css);
    },
    async_loadjs = function(url, callback) {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = url;
        if (typeof callback !== "undefined") {
            if (s.addEventListener) {
                s.addEventListener('load', callback, false);
            } else {
                s.onreadystatechange = function () {
                    if (this.readyState === "complete" || this.readyState === "loaded") {
                        callback.call();
                    }
                };
            }
        }
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
    },
    load = function () {
        var jQ = jQuery;
        jQ(fxec_config.selector).html(loadinghtm);
        jQ.ajax({
            url: fxec_config.host + "/EventDateWidget/GetMini",
            data: getData()
        }).done(function (data) {
            jQ(fxec_config.selector).html(data);
            jQ(document).on("click", ".fxit-eventrow", function (event) {
                event.preventDefault();
                window.location = jQ(this).find(".fxit-eventurl").attr("href");
            });
            if (fxec_config.eventurl) {
                //link cap a event si ens canvien la url
                jQ(".fxit-eventurl").each(function (index, item) {
                    jQ(item).attr("href", fxec_config.eventurl + jQ(item).data("idevent"));
                });
            }
        }).fail();
        jQuery(document).on("click", "a.fxst-refresh", function (event) {
            event.preventDefault();
            load();
        });
    },
    getData = function () {
        //paràmetres comuns entre la versió free i la de pago
        var postData = {
            culture: fxec_config.culture,
            rows: fxec_config.rows,
            pastevents: fxec_config.pastevents,
            hoursbefore: fxec_config.hoursbefore,
            timezone: fxec_config.timezone,
            columns: fxec_config.columns,
            showcountryname: fxec_config.showcountryname,
            countrycode: fxec_config.countrycode,
            isfree: fxec_config.isfree,
            css: fxec_config.css
        };
        //paràmetres només allowed a la versió de pago
        if (fxec_config.view) {
            postData.view = fxec_config.view;
        }
        if (fxec_config.volatility) {
            postData.volatility = fxec_config.volatility;
        }
        if (fxec_config.viewallurl) {
            postData.viewallurl = fxec_config.viewallurl;
        }
        if (fxec_config.viewalltarget) {
            postData.viewalltarget = fxec_config.viewalltarget;
        }
        if (fxec_config.eventurl) {
            postData.eventurl = fxec_config.eventurl;
        }
        if (fxec_config.parentevent) {
            postData.parentevent = fxec_config.parentevent;
        }
        return postData;
    };
    if (typeof fxcalendar_config !== "undefined") {
        for (var key in fxcalendar_config) {
            if (fxcalendar_config.hasOwnProperty(key)) {
                fxec_config[key.toLowerCase()] = fxcalendar_config[key];
            }
        }
    }

    async_loadcss(fxec_config.statichost + "/calendarwidget-css/" + fxec_config.css + ".css");

    if (typeof jQuery === "undefined") {
        async_loadjs(fxec_config.statichost + "/calendarwidget-js/jquery-1.11.1.min.js", function () {
            jsonLoad();
        });
    }
    else {
        version = +(jQuery().jquery.replace(/\./g, ''));
        if (version >= 152) {
            jsonLoad();
        }
        else {
            alert(resources.JQueryRequired);
        }
    }

    function jsonLoad() {
        if (typeof JSON === 'undefined') {
            async_loadjs(fxec_config.statichost + "/calendarwidget-js/json2.js", function () {
                load();
            });
        }
        else {
            load();
        }
    };

})();