
/**************************************
    Webutler V3.0 - www.webutler.de
    Copyright (c) 2008 - 2014
    Autor: Sven Zinke
    Free for any use
    Lizenz: GPL
**************************************/


function WBeditor_loadcontfuncs() {
    WBcontvar_IframeIsLoaded = false;
    WBcontvar_IframeIntervalId = false;
    WBeditor_checkloaded();
}

function WBeditor_checkloaded() {
    if ( WBcontvar_IframeIsLoaded == false ) {
    	if ( WBcontvar_IframeIntervalId == false ) {
    		WBcontvar_IframeIntervalId = window.setInterval( WBeditor_checkiframeload, 100 ) ;
    	}
    	return;
    }
}

WBeditor_checkiframeload = function() {
    WBeditor_checkinstances();
	if(WBcontvar_InstancesLoaded == true) {
		window.clearInterval( WBcontvar_IframeIntervalId ) ;
		WBcontvar_IframeIsLoaded = true;
		
		WBcontvar_InstancesLoaded = false;
		WBeditor_settoolbarheight();
		WBeditor_gotometas();
		$(window).scrollTop(0);
		document.getElementById('webutler_loadingscreen').style.display = 'none';
	}
}

function WBeditor_checkinstances() {
    var ScriptCount = parseInt(WBcontvar_ScriptInstances.length);
    if(ScriptCount == WBcontvar_PageCount) {
        for(var x = 0; x < WBcontvar_ScriptInstances.length; x++) {
            if(!WBeditor_inarray(WBcontvar_ScriptInstances[x], WBcontvar_PageInstances)) {
                return false;
            }
        }
        WBcontvar_InstancesLoaded = true;
    }
    return;
}

function WBeditor_inarray(find, arr) {
    for(var z in arr) {
        if(find == arr[z]) {
            return true;
        }
    }
    return false;
}

function WBeditor_gotometas() {
	CKEDITOR.instances.metas.focus();
	CKEDITOR.instances.metas.readOnlyToolbar( true, false );
}

function WBeditor_settoolbarheight() {
	var paddingtop = $('#WBeditor_toolbar').height();
	$('body').css({
		'padding-top': paddingtop + 'px',
		'height': $(window).height()-paddingtop + 'px'
	});
}


$(document).ready(function()
{
	$('body').click(function(e) {
		if($(e.target).closest('#WBeditor_toolbar').length === 0 && $(e.target).closest('#WBeditor_linealdiv').length === 0 && $(e.target).closest('.cke_textarea_inline').length === 0) {
			var dofocus = true;
			for(var i = 0; i < WBcontvar_PageInstances.length; i++) {
				if($(e.target).closest('.cke_editor_' + WBcontvar_PageInstances[i] + '_dialog').length !== 0) {
					dofocus = false;
					break;
				}
			}
			if(dofocus) {
				WBeditor_gotometas();
			}
		}
	});
	
	WBeditor_loadcontfuncs();
	
	$(window).resize(function() {
		WBeditor_settoolbarheight();
	});
});







