/**
 * Created with PyCharm.
 * User: PNikotin
 * Date: 22.01.13
 * Time: 19:36
 * To change this template use File | Settings | File Templates.
 */



var reorderableTabStrip = kendo.ui.TabStrip.extend({
    options: {
        name: 'ReorderableTabStrip'
    },

    init: function ( element, options ) {
        kendo.ui.TabStrip.fn.init.call(this, element, options);
        this.applyReorderable();
    },

    applyReorderable: function () {
        this.tabGroup.kendoReorderable({
            group: 'tabs',
            filter:'.k-item',
            hint: function(element) {
                return element.clone().wrap('<ul class="k-tabstrip-items k-reset"/>').parent().css({opacity: 0.8});
            },
            change: $.proxy(this.onReorderableChange, this)
        });
    },

    onReorderableChange: function ( event ) {
        if ( event.newIndex < event.oldIndex ) {
            this.tabGroup.children('.k-item::eq('+event.newIndex+')').before( this.tabGroup.children('.k-item:eq('+event.oldIndex+')') );
            this.element.children('.k-content:eq('+event.newIndex+')').before( this.element.children('.k-content:eq('+event.oldIndex+')') );
        }
        else {
            this.tabGroup.children('.k-item:eq('+event.newIndex+')').after( this.tabGroup.children('.k-item:eq('+event.oldIndex+')') );
            this.element.children('.k-content:eq('+event.newIndex+')').after( this.element.children('.k-content:eq('+event.oldIndex+')') );
        }
        this._updateClasses();
    }
});
kendo.ui.plugin( reorderableTabStrip );
