// Personal page editor

var BlockArray = new Array();
var SavedBlockArray = new Array();

var CurrentBlock = null;
var CurrentBlockIndex = -1;

var MouseOperation = null;
var MouseX = -1;
var MouseY = -1;


// HTML Elemenets
var block_0;
var current_block_select;
var block_content;
var content_property;
var display_title_property;
var block_title;
var title_property;
var title_font_name_property;
var title_font_size_property;
var title_italic_property;
var title_bold_property;
var title_margin_property;
var width_property;
var height_property;
var block_padding;
var padding_property;
var display_border_property;
var block_border;
var border_width_property;
var border_style_property;
var border_color_property;
var display_background_property;
var block_skin;
var skin_image_property;
var block_background_color;
var background_color_property;
var block_background_image;
var background_image_property;
var fixed_background_image_property;
var title_color_property;
var bReadOnly;
var language;
var display_bacground_block;

function AppendBlockDataFromDB()
{
	if(SavedBlockArray.length)
	{
		BlockArray.length = 0;
	}
	
	for(var I = 0; I < SavedBlockArray.length; I++)
	{
		BlockArray.push(SavedBlockArray[I]);
	}
}

function SetBlockPropertiesFromArray(Block)
{
	var BlockTitle;
	var BlockBody;
	var BlockIndex;
	
	var Arr;
	for(var I = 0; I < BlockArray.length; I++)
	{
		if(BlockArray[I]['id'] == Block.id)
		{
			Arr = BlockArray[I];
			BlockIndex = I;
			break;
		}
	}
	
	if(Block.id != 'block_0')
	{
		BlockTitle = Block.childNodes[0];
		BlockBody = Block.childNodes[1];
	}
	else
	{
		BlockTitle = null;
		BlockBody = Block;
	}
	
	if(Block.id != 'block_0')
	{
		if(Arr['display_title'])
		{
			BlockTitle.innerHTML = Arr['title'];
			
			BlockTitle.style.color = Arr['title_color'];

			if(typeof(current_block_select) != 'undefined')
			{
				for(var I = 0; I < current_block_select.options.length; I++)
				{
					if(current_block_select.options[I].value == Block.id)
					{
						current_block_select.options[I].text = Block.id + 
							(Arr['title'] != '' ? ' (' + Arr['title'] + ')' : '');
						break;
					}
				}
			}
			
			
			BlockTitle.style.marginBottom = String(Arr['title_margin']) + 'px';
			
			if(Arr['title_font_name'] == 'default')
			{
				BlockTitle.style.fontFamily = '';
			}
			else
			{
				BlockTitle.style.fontFamily = Arr['title_font_name'];
			}
			if(isNaN(Number(Arr['title_font_size'])))
			{
				BlockTitle.style.fontSize = Arr['title_font_size'];
			}
			else
			{
				BlockTitle.style.fontSize = Arr['title_font_size'] + 'px';
			}
			BlockTitle.style.fontStyle = Arr['title_italic'] ? 'italic' : 'normal';
			BlockTitle.style.fontWeight = Arr['title_bold'] ? 'bold' : 'normal';
			
			if(Arr['display_border'])
			{
				BlockTitle.style.border = Arr['border_width'] + ' ' +
					Arr['border_style'] + ' ' + Arr['border_color'];
			}
			else
			{
				BlockTitle.style.borderWidth = '0px';
			}
			
			if(Arr['display_background'])
			{
				BlockTitle.style.backgroundColor = Arr['background_color'];
			}
			else
			{
				BlockTitle.style.backgroundColor = '';
			}
			
			BlockTitle.style.display = 'block';
		}
		else
		{
			BlockTitle.style.display = 'none';
		}
		
		BlockBody.style.padding = String(Arr['padding']) + 'px';
		
		if(!bReadOnly)
		{
			AddCursorListener(Block);
		}
	}

	Block.style.width = String(Arr['width']) + 'px';
	Block.style.height = String(Arr['height']) + 'px';
	
	if(Arr['display_border'])
	{
		BlockBody.style.border = Arr['border_width'] + ' ' +
			Arr['border_style'] + ' ' + Arr['border_color'];
	}
	
	if(Arr['display_background'])
	{	
		BlockBody.style.backgroundColor = Arr['background_color'];
		
		if(Arr['background_image'] != '')
		{
			BlockBody.style.backgroundImage = 'url(' + Arr['background_image'] + ')';
			BlockBody.style.backgroundAttachment = Arr['fixed_background_image'] ? 
				'fixed' : 'scroll';
		}
	}
	
	if(Arr['content'])
	{
		if(Arr['type'] == 'text')
		{
			if(Arr['content'] != 0)
				BlockBody.innerHTML = Arr['content'];
			Arr['content'] = '';
		}
		else
		{
			BlockBody.firstChild.onload = function()
			{
				var frame = document.getElementById('content_frame_' + 
					String(BlockIndex));
 				var oDoc = (frame.contentWindow || frame.contentDocument);
				if (oDoc.document)
    				oDoc = oDoc.document;
				frame.frameBorder = 0;
				frame.style.width = String(oDoc.body.scrollWidth) + 'px';
				frame.style.height = String(oDoc.body.scrollHeight) + 'px';
			}
			BlockBody.firstChild.src = 
				'?res=root._help._service.get_content&content_id=' 
					+ Arr['content'] + '&block_id=' + (bReadOnly ? '0' :
					Block.id);
		}
	}
}

function AddCursorListener(block)
{
	if(block.addEventListener)
	{
		block.addEventListener('mousemove', SetCursor, false);
	}
	else if(block.attachEvent)
	{
		block.attachEvent('onmousemove', SetCursor);
	}
}

function SetCursor(event)
{
	var Block = false;
	
	if(event.srcElement)
	{
		Block = event.srcElement;
	}
	else if(event.currentTarget)
	{
		Block = event.currentTarget;
	}
	
	if(Block)
	{
		if(CurrentBlock.id == Block.id && !bReadOnly)
		{
			var eventX = event.screenX;
			var eventY = event.screenY;

			if(GetAbsoluteOffsetLeft(Block) + Block.offsetWidth - 8 < 
				eventX + document.body.scrollLeft && 
				GetAbsoluteOffsetTop(Block) + Block.offsetHeight - 8 < 
				eventY + document.body.scrollTop)
			{
				Block.style.cursor = "nw-resize";
			}
			else
			{
				Block.style.cursor = "auto";
			}
		}
	}
}

function ApplyPositionToBlockFromArray(Block, Arr)
{
	Block.style.position = 'absolute';
	Block.style.left = String(Arr['x']) + 'px';
	Block.style.top = String(Arr['y']) + 'px';
}

function DisplayBlockArray()
{
	SetBlockPropertiesFromArray(block_0, BlockArray[0]);

	for(var I = 1; I < BlockArray.length; I++)
	{
		var BlockTitle = document.createElement('div');
		var BlockBody = document.createElement('div');
		var Block = document.createElement('div');
		var ContentFrame;
		
		Block.id = 'block_' + String(I);
		BlockArray[I]['id'] = Block.id;
		
		margin_bottom = 0;
		if(navigator.appName == 'Microsoft Internet Explorer')
			margin_bottom = -4;
		
		switch(BlockArray[I]['type'])
		{
			case 'text':
				BlockTitle.className = 'default_block_title';
				BlockBody.className = 'default_block_body';
				Block.className = 'default_block';
				break;
			case 'media':
				BlockTitle.className = 'default_media_block_title';
				BlockBody.className = 'default_media_block_body';
				Block.className = 'default_media_block';
				BlockBody.innerHTML = 
					"<iframe scrolling=no frameborder=0 id=content_frame_" + 
					String(I) + " width=" + BlockArray[I]['iframe_width'] + 
					" height=" + BlockArray[I]['iframe_height'] +
					" allowtransparency=true class=content_frame style='" +
					"width:" + BlockArray[I]['iframe_width']+ "px; height:" + 
					BlockArray[I]['iframe_height'] + "px;margin-bottom:" +
					margin_bottom + "px;' ></iframe>";
				break;
		}
			
		Block.appendChild(BlockTitle);
		Block.appendChild(BlockBody);
		
		block_0.appendChild(Block);
		
		if(!bReadOnly)
			UpdateBlockList();
		
		SetBlockPropertiesFromArray(Block, BlockArray[I]);
		ApplyPositionToBlockFromArray(Block, BlockArray[I]);
	}

}

function UpdateBlockList()
{
	current_block_select.options.length = 0;
	for(var I = 0; I < BlockArray.length; I++)
	{
		var Option = document.createElement('option');
		Option.value = BlockArray[I].id; 
		var Name = '';
		if(BlockArray[I].title != '')
		{
			Name = BlockArray[I].title;
		}
		else if(I == 0)
		{
			Name = 'Main';
		}
		Option.text = BlockArray[I].id + (Name != '' ? ' (' + Name + ')' : '');
		try { current_block_select.add(Option, null); } 
		catch(e) { current_block_select.add(Option); }
	}
}

function SelectBlockInBlockList(Block)
{
	for(var I = 0; I < current_block_select.options.length; I++)
	{
		if(current_block_select.options[I].value == Block.id)
		{
			current_block_select.selectedIndex = I;
			break;
		}
	}
}

function OnBlockListSelectionChange()
{
	SelectBlock(document.getElementById(current_block_select.options[
		current_block_select.selectedIndex].value));
}

function InitEditor(ReadOnly)
{
	block_0 = document.getElementById('block_0');
	//block_0.childNodes.length = 0;
	bReadOnly = ReadOnly;
if(!ReadOnly)
{
    current_block_select = document.getElementById('current_block_select');
    block_content = document.getElementById('block_content');
    content_property = document.getElementById('content_property');
    display_title_property = document.getElementById('display_title_property');
    block_title = document.getElementById('block_title');
    title_property = document.getElementById('title_property');
    title_font_name_property = document.getElementById('title_font_name_property');
    title_font_size_property = document.getElementById('title_font_size_property');
    title_italic_property = document.getElementById('title_italic_property');
    title_bold_property = document.getElementById('title_bold_property');
    title_margin_property = document.getElementById('title_margin_property');
    width_property = document.getElementById('width_property');
    height_property = document.getElementById('height_property');
    block_padding = document.getElementById('block_padding');
    padding_property = document.getElementById('padding_property');
    display_border_property = document.getElementById('display_border_property');
    block_border = document.getElementById('block_border');
    border_width_property = document.getElementById('border_width_property');
    border_style_property = document.getElementById('border_style_property');
    border_color_property = document.getElementById('border_color_property');
    display_background_property = 
		document.getElementById('display_background_property');
    block_skin = document.getElementById('block_skin');
    skin_image_property = document.getElementById('skin_image_property');
    block_background_color = document.getElementById('block_background_color');
    background_color_property = document.getElementById('background_color_property');
    block_background_image = document.getElementById('block_background_image');
    background_image_property = document.getElementById('background_image_property');
    fixed_background_image_property = 
		document.getElementById('fixed_background_image_property');
	title_color_property = document.getElementById('title_color_property');
		
	personal_page_blocks = document.getElementById('personal_page_blocks');
	language = document.getElementById('text_editor_language').value;
	display_bacground_block = document.getElementById('display_bacground_block');
	
}

	var MainBlockData = Array();
	MainBlockData['id'] = 'block_0';
	MainBlockData['type'] = 'text';
	MainBlockData['content'] = 0;
	MainBlockData['display_title'] = 0;
	MainBlockData['title'] = '';
	MainBlockData['title_color'] = 0;
	MainBlockData['title_margin'] = 0;
	MainBlockData['title_font_name'] = '';
	MainBlockData['title_font_size'] = '';
	MainBlockData['title_italic'] = 0;
	MainBlockData['title_bold'] = 0;
	MainBlockData['x'] = 0;
	MainBlockData['y'] = 0;
	MainBlockData['width'] = 640;
	MainBlockData['height'] = 700;
	MainBlockData['padding'] = 0;
	MainBlockData['display_border'] = 0;
	MainBlockData['border_width'] = 'thin';
	MainBlockData['border_style'] = 'solid';
	MainBlockData['border_color'] = '#FFFFFF';
	MainBlockData['display_background'] = 0;
	MainBlockData['skin'] = '';
	MainBlockData['background_color'] = 'transparent';
	MainBlockData['background_image'] = '';
	MainBlockData['fixed_background_image'] = 0;
	
	BlockArray.push(MainBlockData);
	
	AppendBlockDataFromDB();
	
	if(!ReadOnly)
	{
		UpdateBlockList();
	}
	
	DisplayBlockArray();
	
	if(!ReadOnly)
	{
		SelectBlock(block_0);
	} 
}

function GetSelectValue(Select)
{
	return Select.options[Select.selectedIndex].value;
}

function SetSelectValue(Select,Value)
{
	for(var I = 0; I < Select.options.length; I++)
	{
		if(Select.options[I].value == Value)
		{
			Select.selectedIndex = I;
			return true;
		}
	}
	return false;
}

function FillPropertyForm()
{
	var Block = CurrentBlock;
	
	var Arr;
	for(var I = 0; I < BlockArray.length; I++)
	{
		if(BlockArray[I]['id'] == Block.id)
		{
			Arr = BlockArray[I];
			break;
		}
	}
	
	SetSelectValue(content_property, Arr['content']);
	
	display_title_property.checked = Arr['display_title'] ? true : false;
	block_title.style.display = Arr['display_title'] ? 'block' : 'none';
	title_property.value = Arr['title'];
	if(!Arr['title_color'] || Arr['title_color'] == 0)
	{
		Arr['title_color'] = '#000000';
	}
	title_color_property.style.backgroundColor = Arr['title_color'];
	title_margin_property.value = String(Arr['title_margin']);
	SetSelectValue(title_font_name_property, Arr['title_font_name']);
	SetSelectValue(title_font_size_property, Arr['title_font_size']);
	title_italic_property.checked = Arr['title_italic'];
	title_bold_property.checked = Arr['title_bold'];
	
	width_property.value = String(Arr['width']);
	height_property.value = String(Arr['height']);
	
	padding_property.value = String(Arr['padding']);
		
	display_border_property.checked = Arr['display_border'];
	block_border.style.display = Arr['display_border'] ? 'block' : 'none';
	SetSelectValue(border_width_property, Arr['border_width']);
	SetSelectValue(border_style_property, Arr['border_style']);
	border_color_property.style.backgroundColor = Arr['border_color'];
	
	display_background_property.checked = Arr['display_background'];
	block_background.style.display = Arr['display_background'] ? 'block' : 'none';
	background_color_property.style.backgroundColor = Arr['background_color'];
	SetSelectValue(background_image_property, Arr['background_image']);
	fixed_background_image_property.checked = Arr['fixed_background_image'];
	
	SetSelectValue(skin_image_property, Arr['skin']);

	display_title_property.disabled = Block.id == 'block_0';
	padding_property.disabled = Block.id == 'block_0';
	
	block_content.style.display = Arr['type'] == 'text' ? 'none' : 'block';
	block_padding.style.display = Arr['type'] == 'text' ? 'block' : 'none';
	block_skin.style.display = Arr['type'] == 'text' ? 'none' : 'block';
	block_background_color.style.display = Arr['type'] == 'text' ? 'block' : 'none';
	block_background_image.style.display = Arr['type'] == 'text' ? 'block' : 'none';
	display_bacground_block.style.display = Arr['type'] == 'text' ? 'block' : 'none';
}

function SelectBlock(Block)
{
	if(CurrentBlock != null && CurrentBlock.id != 'block_0')
	{
		CurrentBlock.style.borderWidth = '0px';
	}
	
	CurrentBlock = Block;
	FillPropertyForm();
	
	if(CurrentBlock.id != 'block_0')
	{
		CurrentBlock.style.borderBottom = 'thick dashed red';
		CurrentBlock.style.borderRight = 'thick dashed red';
	}
	
	for(CurrentBlockIndex = 0; CurrentBlockIndex < BlockArray.length; 
		CurrentBlockIndex++)
	{
		if(BlockArray[CurrentBlockIndex]['id'] == CurrentBlock.id)
		{
			break;
		}
	}
	
	SelectBlockInBlockList(CurrentBlock);
}

function SetBlockPropertiesFromForm(Block)
{
	var Index;
	for(Index = 0; Index < BlockArray.length; Index++)
	{
		if(BlockArray[Index]['id'] == Block.id)
		{
			break;
		}
	}
	
	BlockArray[Index]['content'] = GetSelectValue(content_property);
	
	BlockArray[Index]['title_font_name'] = GetSelectValue(title_font_name_property);
	BlockArray[Index]['title_font_size'] = GetSelectValue(title_font_size_property);
	BlockArray[Index]['title_italic'] = Number(title_italic_property.checked);
	BlockArray[Index]['title_bold'] = Number(title_bold_property.checked);
		
	BlockArray[Index]['display_title'] = Number(display_title_property.checked);
	BlockArray[Index]['title'] = title_property.value;
	BlockArray[Index]['title_color'] = title_color_property.style.backgroundColor;
	if(title_margin_property.value != '' && 
		!isNaN(Number(title_margin_property.value)))
	{
		BlockArray[Index]['title_margin'] = Number(title_margin_property.value);
	}
	
	if(padding_property.value != '' && !isNaN(Number(padding_property.value)))
	{
		BlockArray[Index]['padding'] = Number(padding_property.value);
	}
	
	BlockArray[Index]['display_border'] = Number(display_border_property.checked);
	BlockArray[Index]['border_width'] = GetSelectValue(border_width_property);
	BlockArray[Index]['border_style'] = GetSelectValue(border_style_property);
	BlockArray[Index]['border_color'] = border_color_property.style.backgroundColor;
	
	if(width_property.value != '' && !isNaN(Number(width_property.value)))
	{
		BlockArray[Index]['width'] = Number(width_property.value);
	}
	
	if(height_property.value != '' && !isNaN(Number(height_property.value)))
	{
		BlockArray[Index]['height'] = Number(height_property.value);
	}
	
	BlockArray[Index]['display_background'] = 
		Number(display_background_property.checked);
	BlockArray[Index]['background_color'] = 
		background_color_property.style.backgroundColor;
	BlockArray[Index]['background_image'] = 
		GetSelectValue(background_image_property);
	BlockArray[Index]['fixed_background_image'] = 
		Number(fixed_background_image_property.checked);
		
	BlockArray[Index]['skin'] = GetSelectValue(skin_image_property);
		
	SetBlockPropertiesFromArray(Block);
}

function NewBlock(Type)
{
	var BlockTitle = document.createElement('div');
	var BlockBody = document.createElement('div');
	var Block = document.createElement('div');
	var ContentFrame;
	var block_id = 'block_' + 
		String(Number(BlockArray[BlockArray.length - 1].id.substr(6)) + 1);
	
	switch(Type)
	{
		case 'text':
			BlockTitle.className = 'default_block_title';
			BlockBody.className = 'default_block_body';
			Block.className = 'default_block';
			break;
		case 'media':
			BlockTitle.className = 'default_media_block_title';
			BlockBody.className = 'default_media_block_body';
			Block.className = 'default_media_block';
			BlockBody.innerHTML = 
				"<iframe scrolling=no frameborder=0  id=content_frame_" + 
				String(BlockArray.length) + " src='?res=root._help._service." +
				"get_content&block_id=" + block_id + "&content_id=-1'" +
				" allowtransparency='true' class=content_frame></iframe>";
			ContentFrame = document.getElementById('content_frame_' + 
				String(BlockArray.length));
			break;
	}
	
	AddCursorListener(Block);
	
	Block.appendChild(BlockTitle);
	Block.appendChild(BlockBody);

	var BlockData = Array();
	BlockData['id'] = block_id;
	BlockData['type'] = Type;
	BlockData['content'] = 0;
	BlockData['display_title'] = 0;
	BlockData['title_font_name'] = '';
	BlockData['title_font_size'] = '';
	BlockData['title_italic'] = 0;
	BlockData['title_bold'] = 0;
	BlockData['title'] = '';
	BlockData['title_color'] = '#000000';
	BlockData['title_margin'] = 0;
	BlockData['x'] = 0;
	BlockData['y'] = 0;
	switch(Type)
	{
		case 'text':
			BlockData['width'] = 100;
			BlockData['height'] = 100;
			break;
		case 'media':
			BlockData['width'] = 320;
			BlockData['height'] = 240;
			break;
	}
	BlockData['padding'] = 0;
	BlockData['display_border'] = 0;
	BlockData['border_width'] = 'thin';
	BlockData['border_style'] = 'solid';
	BlockData['border_color'] = '#FFFFFF';
	BlockData['display_background'] = 0;
	BlockData['skin'] = '';
	BlockData['background_color'] = 'transparent';
	BlockData['background_image'] = '';
	BlockData['fixed_background_image'] = 0;
	
	Block.id = BlockData['id'];
	
	block_0.appendChild(Block);
	
	BlockArray.push(BlockData);
	
	UpdateBlockList();
	
	SelectBlock(Block);
	
	Block.style.left = String(BlockData['x']) + 'px';
	Block.style.top = String(BlockData['y']) + 'px';
	Block.style.width = String(BlockData['width']) + 'px';
	Block.style.height = String(BlockData['height']) + 'px';
}

function DeleteBlock()
{
	if(CurrentBlock.id == 'block_0')
	{
		return;
	}
	
	var I;
	for(I = 0; I < BlockArray.length; I++)
	{
		if(BlockArray[I]['id'] == CurrentBlock.id)
		{
			break;
		}
	}
	
	for(; I < BlockArray.length - 1; I++)
	{
		BlockArray[I] = BlockArray[I + 1];
	}
	BlockArray.pop();
	
	block_0.removeChild(CurrentBlock);
	CurrentBlock = null;
	
	UpdateBlockList();
	
	SelectBlock(block_0);
}

function ReflectPropertyFormChange()
{
	SetBlockPropertiesFromForm(CurrentBlock);
}

function GetEventTarget(e)
{
	var targ
	if (!e) var e = window.event
	if (e.target) targ = e.target
	else if (e.srcElement) targ = e.srcElement
	if (targ.nodeType == 3) // defeat Safari bug
   		targ = targ.parentNode
	while(targ.id.substr(0, 6) != 'block_')
	{
		targ = targ.parentNode;
	}
	return targ;
}

function GetAbsoluteOffsetLeft(Block)
{
	var AbsoluteOffset = 0;
	while(Block.tagName.toLowerCase() != 'body')
	{
		AbsoluteOffset += Block.offsetLeft;
		Block = Block.offsetParent;
	}
	return AbsoluteOffset;
}

function GetAbsoluteOffsetTop(Block)
{
	var AbsoluteOffset = 0;
	while(Block.tagName.toLowerCase() != 'body')
	{
		AbsoluteOffset += Block.offsetTop;
		Block = Block.offsetParent;
	}
	return AbsoluteOffset;
}

function PixelsToInt(Pixels)
{
	return parseInt(Pixels.substr(0, Pixels.length - 2));
}

function OnMouseMove(event)
{
	if(!event)
	{
		event = window.event;
	}
	
	if(MouseX >= 0 && CurrentBlock.id != 'block_0')
	{
		switch(MouseOperation)
		{
			case 'move':
				CurrentBlock.style.left = String(
					parseInt(CurrentBlock.style.left.substr(0, 
					CurrentBlock.style.left.length - 2)) + 
					(event.screenX - MouseX)) + 'px';
				CurrentBlock.style.top = String(
					parseInt(CurrentBlock.style.top.substr(0, 
					CurrentBlock.style.top.length - 2)) + 
					(event.screenY - MouseY)) + 'px';
				BlockArray[CurrentBlockIndex]['x'] = 
					parseInt(CurrentBlock.style.left.substr(0, 
					CurrentBlock.style.left.length - 2));
				BlockArray[CurrentBlockIndex]['y'] = 
					parseInt(CurrentBlock.style.top.substr(0, 
					CurrentBlock.style.top.length - 2));
				break;
			case 'resize':
				if(CurrentBlock.clientWidth + (event.screenX - MouseX) > 0 && 
					CurrentBlock.clientHeight + (event.screenY - MouseY))
				{
					var NewWidth = PixelsToInt(CurrentBlock.style.width) + 
						(event.screenX - MouseX);
					var NewHeight = PixelsToInt(CurrentBlock.style.height) + 
						(event.screenY - MouseY);
					CurrentBlock.style.width = String(NewWidth) + 'px';
					width_property.value = String(NewWidth);
					CurrentBlock.style.height = String(NewHeight) + 'px';
					height_property.value = String(NewHeight);
					BlockArray[CurrentBlockIndex]['width'] = NewWidth;
					BlockArray[CurrentBlockIndex]['height'] = NewHeight;
				}
				break;
		}
		MouseX = event.screenX;
		MouseY = event.screenY;
	}
	return true;
}

function OnMousePressed(event)
{
	var Block;
	if(arguments[1])
	{
		Block = document.getElementById(arguments[1]);
	}
	else
	{
		Block = GetEventTarget(event);
	}
	
	if(!event)
	{
		event = window.event;
	}
	
	MouseX = event.screenX;
	MouseY = event.screenY;
	
	if(CurrentBlock.id != Block.id)
	{
		SelectBlock(Block);
	}
	
	if(GetAbsoluteOffsetLeft(Block) + Block.offsetWidth - 8 < 
		MouseX + document.body.scrollLeft && 
		GetAbsoluteOffsetTop(Block) + Block.offsetHeight - 8 < 
		MouseY + document.body.scrollTop)
	{
		MouseOperation = 'resize';
	}
	else
	{
		MouseOperation = 'move';
	}
	
	return true;
}

function OnMouseReleased(event)
{
	MouseX = -1;
	MouseY = -1;
}

function EditTextBlock()
{
	var CKEditor = document.getElementById('CKEditor');
	var ScreenY = window.scrollX  ? window.scrollX : 
		window.document.body.scrollLeft;
	var ScreenX = window.scrollY ? window.scrollY : 
		window.document.body.scrollTop;
	
	if(CurrentBlock.id == 'block_0')
	{
		return false;
	}
	
	for(var Index = 0; Index < BlockArray.length; Index++)
	{
		if(BlockArray[Index]['id'] == CurrentBlock.id)
		{
			if(BlockArray[Index]['type'] == 'text')
			{
				break;
			}
			else
			{
				return;
			}
		}
	}
	
	var Width = 420; //CurrentBlock.offsetWidth + 16;
	var Height = 370; //CurrentBlock.offsetHeight + 100;
	Width = Width < 250 ? 250 : Width;
	
	var BlockBody = CurrentBlock.childNodes[1];
	CKEDITOR.instances.ckeditor_textarea.setData(BlockBody.innerHTML);
	
	CKEditor.style.top = ScreenX + 10 + 'px';
	CKEditor.style.left = ScreenY + 10 + 'px';
	CKEditor.style.visibility = "visible";
}

function SaveEditor()
{
	hide_page = document.getElementById('hide_pp');
	hide_personal_page = document.getElementById('hide_personal_page');
	personal_page_blocks.value = '';
	for(var I = 0; I < BlockArray.length; I++)
	{
		if(BlockArray[I]['type'] == 'text' && I)
		{
			var EditedBlock = document.getElementById(BlockArray[I]['id']);
			var EditedTag = EditedBlock.childNodes[1]
			BlockArray[I]['content'] = EditedTag.innerHTML;
		}
	
		personal_page_blocks.value += 
			'id=' + String(BlockArray[I]['id']) + ',';
		personal_page_blocks.value += 
			'type=' + String(BlockArray[I]['type']) + ',';
		personal_page_blocks.value += 
			'content=' + escape(String(BlockArray[I]['content'])) + ',';
		personal_page_blocks.value += 
			'display_title=' + String(BlockArray[I]['display_title']) + ',';
		personal_page_blocks.value += 
			'title=' + encodeURIComponent(String(BlockArray[I]['title'])) + ',';
		personal_page_blocks.value += 
			'title_color=' + 
			encodeURIComponent(String(BlockArray[I]['title_color'])) + ',';
		personal_page_blocks.value += 
			'title_margin=' + String(BlockArray[I]['title_margin']) + ',';
		personal_page_blocks.value += 
			'title_font_name=' + String(BlockArray[I]['title_font_name']) + ',';
		personal_page_blocks.value += 
			'title_font_size=' + String(BlockArray[I]['title_font_size']) + ',';
		personal_page_blocks.value += 
			'title_italic=' + String(BlockArray[I]['title_italic']) + ',';
		personal_page_blocks.value += 
			'title_bold=' + String(BlockArray[I]['title_bold']) + ',';
		personal_page_blocks.value += 
			'x=' + String(BlockArray[I]['x']) + ',';
		personal_page_blocks.value += 
			'y=' + String(BlockArray[I]['y']) + ',';
		personal_page_blocks.value += 
			'width=' + String(BlockArray[I]['width']) + ',';
		personal_page_blocks.value += 
			'height=' + String(BlockArray[I]['height']) + ',';
		personal_page_blocks.value += 
			'padding=' + String(BlockArray[I]['padding']) + ',';
		personal_page_blocks.value += 
			'display_border=' + String(BlockArray[I]['display_border']) + ',';
		personal_page_blocks.value += 
			'border_width=' + String(BlockArray[I]['border_width']) + ',';
		personal_page_blocks.value += 
			'border_style=' + String(BlockArray[I]['border_style']) + ',';
		personal_page_blocks.value += 
			'border_color=' + 
			encodeURIComponent(String(BlockArray[I]['border_color'])) + ',';
		personal_page_blocks.value += 
			'display_background='+ String(BlockArray[I]['display_background']) + ',';
		personal_page_blocks.value += 
			'skin=' + encodeURIComponent(String(BlockArray[I]['skin'])) + ',';
		personal_page_blocks.value += 
			'background_color=' + 
			encodeURIComponent(String(BlockArray[I]['background_color'])) + ',';
		personal_page_blocks.value += 
			'background_image=' + 
			encodeURIComponent(String(BlockArray[I]['background_image'])) + ',';
		personal_page_blocks.value += 
			'fixed_background_image=' + 
			String(BlockArray[I]['fixed_background_image']) + ';';
	}
	personal_page_blocks.value = personal_page_blocks.value.substr(0, 
		personal_page_blocks.value.length - 1);
		
	if(hide_page.checked == true)
	{
		hide_personal_page.value = 0;
	}
	else
		hide_personal_page.value = 1;
		
	editor_form.submit();
}
