// ***************************************************************************
//                          wmWindow.js  -  description
//                             -------------------
//    begin                : Fri Dec 9 2005
//    copyright            : (C) 2005 by Andrei Gavrila
//    email                : andrei.gavrila@gmail.com
// ***************************************************************************
//
// ***************************************************************************
// *                                                                         *
// *   This program is free software; you can redistribute it and/or modify  *
// *   it under the terms of the GNU General Public License as published by  *
// *   the Free Software Foundation; either version 2 of the License, or     *
// *   (at your option) any later version.                                   *
// *                                                                         *
// ***************************************************************************

var wmWindowIndex      = 0;

var wmWindowPageActive = 1;

var wmWindowMoveDrag   = Array();
var wmWindowResizeDrag = Array();

var wmWindowPage       = Array();

var wmWindowDefaultBackgroundColor = 'ffffff';
var wmWindowDefaultBorderWidth     = 1;
var wmWindowDefaultBorderColor     = 'ff0000';
var wmWindowDefaultPage            = wmWindowPageActive;

var wmWindowType            = Array();
var wmWindowBackgroundColor = Array();
var wmWindowBorderWidth     = Array();
var wmWindowBorderColor     = Array();
var wmWindowActiveHeader    = Array();

var wmWindowSaved = true;

var wmWindowBgColor = '';

function wmWindow(x, y, w, h, type, setup, content, headers)
{
	if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1) && (type == 'pictures' || type == 'movies')) {
		// Stupid fix for IE and pictures / movies boxes (can't identify the bug to fix it)
		//

		w = w + 1;
		h = h + 1;
	}

	// Create the elements
	//

	var wmWindowBox                   = document.createElement('div');
	wmWindowBox.id                    = 'wmWindow_' + wmWindowIndex;
	wmWindowBox.className             = 'wmWindow';

	var wmWindowSetup                 = document.createElement('div');
	wmWindowSetup.id                  = 'wmWindow_' + wmWindowIndex + '_Setup';
	wmWindowSetup.className           = 'wmWindowSetup';

	var wmWindowContent               = document.createElement('div');
	wmWindowContent.id                = 'wmWindow_' + wmWindowIndex + '_Content';
	wmWindowContent.className         = 'wmWindowContent';

	var wmWindowOnBackgroundBox       = document.createElement('div');
	wmWindowOnBackgroundBox.id        = 'wmWindow_' + wmWindowIndex + '_OnBackgroundBox';
	wmWindowOnBackgroundBox.className = 'wmWindowOnBackground';

	var wmWindowModeBox               = document.createElement('div');
	wmWindowModeBox.id                = 'wmWindow_' + wmWindowIndex + '_ModeBox';
	wmWindowModeBox.className         = 'wmWindowMode';

	var wmWindowCloseBox              = document.createElement('div');
	wmWindowCloseBox.id               = 'wmWindow_' + wmWindowIndex + '_CloseBox';
	wmWindowCloseBox.className        = 'wmWindowClose';

	var wmWindowMoveBox               = document.createElement('div');
	wmWindowMoveBox.id                = 'wmWindow_' + wmWindowIndex + '_MoveBox';
	wmWindowMoveBox.className         = 'wmWindowMove';

	var wmWindowResizeBox             = document.createElement('div');
	wmWindowResizeBox.id              = 'wmWindow_' + wmWindowIndex + '_ResizeBox';
	wmWindowResizeBox.className       = 'wmWindowResize';

/*
	if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1)) {
		// Create the Iframe and attach it to the editor page
		//

		var wmIframe                = document.createElement('iframe');
		wmIframe.id                 = 'wmIframe_' + wmWindowIndex;
		wmIframe.className          = 'wmIframe';
		wmIframe.src                = 'empty.html';

		wmIframe.style.left         = x + 'px';
		wmIframe.style.top          = y + 'px';

		wmIframe.style.width        = w + 'px';
		wmIframe.style.height       = h + 'px';

		// IE: This makes IE freeze until you move the mouse out of the A
		// (only then it displays the created window)
		//

		wmZIndex(wmIframe, wmZIndexIndex);
		wmZIndexIndex++;

		wmGetElementById(wmEditorId).appendChild(wmIframe);
	}
*/

	// Attach events
	//

	eval('wmAddEventListener(wmWindowOnBackgroundBox, "click", function () { wmWindowOnBackground(' + wmWindowIndex + '); }, false);');

	eval('wmAddEventListener(wmWindowModeBox, "click", function () { wmWindowMode(' + wmWindowIndex + '); }, false);');

	eval('wmAddEventListener(wmWindowCloseBox, "click", function () { wmWindowClose(' + wmWindowIndex + '); }, false);');

	eval('wmAddEventListener(wmWindowMoveBox, "mousedown", function () { wmWindowMoveStart(' + wmWindowIndex + ');}, false);');
	eval('wmAddEventListener(wmEditorId,      "mousemove", function () { wmWindowMove(' + wmWindowIndex + ');},      false);');
	eval('wmAddEventListener(wmEditorId,      "mouseup",   function () { wmWindowMoveStop(' + wmWindowIndex + ');},  false);');

	eval('wmAddEventListener(wmWindowResizeBox, "mousedown", function () { wmWindowResizeStart(' + wmWindowIndex + ');}, false);');
	eval('wmAddEventListener(wmEditorId,        "mousemove", function () { wmWindowResize(' + wmWindowIndex + ');},      false);');
	eval('wmAddEventListener(wmEditorId,        "mouseup",   function () { wmWindowResizeStop(' + wmWindowIndex + ');},  false);');

	eval('wmAddEventListener(wmWindowBox, "mousedown", function () { wmWindowSwitch(' + wmWindowIndex + '); }, false);');

	// Attach elements to window
	//

	wmWindowBox.appendChild(wmWindowSetup);
	wmWindowBox.appendChild(wmWindowContent);
	wmWindowBox.appendChild(wmWindowOnBackgroundBox);
	wmWindowBox.appendChild(wmWindowModeBox);
	wmWindowBox.appendChild(wmWindowCloseBox);
	wmWindowBox.appendChild(wmWindowMoveBox);
	wmWindowBox.appendChild(wmWindowResizeBox);

	// Move and resize the window
	//

	wmMove(wmWindowBox, x, y);
	wmResize(wmWindowBox, w, h);

	// Configure the default elements
	//

	wmWindowType[wmWindowIndex]            = type;

	wmWindowBox.style.backgroundColor      = '#' + wmWindowDefaultBackgroundColor;
//	wmWindowSetup.style.backgroundColor    = '#' + wmWindowDefaultBackgroundColor;
	wmWindowContent.style.backgroundColor  = '#' + wmWindowDefaultBackgroundColor;
	wmWindowBackgroundColor[wmWindowIndex] = wmWindowDefaultBackgroundColor;

	wmWindowBox.style.borderWidth          = wmWindowDefaultBorderWidth + 'px';
	wmWindowBorderWidth[wmWindowIndex]     = wmWindowDefaultBorderWidth;

	wmWindowBox.style.borderColor          = '#' + wmWindowDefaultBorderColor;
	wmWindowBorderColor[wmWindowIndex]     = wmWindowDefaultBorderColor;

	wmWindowResizeDrag[wmWindowIndex] = 0;
	wmWindowMoveDrag[wmWindowIndex]   = 0;

	// Select the page
	//

	wmWindowPage[wmWindowIndex] = wmWindowDefaultPage;

	// Move the controls at the bottom of the window
	//

	wmMove(wmWindowOnBackgroundBox,   w - 50, h - 10);
	wmMove(wmWindowModeBox,           w - 40, h - 10);
	wmMove(wmWindowCloseBox,          w - 30, h - 10);
	wmMove(wmWindowMoveBox,           w - 20, h - 10);
	wmMove(wmWindowResizeBox,         w - 10, h - 10);

	// Build the setup and the content
	//

	var setup0 = ' \
<div class="wmWindowSetupHeader" id="wmWindow_' + wmWindowIndex + '_SetupHeader"></div> \
<div class="wmWindowSetupItem" id="wmWindow_' + wmWindowIndex + '_SetupBox_1"> \
<b>' + wmText(1000) + '</b> \
<br /> \
<br /> \
<b>' + wmText(1001) + '</b> : ' + wmWindowIndex + ' - ' + wmWindowType[wmWindowIndex] + 'Box <br /> \
<br /> \
<b>' + wmText(1002) + '</b> <br /> \
#<input type="text" id="wmWindow_' + wmWindowIndex + '_Setup0" class="wmSetupInputColor" size="6" value="' + wmWindowBackgroundColor[wmWindowIndex] + '" onChange="javascript: wmWindowSetupBackgroundColor(' + wmWindowIndex + ', this.value);" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup0\', this, wmMessage(2000));" /> \
<a href="javascript: wmColorPicker(\'wmWindow_' + wmWindowIndex + '_Setup0\', mouse_x, mouse_y, \'wmWindowSetupBackgroundColor(' + wmWindowIndex + ', src);\');"><img border="0" width="15" height="15" src="images/colorpicker.gif" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup0_0\', this, wmMessage(10000));" /></a> \
<br /> \
<b>' + wmText(1003) + '</b> <br /> \
<input type="text" id="wmWindow_' + wmWindowIndex + '_Setup1" class="wmSetupInputWidth" size="2" value="' + wmWindowBorderWidth[wmWindowIndex] + '" onChange="javascript: wmWindowSetupBorderWidth(' + wmWindowIndex + ', this.value);" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup1\', this, wmMessage(2001));" /> ' + wmText(0) + ' \
<br /> \
<b>' + wmText(1004) + '</b> <br /> \
#<input type="text" id="wmWindow_' + wmWindowIndex + '_Setup2" class="wmSetupInputColor" size="6" value="' + wmWindowBorderColor[wmWindowIndex] + '" onChange="javascript: wmWindowSetupBorderColor(' + wmWindowIndex + ', this.value);" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup2\', this, wmMessage(2002));" /> \
<a href="javascript: wmColorPicker(\'wmWindow_' + wmWindowIndex + '_Setup2\', mouse_x, mouse_y, \'wmWindowSetupBorderColor(' + wmWindowIndex + ', src);\');"><img border="0" width="15" height="15" src="images/colorpicker.gif" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup2_0\', this, wmMessage(10000));" /></a> \
<br /> \
<b>' + wmText(1005) + '</b> <br /> \
<select class="wmSetupSelect" onChange="javascript: wmWindowSetupPage(' + wmWindowIndex + ', this.value);" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup3\', this, wmMessage(2003));"> \
';

	switch(type) {
		case 'simple':
		case 'text':
		case 'image':
		case 'pictures':
		case 'movies':
		case 'menu':
			setup0 += ' \
  <option value="0"' + (wmWindowPageActive == 0 ? 'selected' : '') + '>' + wmText(200) + '</option> \
  <option value="1"' + (wmWindowPageActive == 1 ? 'selected' : '') + '>' + wmText(211) + '</option> \
  <option value="2"' + (wmWindowPageActive == 2 ? 'selected' : '') + '>' + wmText(212) + '</option> \
  <option value="3"' + (wmWindowPageActive == 3 ? 'selected' : '') + '>' + wmText(213) + '</option> \
  <option value="4"' + (wmWindowPageActive == 4 ? 'selected' : '') + '>' + wmText(214) + '</option> \
';
		break;

		case 'navigationNext':
		case 'navigationPictures':
		case 'navigationMovies':
			setup0 += ' \
  <option value="0"' + (wmWindowPageActive == 0 ? 'selected' : '') + '>' + wmText(201) + '</option> \
  <option value="2"' + (wmWindowPageActive == 2 ? 'selected' : '') + '>' + wmText(212) + '</option> \
  <option value="3"' + (wmWindowPageActive == 3 ? 'selected' : '') + '>' + wmText(213) + '</option> \
  <option value="4"' + (wmWindowPageActive == 4 ? 'selected' : '') + '>' + wmText(214) + '</option> \
';
		break;

		case 'photoSet':
			setup0 += ' \
  <option value="3"' + (wmWindowPageActive == 3 ? 'selected' : '') + '>' + wmText(213) + '</option> \
';
		break;

		case 'movieSet':
			setup0 += ' \
  <option value="4"' + (wmWindowPageActive == 4 ? 'selected' : '') + '>' + wmText(214) + '</option> \
';
		break;
	}

	setup0 += ' \
</select> \
<br /> \
<b>' + wmText(1006) + '</b> <br /> \
<input readonly id="wmWindow_' + wmWindowIndex + '_Setup4" class="wmSetupInputWidth" size="2" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup4\', this, wmMessage(2004));"/> ' + wmText(0) + ' \
<br /> \
<b>' + wmText(1007) + '</b> <br /> \
<input readonly id="wmWindow_' + wmWindowIndex + '_Setup5" class="wmSetupInputWidth" size="2" onMouseMove = "javascript: wmToolTipQuick(\'wmWindow_' + wmWindowIndex + '_Setup5\', this, wmMessage(2005));"/> ' + wmText(0) + ' \
<br /> \
</div> \
' + setup;
	wmWindowSetup.innerHTML = setup0;

	wmMove(wmWindowSetup, 0, 0);
	wmResize(wmWindowSetup, w, h);

	wmHide(wmWindowSetup);

	wmWindowContent.innerHTML = content;

	wmMove(wmWindowContent, 0, 0);
	wmResize(wmWindowContent, w, h);

	// Create the tooltips
	//

	wmToolTip(wmWindowOnBackgroundBox,   wmMessage(2104));
	wmToolTip(wmWindowModeBox,           wmMessage(2100));
	wmToolTip(wmWindowCloseBox,          wmMessage(2101));
	wmToolTip(wmWindowMoveBox,           wmMessage(2102));
	wmToolTip(wmWindowResizeBox,         wmMessage(2103));

	// Attach the window to the page and display it
	//

	wmGetElementById(wmEditorId).appendChild(wmWindowBox);

	// Fill in the details
	//

	wmGetElementById('wmWindow_' + wmWindowIndex + '_Setup4').value = w;
	wmGetElementById('wmWindow_' + wmWindowIndex + '_Setup5').value = h;

	// Setup the headers
	//

	headers[0] = 'wmWindow_' + wmWindowIndex + '_SetupBox_1';

	var content_header = '';

	for (i = 0; i < headers.length; i++) {
		content_header += '<div class="wmWindowSetupHeaderItem"><a href="javascript: wmWindowChangePart(' + wmWindowIndex + ', \'' + headers[i] + '\');">' + wmText(1008) + (i + 1) + '</a></div>';

		// Hide them. IE requires this or it will display them all
		//

		wmHide(headers[i]);
	}

	content_header += '<div class="clearer"></div>';

	wmGetElementById('wmWindow_' + wmWindowIndex + '_SetupHeader').innerHTML = content_header;

	wmWindowActiveHeader[wmWindowIndex] = headers[0];
	wmWindowChangePart(wmWindowIndex, wmWindowActiveHeader[wmWindowIndex]);

	// Display the elements
	//

	wmZIndex(wmWindowBox, wmZIndexIndex);
	wmZIndexIndex++;

	wmZIndex(wmWindowSetup, wmZIndexIndex);
	wmZIndexIndex++;
	wmZIndex(wmWindowContent, wmZIndexIndex);
	wmZIndexIndex++;

	wmZIndex(wmWindowOnBackgroundBox, wmZIndexIndex);
	wmZIndexIndex++;
	wmZIndex(wmWindowModeBox, wmZIndexIndex);
	wmZIndexIndex++;
	wmZIndex(wmWindowCloseBox, wmZIndexIndex);
	wmZIndexIndex++;
	wmZIndex(wmWindowMoveBox, wmZIndexIndex);
	wmZIndexIndex++;
	wmZIndex(wmWindowResizeBox, wmZIndexIndex);
	wmZIndexIndex++;

	wmShow(wmWindowBox);

	wmWindowIndex++;

	// Refresh elements number
	//

	var oldIndex = wmWindowPageSwitch(wmWindowPageActive);

	// Hide the other configs on IE
	//

	if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1)) {
		// IE Only: Close other configs
		//

		if (oldIndex != '' && oldIndex != wmWindowIndex) {
			wmMove('wmWindow_' + oldIndex + '_Content', 0, 0);
			wmHide('wmWindow_' + oldIndex + '_Setup');
		}
	}

	wmWindowSaved = false;
}

function wmWindowOnBackground(index)
{
	var x = 999999;
	var i = 0;

	wmToolTipHideAll();

	for (i = 0; i < wmWindowIndex; i++)
		if (x > parseInt(wmGetElementById('wmWindow_' + i).style.zIndex))
			x = parseInt(wmGetElementById('wmWindow_' + i).style.zIndex);

	for (i = 0; i < wmWindowIndex; i++)
		wmZIndex('wmWindow_' + i, parseInt(wmGetElementById('wmWindow_' + i).style.zIndex) + 1);

	wmZIndexIndex++;

	wmZIndex('wmWindow_' + index, x);
}

function wmWindowMode(index)
{
	if (wmGetY('wmWindow_' + index + '_Content') != 0 && wmGetY('wmWindow_' + index + '_Content') != wmGetH('wmWindow_' + index + '_Setup'))
		return 0;

	wmToolTipHideAll();

	if (wmGetY('wmWindow_' + index + '_Content') == 0) {
		wmShow('wmWindow_' + index + '_Setup');

		wmSlide('wmWindow_' + index + '_Content', 0, wmGetH('wmWindow_' + index + '_Setup'), 5);
	}
	else {
		wmSlide('wmWindow_' + index + '_Content', 0, 0, 5, 'wmWindow_' + index + '_Setup');
	}
}

function wmWindowClose(index)
{
	wmToolTipHideAll();

	if (confirm(wmMessage(1500))) {
		wmHide('wmWindow_' + index);

		wmWindowPage[index] = -1;

		wmWindowSaved = false;
	}

	// Refresh elements number
	//

	wmWindowPageSwitch(wmWindowPageActive);
}

function wmWindowMoveStart(index)
{
	mouse_oldX = mouse_x;
	mouse_oldY = mouse_y;

	wmWindowMoveDrag[index] = 1;

	// Uncomment this to enable move window on top on move
	//

//	wmZIndex('wmWindow_' + index, wmZIndexIndex);
//	wmZIndexIndex++;

	// Make window transparent while moving
	//

	wmWindowBgColor = wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor;

	wmGetElementById('wmWindow_' + index).style.backgroundColor              = 'transparent';
	wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = 'transparent';
}

function wmWindowMove(index)
{
	if (wmWindowMoveDrag[index]) {
		wmToolTipHideAll();

		var x = wmGetX('wmWindow_' + index) + (mouse_x - mouse_oldX);
		var y = wmGetY('wmWindow_' + index) + (mouse_y - mouse_oldY);

		if (x < wmEditorX)
			x = wmEditorX;
		if (x > wmEditorWidth - wmGetW('wmWindow_' + index) + wmEditorX)
			x = wmEditorWidth - wmGetW('wmWindow_' + index) + wmEditorX;

		if (y < wmEditorY)
			y = wmEditorY;
		if (y > wmEditorHeight - wmGetH('wmWindow_' + index) + wmEditorY)
			y = wmEditorHeight - wmGetH('wmWindow_' + index) + wmEditorY;

		wmMove('wmWindow_' + index, x, y);

		mouse_oldX = mouse_x;
		mouse_oldY = mouse_y;

		wmWindowSaved = false;
	}
}

function wmWindowMoveStop(index)
{
	wmWindowMoveDrag[index] = 0;

	// Restore window settings
	//

	wmGetElementById('wmWindow_' + index).style.backgroundColor              = wmWindowBgColor;
	wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = wmWindowBgColor;
}

function wmWindowResizeStart(index)
{
	mouse_oldX = mouse_x;
	mouse_oldY = mouse_y;

	wmWindowResizeDrag[index] = 1;

	// Uncomment this to enable move window on top on move
	//

//	wmZIndex('wmWindow_' + index, wmZIndexIndex);
//	wmZIndexIndex++;

	// Make window transparent while moving
	//

	wmWindowBgColor = wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor;

	wmGetElementById('wmWindow_' + index).style.backgroundColor              = 'transparent';
	wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = 'transparent';
}

function wmWindowResize(index)
{
	if (wmWindowResizeDrag[index]) {
		wmToolTipHideAll();

		var w = wmGetW('wmWindow_' + index) + (mouse_x - mouse_oldX);
		var h = wmGetH('wmWindow_' + index) + (mouse_y - mouse_oldY);

		if (w < 0)
			w = 0;
		if (w > wmEditorWidth - wmGetX('wmWindow_' + index) + wmEditorX)
			w = wmEditorWidth - wmGetX('wmWindow_' + index) + wmEditorX;

		if (h < 0)
			h = 0;
		if (h > wmEditorHeight - wmGetY('wmWindow_' + index) + wmEditorY)
			h = wmEditorHeight - wmGetY('wmWindow_' + index) + wmEditorY;

		wmResize('wmWindow_' + index, w, h);

		// Move the controls at the bottom of the window
		//

		wmMove('wmWindow_' + index + '_OnBackgroundBox',   wmGetW('wmWindow_' + index) - 50, wmGetH('wmWindow_' + index) - 10);
		wmMove('wmWindow_' + index + '_ModeBox',           wmGetW('wmWindow_' + index) - 40, wmGetH('wmWindow_' + index) - 10);
		wmMove('wmWindow_' + index + '_CloseBox',          wmGetW('wmWindow_' + index) - 30, wmGetH('wmWindow_' + index) - 10);
		wmMove('wmWindow_' + index + '_MoveBox',           wmGetW('wmWindow_' + index) - 20, wmGetH('wmWindow_' + index) - 10);
		wmMove('wmWindow_' + index + '_ResizeBox',         wmGetW('wmWindow_' + index) - 10, wmGetH('wmWindow_' + index) - 10);

		wmResize('wmWindow_' + index + '_Setup', wmGetW('wmWindow_' + index), wmGetH('wmWindow_' + index));
		wmResize('wmWindow_' + index + '_Content', wmGetW('wmWindow_' + index), wmGetH('wmWindow_' + index));

		// If the edit mode is on, hide the content
		//

		if (wmGetY('wmWindow_' + index + '_Content'))
			wmMove('wmWindow_' + index + '_Content', 0, wmGetH('wmWindow_' + index));

		// Fill in the details
		//

		wmGetElementById('wmWindow_' + index + '_Setup4').value = w;
		wmGetElementById('wmWindow_' + index + '_Setup5').value = h;

		mouse_oldX = mouse_x;
		mouse_oldY = mouse_y;

		wmWindowSaved = false;
	}
}

function wmWindowResizeStop(index)
{
	wmWindowResizeDrag[index] = 0;

	// Restore window settings
	//

	wmGetElementById('wmWindow_' + index).style.backgroundColor              = wmWindowBgColor;
	wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = wmWindowBgColor;
}

function wmWindowSwitch(index)
{
	// Don't switch if the window is being moved or resized
	// Comment it to have focus on resize / move

	if ((wmWindowResizeDrag[index] != 0) || (wmWindowMoveDrag[index]) != 0)
		return;

	if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Opera') == -1)) {
		// IE Only: Close other configs
		//

		var oldIndex = wmGetElementById(wmWindowListId).options[wmGetElementById(wmWindowListId).selectedIndex].value;

		if (oldIndex != index) {
			wmMove('wmWindow_' + oldIndex + '_Content', 0, 0);
			wmHide('wmWindow_' + oldIndex + '_Setup');
		}
	}

	// Put it on top and the tool tip over it
	//

	wmZIndex('wmWindow_' + index, wmZIndexIndex);
	wmZIndexIndex++;

	for (i = 0; i < wmToolTipIndex; i++)
		if (wmIsShown('wmToolTip_' + i)) {
			wmZIndex('wmToolTip_' + i, wmZIndexIndex);
			wmZIndexIndex++;
		}

	wmWindowPageSwitch(wmWindowPageActive);

	wmWindowSaved = false;
}

function wmWindowSetupBackgroundColor(index, src)
{
	if (src != 'none') {
		wmGetElementById('wmWindow_' + index).style.backgroundColor              = '#' + src;
//		wmGetElementById('wmWindow_' + index + '_Setup').style.backgroundColor   = '#' + src;
		wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = '#' + src;
	}
	else {
		wmGetElementById('wmWindow_' + index).style.backgroundColor              = 'transparent';
//		wmGetElementById('wmWindow_' + index + '_Setup').style.backgroundColor   = 'transparent';
		wmGetElementById('wmWindow_' + index + '_Content').style.backgroundColor = 'transparent';
	}

	wmWindowBackgroundColor[index] = src;

	wmWindowDefaultBackgroundColor = src;

	wmWindowSaved = false;
}

function wmWindowSetupBorderWidth(index, src)
{
	wmGetElementById('wmWindow_' + index).style.borderWidth = src + 'px';

	wmWindowBorderWidth[index] = src;

	wmMove('wmWindow_' + index + '_OnBackgroundBox',   wmGetW('wmWindow_' + index) - 50, wmGetH('wmWindow_' + index) - 10);
	wmMove('wmWindow_' + index + '_ModeBox',           wmGetW('wmWindow_' + index) - 40, wmGetH('wmWindow_' + index) - 10);
	wmMove('wmWindow_' + index + '_CloseBox',          wmGetW('wmWindow_' + index) - 30, wmGetH('wmWindow_' + index) - 10);
	wmMove('wmWindow_' + index + '_MoveBox',           wmGetW('wmWindow_' + index) - 20, wmGetH('wmWindow_' + index) - 10);
	wmMove('wmWindow_' + index + '_ResizeBox',         wmGetW('wmWindow_' + index) - 10, wmGetH('wmWindow_' + index) - 10);

	wmWindowDefaultBorderWidth = src;

	wmWindowSaved = false;
}

function wmWindowSetupBorderColor(index, src)
{
	wmGetElementById('wmWindow_' + index).style.borderColor = '#' + src;

	wmWindowBorderColor[index] = src;

	wmWindowDefaultBorderColor = src;

	wmWindowSaved = false;
}

function wmWindowSetupPage(index, src)
{
	wmWindowPage[index] = src;

	wmWindowPageSwitch(src);

	if (src == 0)
		wmWindowDefaultPage = 0;

	wmWindowSaved = false;
}

function wmWindowGlobalBackgroundColor(src)
{
	for (i = 0; i < wmWindowIndex; i++) {
		wmWindowSetupBackgroundColor(i, src);

		wmGetElementById('wmWindow_' + i + '_Setup0').value = src;
	}

	if (!wmWindowIndex)
		wmWindowDefaultBackgroundColor = src;

	wmWindowSaved = false;
}

function wmWindowGlobalBorderWidth(src)
{
	for (i = 0; i < wmWindowIndex; i++) {
		wmWindowSetupBorderWidth(i, src);

		wmGetElementById('wmWindow_' + i + '_Setup1').value = src;
	}

	if (!wmWindowIndex)
		wmWindowDefaultBorderWidth = src;

	wmWindowSaved = false;
}

function wmWindowGlobalBorderColor(src)
{
	for (i = 0; i < wmWindowIndex; i++) {
		wmWindowSetupBorderColor(i, src);

		wmGetElementById('wmWindow_' + i + '_Setup2').value = src;
	}

	if (!wmWindowIndex)
		wmWindowDefaultBorderColor = src;

	wmWindowSaved = false;
}

function wmWindowPageSwitch(page)
{
	var oldIndex = wmGetElementById(wmWindowListId).value;

	page = parseInt(page);

	// Avoid doing anything if we're atempting to switch to page 0
	//

	if (page == 0)
		return;

	var i         = 0;
	var windows   = 0;

	// Remove all items from the list
	//

	for (i = wmGetElementById(wmWindowListId).options.length - 1; i >= 0; i--) {
		wmGetElementById(wmWindowListId).options[i] = null;
	}

	// Get the selected item (highest zIndex)
	//

	var highZ = 0;

	for (i = 0; i < wmWindowIndex; i++) {
		if ((wmWindowPage[i] == page) ||
		(wmWindowType[i] == 'simple' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'text' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'image' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'pictures' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'movies' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'menu' && wmWindowPage[i] == 0) ||
		(wmWindowType[i] == 'navigationNext' && wmWindowPage[i] == 0 && (page == 2 || page == 3 || page == 4)) ||
		(wmWindowType[i] == 'navigationPictures' && wmWindowPage[i] == 0 && (page == 2 || page == 3 || page == 4)) ||
		(wmWindowType[i] == 'navigationMovies' && wmWindowPage[i] == 0 && (page == 2 || page == 3 || page == 4)) ||
		(wmWindowType[i] == 'photoSet' && wmWindowPage[i] == 0 && (page == 3)) ||
		(wmWindowType[i] == 'movieSet' && wmWindowPage[i] == 0 && (page == 4))
		) {
			wmShow('wmWindow_' + i);

			if (wmGetElementById('wmWindow_' + i).style.zIndex > highZ) {
				highZ = wmGetElementById('wmWindow_' + i).style.zIndex;
			}

			windows++;

			// Add to window list
			//

			var x       = document.createElement('option');

			x.id        = 'wmWindowListItem_' + i;
			x.value     = i;
			x.text      = i + ' - ' + wmWindowType[i] + 'Box';

			wmGetElementById(wmWindowListId).options.add(x);
		}
		else {
			wmHide('wmWindow_' + i);
		}
	}

	// Set the top most window as selected
	//

	for (i = 0; i < wmGetElementById(wmWindowListId).options.length; i++) {
		if (wmGetElementById('wmWindow_' + wmGetElementById(wmWindowListId).options[i].value).style.zIndex == highZ)
			wmGetElementById(wmWindowListId).selectedIndex = i;
	}

	wmWindowPageActive  = page;
	wmWindowDefaultPage = wmWindowPageActive;

	var elements = ' [ ' + windows + ' ' + wmText(1009) + ' ] ';

	switch (page) {
		case 1:
			wmGetElementById(wmPageId).innerHTML = '<b>' + wmText(211) + '</b>' + elements;
		break;

		case 2:
			wmGetElementById(wmPageId).innerHTML = '<b>' + wmText(212) + '</b>' + elements;
		break;

		case 3:
			wmGetElementById(wmPageId).innerHTML = '<b>' + wmText(213) + '</b>' + elements;
		break;

		case 4:
			wmGetElementById(wmPageId).innerHTML = '<b>' + wmText(214) + '</b>' + elements;
		break;
	}

	return oldIndex;
}

function wmWindowSave(exit)
{
	var content = new String();

	wmWindowSaved = true;

	wmGetElementById(wmSaveStringId).innerHTML = wmMessage(1601);

	wmGetElementById(wmSaveId).style.visibility = 'visible';

	wmZIndex(wmSaveId, wmZIndexIndex);
	wmZIndexIndex++;

	content = ' \
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> \
\
<html xmlns="http://www.w3.org/1999/xhtml"> \
<head> \
<title>Saving settings ...</title> \
</head> \
<body> \
<form name="save_form" id="save_form" action="save.php?site=' + wmSiteID + '&language=' + wmLanguage + '" method="POST"> \
<input type="hidden" name="_browser" value="' + escape(navigator.userAgent) + '" /> \
<input type="hidden" name="_backgroundColor" value="' + wmBackgroundColor + '" /> \
 \
';

	var i = 0;

	for (i = 0; i < wmWindowIndex; i++) {
		if (wmWindowPage[i] != -1) {
			// Add global parameters
			//

			zIndex = parseInt(wmGetElementById('wmWindow_' + i).style.zIndex);

			x = wmGetX('wmWindow_' + i);
			y = wmGetY('wmWindow_' + i);

			w = wmGetW('wmWindow_' + i);
			h = wmGetH('wmWindow_' + i);

			content += '<input type="hidden" name="box_' + i + '_type" value="' + escape(wmWindowType[i]) + '" />';

			content += '<input type="hidden" name="box_' + i + '_page" value="' + escape(wmWindowPage[i]) + '" />';

			content += '<input type="hidden" name="box_' + i + '_zIndex" value="' + escape(zIndex) + '" />';

			content += '<input type="hidden" name="box_' + i + '_x" value="' + escape(x) + '" />';
			content += '<input type="hidden" name="box_' + i + '_y" value="' + escape(y) + '" />';

			content += '<input type="hidden" name="box_' + i + '_w" value="' + escape(w) + '" />';
			content += '<input type="hidden" name="box_' + i + '_h" value="' + escape(h) + '" />';

			content += '<input type="hidden" name="box_' + i + '_backgroundColor" value="' + escape(wmWindowBackgroundColor[i]) + '" />';
			content += '<input type="hidden" name="box_' + i + '_borderWidth" value="' + escape(wmWindowBorderWidth[i]) + '" />';
			content += '<input type="hidden" name="box_' + i + '_borderColor" value="' + escape(wmWindowBorderColor[i]) + '" />';

			// Add type specific parameters
			//

			switch (wmWindowType[i]) {
				case 'simple':
					content += boxSimpleSave(i);
				break;

				case 'text':
					content += boxTextSave(i);
				break;

				case 'image':
					content += boxImageSave(i);
				break;

				case 'pictures':
					content += boxPicturesSave(i);
				break;

				case 'movies':
					content += boxMoviesSave(i);
				break;

				case 'menu':
					content += boxMenuSave(i);
				break;

				case 'navigationNext':
					content += boxNavigationNextSave(i);
				break;

				case 'navigationPictures':
					content += boxNavigationPicturesSave(i);
				break;

				case 'navigationMovies':
					content += boxNavigationMoviesSave(i);
				break;

				case 'photoSet':
					content += boxPhotoSetSave(i);
				break;

				case 'movieSet':
					content += boxMovieSetSave(i);
				break;
			}
		}
	}

	content += ' \
</form> \
<script language="JavaScript1.3">document.getElementById("save_form").submit();</script> \
</body> \
</html> \
';

	save_frame.document.open();
	save_frame.document.write(content);
	save_frame.document.close();

	if (exit) {
		window.close();
	}
}

function wmWindowReload()
{
	if (!wmWindowSaved) {
		if (confirm(wmMessage(1501))) {
			document.location = 'load.php?site=' + wmSiteID + '&language=' + wmLanguage;
		}
	}
	else {
		document.location = 'load.php?site=' + wmSiteID + '&language=' + wmLanguage;
	}
}

function wmWindowStatusCheck()
{
	var status = '';

	if (!wmWindowSaved) {
		status = wmMessage(1301);
	}
	else {
		status = wmMessage(1300);
	}

	wmGetElementById(wmStatusId).innerHTML = status;

	setTimeout('wmWindowStatusCheck()', 500);
}

function wmWindowAtExit()
{
	// Quite pointless, the browser doesn't allow me to cancel the quit request.
	//

	if (!wmWindowSaved) {
		if (confirm(wmMessage(1502)))
			wmWindowSave(true);

			// Return false (cancel the quit request) and wait to the save iframe to close the window
			//

			return false;
	}

	return false;
}

function wmWindowChangePart(index, part)
{
	wmHide(wmWindowActiveHeader[index]);

	wmWindowActiveHeader[index] = part;

	wmShow(wmWindowActiveHeader[index]);

	wmZIndex(part, wmZIndexIndex);
	wmZIndexIndex++;
}

