/* Cached version. Created: 2013-08-14T12:22:46+04:00 Now: 2013-08-14T12:41:30+04:00. Expire: 1970-01-08T03:00:00+03:00 */
(function() {
//Begin Widget
var WidgetObject = function() {
    this.render = function() {
        return this.widgetMainWrapper;
    }

    this.ce = function(el) { return document.createElement(el); }
    this.ge = function(id) { return document.getElementById(id); }

    this.hasClass = function (obj, className) {
        var classAttr = obj.getAttribute('class');
        classAttr = classAttr ? String(classAttr) : '';
        var classes = classAttr.split(' ');

        for (var i=0;i<classes.length;i++) {
            if (classes[i] == className) {
                return true;
            }
        }

        return false;
    }

    this.addClass = function (obj, className) {
        var classAttr = obj.getAttribute('class');
        classAttr = !classAttr ? '' : classAttr;
        if (!this.hasClass(obj, className)) {
            obj.setAttribute('class', classAttr + (classAttr.length>0 ? ' ' : '') + className);
        }
    }

    this.css = function(obj, param, value) {
        var style = obj.getAttribute('style');
        if (!style) {
            style = '';
        }

        var replace = '';
        if (value != undefined) {
            replace = param + ':' + value + ';';
        }


        var pattern = param + '\:([^;]+;?)';
        var regExp = new RegExp(pattern, 'i');
        if (regExp.test(style)) {
            style = style.replace(regExp, replace);
            console.log(style);
        }
        else {
            style += (style.length>0 ? ';' : '') + replace;
        }

        if (style.length > 0) {
            obj.setAttribute('style', style);
        }
    }

    this.init = function() {
        //Ссылка на текущий объект в замыкающих функциях
        var WidgetObj = this;
        var widgetMainWrapper = this.ce('div');
        widgetMainWrapper.setAttribute('id', 'gosmonitor_widget_wrapper_main');
            //attr('id', 'gosmonitor_widget_wrapper_main');


        var widgetBody = '\
        <div class="miniwidget-wrapper">\
            <div class="prefix"></div>\
            <div class="numbers">\
                <span class="digit digit-position-1 digit-0"></span><span class="digit digit-position-2 digit-0"></span><span class="digit digit-position-3 digit-2"></span>\
            </div>\
            <div class="prefix"></div>\
        </div>\
        ';

        widgetMainWrapper.innerHTML = widgetBody;

        var widgetWrapper = this.ce('div');
        this.addClass(widgetWrapper, 'widget-wrapper');
        widgetMainWrapper.appendChild(widgetWrapper);

        var miniWrapper = widgetMainWrapper.getElementsByTagName('div')[0];

        //Hide widget body cause we don't have to show it yet
        this.css(widgetWrapper, 'display', 'none');

        //Additional reference to current object to access it at event handlers
        var currentObj = this;
        miniWrapper.addEventListener('click', function() {
            if (!currentObj.hasClass(widgetWrapper, 'widget-rendered')) {

                var closeButton = currentObj.ce('a');
                closeButton.setAttribute('href', 'javascript:');
                closeButton.setAttribute('class', 'widget-button-close');
                closeButton.innerHTML = '<span>&nbsp;</span>';

                closeButton.addEventListener('click', function() {
                    WidgetObj.toggleVisible(false);
                });

                widgetWrapper.appendChild(closeButton);
                widgetWrapper.appendChild(WidgetObj.renderWidget());
                currentObj.addClass(widgetWrapper, 'widget-rendered');

            }

            WidgetObj.toggleVisible(true);

        })

        this.widgetWrapper = widgetWrapper;
        this.widgetMainWrapper = widgetMainWrapper;
    }
    this.renderWidget = function() {

        var _host = "http://dev2.gosmonitor.ru/sites/default/modules/widget/widget/index.php?id=129&oldsid=1&position=left_center&clr1=00152e";
        var frame = this.ce('iframe');
        frame.setAttribute('width', '100%');
        frame.setAttribute('height', '100%');
        frame.setAttribute('src', _host);

        return frame;
    }
    this.toggleVisible = function(state) {
        if (state === true) {
            this.css(this.widgetWrapper, 'display', 'block')
        }
        else if (state === false) {
            this.css(this.widgetWrapper, 'display', 'none');
        }
        else {
            //@todo Visibility toggle acording to state
        }
    }

    this.init();

    return this;
}

try {
  window.addEventListener('load', function() {
      var widget = WidgetObject();
      document.body.appendChild(widget.render());
  });
}
catch (e) {
  //silence!
  throw e;
}}) ();

