var bootstrapTab_popup = 'toolbar=no,status=no,resizable=yes,dependent=yes,width=' + (screen.width * 0.7) + ',height=' + (screen.height * 0.7) + ',left=' + ((screen.width - (screen.width * 0.7)) / 2) + ',top=' + ((screen.height - (screen.height * 0.7)) / 2);
var bootstrapTab_wrapper = bootstrapTab_deleteMessage = bootstrapTab_minimum = bootstrapTab_managePopupContent = '';
var bootstrapTab_id = '';
var bootstrapTab_id_ctr = 3;
var bootstrapTab_d = new Date();
bootstrapTab_id = bootstrapTab_d.getTime();

function bootstrapTab_up(bootstrapTab_element) {
    var bootstrapTab_elementIndex = bootstrapTab_index(bootstrapTab_element.parentNode.parentNode);
    if (bootstrapTab_elementIndex > 0) {
        var bootstrapTab_titleSwap1 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[1].firstChild.value;
        var bootstrapTab_titleSwap2 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) - 1].children[1].firstChild.value;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) - 1].children[1].firstChild.value = bootstrapTab_titleSwap1;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[1].firstChild.value = bootstrapTab_titleSwap2;
        var bootstrapTab_tabSwap1 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[2].firstChild.value;
        var bootstrapTab_tabSwap2 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) - 1].children[2].firstChild.value;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) - 1].children[2].firstChild.value = bootstrapTab_tabSwap1;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[2].firstChild.value = bootstrapTab_tabSwap2
    }
    return false
};

function bootstrapTab_down(bootstrapTab_element) {
    var bootstrapTab_elementIndex = bootstrapTab_index(bootstrapTab_element.parentNode.parentNode);
    if (bootstrapTab_elementIndex < bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_title').length - 1) {
        var bootstrapTab_titleSwap1 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[1].firstChild.value;
        var bootstrapTab_titleSwap2 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) + 1].children[1].firstChild.value;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) + 1].children[1].firstChild.value = bootstrapTab_titleSwap1;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[1].firstChild.value = bootstrapTab_titleSwap2;
        var bootstrapTab_tabSwap1 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[2].firstChild.value;
        var bootstrapTab_tabSwap2 = bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) + 1].children[2].firstChild.value;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[parseInt(bootstrapTab_elementIndex) + 1].children[2].firstChild.value = bootstrapTab_tabSwap1;
        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].getElementsByTagName('tr')[bootstrapTab_elementIndex].children[2].firstChild.value = bootstrapTab_tabSwap2
    }
    return false
};

function bootstrapTab_addNewItem() {
    bootstrapTab_id_ctr++;
    var bootstrapTab_newItem = document.createElement('tr');
    bootstrapTab_newItem_template = ' 		<td> 	';
    if ((/\bMSIE 6/.test(navigator.userAgent) || /\bMSIE 7/.test(navigator.userAgent) || /\bMSIE 8/.test(navigator.userAgent) || /\bMSIE 9/.test(navigator.userAgent)) && !window.opera) {
        bootstrapTab_newItem_template += ' 			<input type="button" class="bootstrapTab_up" value="&and;" onclick="bootstrapTab_up(this)" /> 			<input type="button" class="bootstrapTab_down" value="&or;" onclick="bootstrapTab_down(this)" /> 		'
    } else {
        bootstrapTab_newItem_template += '<div class="bootstrapTab_move"></div>'
    }
    bootstrapTab_newItem_template += ' 		</td> 		<td><input type="text" class="bootstrapTab_title" /></td> 		<td><textarea class="bootstrapTab_tab"' + (bootstrapTab_managePopupContent ? ' onclick="window.open(\'' + CKEDITOR.plugins.getPath('bootstrapTab') + 'ckeditor.html?edit=bootstrapTab_tab&content=\'+bootstrapTab_index(this.parentNode.parentNode), \'Bootstrap Tab ' + bootstrapTab_id_ctr + bootstrapTab_id + '\', \'' + bootstrapTab_popup + '\')"' : '') + '></textarea></td> 		<td><input type="button" class="bootstrapTab_remove" value="x" onclick="bootstrapTab_remove(this)" /></td> 	';
    bootstrapTab_newItem.innerHTML = bootstrapTab_newItem_template;
    bootstrapTab_wrapper.getElementsByTagName('tbody')[0].appendChild(bootstrapTab_newItem);
    return false
};

function bootstrapTab_remove(bootstrapTab_element) {
    if (bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_title').length > 1) {
        if (confirm(bootstrapTab_deleteMessage)) {
            bootstrapTab_element.parentNode.parentNode.parentNode.removeChild(bootstrapTab_element.parentNode.parentNode)
        }
    } else {
        alert(bootstrapTab_minimum)
    }
    return false
};

function bootstrapTab_in_array(bootstrapTab_needle, bootstrapTab_haystack) {
    for (var i in bootstrapTab_haystack) {
        if (bootstrapTab_haystack[i] == bootstrapTab_needle) return true
    }
    return false
};

function bootstrapTab_index(node) {
    var children = node.parentNode.childNodes;
    var num = 0;
    for (var i = 0; i < children.length; i++) {
        if (children[i] == node) return num;
        if (children[i].nodeType == 1) num++
    }
    return -1
};

function bootstrapTab_blur(bootstrapTab_event) {
    if (bootstrapTab_event.keyCode == 9 && (bootstrapTab_event.target.className == 'bootstrapTab_title' || bootstrapTab_event.target.className == 'bootstrapTab_tab')) {
        if (bootstrapTab_event.target.className == 'bootstrapTab_title') {
            setTimeout(function () {
                bootstrapTab_event.target.parentNode.parentNode.children[2].children[0].focus()
            }, 1)
        } else if (bootstrapTab_event.target.className == 'bootstrapTab_tab') {
            if (bootstrapTab_event.target.parentNode.parentNode.parentNode.children[bootstrapTab_event.target.parentNode.parentNode.rowIndex]) {
                setTimeout(function () {
                    bootstrapTab_event.target.parentNode.parentNode.parentNode.children[bootstrapTab_event.target.parentNode.parentNode.rowIndex].children[1].children[0].focus()
                }, 1)
            }
        }
    }
    if (bootstrapTab_event.target) {
        setTimeout(function () {
            bootstrapTab_event.target.style.border = bootstrapTab_event.target.value == '' ? '1px solid #f00' : ''
        }, 1)
    }
};
CKEDITOR.dialog.add('bootstrapTabDialog', function (editor) {
    return {
        title: editor.lang.bootstrapTab.plugin,
        minWidth: 600,
        minHeight: 400,
        resizable: false,
        contents: [{
            id: 'bootstrapTabTab',
            label: 'bootstrapTabTab',
            elements: [{
                type: 'html',
                html: ' 							<div id="bootstrapTab"> 								<div id="bootstrapTab_Container"> 									<table> 										<thead> 											<tr> 												<th></th> 												<th>' + editor.lang.bootstrapTab.title + '</th> 												<th>' + editor.lang.bootstrapTab.content + (editor.config.mj_variables_allow_html ? ' (' + editor.lang.bootstrapTab.htmlIsAllowed + ')' : '') + '</th> 												<th></th> 											</tr> 										</thead> 										<tbody></tbody> 									</table> 								</div> 								<input type="button" value="+ ' + editor.lang.bootstrapTab.addNewItem + '" class="bootstrapTab_addNew" onclick="bootstrapTab_addNewItem()" /> 							</div> 						',
                validate: function () {
                    var bootstrapTab_titles = bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_title');
                    var bootstrapTab_contents = bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_tab');
                    var bootstrapTab_error = false;
                    for (bootstrapTab_i = 0; bootstrapTab_i < bootstrapTab_titles.length; bootstrapTab_i++) {
                        if (bootstrapTab_titles[bootstrapTab_i].value == '') {
                            bootstrapTab_titles[bootstrapTab_i].parentNode.children[0].style.border = '1px solid #f00';
                            bootstrapTab_error = true
                        } else {
                            bootstrapTab_titles[bootstrapTab_i].parentNode.children[0].style.border = ''
                        }
                        if (bootstrapTab_contents[bootstrapTab_i].value == '') {
                            bootstrapTab_contents[bootstrapTab_i].parentNode.children[0].style.border = '1px solid #f00';
                            bootstrapTab_error = true
                        } else {
                            bootstrapTab_contents[bootstrapTab_i].parentNode.children[0].style.border = ''
                        }
                    }
                    return !bootstrapTab_error
                },
                commit: function (widget) {
                    var bootstrapTab_items = bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_title');
                    var bootstrapTab_tabs = bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_tab');
                    for (var bootstrapTab_i = 0; bootstrapTab_i <= bootstrapTab_items.length; bootstrapTab_i++) {
                        if (bootstrapTab_items[bootstrapTab_i]) {
                            widget.setData('bootstrapTab_item' + bootstrapTab_i, bootstrapTab_items[bootstrapTab_i].value);
                            widget.setData('bootstrapTab_content' + bootstrapTab_i, bootstrapTab_tabs[bootstrapTab_i].value);
                            if (bootstrapTab_i == 0) {
                                widget.setData('bootstrapTab_itemClass' + bootstrapTab_i, 'active');
                                widget.setData('bootstrapTab_contentClass' + bootstrapTab_i, 'tab-pane active')
                            } else {
                                widget.setData('bootstrapTab_itemClass' + bootstrapTab_i, '');
                                widget.setData('bootstrapTab_contentClass' + bootstrapTab_i, 'tab-pane')
                            }
                        } else {
                            widget.setData('bootstrapTab_item' + bootstrapTab_i, '');
                            widget.setData('bootstrapTab_content' + bootstrapTab_i, '')
                        }
                    }
                    widget.setData('bootstrapTab_total', bootstrapTab_items.length);
                    document.getElementsByClassName('bootstrapTab_sortable_script')[0].parentElement.removeChild(document.getElementsByClassName('bootstrapTab_sortable_script')[0])
                },
                setup: function (widget) {
                    var selection = editor.getSelection();
                    element = selection.getStartElement();
                    var bootstrapTab_item = '';
                    var bootstrapTab_attributes = false;
                    if (element.$.children[0]) {
                        for (var bootstrapTab_i = 0; bootstrapTab_i < element.$.children[0].attributes.length; bootstrapTab_i++) {
                            if (element.$.children[0].attributes[bootstrapTab_i].value == 'tabpanel') {
                                bootstrapTab_attributes = true
                            }
                        }
                    }
                    if ((element.$.tagName).toLowerCase() == 'div' && bootstrapTab_attributes) {
                        for (var bootstrapTab_i = 0; bootstrapTab_i < element.$.children[0].children[0].children[0].children.length; bootstrapTab_i++) {
                            eval("bootstrapTab_title = widget.data.bootstrapTab_item" + bootstrapTab_i);
                            bootstrapTab_title = bootstrapTab_title != undefined ? bootstrapTab_title : '';
                            eval("bootstrapTab_tab = widget.data.bootstrapTab_content" + bootstrapTab_i);
                            bootstrapTab_tab = bootstrapTab_tab != undefined ? bootstrapTab_tab : '';
                            bootstrapTab_item += ' 										<tr> 											<td> 									';
                            if ((/\bMSIE 6/.test(navigator.userAgent) || /\bMSIE 7/.test(navigator.userAgent) || /\bMSIE 8/.test(navigator.userAgent) || /\bMSIE 9/.test(navigator.userAgent)) && !window.opera) {
                                bootstrapTab_item += ' 											<input type="button" class="bootstrapTab_up" value="&and;" onclick="bootstrapTab_up(this)" /> 											<input type="button" class="bootstrapTab_down" value="&or;" onclick="bootstrapTab_down(this)" /> 										'
                            } else {
                                bootstrapTab_item += '<div class="bootstrapTab_move"></div>'
                            }
                            bootstrapTab_item += ' 											</td> 											<td><input type="text" class="bootstrapTab_title" value="' + bootstrapTab_title + '" /></td> 											<td><textarea class="bootstrapTab_tab"' + (bootstrapTab_managePopupContent ? ' onclick="window.open(\'' + CKEDITOR.plugins.getPath('bootstrapTab') + 'ckeditor.html?edit=bootstrapTab_tab&content=\'+bootstrapTab_index(this.parentNode.parentNode), \'Bootstrap Tab ' + bootstrapTab_i + bootstrapTab_id + '\', \'' + bootstrapTab_popup + '\')"' : '') + '>' + bootstrapTab_tab + '</textarea></td> 											<td><input type="button" class="bootstrapTab_remove" value="x" onclick="bootstrapTab_remove(this)" /></td> 										</tr> 									'
                        }
                        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].innerHTML = bootstrapTab_item
                    } else {
                        var bootstrapTab_template = ' 									<tr> 										<td> 								';
                        if ((/\bMSIE 6/.test(navigator.userAgent) || /\bMSIE 7/.test(navigator.userAgent) || /\bMSIE 8/.test(navigator.userAgent) || /\bMSIE 9/.test(navigator.userAgent)) && !window.opera) {
                            bootstrapTab_template += ' 												<input type="button" class="bootstrapTab_up" value="&and;" onclick="bootstrapTab_up(this)" /> 												<input type="button" class="bootstrapTab_down" value="&or;" onclick="bootstrapTab_down(this)" /> 											'
                        } else {
                            bootstrapTab_template += '<div class="bootstrapTab_move"></div>'
                        }
                        bootstrapTab_template += ' 										</td> 										<td><input type="text" class="bootstrapTab_title" /></td> 										<td><textarea class="bootstrapTab_tab"' + (bootstrapTab_managePopupContent ? ' onclick="window.open(\'' + CKEDITOR.plugins.getPath('bootstrapTab') + 'ckeditor.html?edit=bootstrapTab_tab&content=\'+bootstrapTab_index(this.parentNode.parentNode), \'Bootstrap Tab 1' + bootstrapTab_id + '\', \'' + bootstrapTab_popup + '\')"' : '') + '></textarea></td> 										<td><input type="button" class="bootstrapTab_remove" value="x" onclick="bootstrapTab_remove(this)" /></td> 									</tr> 									<tr> 										<td> 								';
                        if ((/\bMSIE 6/.test(navigator.userAgent) || /\bMSIE 7/.test(navigator.userAgent) || /\bMSIE 8/.test(navigator.userAgent) || /\bMSIE 9/.test(navigator.userAgent)) && !window.opera) {
                            bootstrapTab_template += ' 												<input type="button" class="bootstrapTab_up" value="&and;" onclick="bootstrapTab_up(this)" /> 												<input type="button" class="bootstrapTab_down" value="&or;" onclick="bootstrapTab_down(this)" /> 											'
                        } else {
                            bootstrapTab_template += '<div class="bootstrapTab_move"></div>'
                        }
                        bootstrapTab_template += ' 										</td> 										<td><input type="text" class="bootstrapTab_title" /></td> 										<td><textarea class="bootstrapTab_tab"' + (bootstrapTab_managePopupContent ? ' onclick="window.open(\'' + CKEDITOR.plugins.getPath('bootstrapTab') + 'ckeditor.html?edit=bootstrapTab_tab&content=\'+bootstrapTab_index(this.parentNode.parentNode), \'Bootstrap Tab 2' + bootstrapTab_id + '\', \'' + bootstrapTab_popup + '\')"' : '') + '></textarea></td> 										<td><input type="button" class="bootstrapTab_remove" value="x" onclick="bootstrapTab_remove(this)" /></td> 									</tr> 									<tr> 										<td> 								';
                        if ((/\bMSIE 6/.test(navigator.userAgent) || /\bMSIE 7/.test(navigator.userAgent) || /\bMSIE 8/.test(navigator.userAgent) || /\bMSIE 9/.test(navigator.userAgent)) && !window.opera) {
                            bootstrapTab_template += ' 												<input type="button" class="bootstrapTab_up" value="&and;" onclick="bootstrapTab_up(this)" /> 												<input type="button" class="bootstrapTab_down" value="&or;" onclick="bootstrapTab_down(this)" /> 											'
                        } else {
                            bootstrapTab_template += '<div class="bootstrapTab_move"></div>'
                        }
                        bootstrapTab_template += ' 										</td> 										<td><input type="text" class="bootstrapTab_title" /></td> 										<td><textarea class="bootstrapTab_tab"' + (bootstrapTab_managePopupContent ? ' onclick="window.open(\'' + CKEDITOR.plugins.getPath('bootstrapTab') + 'ckeditor.html?edit=bootstrapTab_tab&content=\'+bootstrapTab_index(this.parentNode.parentNode), \'Bootstrap Tab 3' + bootstrapTab_id + '\', \'' + bootstrapTab_popup + '\')"' : '') + '></textarea></td> 										<td><input type="button" class="bootstrapTab_remove" value="x" onclick="bootstrapTab_remove(this)" /></td> 									</tr> 								';
                        bootstrapTab_wrapper.getElementsByTagName('tbody')[0].innerHTML = bootstrapTab_template
                    }
                }
            }]
        }],
        onShow: function () {
            bootstrapTab_javascript = document.createElement('script');
            bootstrapTab_javascript.type = 'text/javascript';
            bootstrapTab_javascript.src = CKEDITOR.plugins.getPath('bootstrapTab') + 'js/Sortable.min.js';
            bootstrapTab_javascript.className = 'bootstrapTab_sortable_script';
            document.body.appendChild(bootstrapTab_javascript);
            for (var bootstrapTab_a = 0; bootstrapTab_a < CKEDITOR.dialog.getCurrent()._.element.$.getElementsByTagName('div').length; bootstrapTab_a++) {
                if (CKEDITOR.dialog.getCurrent()._.element.$.getElementsByTagName('div')[bootstrapTab_a].id == 'bootstrapTab_Container') {
                    bootstrapTab_wrapper = CKEDITOR.dialog.getCurrent()._.element.$.getElementsByTagName('div')[bootstrapTab_a]
                }
            }
            bootstrapTab_managePopupContent = editor.config.bootstrapTab_managePopupContent;
            bootstrapTab_deleteMessage = editor.lang.bootstrapTab.delete;
            bootstrapTab_minimum = editor.lang.bootstrapTab.minimum;
            bootstrapTab_wrapper.onkeydown = bootstrapTab_blur;
            setTimeout(function () {
                bootstrapTab_wrapper.getElementsByClassName('bootstrapTab_title')[0].focus()
            }, 1)
        },
        onCancel: function () {
            document.getElementsByClassName('bootstrapTab_sortable_script')[0].parentElement.removeChild(document.getElementsByClassName('bootstrapTab_sortable_script')[0])
        }
    }
});