$(function() {
	// var mixedMode = [{
	// 	name: "htmlmixed"
	// }, {
	// 	name: "css"
	// }, {
	// 	name: "javascript"
	// }];
	if(/MSIE 8.0/g.test($ua) || /MSIE 9.0/g.test($ua)) {
		$('#code-theme').closest('.btn-group').hide();
	}
	if(!window.localStorage.getItem('code-theme')) {
		window.localStorage.setItem('code-theme', 'default')
	};
	$(window).resize(function() {
		getH();
	})
	getH = function() {
		H = $(window).height();
		H = H-$('#orbit-bar').height();
		H = H-parseInt($('.wrap-inner').css('padding-top'))-parseInt($('.wrap-inner').css('padding-bottom'));
		H = H-$('.wrap-inner > h3').outerHeight()-$('#code-tab').outerHeight()-28;
		setH();
	};
	setH = function() {
		$('.CodeMirror').css('height',H);
	};
	codeEditor = [];
	$(".code").each(function(i) {
		var cE = "cE"+i;
		var cE = $(this).codemirror({
			mode: $(this).data('mode'),
			styleActiveLine: true,
			lineNumbers: true,
			lineWrapping: true,
			autoCloseBrackets: true,
	        autoCloseTags: true,
	        tabMode: "indent",
	        tabSize: 4,
	        indentUnit: 4,
	        indentWithTabs: true,
		});
		cE.setOption("theme", window.localStorage.getItem('code-theme'));
		CodeMirror.commands["selectAll"](cE);
		var range = { from: cE.getCursor(true), to: cE.getCursor(false) };
		cE.autoFormatRange(range.from, range.to);
		codeEditor.push(cE);
		$('#code-theme > li').eq(window.localStorage.getItem('lidx')).addClass('active')
	});
	getH();
	$('.CodeMirror-sizer').each(function(i) {
		if($(this).height() < H ) {
			$(this).siblings('.CodeMirror-gutters').css('height',H-30);
		}
	});
	$('#code-theme').delegate('a', clickEvent, function(e) {
		theme = $(this).attr('href');
		lidx = $(this).parent('li').index();
		window.localStorage.setItem('code-theme', theme);
		window.localStorage.setItem('lidx', lidx);
		var j = null;
		$('.tab-pane').each(function() {
			if($(this).hasClass('in')) {
				j = $(this).index()
			}
		});
		codeEditor[j].setOption("theme", theme);
		$(this).parent('li').addClass('active').siblings('li').removeClass('active')
		e.preventDefault();
	});
	$('a[data-toggle="tab"]').on('shown', function (e) {
		var j = null;
		$('.tab-pane').each(function() {
			if($(this).hasClass('in')) {
				j = $(this).index()
			}
		});
		codeEditor[j].setOption("theme", window.localStorage.getItem('code-theme'));
	});
	setTimeout(sethide, 1);
    function sethide(){
    	$(".tab-pane:eq(0)").nextAll(".tab-pane").removeClass("active in");
    };
});