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

(function() {
	var d = document.domain ;

	while ( true ) {
		try {
			var test = window.opener.document.domain ;
			break ;
		}
		catch( e ) {}

		d = d.replace( /.*?(?:\.|$)/, '' ) ;

		if ( d.length == 0 )
			break ;
		try {
			document.domain = d ;
		}
		catch (e) {
			break ;
		}
	}
})();

function setuploadpath(elem) {
    var filename = elem.value;
    elem.nextSibling.value = filename;
}

function setheights() {
    var fullheight = parseInt(document.body.offsetHeight);
    var frameheight = parseInt(fullheight-135) + 'px';
    document.getElementById('folderswin').style.height = frameheight;
    document.getElementById('fileswin').style.height = frameheight;
    document.getElementById('renamebg').style.height = fullheight + 'px';
}

function getUrlParam(paramName)
{
    var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i') ;
    var match = window.top.location.search.match(reParam) ;
    
    return (match && match.length > 1) ? match[1] : '' ;
}

function renamepopupopen(oldname)
{
	document.getElementById('renamebg').style.display = 'block' ;
	document.getElementById('renamepopup').style.display = 'block' ;
	document.getElementById('renamepopup_oldname').value = oldname;
    var newname = oldname.substring(0, oldname.lastIndexOf('.'));
    var ext = ''
    if(newname == '') {
        newname = oldname;
    }
    else {
        ext = oldname.substring(oldname.lastIndexOf('.'), oldname.length);
    }
	document.getElementById('renamepopup_newname').value = newname;
    document.getElementById('renamepopup_oldext').innerHTML = ext;
}

function renamepopupclose()
{
	document.getElementById('renamebg').style.display = 'none' ;
	document.getElementById('renamepopup').style.display = 'none' ;
}

function streamfileupload() {
	if(document.getElementById('ResourceSelector'))
	{
		document.getElementById('ResourceSelector').style.display = 'none';
	}
	document.getElementById('webutler_loadingscreen').style.display = 'block';
	
	document.getElementById('uploadscreen').style.display = 'block';
	
    var prozent = document.getElementById('prozentbar');
    var progress = document.getElementById('progressbar');
	progress.style.display = '';
	
    var upload = document.getElementsByName('uploadfile')[0];
	var file = upload.files[0];
	var filename = file.name;
	var filetype = file['type'];
	
	var url = document.getElementById('actionpath').value;
	url += '&filename=' + encodeURIComponent(filename) + '&filetype=' + encodeURIComponent(filetype);
	if(document.getElementById('imgwidth')) {
		var imgwidth = parseInt(document.getElementById('imgwidth').value);
		if(imgwidth != '')
			url += '&imgwidth=' + imgwidth;
	}
	
	var request = new XMLHttpRequest();
	request.open('POST', url, true);
	request.setRequestHeader("X_FILENAME", filename);
	
	request.upload.addEventListener('progress', function(evt) {
		var uploadstate = Number((100 / evt.total) * evt.loaded).toFixed(2);
        prozent.innerHTML = uploadstate + ' %';
		progress.style.width = (uploadstate * 2) + 'px';
	}, false);
	
	request.addEventListener('load', function(evt) {
		file.name = '';
		alert(request.responseText);
		window.top.location.reload();
	}, false);
	
	var canceling = function() {
		document.getElementById('uploadcancel').removeEventListener('click', canceling, false);
        request.abort();
		window.top.location.reload();
    }
	
	document.getElementById('uploadcancel').addEventListener('click', canceling, false);
	
	request.send(file);
}

function OpenFile( fileUrl )
{
	if( window.top.name == 'CKBrowseNoneDialog' || window.top.name == 'CKBrowseTplIcons' )
	{
		return false ;
	}
    var funcNum = getUrlParam('CKEditorFuncNum');
    window.top.opener.CKEDITOR.tools.callFunction(funcNum, fileUrl, '');
	window.top.close();
}

function setTplIcon( file )
{
	if( window.top.name != 'CKBrowseTplIcons' )
	{
		return false ;
	}
    window.top.opener.document.getElementById('webutler_tplicon').value = file;
	window.top.close();
}

function OpenWindow( URL, preview, player )
{
	var breite = '600';
	var hoehe = '450';
	var oben = parseInt((window.screen.height - hoehe) / 2);
	var links = parseInt((window.screen.width  - breite) / 2);
	
	var options = 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes,scrollbars=yes,width=' + breite + ',height=' + hoehe + ',left=' + links + ',top=' + oben;
	
	var popupWindow = window.open( '', preview, options, true );
	
	if(!popupWindow)
		return false;
	
	try
	{
		popupWindow.resizeTo( breite, hoehe );
		popupWindow.moveTo( links, oben );
		
    	if(player == 'true') {
        	var zDoc = popupWindow.document;
        	zDoc.open();
        	zDoc.write('<html><head></head><body><embed height="100%" width="100%" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" src="' + URL + '" type="application/x-shockwave-flash"></embed></body></html>');
        	zDoc.close();
            popupWindow.focus();
    	}
		else {
    		popupWindow.focus();
    		popupWindow.location.href = URL;
		}
	}
	catch ( e )
	{
		popupWindow = window.open( URL, preview, options, true );
	}
}

function OpenEditWin( WB_Host, ImageURL )
{
	var breite = imageeditorWindowWidth;
	var hoehe = imageeditorWindowHeight;
	
	var url = WB_Host + '/admin/imageedit/tools.php?imgfile=' + ImageURL;
	
	if(typeof breite == 'string' && breite.length > 1 && breite.substr(breite.length - 1, 1) == '%')
		breite = parseInt(window.screen.width * parseInt(breite.substr(0, breite.length - 1)) / 100);

	if(typeof hoehe == 'string' && hoehe.length > 1 && hoehe.substr(hoehe.length - 1, 1) == '%')
		hoehe = parseInt(window.screen.height * parseInt(hoehe.substr(0, hoehe.length - 1)) / 100);

	if(breite < 640) breite = 640;
	if(hoehe < 420) hoehe = 420;
	
	var oben = parseInt((window.screen.height - hoehe) / 2);
	var links = parseInt((window.screen.width - breite) / 2);
	
	var options = 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes,scrollbars=yes,width=' + breite + ',height=' + hoehe + ',left=' + links + ',top=' + oben;
	
	var popupWindow = window.open( '', 'ImgEdit', options, true );
	
	if(!popupWindow)
		return false;
	
	try
	{
		popupWindow.resizeTo( breite, hoehe );
		popupWindow.moveTo( links, oben );
		popupWindow.focus();
		popupWindow.location.href = url;
	}
	catch ( e )
	{
		popupWindow = window.open( url, 'ImgEdit', options, true );
	}
}
