// Variables to control onmouseover and onmouseout
mousecapture = false
mousex = 0
mousey = 0
divtop = 0
divleft = 0
function beginmove(event) {
	e = event
	mousecapture = true
	mousex = e.screenX
	mousey = e.screenY
}
function moveitem(event) {
	e = event
	if ( mousecapture && e ) {
		divleft += e.screenX - mousex
		if ( divleft < 0 ) {
			divleft = 0;
		}
		divtop += e.screenY - mousey
		if ( divtop < 0 ) {
			divtop = 0;
		}
		mousex = e.screenX
		mousey = e.screenY
		document.getElementById('moveablediv').style.top = divtop + 'px'
		document.getElementById('moveablediv').style.left = divleft + 'px'
	}
}
function FCKeditor_OnComplete( editorInstance ) {
	if(window.addEventListener){ // Mozilla, Netscape, Firefox
		editorInstance.EditorWindow.document.addEventListener('mousemove', moveitem, false);
	} else { // IE
		editorInstance.EditorWindow.document.attachEvent('onmousemove', moveitem);
	}
	for ( i = 0; i < frames.length; i++ ) {
		if ( window.frames[i] ) {
			window.frames[i].scrolling="no"
			if ( window.frames[i].document ) {
				if(window.addEventListener){ // Mozilla, Netscape, Firefox
					window.frames[i].document.addEventListener('mousemove', moveitem, false);
				} else { // IE
					window.frames[i].document.attachEvent('onmousemove', moveitem);
				}
			}
		}
	}
	editorInstance.LinkedField.form.onsubmit = doSave;
	editorInstance.EditorWindow.parent.document.getElementById('xEditingArea').style.border=0
	editorInstance.EditorWindow.parent.document.body.style.margin=0
	editorInstance.EditorWindow.parent.document.body.style.padding=0
	editorInstance.EditorWindow.parent.document.body.style.overflow='hidden'
	editorInstance.Events.AttachEvent( 'OnSelectionChange', updateEditor )
//	editorInstance.Events.AttachEvent( 'OnFocus', focuseditor )
	editorInstance.Events.AttachEvent( 'OnAfterSetHTML', sethtml )
	updateEditor(editorInstance)
	//docprops(editorInstance)
	editorInstance.Focus()
	editorInstance.ResetIsDirty()
}
var cancel = 0
var delete_id = 0
var count = 0
var keepclean = 0
function updateEditor ( editorInstance ) {
	if ( editorInstance.EditorDocument.body.offsetHeight > 200 ) {
		document.getElementById('html___Frame').height = editorInstance.EditorDocument.body.offsetHeight
	} else if ( editorInstance.EditorDocument.body.scrollHeight > 200 ) {
		document.getElementById('html___Frame').height = editorInstance.EditorDocument.body.scrollHeight
	}
}
function sethtml ( editorInstance ) {
	checkclean(editorInstance)
}
function focuseditor( editorInstance ) {
//	checkclean(editorInstance)
//	docprops(editorInstance)
}
function setclean(editorInstance) {
	if ( ! editorInstance.IsDirty() ) {
		keepclean = 1
	}
}
function checkclean(editorInstance) {
	if ( keepclean ) {
		editorInstance.ResetIsDirty()
		keepclean = 0
	}
}
function docprops( editorInstance ) {
//	setclean(editorInstance)
//	var html = editorInstance.GetHTML()
//	checkclean(editorInstance)
//	if ( html ) {
//		matches = html.match(/^<div/)
//		if ( ! matches ) {
//			setclean(editorInstance)
//			editorInstance.SetHTML('<div>'+html+'</div>')
//		}
//	}
}
function newpage() {
	var oEditor = FCKeditorAPI.GetInstance('html')
	if ( oEditor.IsDirty() ) {
		if ( confirm('Are you sure you want to discard your changes?') ) {
			location.href=base_dir + '/' + edit_dir + '/?create=1'
		}
	} else {
		location.href=base_dir + '/' + edit_dir + '/?create=1'
	}
}
function preview() {
	setclean(FCKeditorAPI.GetInstance('html'))
	window.open(location.protocol+'//'+location.host+location.pathname+'?preview=1');
}
function cancelling() {
	cancel = 1
	setclean(FCKeditorAPI.GetInstance('html'))
}
function logout() {
	var oEditor = FCKeditorAPI.GetInstance('html')
	if ( oEditor.IsDirty() ) {
		if ( confirm('Are you sure you want to discard your changes?') ) {
			return true
		} else {
			return false
		}
	} else {
		return true
	}
}
function doSave(){
	var oEditor = FCKeditorAPI.GetInstance('html')
	checkclean(oEditor)
	if ( delete_id ) {
		var selected = document.getElementById('id').selectedIndex
		var value = document.getElementById('id').options[selected].value
		if ( confirm('Are you sure you want to delete ' + value) ) {
			return true
		} else {
			delete_id = 0;
			return false
		}
	}
	if ( cancel ) {
		if ( oEditor.IsDirty() ) {
			if ( confirm('Are you sure you want to cancel your changes?') ) {
				return true
			} else {
				return false
			}
		} else {
			return true
		}
	}
	new_id = document.getElementById('new_id').value
	if ( document.getElementById('id') ) {
		options = document.getElementById('id').options
		if ( new_id ) {
			for ( i = 0; i < options.length; i++ ) {
				if ( new_id == options[i].value ) {
					if ( confirm('Page ID already exists. Would you like to overwrite?') ) {
						return true
					} else {
						return false
					}
				}
			}
		}
		return true
	} else {
		if ( new_id ) {
			for ( i = 0; i < array.length; i++ ) {
				if ( new_id == array[i] ) {
					if ( confirm('Page ID already exists. Would you like to overwrite?') ) {
						return true
					} else {
						return false
					}
				}
			}
			return true
		} else {
			alert('Invalid Page ID')
		}
	}
	return false
}
