/**
 * Cross-browser function to access a DOM:Document element
 * Example: $('#foo').contentDocument();
 *
 * @uses jQuery
 *
 * @return object DOM:Document - the document of the frame, iframe or window
 */
jQuery.fn.contentDocument = function() {
	var frame = this[0];
	if (frame.contentDocument) {
		return frame.contentDocument;
	} else if (frame.contentWindow && frame.contentWindow.document) {
		return frame.contentWindow.document;
	} else if (frame.document) {
		return frame.document;
	} else {
		return null;
	}
}

/**
 * Cross-browser function to set the designMode property
 * Example: $('#foo').designMode('on');
 *
 * @uses jQuery, jQuery.fn.contentDocument
 *
 * @param string mode - Which mode to use, should either 'on' or 'off'
 *
 * @return jQuery element - The jQuery element itself, to allow chaining
 */
jQuery.fn.designMode = function(mode) {
	// Default mode is 'on'
	var mode = mode || 'on';
	this.each(function() {
		var frame = $(this);
		var doc = frame.contentDocument();
		if (doc) {
			doc.designMode = mode;
			// Some browsers are kinda slow, so you'll have to wait for the window to load
			frame.load(function() {
				$(this).contentDocument().designMode = mode;
			});
		}
	});
	return this;
}

/**
 * Cross-browser function to execute designMode commands
 * Example: $('#foo').execCommand('formatblock', '<p>');
 *
 * @uses jQuery, jQuery.fn.contentDocument
 *
 * @param string cmd - The command to execute. Please see http://www.mozilla.org/editor/midas-spec.html
 * @param string param - Optional parameter, required by some commands
 *
 * @return jQuery element - The jQuery element itself, to allow chaining
 */
jQuery.fn.execCommand = function(cmd, param) {
	this.each(function() {
		var doc = $(this).contentDocument();
		if (doc) {
			// Use try-catch in case of invalid or unsupported commands
			try {
				// Non-IE-browsers requires all three arguments
				doc.execCommand(cmd, false, param);
			} catch (e) {
			}
		}
	});
	return this;
}

//another inline editor possibility
function wys_ToggleEditable(button, what) {
	var ele = document.getElementById(what);
	if (ele.contentEditable == "true") {
		ele.contentEditable = "false";
		button.innerHTML = "Enable editing!";
	}
	else {
		ele.contentEditable = "true";
		button.innerHTML = "Disable editing!";
	}
}

//Toggle admin options, for zpiderz-admin only!
function wys_admin(frame){
	var admin_cont = $('#wys_admin_options');
	if(admin_cont.display="hidden"){
		admin_cont.slideToggle();
		admin_cont.display="block";
	}else if(admin_cont.display="block"){
		admin_cont.slideUp();
		admin_cont.display="hidden";
	}
}
//Simple activation
function wys_activate(frame, textarea, textid){
	var iframeRef = $('#'+frame);
	iframeRef.designMode();
	if(textarea!=null){
		//iframeRef.contents().find('html body').html(content);
		// org == text -> empty -> html
		var content = $('#'+textarea).text();
		iframeRef.contents().find('html body').empty;
		iframeRef.contents().find('html body').css("background-color", "#FFF");
		if(content.search(/<script/i)>=0 && $("#current_view")!=null){
			iframeRef.contents().find('html body').text(content);
			$("#current_view").val("html");
			if($("#wys_switch")!=null){
				$("#wys_switch").val("text");
			}
		}else if($("#current_view")!=null){
			iframeRef.contents().find('html body').html(content);
			$("#current_view").val("text");
			if($("#wys_switch")!=null){
				$("#wys_switch").val("html");
			}
		}else{
			iframeRef.contents().find('html body').html(content);
		}
	}
	var Fsize = $("#textedit_cursize_"+textid);
	if(Fsize != null){
		if(Fsize.val()=="big"){
			iframeRef.css("height", "500px");
		}
	}
}
//simple functions
function wys_function(frame, func){
	$('#'+frame).designMode().execCommand(func);
}
//advanced functions
function wys_advanced(frame, func){
	$('#'+frame).designMode().execCommand(func);
}
//Custom function applied with html or php
//Sadly IE isn't Firefox or Chrome and insertHTML does nothing
function wys_custom(frame, source, code){
	if(source!=null){
		var code = document.getElementById(source).value;
	}
	if(code!=null){
		$('#'+frame).designMode().execCommand('insertHTML', code);
	}else{
		$('#'+frame).designMode().execCommand('insertHTML', '<b style="color:red;">OOPS!</b>');
	}
}

function wys_SizeChange(frame, size){
	var selected_size = document.getElementById(size).value;
	if(selected_size!=null){
		$('#'+frame).execCommand('fontsize', selected_size);
		//$('#'+frame).execCommand('formatblock', '<p style="font-size:'+selected_size+'px;">');
		//$('#'+frame).designMode().execCommand('insertHTML', '<span style="font-size:'+selected_size+'px;"></span>');
	}else{
		$('#'+frame).execCommand('fontsize', false);
	}
}
function wys_FontChange(frame, font){
	var selected_font = document.getElementById(font).value;
	if(selected_font!=""){
		$("#"+frame).designMode().execCommand("fontname", selected_font);
	}else{
		$("#"+frame).designMode().execCommand("fontname", false);
	}
}

function wys_changeHeader(frame, header_input){
	var selected_header = document.getElementById(header_input).value;
	if(selected_header!=""){
		$("#"+frame).designMode().execCommand("formatblock", selected_header);
		document.getElementById("no_header").selected=true;
	}else{
		$("#"+frame).designMode().execCommand("formatblock", false);
	}
}

function wys_color(frame){
	dColorPicker('wys_color', 'popbase_r', 'popbar_r', 'Fontcolor', wys_colorHandler)
}

function wys_colorHandler(color){
	var frame = document.getElementById("wys_wtb").value;
	var inp = document.getElementById("wys_color_input");
	inp.value = color;
	if(color.substr(0, 4) == 'sys_') {
		var cid = color.replace(/sys_/, "")
		inp.style.backgroundColor = getSystemColorById(cid);
		$('#'+frame).designMode().execCommand('forecolor', color);
	} else {
		inp.style.backgroundColor = color;
		$('#'+frame).designMode().execCommand('forecolor', color);
	}
}
function Wys_rightclick(event){
	if (event.button==2){
		alert("You clicked the right mouse button!")
	}else{
		alert("You clicked the left mouse button!")
	}
}

function wys_SimpleImage(frame, location){
	if(location!=null){
		var imgSrc = location;
	}else{
		var imgSrc = prompt('Enter image location', '');
	}
	if(imgSrc != null){
		$('#'+frame).designMode().execCommand('insertimage', imgSrc);
	}
}

function wys_advImage(frame, src, w, h, a, pad, marg, border, txtalign, onclick, onmouseover, onmouseout){
	var style = 'width:'+w+'px;height:'+h+'px;text-align:'+a+';';
	if(pad!=false){
		style += 'padding:'+pad+'px;';
	}
	if(marg!=false){
		style += 'margin:'+marg+'px;';
	}
	if(border!=false){
		style += 'border:'+border+'px solid black;';
		//more style options later
	}
	if(txtalign!=false){
		style += 'float:'+txtalign+';';
	}
	var mouse = "";
	if(onclick!=false){
		mouse +='onclick="'+onclick+'" ';
		style += 'cursor:pointer;';
	}
	if(onmouseover!=false){
		mouse +='onmouseover="'+onmouseover+'" ';
	}
	if(onmouseout!=false){
		mouse +='onmouseout="'+onmouseout+'" ';
	}
	var code =  '<img src="'+src+'" alt="'+frame+'_image" style="'+style+'" '+mouse+'/>';
	$('#'+frame).designMode().execCommand('insertHTML', code);
}

function wys_advLink(frame, link, name, target){
	if(name==null){
		var name = link;
	}
	if(link.search(OWN_ADDRESS)==0){
		var klass='class="wys_intern" ';
	}else{
		var klass="";
	}
	var code = '<a '+klass+'href="'+link+'" target="'+target+'">'+name+'</a>';
	$('#'+frame).designMode().execCommand('insertHTML', code);
}

function wys_tableInsert(frame){
	var nr_rows = prompt("Number of rows");
	var nr_columns = prompt("Number of columns");
	if (nr_rows<1){ var nr_rows = 2; }
	if (nr_columns<1){ var nr_columns = 2; }
	if(nr_rows && nr_columns){
		var rows = "";
		var columns = "";
		for(i = 0; i < nr_columns; i++){
			columns += '<td style="width:20px;height:10px;border:1px solid black;margin:0px;padding:0px;"></td>';
		}
		if(columns!=""){
			for(i = 0; i < nr_rows; i++){
				rows += '<tr style="margin:0px;padding:0px;">'+columns+'</tr>';
			}
		}
		if(rows!=""){
			var code = '<table style="margin:0px;padding:0px;">'+rows+'</table>';
			$('#'+frame).designMode().execCommand('insertHTML', code);
		}
	}
}

function wys_Youtube(frame){
	var ytcode = prompt("Enter videocode here");
	if(ytcode!=null && ytcode!=""){
		var code = '<table><tr><td height="310px" width="560px"><iframe src="http://www.youtube.com/embed/'+ytcode+'" allowfullscreen="" frameborder="0" height="100%" width="100%"></iframe></td></tr></table>';
		$('#'+frame).designMode().execCommand('insertHTML', code);
	}
}

function wys_email(frame){
	var email = prompt("Enter email here");
	if(email!=null && email!=""){
	var name = prompt("Enter name here");
		if(name==null || name==""){
			var name = email;
		}
		var code = '<a href="mailto:'+email+'">'+name+'</a>';
		$('#'+frame).designMode().execCommand('insertHTML', code);
	}
}

function wys_predefined(frame, object){
	wys_selection(frame, object);
}

//for html contents
function wys_img(frame, textid, texlang, pid, lang, page){
	var selection = "";
	var framed = document.getElementById(frame).contentWindow.document;
	var needle = framed.getSelection ? framed.getSelection() : framed.selection.createRange(); // FF : IE
	if(needle!="" && needle!=null){
		var haystack = $('#'+frame).contents().find('html body').html();
		var newText = haystack.replace(needle,'<b>' + needle + '</b>');
		//$('#'+frame).contents().find('html body').html(newText);
		var range = document.getElementById(frame).contentWindow.getSelection().getRangeAt(0);
		//alert(range.startOffset);
		//alert(range.endOffset);
		var docFragment = range.cloneContents ();
		var tmpDiv = framed.createElement ("div");
		tmpDiv.appendChild(docFragment);
		selHTML = tmpDiv.innerHTML;
		if(selHTML == null){      // Internet Explorer before version 9
			var textRange = framed.selection.createRange ();
			selText = textRange.text;
			selHTML = textRange.htmlText;
        }
		if(selHTML.search('<img')>=0){
			//alert(selHTML);
			var edit_img = "";
			var src_image = "images/images/";
			edit_img += "&src="+selHTML.substring(selHTML.indexOf(src_image)+src_image.length, selHTML.search('" '));
			selHTML = selHTML.slice(selHTML.search('" ')+1);
			edit_img += "&width="+selHTML.substring(selHTML.indexOf('width:')+6, selHTML.search('px;'));
			selHTML = selHTML.slice(selHTML.search(';')+1);
			edit_img += "&height="+selHTML.substring(selHTML.indexOf('height:')+7, selHTML.search('px;'));
			selHTML = selHTML.slice(selHTML.search(';')+1);
			edit_img += "&talign="+selHTML.substring(selHTML.indexOf('text-align:')+11, selHTML.search(';'));
			selHTML = selHTML.slice(selHTML.search(';')+1);
			var padding = selHTML.substring(selHTML.indexOf('padding:')+8, selHTML.search('px;'));
			if(selHTML.indexOf('padding:')>=0){
				edit_img += "&padding="+padding;
				selHTML = selHTML.slice(selHTML.search(';')+1);
			}
			var margin = selHTML.substring(selHTML.indexOf('margin:')+7, selHTML.search('px;'));
			if(selHTML.indexOf('margin:')>=0){
				edit_img += "&margin="+margin;
				selHTML = selHTML.slice(selHTML.search(';')+1);
			}
			var border = selHTML.substring(selHTML.indexOf('border:')+7, selHTML.search('px solid black;'));
			if(selHTML.indexOf('border:')>=0){
				edit_img += "&border="+border;
				selHTML = selHTML.slice(selHTML.search(';')+1);
			}
			var floaty = selHTML.substring(selHTML.indexOf('float:')+6, selHTML.search(';'));
			if(selHTML.indexOf('float:')>=0){
				edit_img += "&float="+floaty;
				selHTML = selHTML.slice(selHTML.search(';')+1);
			}
			if(selHTML.indexOf('onclick="')>=0){
				selHTML = selHTML.slice(selHTML.indexOf('onclick="')+9);
				var click = selHTML.substring(0, selHTML.search('"'))+"_";
				selHTML = selHTML.slice(selHTML.search('"')+1);
				if(click.indexOf('popMyImage')>=0){
					var click = "1~1"; //Image in new page
				}else if(click.indexOf('window.open(\''+OWN_ADDRESS+'/images/')>=0){
					var click = "1~2"; //Image in pop-up
				}else if(click.indexOf('document.location = \''+OWN_ADDRESS)>=0){
					var link = click.substring(click.indexOf('page'), click.search('\'_'));
					var click = "2~"+link; //Link to internal page
				}else if(click.indexOf('window.open')>=0){
					var link = click.substring(click.indexOf('http:\/\/'), click.search('_')-2);
					var click = "3~"+link; //Link to external page
					edit_img += "&targ=new";
				}else if(click.indexOf('document.location = \'')>=0){
					var link = click.substring(click.indexOf('http:\/\/'), click.search('_')-1);
					var click = "3~"+link; //Link to external page	
					edit_img += "&targ=same";
				}else if(click.indexOf('showMyPop')>=0){
					var click = "4~"; // open custom pop-up
				}
				edit_img += "&onclick="+click;
			}
			if(selHTML.indexOf('onmouseover="')>=0){
				selHTML = selHTML.slice(selHTML.indexOf('onmouseover="')+13);
				var mouseover = selHTML.substring(0, selHTML.search('"'))+"~!";
				if(mouseover.indexOf('showMyPic')>=0 && mouseover.indexOf('false')>=0){
					var mouseover = "1~1";
				}
				else if(mouseover.indexOf('showMyPic')>=0){
					var mouseover = "1~2";
				}
				else if(mouseover.indexOf('swapMyPic')>=0){
					var mousepic = mouseover.substring(mouseover.indexOf(src_image)+src_image.length, mouseover.search('~!')-2);
					var mouseover = "2~"+mousepic;
				}
				edit_img += "&mouseover="+mouseover;
			}
			//alert(selHTML);
			if(edit_img!=""){
				textImageHandler(textid, texlang, pid, lang, page, edit_img);
			}
		}else{
			textImageHandler(textid, texlang, pid, lang, page);
		}
	}else{
		textImageHandler(textid, texlang, pid, lang, page);
	}
}
//impossible to undo
function wys_selection(frame, object, style){
	var edit = document.getElementById(frame).contentDocument;
	var selection = document.getElementById(frame).contentWindow.getSelection();
	var rangeCount = selection.rangeCount;
	//var attrib = source[source.selectedIndex].getAttribute("attrib");
	var iframe = document.getElementById(frame).contentWindow;
	range = selection.getRangeAt(0).cloneRange();
	var temp = range.toString(); 
	if(temp == ""){
		if(object!=null){
			var newNode = document.createElement(object);
		}else{
			var newNode = document.createElement("span");
			var object = "span";
		}
		var textNode = document.createTextNode(" ");
		newNode.appendChild(textNode);
		range.insertNode(newNode);
		if(style!=null){
			var value = style;
		}else{
			var value = "font-size: 20px;";//defined for changes
		}
		newNode.setAttribute("style", value);
		var kids = newNode.childNodes;
		var count =0;
		for(var index=0; index < kids.length; index++){
			if(kids[index].nodeType== 3){
				count = index;
			}
		}
		var endNode = newNode.childNodes.item(count);
		var endOffset = newNode.childNodes.item(count).length;
		range.setEnd(endNode,endOffset);
		range.setStart(endNode,endOffset);
		selection.removeAllRanges();
		selection.addRange(range);
		iframe.focus();
	} else{
		if (rangeCount == 1){
			range = selection.getRangeAt(0).cloneRange();
		}else{
			range = document.createRange();
			start = selection.getRangeAt(0);
			range.setStart(start.startContainer, start.startOffset);
			end = selection.getRangeAt(--rangeCount);
			range.setEnd(end.endContainer, end.endOffset);
		}
		alert(range);
		var newNode = wys_surround(range, object);
		var value = "font-size: 20px;";
		newNode.setAttribute("style", value);
		range.selectNodeContents(newNode);
		//selection.removeRange(range);
		selection.addRange(range);
		document.getElementById(frame).contentWindow.focus();
	}
}

function wys_surround(thisRange, tagName){
	var newNode = document.createElement(tagName);
	newNode.appendChild(thisRange.extractContents());
	thisRange.insertNode(newNode);
	return newNode;
}

function wys_ToggleView(frame){
	var iframeRef = $('#'+frame);
	var buttonRef = $('#wys_switch').val();
	var hiddenRef = $("#current_view");
	if(buttonRef=="html"){
		var content = iframeRef.contents().find('html body').html();
		iframeRef.contents().find('html body').empty();
		iframeRef.contents().find('html body').text(content);
		$('#wys_switch').val("text");
		hiddenRef.val("html");
	}else{
		var content = iframeRef.contents().find('html body').text();
		var scripted = content.search(/<script/i);
		if(scripted>=0){
			var conf_sw = confirm("Als u doorgaat zal alle javascript verloren gaan, wilt u doorgaan?");
		}
		if(conf_sw==true || conf_sw==null){
			iframeRef.contents().find('html body').empty();
			iframeRef.contents().find('html body').html(content);
			$('#wys_switch').val("html");
			hiddenRef.val("text");
		}
	}
}
function wys_ChangeTextId(frame, type, lang){
	var iframeRef = $('#'+frame);
	var hiddenRef = $("#current_view").val();
	var old_text = $("#org_txt_id").val();
	var new_text = $("#new_txt_id").val();
	var wind = $("#textedit_wind_"+old_text).val();
	if(type=="reset"){
		var new_text = old_text;
	}
	if(wind==null){
		var wind="";
	}
	if(old_text!="" && new_text!="" && wind!=""){
		$.post(OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.copytext.php?lang="+lang+"&newid="+new_text+"&oldid="+old_text+"&type="+type+"&wind="+wind, function(data) {
			if(data=="same" && type=="replace"){
				alert("Het door U opgegeven id is gelijk aan de huidige.");
			}else if(data=="false_id"){
				alert("Het door U opgegeven id bestaat niet.");
			}else if(data=="no_content"){
				alert("Het door U opgegeven id bestaat niet.");
			}else if(type == "copy" || type == "reset"){
				//alert(data);
				iframeRef.contents().find('html body').empty();
				if(hiddenRef == "text"){
					iframeRef.contents().find('html body').html(data);
				}else{
					iframeRef.contents().find('html body').text(data);
				}
			}else if(type == "replace"){
				var checkIfOk = confirm("Weet u zeker dat u het huidige Id wilt overschrijven?");
				if(checkIfOk){
					$.post(OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.copytext.php?lang="+lang+"&newid="+new_text+"&oldid="+old_text+"&type=agreed&wind="+wind, function(data) {
						location.reload();
					});
				}
			}
			$("#new_txt_id").val("");
		});
	}
}
function wys_ViewChoice(frame){
	var iframeRef = $('#'+frame);
	var hiddenRef = $("#current_view");
	var view_choice = document.getElementById("view_select").value;
	if(view_choice == "html" ){
		var content = iframeRef.contents().find('html body').html();
		iframeRef.contents().find('html body').empty();
		iframeRef.contents().find('html body').text(content);
		hiddenRef.val("html");
	}
	else if(view_choice == "wys") {
		var content = iframeRef.contents().find('html body').text();
		iframeRef.contents().find('html body').empty();
		iframeRef.contents().find('html body').html(content);
		hiddenRef.val("text");
	}
	else if(view_choice == "bb"){
	
	}
}

//Checking it
function wys_check(id, type){
	if(type=="html"){
		var result = $('#'+id).html();		
	}else{
		var result = $('#'+id).text();
	}
	if(result!=null){
		alert(result);
	}else{
		alert('FAIL!');
	}
}

// "get" can be included with the destination as well
function wys_post(input, destination, get) {
	var script = OWN_ADDRESS+SYS_DIR+destination;
	if (get!=null){
		script += get;
	}
	var handler = wys_handlesend;
	var args = new Array();
	args["wut"] = input;
	ajaxSendGet(script, handler, args);
}

function wys_handlesend(args) {
	var wot = args["response"];
	if (wot == true) {
		document.location = document.location;
	}
}
//Sending it
// styles for now html, text(text-only), bb-code
function wys_send(frame, style, destination, lang){
	var iframeRef = $('#'+frame);
	var textarea = $('#'+destination);
	if(textarea!=null){
		textarea.empty();
	}
	var current_style = document.getElementById("current_view").value;
	if(current_style == null){
		var current_style = "text";
	}
	if(current_style == "html" && style == "html"){
		var icontent = iframeRef.contents().find('html body').text().replace(/\.\.\//g, "");
	}
	else if(current_style == "text" && style == "text"){
		var icontent = iframeRef.contents().find('html body').text().replace(/\.\.\//g, "");
	}
	else if(current_style == "html" && style == "text"){
		var icontent = iframeRef.contents().find('html body').text().replace(/\.\.\//g, "");
	}
	else if(current_style == "text" && style == "html"){
		var icontent = iframeRef.contents().find('html body').html().replace(/\.\.\//g, "");
	}
	var xcontent = icontent.replace(/images\/images\//g, OWN_ADDRESS+"/images/images/");
	var url_check = xcontent.replace(/class=\"wys_intern\" href=\"/g, "href=\""+OWN_ADDRESS+"/");
	var rex = new RegExp(OWN_ADDRESS+"/"+OWN_ADDRESS, "g");
	var content = url_check.replace(rex, OWN_ADDRESS);
	var decode = textarea.text(content);
	if(decode!=null ){
		//decode.replace("../", OWN_ADDRESS+"/");
		u_doneEditItem(lang);
	}
}
function wys_cancel(win){
	//Will change
	if (win!=null){
		$('#'+win).destroy();
	}else{
		u_cancelEditItem();
	}
}

function textLinkHandler(textid, lang) {
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.textlink.php?pid="+PID+"&lang="+LANG+"&textid="+textid+"&textlang="+lang+"&page="+PAGE+"&wys=true";
	var handler = showTextLinkHandler;
	var args = new Array();
	args["textid"] = textid;
	args["textlang"] = lang;
	ajaxSendGet(script, handler, args);
}

function showTextLinkHandler(args) {
	timgPop = standardPop("texinput_link_"+args["textid"]+"_"+args["textlang"], "popbase_r", "popbar_r", "Add new link:", true);
	timgPop.setWidth(300);
	timgPop.fill(args["response"]);
	timgPop.center();
	timgPop.show();
}

function textLinkSubmit(suffix) {
	var suberr	= "";
	var sfx		= suffix.split("_");
	var form	= document.getElementById("textlink_form_"+suffix);
	var target	= form["target_"+suffix].value;
	var text	= form["text_"+suffix].value;
	if(text == "") {
		suberr += "-No Text given.<br />";
	}
	//	onclick
	var href;
	if(document.getElementById("i_href_"+suffix+"_1").checked == true) {
		var shor = document.getElementById("link_page_"+suffix);
		var href = shor.value;
	} else if(document.getElementById("i_href_"+suffix+"_2").checked == true) {
		var shor = document.getElementById("link_address_"+suffix);
		if(shor.value == "") {
			suberr += "-No outside link given.<br />";
		} else {
			href = 'http://'+shor.value;
		}
	}

	if(suberr) {
		document.getElementById("textlink_error_"+suffix).innerHTML = suberr;
	} else {
		var frame = $("#framename").val();
		if(frame==null){
			var frame = "input_frame";
		}
		wys_advLink(frame, href, text, target);
		var killme;
		if(killme = document.getElementById("texinput_link_"+suffix)) {
			killme.destroy();
		}
	}
}

function cancelTextLink(suffix) {
	var killme;
	if(killme = document.getElementById("texinput_link_"+suffix)) {
		killme.destroy();
	}
}

function textImageHandler(textid, texlang, pid, lang, page, edit) {
	//new inputs don't need textid's
	if(textid==""){
		var textid = 0;
	}
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.textimage.php?pid="+pid+"&lang="+lang+"&textid="+textid+"&textlang="+texlang+"&page="+page+"&wys=true";
	if(edit!=null){
		script += edit;
	}
	var handler = showTextImageHandler;
	var args = new Array();
	args["textid"] = textid;
	args["textlang"] = lang;
	ajaxSendGet(script, handler, args);
}

function showTextImageHandler(args) {
	timgPop = standardPop("texinput_image_"+args["textid"]+"_"+args["textlang"], "popbase_r", "popbar_r", "Add image:", true);
	timgPop.setWidth(300);
	timgPop.fill(args["response"]);
	timgPop.center();
	timgPop.show();
}

function textImageSelect(img, wind, name, which, width, height) {
	hideMyPic();
	if(which == "textimage") {
		var inp = document.getElementById("textimage_select_"+wind);
		inp.value = name;
		var pwt = document.getElementById("text_i_width_"+wind);
		pwt.value = width;
		var pht = document.getElementById("text_i_height_"+wind);
		pht.value = height;
		var prev = document.getElementById("textimage_preview_"+wind);
		var prevct = '<img src="'+OWN_ADDRESS+'/images/images/thumbs/'+name+'" onmouseover="javascript: parent.showMyPic(\'images/images/previews/'+name+'\', this, false, true);" onmouseout="javascript: parent.hideMyPic();" style="border: 1px solid black" />';
		prev.innerHTML = prevct;
	} else if(which == "textswap") {
		var inp = document.getElementById("textswap_image_select_"+wind);
		inp.value = name;
		var prev = document.getElementById("textswap_image_preview_"+wind);
		var prevct = '<img src="'+OWN_ADDRESS+'/images/images/thumbs/'+name+'" onmouseover="javascript: parent.showMyPic(\'images/images/previews/'+name+'\', this, false, true);" onmouseout="javascript: parent.hideMyPic();" style="border: 1px solid black" />';
		prev.innerHTML = prevct;
	}
	simid = "set_image_"+wind;
	document.getElementById(simid).destroy();
}

function cancelTextImage(suffix) {
	var killme;
	if(killme = document.getElementById("texinput_image_"+suffix)) {
		killme.destroy();
	}
}

function textImageSubmit(suffix, frame) {
	var suberr = "";
	var sfx = suffix.split("_");
	var form = document.getElementById("textimage_form_"+suffix);
	// src
	var srcname = "image_select_"+suffix;
	var imgsrc = form[srcname].value;
	if(imgsrc == "") {
		suberr += "-No image selected.<br />";
	}
	// height
	var heightname = "text_i_height_"+suffix;
	var imgheight = form[heightname].value;
	// width
	var widthname = "text_i_width_"+suffix;
	var imgwidth = form[widthname].value;
	// align
	var alignname = "img_align_"+suffix;
	var imgalign = form[alignname].value;
	imgalign = (imgalign == "none")?false:imgalign;
	
	// style options
	//padding
	var paddname = "text_i_padding_"+suffix;
	var imgpadding = form[paddname].value;
	if(imgpadding==""){
		var imgpadding = false;
	}
	//margin
	var margname = "text_i_margin_"+suffix;
	var imgmargin = form[margname].value;
	if(imgmargin==""){
		var imgmargin = false;
	}
	//border
	var bordname = "text_i_border_"+suffix;
	var imgborder = form[bordname].value;
	if(imgborder==""){
		var imgborder = false;
	}
	//textalign
	var txtname = "text_i_txt_"+suffix;
	var imgtxt = form[txtname].value;
	if(imgtxt==""){
		var imgtxt = false;
	}
	//	onclick
	var i_onclick;
	if(document.getElementById("i_onclick_"+suffix+"_0").checked == true) {
		i_onclick = false;
	} else if(document.getElementById("i_onclick_"+suffix+"_1").checked == true) {
		var shor = document.getElementById("show_original_"+suffix);
		if(shor.value == "popup") {
			i_onclick = "popMyImage('imgpop_"+imgsrc+"', 'Image:', '"+OWN_ADDRESS+"/images/images/"+imgsrc+"')";
		} else if(shor.value == "seperate") {
			i_onclick = "window.open('"+OWN_ADDRESS+"/images/images/"+imgsrc+"')";
		}
	} else if(document.getElementById("i_onclick_"+suffix+"_2").checked == true) {
		var shor = document.getElementById("link_page_"+suffix);
		i_onclick = "document.location = '"+OWN_ADDRESS+"/index.php?lang="+sfx[1]+"&page="+shor.value+"'";
	} else if(document.getElementById("i_onclick_"+suffix+"_3").checked == true) {
		var shor = document.getElementById("outside_address_"+suffix);
		var targ = document.getElementById("outside_target_"+suffix);
		if(shor.value == "") {
			suberr += "-No outside link given.<br />";
		} else if(targ.value == "new") {
			i_onclick = "window.open('http://"+shor.value+"')";
		}else{
			i_onclick = "document.location = 'http://"+shor.value+"'";
		}
	} else if(document.getElementById("i_onclick_"+suffix+"_4").checked == true) {
		var shor = document.getElementById("togglepop_"+suffix);
		i_onclick = "showMyPop('container_"+shor.value+"', '1')";
	}

	// onmouseover
	var i_onmouseover;
	var i_onmousout;
	if(document.getElementById("i_mouseover_"+suffix+"_0").checked == true) {
		i_onmouseover = false;
		i_onmouseout = false;
	} else if(document.getElementById("i_mouseover_"+suffix+"_1").checked == true) {
		var shor = document.getElementById("mouse_preview_"+suffix);
		if(shor.value == "mouse") {
			i_onmouseover = "showMyPic('"+OWN_ADDRESS+"/images/images/"+imgsrc+"', this, false, false)";
			i_onmouseout = "hideMyPic()";
		} else if(shor.value == "default") {
			i_onmouseover = "showMyPic('"+OWN_ADDRESS+"/images/images/"+imgsrc+"')";
			i_onmouseout = "hideMyPic()";
		}
	} else if(document.getElementById("i_mouseover_"+suffix+"_2").checked == true) {
		var shor = document.getElementById("textswap_image_select_"+suffix);
		if(shor.value == "") {
			suberr += "-No swap image given.<br />";
		} else {
			i_onmouseover = "swapMyPic(this, '"+OWN_ADDRESS+"/images/images/"+shor.value+"')";
			i_onmouseout = "swapMyPic(this, '"+OWN_ADDRESS+"/images/images/"+imgsrc+"')";
		}
	}

	if(suberr) {
		document.getElementById("textimage_error_"+suffix).innerHTML = suberr;
	} else {
		var textinp;
		var fullsrc = OWN_ADDRESS+"/images/images/"+imgsrc;
		var frame = $("#framename").val();
		if(frame==null){
			var frame = "input_frame";
		}
		//wys_SimpleImage(frame, fullsrc);
		wys_advImage(frame, fullsrc, imgwidth, imgheight, imgalign, imgpadding, imgmargin, imgborder, imgtxt, i_onclick, i_onmouseover, i_onmouseout);
		var killme;
		if(killme = document.getElementById("texinput_image_"+suffix)) {
			killme.destroy();
		}
	}
}

function setImage(wind, which) {
	simid = "set_image_"+wind;
	var iPop;
	if(iPop = document.getElementById(simid)) {
		iPop.destroy();
	}
	iPop = standardPop(simid, "popbase_r", "popbar_r", "Choose image:", true);
	iPop.setWidth(600);
	iPop.show();
	iPop.center();
	iPop.fill('<div style="text-align: center;"><img src="'+OWN_ADDRESS+'/images/textinput/loading.gif" /></div>');
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.setimage.php?pid="+PID+"&lang="+LANG+"&which="+which+"&wind="+wind;
	var handler = showImages;
	var args = new Array();
	args["popup"] = iPop;
	args["wind"] = wind;
	ajaxSendGet(script, handler, args);
}

function showImages(args) {
	args["popup"].fill(args["response"]);
	args["popup"].center();
}

function updateImage(img, wind, name, which, width, height) {
	hideMyPic();
	if(document.getElementById("img_init_"+wind)!=null){
		var imgframe = document.getElementById("img_init_"+wind).contentWindow.document;
	}
	if(which == "image") {
		var inp = imgframe.getElementById("image_select_"+wind);
		inp.value = img;
		var pwt = imgframe.getElementById("i_width_"+wind);
		pwt.value = width;
		var pwtr = imgframe.getElementById("i_width_"+wind+"_ref");
		pwtr.value = width;
		var pht = imgframe.getElementById("i_height_"+wind);
		pht.value = height;
		var phtr = imgframe.getElementById("i_height_"+wind+"_ref");
		phtr.value = height;
		var prev = imgframe.getElementById("image_preview_"+wind);
		var prevct = '<img src="'+OWN_ADDRESS+'/images/images/thumbs/'+name+'" onmouseover="javascript: parent.showMyPic(\''+OWN_ADDRESS+'/images/images/previews/'+name+'\', this, false, true);" onmouseout="javascript: parent.hideMyPic();" style="border: 1px solid black" />';
		prev.innerHTML = prevct;
	} else if(which == "swap") {
		var inp = imgframe.getElementById("swap_image_select_"+wind);
		inp.value = img;
		var prev = imgframe.getElementById("swap_image_preview_"+wind);
		var prevct = '<img src="'+OWN_ADDRESS+'/images/images/thumbs/'+name+'" onmouseover="javascript: parent.showMyPic(\''+OWN_ADDRESS+'/images/images/previews/'+name+'\', this, false, true);" onmouseout="javascript: parent.hideMyPic();" style="border: 1px solid black" />';
		prev.innerHTML = prevct;
	} else if(which == "textimage"){
		//changes might occur
		var inp = document.getElementById("textimage_select_"+wind);
		inp.value = name;
		var pwt = document.getElementById("text_i_width_"+wind);
		pwt.value = width;
		var pht = document.getElementById("text_i_height_"+wind);
		pht.value = height;
		var prev = document.getElementById("textimage_preview_"+wind);
		var prevct = '<img src="'+OWN_ADDRESS+'/images/images/thumbs/'+name+'" onmouseover="javascript: parent.showMyPic(\''+OWN_ADDRESS+'/images/images/previews/'+name+'\', this, false, true);" onmouseout="javascript: parent.hideMyPic();" style="border: 1px solid black" />';
		prev.innerHTML = prevct;
	}
	simid = "set_image_"+wind;
	document.getElementById(simid).destroy();
}
//will be able to put anywhere, I hope..
function wys_editpop(textid, element){
	simid = "set_text_"+textid;
	var iPop;
	if(iPop = document.getElementById(simid)) {
		iPop.destroy();
	}
	iPop = standardPop(simid, "popbase_r", "popbar_r", "Edit text:", true);
	iPop.setWidth(600);
	iPop.show();
	iPop.center();
	iPop.fill('<div style="text-align: center;"><img src="'+OWN_ADDRESS+'/images/textinput/loading.gif" /></div>');
	var script = OWN_ADDRESS+"/"+SYS_DIR+"/modules/cms/ajax/ajax.newtextedit.php?pid="+PID+"&lang="+LANG+"&textid="+textid+"&element="+element;
	var handler = wys_pophandler;
	var args = new Array();
	args["popup"] = iPop;
	ajaxSendGet(script, handler, args);
}

function wys_pophandler(args) {
	args["popup"].fill(args["response"]);
	args["popup"].center();
}
function wys_black_white(frame){
	var iframeRef = $('#'+frame);
	var buttonRef = $('#temp_bgswitch').val();
	if(buttonRef=="black"){
		$('#temp_bgswitch').val("white");
		iframeRef.contents().find('html body').css("background-color", "#000");
	}else{
		$('#temp_bgswitch').val("black");
		iframeRef.contents().find('html body').css("background-color", "#FFF");
	}
}
//Copied CMS editor for general use
