
var nucleusConvertBreaks = 1;
var nucleusMediaPopupURL = '';
var nucleusMediaURL = 'media/';
var nucleusAuthorId = 0;
var scrollTop = -1;

function setConvertBreaks(newval) {	nucleusConvertBreaks = newval; }
function setMediaUrl(url) { nucleusMediaURL = url; }
function setAuthorId(id) { nucleusAuthorId = id; }

function preview(id, value) {
	elem = document.getElementById(id);
	if (!elem) return;
	
	var preview = nucleusConvertBreaks ? str_replace("\n","<br />",value)+"&nbsp;" : value+"&nbsp;";
	
	// expand the media commands (without explicit collection)
	preview = preview.replace(/\<\%image\(([^\/\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+nucleusAuthorId+"/$1' width='$2' height='$3' alt=\"$4\" />");
	
	// expand the media commands (with collection)
	preview = preview.replace(/\<\%image\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<img src='"+nucleusMediaURL+"$1' width='$2' height='$3' alt=\"$4\" />");
	preview = preview.replace(/\<\%popup\(([^\|]*)\|([^\|]*)\|([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' onclick='if (event &amp;&amp; event.preventDefault) event.preventDefault(); alert(\"popup image\"); return false;' title='popup'>$4</a>");
	preview = preview.replace(/\<\%media\(([^\|]*)\|([^)]*)\)\%\>/g,"<a href='' title='media link'>$2</a>");	
	
	elem.innerHTML = preview;
}

function showedit() {
	prevval = document.getElementById('edit').style.display;
	if (prevval == "block")
		newval = "none";
	else
		newval = "block";
	document.getElementById('edit').style.display = newval;

	if (newval == "block")
		updAllPreviews();	
}

function updAllPreviews() {
	updPreview('title');
	updPreview('body');
	updPreview('more');
}

function isEditVisible() {
	var editform = document.getElementById('edit');
	if (!editform) return true;
	var prevval = editform.style.display;
	return (prevval == "none") ? false : true;
}

function updPreview(id) {
	// don't update when preview is hidden
	if (!isEditVisible()) return;
	
	var inputField = document.getElementById(id);
	if (!inputField) return;
	preview('prev' + id, inputField.value);
}

// replace a in s by b (taken from milov.nl)
function str_replace(a, b, s)
{
	if (a == b || !s.length || !a.length) return s;
	if ((p=s.indexOf(a)) == -1) { return s; }
	else { ns = s.substring(0,p) + b + s.substring(p+a.length,s.length); }
	return (s.indexOf(a) != -1) ? str_replace(a, b, ns) : ns;
}

function shortCuts() {
	if (!event || (event.ctrlKey != true)) return;
	
	switch (event.keyCode) {
		case 1:
			ahrefThis(); break; // ctrl-shift-a
		case 2:
			boldThis(); break; // ctrl-shift-b
		case 9:
			italicThis(); break; // ctrl-shift-i
		case 13:
			addMedia(); break; // ctrl-shift-m
		default: 
			return;
	}
	return;
}

function cutThis() { execAndUpdate('cut'); }
function copyThis() { execAndUpdate('copy'); }
function pasteThis() { execAndUpdate('paste'); }
function boldThis() { insertAroundCaret('<b>','</b>'); }
function italicThis() { insertAroundCaret('<i>','</i>'); }
function leftThis() { insertAroundCaret('<div class="leftbox">','</div>'); }
function rightThis() { insertAroundCaret('<div class="rightbox">','</div>'); }
function alignleftThis() { insertAroundCaret('<div style="text-align: left">','</div>'); }
function alignrightThis() { insertAroundCaret('<div style="text-align: right">','</div>'); }
function aligncenterThis() { insertAroundCaret('<div style="text-align: center">','</div>'); }


function ahrefThis() {
	if (document.selection)
		strSelection = document.selection.createRange().text;
	else
		strSelection = '';
		
	strHref = prompt("Create a link to:","http://");
	if (strHref == null) return;
	
	var textpre = "<a href=\"" + strHref.replace(/&/g,'&amp;') + "\">";
	insertAroundCaret(textpre, "</a>");
}

function execAndUpdate(action) {
	lastSelected.caretPos.execCommand(action);
	updAllPreviews();	
}


var nonie_FormType = 'body';

// Add media to new item
function addMedia() {
	
	var mediapopup = window.open(nucleusMediaPopupURL + 'media_insert.php?type=img','name',
		'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');

	return;
} 

function setMediaPopupURL(url) {
	nucleusMediaPopupURL = url;
}

function pushCode( code ) {
	insertAtCaret( code );	
}

function addquote( cid, nick ) {
	insertAtCaret( "<%quote(" + cid + "|" + nick + "|" + text + ")%>" );	
}
function includeImage(filename, type, width, height) {
	if (isCaretEmpty()) {
		text = prompt("Text to display ?","");
	} else {
		text = getCaretText();
	}
	
	// add collection name when not private collection (or editing a message that's not your)
	var fullName;
//	if (isNaN(collection) || (nucleusAuthorId != collection)) {
//		fullName = collection + '/' + filename;
//	} else {
		fullName = filename;
//	}
		
	
	var replaceBy;
	switch(type) {
		case 'popup':
			replaceBy = '<%popup(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
			break;
		case 'inline':
		default:
			replaceBy = '<%image(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
	}
	
	insertAtCaret(replaceBy);
	updAllPreviews();	

}


function includeOtherMedia(collection, filename) {
	if (isCaretEmpty()) {
		text = prompt("Text to display ?","");
	} else {
		text = getCaretText();
	}
	
	// add collection name when not private collection (or editing a message that's not your)
	var fullName;
	if (isNaN(collection) || (nucleusAuthorId != collection)) {
		fullName = collection + '/' + filename;
	} else {
		fullName = filename;
	}	
	
	var replaceBy = '<%media(' +  fullName + '|' + text +')%>';
	
	insertAtCaret(replaceBy);
	updAllPreviews();	
}



// function to prevent submitting form data twice
var submitcount=0;
function checkSubmit() {
	if (submitcount == 0) {
		submitcount++;
		return true;
	} else {
		return false;
	}
}


// code to store the caret (cursor) position of a text field/text area
// taken from javascript.faqts and modified
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130

// stores the caret
function storeCaret (textEl) {

	// store caret
	if (textEl.createTextRange)
		textEl.caretPos = document.selection.createRange().duplicate();

	// also store lastselectedelement
	lastSelected = textEl;

	nonie_FormType = textEl.name;

	scrollTop = textEl.scrollTop;
}

var lastSelected;
var nonie_FormType;
 // inserts text at caret (overwriting selection)
function insertAtCaret (text) {
	var textEl = lastSelected;
	if (textEl && textEl.createTextRange && textEl.caretPos) {
		trace ('1');
		var caretPos = textEl.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else if (!document.all && document.getElementById) {
		mozReplace(document.getElementById(nonie_FormType), text);
		if(scrollTop>-1) {
			document.getElementById(nonie_FormType).scrollTop = scrollTop;
		}
	} else if (textEl) {
		textEl.value  += text;
	} else {
		trace ('4');
		document.getElementById(nonie_FormType).value += text;
		if(scrollTop>-1) {
			document.getElementById(nonie_FormType).scrollTop = scrollTop;
		}
	}
	document.getElementById(nonie_FormType).focus()
//	updAllPreviews();
}

// inserts a tag around the selected text
function insertAroundCaret (textpre, textpost) {
	var textEl = lastSelected;
	
//	trace('input' + nonie_FormType);
	
	if (textEl && textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;
		caretPos.text = textpre + caretPos.text + textpost;
	} else if (!document.all && document.getElementById) {
		mozWrap(document.getElementById(nonie_FormType), textpre, textpost);		
		if(scrollTop>-1) {
			document.getElementById(nonie_FormType).scrollTop = scrollTop;
		}
	} else {
		document.getElementById(nonie_FormType).value += textpre + textpost;
		if(scrollTop>-1) {
			document.getElementById(nonie_FormType).scrollTop = scrollTop;
		}
	}

	updAllPreviews();
}

/* some methods to get things working in Mozilla as well */
function mozWrap(txtarea, lft, rgt) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
}
function mozReplace(txtarea, newText) {
	trace('...'+txtarea.textLength);
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	trace (s1 + newText + s3);
	txtarea.value = s1 + newText + s3;
}
function mozSelectedText() {
	var txtarea = document.getElementById(nonie_FormType);
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	return (txtarea.value).substring(selStart, selEnd);
}

function getCaretText() {
	if (!document.all && document.getElementById)
		return mozSelectedText();
	else
		return lastSelected.caretPos.text;
}

function isCaretEmpty() {
	if (lastSelected && lastSelected.createTextRange && lastSelected.caretPos)
		return (lastSelected.caretPos.text == '');
	else if (!document.all && document.getElementById)
		return (mozSelectedText() == '');
	else
		return true;
}

function BtnHighlight(el) {
	with(el.style){
		borderLeft="1px solid gray";
		borderRight="1px solid #e9e9e9";
		borderTop="1px solid gray";
		borderBottom="1px solid #e9e9e9";
	}
}

function BtnNormal(el) {
	with(el.style){
		padding="3px";
		border="1px solid #dddddd";
	}
}

function A_bbcode_link() {
	var href1 = 'index.php?link_maker&caption='+getCaretText()+'&mode=AJAX';
	http.msg_target = 'full_value_mid';
	http.mode = 'get';
	
	http.doAAction(href1, null);
	
}

function mk_bbcode_link() {
	var p_link = document.getElementById('bbcode_link').value;
	var p_caption = document.getElementById('bbcode_caption').value;
	if (p_caption == '') {
		insertAtCaret('[url]'+p_link+'[/url]');
	}
	else {
		insertAtCaret('[url='+p_link+']'+p_caption+'[/url]');
	}
	hide_full_front();
}


function chg_bgcolbyName( name, coln )
{
/*	alert(name)*/
	var x=document.getElementsByName("td_" + name)
	for (var i=0;i<x.length;i++)
	{
		x.item(i).style.backgroundColor = coln
/*		x.item(i).className = coln*/
	}
/*	alert(x.length + "td_" + name)*/
}

function SeedFormChg( id )
{
/*	alert("s_" + id)*/
	var lista = new Array();

	var el = seedform.elements;
/*	alert(el.length)*/
	for (var i=0; i<el.length; i++) 
	{
		if (el[i].name.substr(0, 2) == "s_")
		{
			x = el[i].value;
//			javatest.value += x;
			if (isNaN(lista[x]))
			{
				lista[x] = 1;
			}
			else
			{
				lista[x] += 1;	
			}
//javatest.value += el[i].value
		}
	}

//	javatest.value = "";
	for (var i=1; i<=lista.length; i++)
	{
		var span = document.getElementById("span_" + i);
		var inp = document.getElementById("inp_" + i);
//		javatest.value += i + ", ";
		if (isNaN(lista[i]))
		{
			span.style.display = "none";
		}
		else
		{
			span.style.display = "list-item";
			inp.value = lista[i];
		}
	}
/*	var el = seedform;
	for (var i=0; i<el.length; i++) { 
	for (var x in seedform)
	{
	javatest.value += x
	}
*/
}

function chg_pic( img, kep1, kep2)
{
	var subpath = '/SC2.RSHU/';
//	alert(document.domain + subpath + kep1);
	if ( img.src == 'http://' + document.domain + subpath + kep1)
	{
		img.src = kep2;	
	}
	else
	{
		img.src = kep1;
	}
	
	
}


