//
// (C) Electric Dream Factory
//     http://www.edf.nl 
//

//--------------------------------------------------------------------
// General
//

function ecc_trace(s)
{
    var div = document.all("ecc_tracediv");
    if (div)
      div.innerHTML += s + "<br>";
    return;
}

//--------------------------------------------------------------------
// Commands
//

function ecc_command_disable(id)
{
    ecc_command_set_enabled_state(id, false);
}

function ecc_command_enable(id)
{
    ecc_command_set_enabled_state(id, true);
}

function ecc_command_set_enabled_state(id, enabled)
{
    var items = document.all(id);
    if (items)
    {
    if (items.length >= 1)
    {
    }
    else
    {
          items.disabled = !enabled;
      }
    }
}

function ecc_command_check(id)
{
    ecc_command_set_checked_state(id, true);
}

function ecc_command_uncheck(id)
{
    ecc_command_set_checked_state(id, false);
}

function ecc_command_set_checked_state(id, enabled)
{
    var items = document.all(id);
    if (items)
    {
    if (items.length >= 1)
    {
          // for (var i = 1; )...
    }
    else
    {
          items.checked = enabled;
          if (enabled)
            items.className = "ecc_toolbarbutton_checked";
          else
            items.className = "";
      }
    }
}

//--------------------------------------------------------------------
// Toolbar
//

function ecc_toolbar_mouseenter(o)
{
    if (o.checked == 'true')
      o.className = 'ecc_toolbarbutton_down';
    else
      o.className = 'ecc_toolbarbutton_up';
    event.cancelBubble = true;
}

function ecc_toolbar_mouseout(o)
{
    if (o.contains(event.toElement))
      return;
    if (o.checked == true)
      o.className = 'ecc_toolbarbutton_checked';
    else
      o.className = '';
    event.cancelBubble = true;
}

function ecc_toolbar_mousedown(o)
{
    o.className = 'ecc_toolbarbutton_down';
}

function ecc_toolbar_mouseup(o)
{
    if (o.checked == false)
    {
      o.checked = true;
      o.className = 'ecc_toolbarbutton_checked';
    } 
    else if (o.checked == true)
    {
      o.checked = false;
      o.className = '';
    } 
    else
    {
      o.className = '';
    }
}

function ecc_toolbarmenu_mousedown(o)
{
    o.className = 'ecc_toolbarbutton_down';
    var x,y,e;
    e = o;
    x = 0;
    y = 0;
    while (e && e.style.position != 'absolute')
    {
        x += e.offsetLeft;
        y += e.offsetTop;
        e = e.offsetParent;
    }
    if (o.topmenu)
        y += o.offsetHeight;
    else
        x += o.offsetWidth;
    var popup = o.children('popupmenu');
    popup.style.left = x;
    popup.style.top = y;
    popup.style.display = 'block';
}

function ecc_toolbarmenu_mouseup(o)
{
    o.className = 'ecc_toolbarbutton_up';
}

function ecc_toolbarmenu_mouseenter(o)
{
    o.className = 'ecc_toolbarbutton_up';
}

function ecc_toolbarmenu_mouseout(o)
{
    o.className = '';
}

//--------------------------------------------------------------------
// Text formatting
//

var ecc_cur_editor = null;
var ecc_saved_editor = null;

function ecc_text_focusin(e)
{
    ecc_cur_editor = e;
    ecc_saved_editor = null;
    if (event)
      event.cancelBubble = true;
    if (ecc_cur_editor.iseditingsource == null)
    {
        ecc_cur_editor.iseditingsource = false;
    }
}

function ecc_text_focusout(e)
{
    if (e == ecc_cur_editor)
    {
      var ctl = document.all(ecc_cur_editor.id.substr(0, e.id.length - 4));
      if (ctl)
      {
        if (ecc_cur_editor.iseditingsource == true)
            ctl.value = e.innerText.replace(/</g, "[").replace(/>/g, "]");
        else
      ctl.value = e.innerHTML.replace(/</g, "[").replace(/>/g, "]");
    }
    ecc_saved_editor = ecc_cur_editor;
    ecc_cur_editor = null;
    if (event)
      event.cancelBubble = true;
}
}

function ecc_text_command(command)
{
	ecc_trace("ecc_text_command: " + command);
	if (ecc_cur_editor)
	{
		document.execCommand(command,false,'');
	}
	event.cancelBubble = true;
}

function ecc_text_command2(command, argument)
{
	ecc_trace("ecc_text_command: " + command + "," + argument);
		document.execCommand(command,false,argument);
	event.cancelBubble = true;
}

function ecc_text_fontname(value)
{
  if (ecc_cur_editor)
  {
    document.execCommand('fontname',false,value);
  }
}

function ecc_text_fontsize(value)
{
  if (ecc_cur_editor)
  {
    document.execCommand('fontsize',false,value);
  }
}

function ecc_text_insertlink(baseUrl)
{
  ecc_trace("ecc_text_insertlink");
  var value = window.showModalDialog("/cms/modules/standard/selectlink.aspx","","dialogWidth:600px;dialogHeight:460px;scroll:no;status:no;resizable:yes");
  if (value)
  {
    var args = value.split('|');
    if (args[1]) { // target
      var r = document.selection.createRange();
      if (document.execCommand('createlink', false, args[0])) {
        var html = r.htmlText.replace('href=', 'target="' + args[1] + '" href=');
        r.pasteHTML(html);
      }
    } else
    {
    document.execCommand('createlink', false, value);
  }
}
}

function ecc_text_insertimage(baseUrl)
{
  if (ecc_cur_editor)
  {
    ecc_trace("ecc_text_insertimage");
    var value = window.showModalDialog("/cms/modules/images/list.aspx?typeid=11&dialog=1","","dialogWidth:624px;dialogHeight:490px;scroll:no;status:no;resizable:yes");
    if (value)
    {
      document.execCommand('insertimage', false, value);
    }
  }
}

function ecc_text_insertasset(baseUrl)
{
  ecc_trace("ecc_text_insertasset");
  var value = window.showModalDialog("/cms/modules/assets/list.aspx?typeid=10&dialog=1","","dialogWidth:624px;dialogHeight:490px;scroll:no;status:no;resizable:yes");
  if (value)
  {
    document.execCommand('createlink', false, value);
  }
}

function ecc_text_switch_edit_mode()
{
    if (ecc_cur_editor)
    {
      if (ecc_cur_editor.iseditingsource == null)
        ecc_cur_editor.iseditingsource = false;
      else
        ecc_text_set_edit_mode(!ecc_cur_editor.iseditingsource)
    }
}

function ecc_text_set_edit_mode(editsource)
{
	if (ecc_cur_editor)
	{
		if (!editsource && ecc_cur_editor.iseditingsource == true)
		{
      ecc_cur_editor.iseditingsource = false;
      ecc_command_uncheck("contentBox__ctl0_editsource");
      ecc_cur_editor.innerHTML = ecc_cur_editor.innerText;
      ecc_cur_editor.className = "";
		}
		else if (editsource && ecc_cur_editor.iseditingsource == false)
		{
			ecc_cur_editor.iseditingsource	=	true;
      ecc_command_check("contentBox__ctl0_editsource");
      ecc_cur_editor.innerText = ecc_cur_editor.innerHTML;
      ecc_cur_editor.className = "ecc_text_source_mode";
		}
  }
}

function ecc_text_switchtextmode()
{
	ecc_trace("ecc_text_htmlmode");
	if (ecc_saved_editor)
	{
		if (ecc_saved_editor.isinhtmlmode)
		{
			// to	html mode
			ecc_saved_editor.isinhtmlmode	=	false;
			try	{
				ecc_saved_editor.innerHTML = ecc_saved_editor.innerText;
				ecc_cur_toolbar.all("ecc_cmd_text_bold").disabled	=	false;
				ecc_cur_toolbar.all("ecc_cmd_text_italic").disabled	=	false;
			}	catch	(e)	{}
		}
		else 
		{
			// to	text mode
			ecc_saved_editor.isinhtmlmode	=	true;
			try	{
				ecc_saved_editor.innerText = ecc_saved_editor.innerHTML;
				ecc_cur_toolbar.all("ecc_cmd_text_bold").disabled	=	true;
				ecc_cur_toolbar.all("ecc_cmd_text_italic").disabled	=	true;
			}	catch	(e)	{}
		}
	}
	event.cancelBubble = true;
}	 

//--------------------------------------------------------------------
// Popup functions
//

var curr_popup_parentframe;
var drag_down_x;
var drag_down_y;
var drag_start_x;
var drag_start_y;
var dragging;

function ecc_bringFrameToTop()
{
  parentframes = window.parent.document.frames;
    for(i=0; i<parentframes.length; i++)
    {
      if(parentframes[i].name.indexOf('_ecc_ifrmPopup') > 0)
      {
        frame = window.parent.document.all(parentframes[i].name);
        frame.style.zIndex = 5;
      }
    }
    curr_popup_parentframe.style.zIndex = 10;
    return;
}

function dragstart()
{
    // Set the z-index for this box to be higher than the others, and reduce the others a lower value
    ecc_bringFrameToTop();
    
    caption.setCapture(true);
    drag_down_x = window.event.screenX;
    drag_down_y = window.event.screenY;
    drag_start_x = parseInt(curr_popup_parentframe.style.left);
    drag_start_y = parseInt(curr_popup_parentframe.style.top);
    dragging = true;
}
function drag()
{
    if (dragging)
    {
    curr_popup_parentframe.style.left = drag_start_x + window.event.screenX - drag_down_x;
    curr_popup_parentframe.style.top = drag_start_y + window.event.screenY - drag_down_y;
    }
}
function dragend()
{
    dragging = false;
    caption.releaseCapture();
}

/// CONTROL OF DHTML POPUP BOX IN DIV (NON IE BROWSERS)
var ecc_div_popuplocked = false;
var ecc_div_popup_md = false;
var ecc_div_popup_oX=0;
var ecc_div_popup_oY=0;

function ecc_div_showPopup(lock,left,top)
{
  ecc_div_popuplocked = lock;
  document.all.popUpHelpText.style.visibility = 'visible';
  return;

}

function ecc_div_hidePopup(enforce)
{
  if (!ecc_div_popuplocked || enforce)
  {
    document.all.popUpHelpText.style.visibility='hidden';
  }
  return;
}

function ecc_div_mouseOutPopup()
{
    dragging = false;
    ecc_div_popup_md = false;      
}    

function ecc_div_mouseDownPopup(caption)
{

  curr_popup_parentframe = caption.parentElement.parentElement.parentElement.parentElement;
  
  caption.setCapture(true);
  drag_down_x = window.event.screenX;
  drag_down_y = window.event.screenY;
  drag_start_x = parseInt(curr_popup_parentframe.style.left);
  drag_start_y = parseInt(curr_popup_parentframe.style.top);
  dragging = true;
	
	return;      
}

function ecc_div_mouseUpPopup(caption)
{
    dragging = false;
    caption.releaseCapture();
}

function ecc_div_mouseMovePopup(caption) 
{
    if (dragging)
    {
      curr_popup_parentframe.style.left = drag_start_x + window.event.screenX - drag_down_x;
      curr_popup_parentframe.style.top = drag_start_y + window.event.screenY - drag_down_y;
    }
	
}    

function ecc_div_closePopup(box)  
{
  thePopup = document.getElementById(box);  
  thePopup.style.visibility='hidden';
}

//--------------------------------------------------------------------
// TreeView functions
//

var treeSelected = null; //last treeNode clicked

//pre-load tree nodes images
var imgPlus = new Image();
imgPlus.src="/cms/client/treenodeplus.gif";
var imgMinus = new Image();
imgMinus.src="/cms/client/treenodeminus.gif";
var imgDot = new Image();
imgPlus.src="/cms/client/treenodedot.gif";


function findNode(el)
{
// Takes element and determines if it is a treeNode.
// If not, seeks a treeNode in its parents.
	while (el != null)
	{
		if (el.className == "treeNode")
		{
			break;
		}
		else
		{
			el = el.parentNode;
		}
	}
	return el;
}


function clickAnchor(el)
{
// handles click on a TOC link
//
	expandNode(el.parentNode);
	selectNode(el.parentNode);
	el.blur();
}


function selectNode(el)
{
// Un-selects currently selected node, if any, and selects the specified node
//
	if (treeSelected != null)
	{
		setSubNodeClass(treeSelected, 'A', 'treeUnselected');
	}
	setSubNodeClass(el, 'A', 'treeSelected');
	treeSelected = el;
}


function setSubNodeClass(el, nodeName, className)
{
// Sets the specified class name on el's first child that is a nodeName element
//
	var child;
	for (var i=0; i < el.childNodes.length; i++)
	{
		child = el.childNodes[i];
		if (child.nodeName == nodeName)
		{
			child.className = className;
			break;
		}
	}
}


function expandCollapse(el)
{
//	If source treeNode has child nodes, expand or collapse view of treeNode
//
	if (el == null)
		return;	//Do nothing if it isn't a treeNode
		
	var child;
	var imgEl;
	for(var i=0; i < el.childNodes.length; i++)
	{
		child = el.childNodes[i];
		if (child.src)
		{
			imgEl = child;
		}
		else if (child.className == "treeSubnodesHidden")
		{
			child.className = "treeSubnodes";
			imgEl.src = "/cms/client/treenodeminus.gif";
			break;
		}
		else if (child.className == "treeSubnodes")
		{
			child.className = "treeSubnodesHidden";
			imgEl.src = "/cms/client/treenodeplus.gif";
			break;
		}
	}
}


function expandNode(el)
{
//	If source treeNode has child nodes, expand it
//
	var child;
	var imgEl;
	for(var i=0; i < el.childNodes.length; i++)
	{
		child = el.childNodes[i];
		if (child.src)
		{
			imgEl = child;
		}
		if (child.className == "treeSubnodesHidden")
		{
			child.className = "treeSubnodes";
			imgEl.src = "/cms/client/treenodeminus.gif";
			break;
		}
	}
}

function findTree()
{
	var el = document.getElementById('tree');
	if (!el)
	    el = document.frames("contents").document.getElementById('tree');
	return el;
}

function syncTree(href)
{
// Selects and scrolls into view the node that references the specified URL
	var loc = new String();
	loc = href;
	if (loc.substring(0, 7) == 'file://')
	{
		loc = 'file:///' + loc.substring(7, loc.length);
		loc = loc.replace(/\\/g, '/');
	}
	
	var base = loc.substr(0, loc.lastIndexOf('/') + 1);
	
	var tocEl = findHref(findTree(), loc, base);
	if (tocEl != null)
	{
		selectAndShowNode(tocEl);
	}
}

function findHref(node, href, base)
{
	if (!node)
	  return;
// find the <a> element with the specified href value
	var el;
	var anchors = node.getElementsByTagName('A');
	for (var i = 0; i < anchors.length; i++)
	{
		el = anchors[i];
		var aref = new String();
		aref = el.getAttribute('href');
		
		if ((aref.substring(0, 7) != 'http://') 
			&& (aref.substring(0, 8) != 'https://')
			&& (aref.substring(0, 7) != 'file://'))
		{
			aref = base + aref;
		}
		
		if (aref == href)
		{
			return el;
		}
	}
	return null;
}

function selectAndShowNode(node)
{
// Selects and scrolls into view the specified node
//
	var el = findNode(node);
	if (el != null) 
	{
		selectNode(el);
		do 
		{
			expandNode(el);
			el = findNode(el.parentNode);
		} while ((el != null))  
		
		//vertical scroll element into view
		var windowTop;
		var windowBottom;
		var treeDiv = findTree();
		
		var ua = window.navigator.userAgent.toLowerCase();
		if ((i = ua.indexOf('msie')) != -1)
		{
			windowTop = node.offsetTop - treeDiv.scrollTop;
			windowBottom = treeDiv.clientHeight - windowTop - node.offsetHeight;
		}
		else if (ua.indexOf('gecko') != -1)
		{
			windowTop = node.offsetTop - treeDiv.offsetTop - treeDiv.scrollTop;
			windowBottom = treeDiv.clientHeight - windowTop - node.offsetHeight;
		}
		else 
		{
			return;
		}
		
		if (windowTop < 0)
		{
			treeDiv.scrollTop += windowTop - 18;
			return;
		}
		if (windowBottom < 0)
		{
			treeDiv.scrollTop -= windowBottom - 18;
			return;
		}
	}
}


function resizeTree()
{
	var treeDiv = document.getElementById('tree');
	//treeDiv.setAttribute('style', 'width: ' + document.body.offsetWidth + 'px; height: ' + (document.body.offsetHeight - 27) + 'px;');
	treeDiv.style.width = document.documentElement.offsetWidth;
	treeDiv.style.height = document.documentElement.offsetHeight;
}

//--------------------------------------------------------------------
// ImageSelector functions
//
function ecc_imageSelect(elementId, typeId)
{
  var selectorImg = document.getElementById(elementId);
  var selectorInp = document.getElementById(elementId + "_State");
  var url = window.showModalDialog("/cms/modules/images/list.aspx?typeid=" + typeId + "&dialog=1","","dialogWidth:624px;dialogHeight:580px;scroll:no;status:no;resizable:yes");
  if (url)
  {
    selectorImg.src = url;
    selectorInp.value = url;
  }
}

//--------------------------------------------------------------------
// AssetSelector functions
//
function ecc_assetSelect(elementId, typeId)
{
  var selectorInp = document.getElementById(elementId + "_State");
  var selectorLabel = document.getElementById(elementId);
  var url = window.showModalDialog("/cms/modules/assets/list.aspx?typeid=" + typeId + "&dialog=1","","dialogWidth:624px;dialogHeight:580px;scroll:no;status:no;resizable:yes");
  if (url)
  {
    selectorInp.value = url;
    selectorLabel.innerHTML = "[" + url + "]";
  }
}

//--------------------------------------------------------------------
// RollOver functions
//

function ecc_swap_img() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=0; n<args.length;
 n+=2) { elm=ecc_get_object_by_id(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function ecc_preload_imgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 var j=d.FP_imgs.length; for(var i=0; i<a.length; i++) { d.FP_imgs[j]=new Image; d.FP_imgs[j++].src=a[i]; }
}

function ecc_preload_imgs_array(a) {//v1.0
 var d=document; if(!d.FP_imgs) d.FP_imgs=new Array();
 var j=d.FP_imgs.length; for(var i=0; i<a.length; i++) { d.FP_imgs[j]=new Image; d.FP_imgs[j++].src=a[i]; }
}

function ecc_get_object_by_id(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=ecc_get_object_by_id(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=ecc_get_object_by_id(id,els[n]); if(el) return el; } }
 return null;
}
