/**
 * Addons for FCKeditor
 *
 * @author Evgueni Palchukovsky <epalchukovsky@swsoft.com>
 * @version $Id: sbAddons.js 22605 2007-05-15 08:53:57Z adobrenko $
 * @since 2006/08/09
 * @package SB
 * @subpackage WYSIWYG
 * @copyright (c) 2004-2006, SWsoft
 */

function SetModuleEventHandler(control, event, func) {
	if (document.all) {
		control.attachEvent("on"+event, func);
	} else {
		control.addEventListener(event, func, true);
	}
}

function SetModulePropsEventHandler(win) {
	var propsElements = win.document.getElementsByName('props');
	
	for (var i = 0; i < propsElements.length; i++) {
		SetModuleEventHandler(win.document.getElementById(propsElements[i].id), "click", propsElements[i].onclick);
	}
}

function SetModuleCloseEventHandler(win) {
	var func = function() {
		win.blockModule.removeModule();
		return false;
	}
	SetModuleEventHandler(win.document.getElementById("close"), "click", func);
}

function SetModuleLoadEventHandler(win) {
	var funcFrame = function() {
		win.blockModule.resizeMainFrame();
		return false;
	}
	var funcPreview = function() {
		win.blockModule.resizePreviewFrame();
		setTimeout(funcFrame, 500);
		return false;
	}
	SetModuleEventHandler(win.document.body, "load", funcFrame);
	SetModuleEventHandler(win.document.getElementById("preview"), "load", funcPreview);
	SetModuleEventHandler(win.document.getElementById("preview"), "resize", funcPreview);
}

function ResizeBlockModuleFrameInMozilla(frame, win) {
	if (document.all || !win) {
		return;
	}
	setTimeout(function() {
		if (!win) {
			return;
		} else if (!win.blockModule) {
			CreateBlockModuleObject(frame, win);
		}
		if (win.blockModule) {
			win.blockModule.resizePreviewFrame();
			setTimeout(function() {
				if (win && win.blockModule) {
					win.blockModule.resizeMainFrame();
				}
			}, 1000);
		}
	}, 1000);
}

function CreateBlockModuleObject(frame, win) {
	if (win && win.document) {
		var link = win.document.getElementById("viewLink");
		var title = win.document.getElementById("formTitle");
		var previewFrame = win.document.getElementById("preview");
		if (link && title && previewFrame) {
			win.blockModule = new BlockModule(frame, title.value);
			SetModuleLoadEventHandler(win);
			SetModulePropsEventHandler(win);
			SetModuleCloseEventHandler(win);
		}
	}
}


/*
 * :NOTE: epalchukovsky@: Workaround for events in new Mozilla
 */
function SetBlockModulesEvents() {
	var eEditorArea =	document.getElementById('eEditorArea').
						contentWindow.document.getElementById('eEditorArea');
	if (!eEditorArea) {
		return;
	}
	var modFrames = eEditorArea.contentWindow.document.getElementsByTagName('IFRAME');
	for (var i = 0; i < modFrames.length; ++i) {
		var frame = modFrames[i];
		var win   = frame.contentWindow;
		var link   = win.document.getElementById("viewLink");
		var title  = win.document.getElementById("formTitle");
		var previewFrame = win.document.getElementById("preview");
		if (!link 
			|| !title
			|| !previewFrame
			|| !previewFrame.contentWindow
			|| !previewFrame.contentWindow.document
			|| !previewFrame.contentWindow.document.getElementById('reloadInProgress')
			||  previewFrame.contentWindow.document.getElementById('reloadInProgress').value != 'false') {

				setTimeout(SetBlockModulesEvents, 100);
				return;
		}
	}
	_SetBlockModulesEvents();
}

function _SetBlockModulesEvents() {
	var eEditorArea =	document.getElementById('eEditorArea').
						contentWindow.document.getElementById('eEditorArea');
	if (!eEditorArea) {
		return;
	}
	var modFrames = eEditorArea.contentWindow.document.getElementsByTagName('IFRAME');
	for (var i = 0; i < modFrames.length; ++i) {
		var frame = modFrames[i];
		var win = frame.contentWindow;
		var link = win.document.getElementById("viewLink");
		var title = win.document.getElementById("formTitle");
		var previewFrame = win.document.getElementById("preview");
		if (!link || !title || !previewFrame) {
			setTimeout(SetBlockModulesEvents, 100);
			break;
		} else {
			if (!win.blockModule) {
				CreateBlockModuleObject(frame, win);
			}
			ResizeBlockModuleFrameInMozilla(frame, win);
		}
	}
}