(function (lmig, $, undefined) {

    // Multiview
    lmig.MultiView = function (container, controller, on_view_changed) {
        this.container = container;
        this.controller = controller;
        this._viewChanged = on_view_changed || function (cv) { }

        $(this.controller).children().click(this, function (event) {
            event.data.setActiveView($(this).data('view'));
        });
    };
    lmig.MultiView.prototype = {
        getActiveView: function () {
            $(this.container).children('div:visible');
        },

        setActiveView: function (view) {
            $(this.container).children('div').not(view).hide();
            this._viewChanged(view);
            $(view).show();
        }
    };

    // Content rotator
    lmig.ContentRotator = function (container, template, data, interval, on_rotate) {
        var _instance = this;

        // Properties
        this.container = $(container);
        this.containerRaw = container;
        this.template = $(template);
        this.data = data;

        // Callback functions
        this.rotated = on_rotate || function (c) { };

        // Fields
        this._cursor = -1;
        this.controller = null;
        this.controller_template = null;

        window.setInterval((function (self) { return function () { self.rotate(); } })(_instance), interval);
    };
    lmig.ContentRotator.prototype = {
        // Check if it reaches the end of the dataset
        _next: function () {
            return this._cursor < this.data.length - 1;
        },

        rotate: function () {
            // Increment or reset the current index
            if (this._next()) { ++this._cursor; }
            else { this._cursor = 0; }

            if (this.controller) {
                this.controller.children('li').removeClass("-lmig-cr-item-on");
                $(this.controller.children('li')[this._cursor]).addClass("-lmig-cr-item-on");
            }

            var _instance = this;
            this.container.children().fadeOut('slow', function () {
                _instance.container.empty();

                // Bind current data item to an HTML element
                _instance.template.tmpl(_instance.data[_instance._cursor]).fadeIn('slow').appendTo(_instance.container);
            });

            // Execute the callback function after each rotation
            if (this.rotated) {
                this.rotated({ i: this._cursor, v: this.data[this._cursor] });
            }
        },

        // Start the content rotator
        setup: function () {
            this.template.tmpl(this.data[this._cursor]).appendTo(this.container);
            this.rotate();
            return this;
        },

        // Get the underlying dataset
        getData: function () {
            return this.data;
        },

        // Set the underlying dataset
        setData: function (data) {
            this.data = data;
        },

        getController: function () {
            return this.controller;
        },

        setController: function (controller, ctrl_tmpl) {
            this.controller = $(controller);
            if (ctrl_tmpl) {
                this.controller_template = $(ctrl_tmpl);
                this.controller_template.tmpl(this.data).appendTo(this.controller);
            }
        }
    };

    // Watermark
    lmig.Watermark = function (control, def_text, focus_in, focus_out) {
        this.bag = {};
        this.bag.def_text = def_text || 'Please input here';
        this.bag.stl_focus_in = focus_in || 'wmk_focusin'
        this.bag.stl_focus_out = focus_out || 'wmk_focusout';

        this.control = $(control);
        this.control.val(def_text);
        this.control.attr('class', this.bag.stl_focus_out);

        this.control.focusin(this.bag, function (event) {
            if ($(this).val() && $(this).val() === event.data.def_text) { $(this).val(''); }
            $(this).attr('class', event.data.stl_focus_in);
        });

        this.control.focusout(this.bag, function (event) {
            var _control = $(this);
            if (!_control.val()) {
                _control.val(event.data.def_text).attr('class', event.data.stl_focus_out);
            }
        });
    };

    // Notice
    lmig.Notice = function (conf, duration) {
        var instance = {};
        if (conf[window.location.href] && conf[window.location.href].is_on) {
            var instance = $("<div class='lmig_notice'><div>" + conf[window.location.href].content + "</div><img src='Images/Cross.gif' alt='' title='Close this'/></div>").appendTo($('body'));

            $('.lmig_notice img').click(function () {
                $('.lmig_notice').fadeOut('slow');
            });
            $('.lmig_notice').fadeIn('slow');
            window.setTimeout((function (self) { return function () { self.fadeOut('slow'); } })(instance), duration || 5000);
        }
    }
} (window.lmig = window.lmig || {}, jQuery));
