// Copyright (c) 2006 Filip Heitbrink (http://www.24webcenter.com)
// CMS javascript functions
// Some functions need the prototype.js file

var dwin = null
function debug_console(s){
	if ((dwin == null) || (dwin.closed)) {  
		dwin = window.open("","debug_console", "scrollbars=yes,resizable=yes,height=200,width=400")
		dwin.document.open("text/html")
	}
	dwin.document.writeln(s)
	dwin.scrollTo(0,10000)
	//dwin.document.close()
	dwin.focus()
}

var mousepos_x = 0
var mousepos_y = 0
function cms_savePosition(e) {
// check if document is completely loaded yet
if (document.getElementsByTagName("body").item(0) == null){ return false }
// save mouse position
mousepos_x = Event.pointerX(e)
mousepos_y = Event.pointerY(e)
if ($('div_cmshelp')){
	// assign position to help div
	$('div_cmshelp').style.left = mousepos_x + 10 + 'px'
	$('div_cmshelp').style.top = mousepos_y + 20 + 'px'
}
}

Event.observe(document, "mousemove", cms_savePosition, false);

function cms_createHelpDiv(){
// create div dynamicaly
var mybody = document.getElementsByTagName("body").item(0)
var mydiv = document.createElement('DIV')
mydiv.id = 'div_cmshelp'
mydiv.name = 'div_cmshelp'
mydiv.style.display = 'none'
mydiv.style.left = mousepos_x + 10
mydiv.style.top = mousepos_y + 20
mybody.appendChild(mydiv);
}

function cms_showHelp(s){
// check if help div exists, if not, create it
if (!$('div_cmshelp')){ cms_createHelpDiv() }
$('div_cmshelp').innerHTML = s
$('div_cmshelp').style.display = 'block'
}

function cms_hideHelp(){
if ($('div_cmshelp')){
	$('div_cmshelp').style.display = 'none'
	$('div_cmshelp').innerHTML = ''
}
}

function cms_debug_showAllElements(tagname){
// shows all the elements of given tagname in the current document
var debugstr = ''
for (i=0; i<document.all.length; i++){
	if (document.all[i].tagName == tagname){
		debugstr += document.all[i].tagName + ' - ' + document.all[i].id + ' - ' + document.all[i].name + '\n'
	}
}
alert(debugstr)
}


var currentContentId, currentContentData;

function cms_handlerFunc_showContentEditor(text){
$('divblock_' + currentContentId).innerHTML = unescape(text)
}

function cms_doAjaxRequest(url, query_string, func, func_vars){

var handlerFunc = function(t) {
// call the function that will handle the response
if (func_vars != ''){ func_vars += ',' }
if (func != ''){ eval(func + "(" + func_vars + "t.responseText)") }
}

var notfoundFunc = function(t) {
    alert('Error 404: location "' + url + '" was not found.')
}

var errorFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

var opt = {
	method: 'post',
	postBody: query_string,
	onSuccess: handlerFunc,
	on404: notfoundFunc,
	onFailure: errorFunc
}

new Ajax.Request(url, opt)
}


function cms_showContentEditor(id, pid){
var url = baseHref + 'admin/update.php'
var dimensions = $('divblock_' + id).getDimensions() // get div dimensions

// if already active, deactivate previous editor
if (currentContentId){ cms_cancelContentEditor() }

// if div is small open editor in popup
if (dimensions.width < '500' || editor_always_popup == '1'){

	var win = window.open(url + '?pid=' + pid + '&action=get_editor_popup&area=' + id,'editor','width=700, height=500')

}else{

	// save current id and old content
	currentContentId = id
	currentContentData = $('divblock_' + id).innerHTML
	// get editor
	cms_doAjaxRequest(url, 'pid=' + pid + '&action=get_editor&area=' + id + '&width=' + dimensions.width + '&height=' + dimensions.height, 'cms_handlerFunc_showContentEditor', '')
}
}

function cms_cancelContentEditor(){
$('divblock_' + currentContentId).innerHTML = currentContentData
currentContentId = null
currentContentData = null
window.parent.__FCKeditorNS = null
NS = null
}


function cms_saveContentEditor(id, pid){
// save new data
var url = baseHref + 'admin/update.php'
var oEditor = FCKeditorAPI.__Instances['cms_editor']
var content = oEditor.EditorDocument.body.innerHTML

// send data to server
cms_doAjaxRequest(url, 'pid=' + pid + '&action=save_content&area=' + id + '&content=' + escape(content), '', '')

$('divblock_' + id).innerHTML = content
currentContentId = null
currentContentData = null
window.parent.__FCKeditorNS = null
NS = null
}