/* 
 * Drag & Drop functionality sourced from BranJar.com
 * It works with Firefox and MSIE (the old code was ie-only)
 * http://www.brainjar.com/dhtml/drag/
 * See http://www.brainjar.com/dhtml/drag/demo.html for a demonstration
 *
 * I have updated the code in places to store position in cookies
 * where this is done it is tagged with '--dajw--' to start and --/dajw--
 * to terminate. dajw = Dominic Winsor
 *
 * usage: 
 *  drag self:          onmousedown="dragStart(event)"
 *  drag named element: onmousedown="dragStart(event, 'boxB')"
 *
 * The remainder of this file is sourced from code from
 *  Dominic Winsor at get2dom.com
 */




// BEGIN brainjar.com code -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// --dajw--: store position globally
var dragX, dragY;
// --/dajw--

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
  
  // --dajw--: copy pos to global var
  dragX = dragObj.elNode.style.left;
  dragY = dragObj.elNode.style.top;
  // --/dajw--
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }

  // --dajw--: store position in cookie
  createCookie(cookieNamePosition,dragX+","+dragY,cookieExpiryDays);
  // --/dajw--
}


// END brainjar.com code -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



// BEGIN get2dom.com code -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 * Added below by Dominic Winsor to support advanced edit console
 *  1: visibility for maximise & minimise function
 *  2: draggable div now sticks where you left on subsequent pages (JS cookies)
 *  3: draggable div is minimisable, this is also preserved on subsequent pages (JS Cookies)
 *  4: draggable div 'tab' alerts user to position (easy to lose if minimised and positioned out of the way)
 */
var cookieNamePosition   = "McmsEditConsolePosition";
var cookieNameVisibility = "McmsEditConsoleVisibility";
var cookieExpiryDays = 365;
var consoleId = "editConsole";
var consoleTitleId = "consoleTitle";
var defaultPosition = "775,10";


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* [min/max]imise a div, also toggle an icon to show state
 * provides visibility to function previously hidden by being fired onDblClick
*/
function toggleDisplay( el, ico )
{
	fadeElement = el;
	el  = document.getElementById(el);
	ico = document.getElementById(ico);
	el.style.display = (el.style.display=="block") ? "none" : "block";
	// http://domain.com/Internet/furniture/icons/shortcut-maximise.gif
	ico.src = (ico.src.indexOf('maximise.gif')!=-1) ? ico.src.replace('maximise.gif','minimise.gif') : ico.src.replace('minimise.gif','maximise.gif');
	
	// set cookie to store visibility for next time
    createCookie(cookieNameVisibility,el.id+"|"+el.style.display,cookieExpiryDays);
    
}



// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* JS Cookie code from QuirksMode.org. Very good, why rewrite?
 * http://www.quirksmode.org/js/cookies.html
 */
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* reposition the whole edit console according to where the user last left it
 * Dominic Winsor
 */
windowOnloadPreserve = window.onload;
window.onload = function()
{

	// ensure any previously defined window.onload() functions are still fired
	// (append, rather than override)
	if (windowOnloadPreserve) windowOnloadPreserve();

	var el  = document.getElementById(consoleId);
	var ico = document.getElementById('iconMaximiseMinimise');	
	
	// position the edit console
	if (el)
	{
		var cookiePositionData = readCookie(cookieNamePosition);
		if (!cookiePositionData) cookiePositionData = defaultPosition;
		var pos = cookiePositionData.split(',');
		if (pos)
		{	
			el.style.left = pos[0];
			el.style.top = pos[1];
		}
	}
	
	// set the edit console visibility
	var cookieVisibilityData = readCookie(cookieNameVisibility);
	var cookieVisibilityTargetObj;
	if (cookieVisibilityData) 
	{
		cookieVisibilityData = cookieVisibilityData.split("|");
		if (document.getElementById(cookieVisibilityData[0]) && cookieVisibilityData[1]!="block") document.getElementById(cookieVisibilityData[0]).style.display = 'none';
	}
	
	// position the icon top right of the 'drag-tab'
	if (ico)
	{
		//if (ico.parentElement) ico.style.left = (ico.parentElement.parentElement.clientWidth-ico.width)+'px';
		ico.style.left = (document.getElementById(consoleTitleId).clientWidth-ico.width)+'px';
		ico.style.position = 'absolute';
		ico.style.top = 0+'px';
	}
	//window.setInterval("alertPosition()",alertInterval);
	//window.setInterval("fadeIn()",10);
}



// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* a subtle way to alert the user to the position of the editConsole
 * Dominic Winsor
*/
var fadePercent = 0;
function fadeIn()
{
	fadePercent=fadePercent+10;
	if (fadePercent<=90)
	{
		var el  = document.getElementById(consoleId);
		// FIX_ME: this is MSIE only. Test for UA, then supply opacity or -moz-opacity as appropriate
		el.style.filter = 'progid:DXImageTransform.Microsoft.Alpha( opacity='+fadePercent+' )';
	}
	else 
	{
		window.clearInterval();
	}
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* a very obvious way to alert the user to the position of the editConsole
 * Dominic Winsor
*/
var alertInterval = 120; // milliseconds
var alertPositionCount = 3; // how many times to flash on/off
var alertColorBg = 'orange';  // background colour
var alertColorFg = '#000';  // foreground colour
function alertPosition()
{
	alertPositionCount--;
	if (alertPositionCount>0)
	{
		var el  = document.getElementById(consoleId);
		var tab = el.children[0];
		//el.style.border = (el.style.border=='' || el.style.border=='0px') ? '2px dotted red' : '0';
		//el.style.margin = (el.style.margin=='' || el.style.margin=='0px') ? '-2px 0 0 -2px' : '0';	
		tab.style.backgroundColor = (tab.style.backgroundColor=='') ? alertColorBg : '';
		tab.style.color = (tab.style.color=='') ? alertColorFg : '';
	}
	else 
	{
		window.clearInterval();
	}
}

// END get2dom.com code -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=