(function() {
    CP_Widget = new function(){
        if (typeof(croportal_widget_theme) != 'string') {
            croportal_widget_theme = 'default';
        };
		if (typeof(croportal_widget_type) != 'string') {
            croportal_widget_type = 'new';
        };
		var BASE_URL = 'http://www.croportal.net/';
        var WIDGET_URL = BASE_URL + 'api/widget?type=' + croportal_widget_type;
        var STYLESHEET_URL = BASE_URL + 'assets/css/widgets/' + croportal_widget_theme + '/' + croportal_widget_theme + '.css';
        this.browser = {
            version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
            msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
            mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
	    };
	    // override the css file for MSIE 6
	    if (this.browser.msie && this.browser.version == '6.0') {
	        STYLESHEET_URL = BASE_URL + 'assets/css/widgets/' + croportal_widget_theme + '/' + croportal_widget_theme + '-ie6.css';
	    }
        var TARGET_ELEMENT_ID = 'croportal_widget';
        function requestCss(url) {
            var l = document.createElement("link");
            l.rel = 'stylesheet';
            l.type = 'text/css';
            l.href = url;
            l.media = 'all';
            document.lastChild.firstChild.appendChild(l);
            l = null;
        };
        function requestContent(local) {
            var s = document.createElement('script');
            // s.src = WIDGET_URL + '&url=' + encodeURIComponent(local || window.location.href);
            s.src = WIDGET_URL;
            // document.getElementsByTagName('head')[0].appendChild(s);
            document.lastChild.firstChild.appendChild(s);
            s = null;
        };
        this.callback = function(data) {
            if (!data) return;
            var target = document.getElementById(TARGET_ELEMENT_ID);
            if (target) {
                target.innerHTML = data;
                target.style.display = 'block';
                target.style.visibility = 'visible';
            };
        };
        this.init = function() {
            requestCss(STYLESHEET_URL);
            document.write('<div id="' + TARGET_ELEMENT_ID + '" style="display: none;"></div>');
            requestContent();
        };
    };
    CP_Widget.init();
})();
