// .-==================================================================
// .	FusionBB Version 2.2
// .
// .	Created August 20, 2003 by InteractivePHP
// .
// .	All scripts and files used by this program
// .	are copyright InteractivePHP, Inc
// .	For discussion, bug reports and updates
// .	visit http://www.FusionBB.com
// .
// .	You may not redistribute this program in any form, without
// .	express written permisson from InteractivePHP.
// .
// .	WARNING: If you modify any scripts within FusionBB
// .	you will be unable to obtain official support.
// .	Modify at your own risk!
// .
// .	FusionBB Copyright 2003-2007, InteractivePHP, Inc
// .-==================================================================
// .	$Date: 2010/12/04 00:08:16 $	$Author: couch $
// .	$RCSfile: markup_code.js,v $	$Revision: 1.10 $
// .-==================================================================
// .	File Description:
// .		Javascript for Markup Code
// .-==================================================================

var spoilernum = 1;
function countSpoiler() {
	var spoilernum = spoilernum + 1;
}

function storeCaret(position) {
	if (position.createTextRange){
		position.caretPos = document.selection.createRange().duplicate();
	}
}

function insertColor(TextPosition,Color) {
	if(TextPosition == null || TextPosition == 'undefined'){
		var TextPosition = document.post.body;
	}
	surroundText("[color:" + Color + "]" , "[/color]", TextPosition);
	TextPosition.focus();
	return;
}   

function askBox(TextPosition,what,tag,option) {
	if(TextPosition == null || TextPosition == 'undefined'){
		var TextPosition = document.post.body;
	}
	if (what == "custom") {
		if(option == 1){
			var ModText = prompt("Enter Option:");
			if (ModText != null && ModText != ''){
				var LinkText = prompt("Enter Paramater:");
				if (LinkText != null && LinkText != ''){ 
					insertCode(TextPosition, ' ['+tag+'=' + ModText + ']' + LinkText + '[/'+tag+'] ');
				}
			}
		}
		else if(option == 2){
			insertCode(TextPosition, ' ['+tag+']' + '[/'+tag+'] ');
		}
		else{
			var ModText = prompt("Enter Paramater:");
			if (ModText != null && ModText != ''){
				insertCode(TextPosition, ' ['+tag+']' + ModText + '[/'+tag+'] ');
			}
		}
		return;
	} 
	else if (what == "url") {
		var ModText = prompt("Enter URL:", "http://");
		if (ModText != null && ModText != '' && ModText != 'http://'){
			var LinkText = prompt("Text For Link:","Link");
			if (LinkText != null && LinkText != ''){ 
				insertCode(TextPosition, ' [url=' + ModText + ']' + LinkText + '[/url] ');
			}
		}
		return;
	} 
	else if (what == "spoiler") {
		var SpoilerQ = prompt("Spoiler Question " + spoilernum + ":","");
		if (SpoilerQ != null && SpoilerQ != '') {
			var SpoilerA = prompt("Spoiler Answer " + spoilernum + ":","");
			if (SpoilerA != null && SpoilerA != '') {
				insertCode(TextPosition, ' [spoilerq:' + spoilernum + ']' + SpoilerQ + '[/spoilerq] \n'
				+ ' [spoilera:' + spoilernum + ']' + SpoilerA + '[/spoilera] ');
				countSpoiler();
			}
		}	
	}
	else if (what == "image") {
		var ModText = prompt("Enter URL to Image:", "http://");
		if (ModText == null){
			return;
		}
		insertCode(TextPosition, ' [image]' + ModText + '[/image] ');
	}
	else if (what == "lightbox") {
		var ModText = prompt("Enter URL to Image:", "http://");
		if (ModText == null){
			return;
		}
		insertCode(TextPosition, ' [lightbox]' + ModText + '[/lightbox] ');
	}
	else if (what == "color") {
		var ModText = prompt("Enter Color Code:", "#");
		if (ModText == null) {
			return;
		}
		insertColor(ModText);
	}
	else if (what == "list") {
		var ModText = "init";
		var listoutput = " [list]\n";
		while ( (ModText != null) && (ModText != "") ) {
			ModText = prompt("Enter List Item (leave blank to end list).","");
			if ( (ModText != null) && (ModText != "") ) {
				listoutput = listoutput + "[li]" + ModText + "[/li]";		
			}
		}
		listoutput = listoutput + "[/list]\n ";
		insertCode(TextPosition, listoutput);
	}
	else {
		surroundText('['+ what + ']' , '[/'+ what + ']', TextPosition);
	}
	TextPosition.focus();
	return;
}	

function markup() {
	return;
}	

function insertCode(TextPosition,text) {
	if (TextPosition.createTextRange && TextPosition.caretPos) {
	//M$ IE browsers
		var caretPos = TextPosition.caretPos;
		caretPos.text =
			caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
			text + ' ' : text;
			TextPosition.focus();
			return;
	}
	//Mozilla browsers
	else if (typeof(TextPosition.selectionStart) != "undefined"){
		var begin = TextPosition.value.substr(0, TextPosition.selectionStart);
		var end = TextPosition.value.substr(TextPosition.selectionEnd);
		var scrollPos = TextPosition.scrollTop;

		TextPosition.value = begin + text + end;

		if (TextPosition.setSelectionRange){
			TextPosition.focus();
			TextPosition.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		TextPosition.scrollTop = scrollPos;
	}

	// other browsers
	else{
			TextPosition.value += text;
			TextPosition.focus(TextPosition.value.length - 1);
	}
	return true;
}


function surroundText(text1, text2, textarea){
	//M$ IE browsers
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange){
		var caretPos = textarea.caretPos;
		// remove white space at the start and end of selection
		selection = caretPos.text;
		var spacebefore = 0;
		var spaceafter = 0;
		while (selection.substring(0,1) == ' ') {
			selection = selection.substring(1);
			spacebefore = spacebefore + 1;
		}
		while (selection.substring(selection.length-1,selection.length) == ' '){
			selection = selection.substring(0,selection.length-1);
			spaceafter = spaceafter + 1;
		}

        if(spacebefore > 0 ) {
			movedStart = caretPos.moveStart("character", +spacebefore);
		}
		if(spaceafter > 0 ) {
			movedEnd = caretPos.moveEnd ("character", -spaceafter);
		}

		textlength = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' '
			? text1 + caretPos.text + text2 + ' '
			: text1 + caretPos.text + text2;

		if (textlength==0){
            n = text2.length;
            caretPos.moveStart("character", -n);
            caretPos.moveEnd ("character", -n);
        }
		if (textlength > 0){
			n = text1.length + text2.length + textlength;
			caretPos.moveStart("character", -n);
		}
		caretPos.select();
	}
	//Mozilla browsers
	else if (typeof(textarea.selectionStart) != "undefined"){
		var start =   textarea.selectionStart;
		var finish =  textarea.selectionEnd;
		var begin =     textarea.value.substr(0, start);
		var selection = textarea.value.substr(start, finish - start);
		var end = textarea.value.substr(finish);
		var newCursorPos = start;
		var scrollPos = textarea.scrollTop;

		//  remove white space at the beginning and end of selection
		var spacebefore = 0;
		var spaceafter = 0;
		while (selection.substring(0,1) == ' ') {
			selection = selection.substring(1);
			spacebefore = spacebefore + 1;
		}
		while (selection.substring(selection.length-1,selection.length) == ' '){
			selection = selection.substring(0,selection.length-1);
			spaceafter = spaceafter + 1;
		}

    	if(spaceafter > 0  || spacebefore > 0 ) {
		  	start = start + spacebefore;
		  	finish = finish - spaceafter ;
			var begin = textarea.value.substr(0, start);
			var selection = textarea.value.substr(start, finish - start);
			var end = textarea.value.substr(finish);
			var newCursorPos = start;
			var scrollPos = textarea.scrollTop;
        }

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange){
			if (selection.length == 0){
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			}else{
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
				textarea.focus();
			}
			textarea.scrollTop = scrollPos;
		}
    }else{ // other browsers
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
 	}

}

