var wyziwygs = new Array();
var commands = {
	bold: ['<b>', '</b>'],
	italic: ['<i>', '</i>'],
	underline: ['<u>', '</u>']
};
var wysiwygSyncWorking = 0;
var wysiwygSupported = document.designMode;
var wysiwygCount = 0;
var wysiwygIE = 0;
if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
	if (version>=5.5) //NON IE browser will return 0
		wysiwygIE = 1;
}

setInterval("wysiwygSync(0);", 500);

function wysiwygInit (rewadId, id, content, style, forceTextarea, resizeable) {
	wysiwygEnabled = 1;
	var div = dgbi(id);
	
	// Saving common data
	wyziwygs[wyziwygs.length] = div;
	div.rewadId = rewadId;
	div.old = content;
	div.innerHTML = '';
	div.minHeight = 150;
	div.resizeable = resizeable;
	
	// If browser supports editing
	if (!forceTextarea && wysiwygSupported) {
		
		// Creating iframe
		iframe = document.createElement('iframe');
		iframe.className = style ? style : "wysiwyg";
		iframe.src = "javascript:;";
		iframe.id = id + "_if";
		div.appendChild(iframe);
		div.iframe = iframe;
		doc = iframe.contentWindow.document;
		
		// Writing initial HTML
		var head = '';
		for (i = 0; i < document.styleSheets.length; i++)
			head += '<link type="text/css" href="'+document.styleSheets[i].href+'" rel="stylesheet">';
		head += '<link type="text/css" href="/rewad-web/styles/wysiwyg.css" rel="stylesheet">';
		
		onLoadScript = 'document.designMode = \'on\'; document.body.contentEditable = \'true\';';
		
		doc.open();
		doc.write('<html><head>' + head + '</head><body class=iframeBody onload="' + onLoadScript + '">' + content + '</body></html>');
		doc.close();
		div.wyzType = 1;
	}
	else {
		var textarea = document.createElement('textarea');
		textarea.className = "wysiwyg";
		textarea.id = id + "_ta";
		textarea.value = content;
		textarea.style.height = div.minHeight;
		div.appendChild(textarea);
		div.textarea = textarea;
		div.wyzType = 0;
	}
	
}

function wysiwygFromDiv (rewadId, id, style) {
	var div = dgbi(id);
	div.oldHTML = div.innerHTML;
	div.innerHTML = '';
	wysiwygInit(rewadId, id, div.oldHTML, style);
}

function wysiwygToDiv (id, cancel) {
	var div = dgbi(id);
	div.innerHTML = cancel ? div.oldHTML : div.old;
	div.style.height = '';
}

function emoticonInsert(id, image, alt) {
	wysiwygExecute('insertHtml', id, '<img class="emoticon" src="' + image + '" alt="' + alt + '" contentEditable="false" />');
}

function wysiwygExecute (command, id, option) {
	var div = dgbi(id);
	if (command == 'switchmode') {
		if (div.wyzType) {
			wysiwygInit(div.rewadId, id, div.iframe.contentWindow.document.body.innerHTML, div.iframe.className, 1);
		}
		else {
			wysiwygInit(div.rewadId, id, div.textarea.value, div.textarea.className);
		}
	}
	else if (div.wyzType) {
		if ((command == 'insertHtml') && wysiwygIE) {
			dgbi(id+"_if").contentWindow.document.body.focus();
			dgbi(id+"_if").contentWindow.document.selection.createRange().pasteHTML(option);
		}
		else
			dgbi(id+"_if").contentWindow.document.execCommand(command, false, option);
		dgbi(id+"_if").contentWindow.document.body.focus();
	}
	else {
		if (command == 'insertHtml') {
			area = dgbi(id+"_ta");
			if (document.selection) {
				sel = document.selection.createRange();
				sel.text = option;
			}
			else {
				before = area.value.slice(0, area.selectionStart);
				after = area.value.slice(area.selectionEnd);
				area.value = before + option + after;
			}
		}
		else if (commands[command]) {
			area = dgbi(id+"_ta");
			oTag = commands[command][0].replace(/__O__/, option);
			if (document.selection) {
				sel = document.selection.createRange();
				sel.text = oTag + sel.text + commands[command][1];
			}
			else {
				before = area.value.slice(0, area.selectionStart);
				sel = area.value.slice(area.selectionStart, area.selectionEnd);
				after = area.value.slice(area.selectionEnd);
				area.value = before + oTag + sel + commands[command][1] + after;
			}
		}
		else
			alert(command + '(' + option + ') not implemented yet for textarea');
	}
}

function wysiwygSync (force) {
	var i, el, wyz;
	if (!wysiwygSyncWorking || force) {
		wysiwygSyncWorking = 1;
		wysiwygCount = (wysiwygCount + 1) % 5;
		for (i = 0; i < wyziwygs.length; i++) {
			try {
				el = wyziwygs[i];
				var newText;
				
				if (el.iframe.contentWindow && !el.iframe.contentWindow.document.body)
					continue;
				
				if (el.wyzType) {
					wyz = el.iframe.contentWindow.document.body;
					h = el.iframe.contentWindow.document.documentElement.clientHeight ? el.iframe.contentWindow.document.documentElement.clientHeight : el.iframe.contentWindow.document.body.scrollHeight;
					if (h < el.minHeight)
						h = el.minHeight;
					if (el.resizeable)
						el.style.height = h + 'px';
					newText = wyz.innerHTML;
				}
				else {
					wyz = el.textarea;
					newText = wyz.value;
				}
				var oldText = el.old;
				if ((oldText != newText) && (!wysiwygCount || force)) { /// TODO: Diff
					showdbg("/:/" + el.rewadId + ' ' + newText);
					el.old = newText;
					messageREWAD("/:/" + el.rewadId, 'value='+encodeURIComponent(newText));
				}
			}
			catch (e) {
				wyziwygs.splice(i, 1);
				i--;
				continue;
			}
		}
	}
	else if (!force)
		showdbg('<b> NOT SINKING</b>');
	wysiwygSyncWorking = 0;
}
