jQuery.fn.getContentDocument = 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;
	}
}

jQuery.fn.designMode = function(mode) {
	// Default mode is 'on'
	var mode = mode || 'on';
	this.each(function() {
		var frame = $(this);
		var doc = frame.getContentDocument();
		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).getContentDocument().designMode = mode;
			});
		}
	});
	return this;
}

jQuery.fn.execCommand = function(cmd, param) {
	this.each(function() {
		var doc = $(this).getContentDocument();
		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) {
			//alert('j - ' + e);
			}
		}
	});
	return this;
}


var editors = new Array();
//yuiTextEditor_text
function initYUI (targetID, externalStyleURL) {

	var Dom = YAHOO.util.Dom,
	Event = YAHOO.util.Event;
var myConfig = {
    height: '200px',
    width: '100%',
	toolbar: {
	collapse: true,
	titlebar: 'Text Edit',
	draggable: false,
	buttonType: 'advanced',
	buttons: [
		{ group: 'fontstyle', label: ' <br />',
			buttons: [
				{ type: 'select', label: 'Arial', value: 'fontname', disabled: false,
					menu: [
						{ text: 'Arial', checked: true },
						{ text: 'Arial Black' },
						{ text: 'Comic Sans MS' },
						{ text: 'Courier New' },
						{ text: 'Lucida Console' },
						{ text: 'Tahoma' },
						{ text: 'Times New Roman' },
						{ text: 'Trebuchet MS' },
						{ text: 'Verdana' }
					]
				},
				{ type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: false }
			]
		},
		{ type: 'separator' },
		{ group: 'textstyle', label: ' <br />',
			buttons: [
				{ type: 'push', label: 'Bold CTRL + SHIFT + B', value: 'bold' },
				{ type: 'push', label: 'Italic CTRL + SHIFT + I', value: 'italic' },
				{ type: 'push', label: 'Underline CTRL + SHIFT + U', value: 'underline' },
				{ type: 'separator' },
				{ type: 'push', label: 'Subscript', value: 'subscript', disabled: false },
				{ type: 'push', label: 'Superscript', value: 'superscript', disabled: false },
				{ type: 'separator' },
				{ type: 'color', label: 'Font Color', value: 'forecolor', disabled: false },
				{ type: 'color', label: 'Background Color', value: 'backcolor', disabled: false },
				{ type: 'separator' },
				{ type: 'push', label: 'Remove Formatting', value: 'removeformat', disabled: false },
				{ type: 'push', label: 'Show/Hide Hidden Elements', value: 'hiddenelements' }
			]
		},
		{ type: 'separator' },
		{ group: 'alignment', label: ' <br />',
			buttons: [
				{ type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft' },
				{ type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter' },
				{ type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright' },
				{ type: 'push', label: 'Justify', value: 'justifyfull' }
			]
		},
		{ type: 'separator' },
		{ group: 'parastyle', label: ' <br />',
			buttons: [
			{ type: 'select', label: 'Normal', value: 'heading', disabled: false,
				menu: [
					{ text: 'Normal', value: 'none', checked: true },
					{ text: 'Header 1', value: 'h1' },
					{ text: 'Header 2', value: 'h2' },
					{ text: 'Header 3', value: 'h3' },
					{ text: 'Header 4', value: 'h4' },
					{ text: 'Header 5', value: 'h5' },
					{ text: 'Header 6', value: 'h6' }
				]
			}
			]
		},
		{ type: 'separator' },
		{ group: 'indentlist', label: ' <br />',
			buttons: [
				{ type: 'push', label: 'Indent', value: 'indent', disabled: false },
				{ type: 'push', label: 'Outdent', value: 'outdent', disabled: false },
				{ type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' },
				{ type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }
			]
		},
		{ type: 'separator' },
		{ group: 'insertitem', label: ' <br />',
			buttons: [
				{ type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: false }
			]
		}
	]
	}

};


	editors[targetID] = new YAHOO.widget.Editor('yuiTextEditor_text' + targetID, myConfig);
	editors[targetID]._defaultToolbar.buttonType = 'advanced';
	editors[targetID].on('toolbarLoaded', function() {
    editors[targetID].toolbar.on('createlinkClick', function() {
    editors[targetID].on('afterExecCommand', function() {
              var a = editors[targetID].currentElement[0];
               if (a) {
                    a.setAttribute('class', 'linkintext');
					//alert(a.getAttribute('href'));
               }
            });
        });
    });


	if (externalStyleURL != undefined) {
		editors[targetID].on('editorContentLoaded', function() {
			var head = this._getDoc().getElementsByTagName('head')[0];
			var link = this._getDoc().createElement('link');
			link.setAttribute('rel', 'stylesheet');
			link.setAttribute('type', 'text/css');
			link.setAttribute('href', externalStyleURL);
			head.appendChild(link);

		}, editors[targetID], true);
	}





	editors[targetID].render();
}




function YUIEditorSubmit(targetID) { 

	var HiddenField = document.getElementById('hdn' + targetID);
	editors[targetID].saveHTML();
	str = editors[targetID].get('element').value;
	var aStr = str.split(''),
	i = aStr.length,
	aRet = [];
	while (i--) {
		var iC = aStr[i].charCodeAt();
		if (iC < 33 || iC > 127) {
			aRet.push('&#'+iC+';');
		} else {
			aRet.push(aStr[i]);
		}
	}
	//HiddenField.value =  aRet.reverse().join('');
	HiddenField.value = str;
}





function selectLinkType(type) {
	//$(".linkData").hide();
	$('input[name=itmlinktype][value=' + type + ']').attr('checked','checked');

	if (type == 'externurl') {
		$('strong[rel=externurlLabel]').show();
		$('strong[rel=mailLabel]').hide();
		$('label[rel=_createlink_url]').show();
		$('label[rel=_createlink_target]').show();
		$('label[rel=_createlink_popoup_options]').show();
		$('label[rel=_createlink_choose_file]').hide();
		$('label[rel=_createlink_choose_page]').hide();
	}

	if (type == 'link') {
		$('label[rel=_createlink_url]').hide();
		$('label[rel=_createlink_target]').show();
		$('label[rel=_createlink_popoup_options]').show();
		$('label[rel=_createlink_choose_file]').hide();
		$('label[rel=_createlink_choose_page]').show();
	}

	if (type == 'mail') {
		$('strong[rel=externurlLabel]').hide();
		$('strong[rel=mailLabel]').show();
		$('label[rel=_createlink_url]').show();
		$('label[rel=_createlink_target]').hide();
		$('label[rel=_createlink_popoup_options]').hide();
		$('label[rel=_createlink_choose_file]').hide();
		$('label[rel=_createlink_choose_page]').hide();
	}

	if (type == 'download') {
		$('label[rel=_createlink_url]').hide();
		$('label[rel=_createlink_choose_file]').show();
		$('label[rel=_createlink_target]').show();
		$('label[rel=_createlink_popoup_options]').show();
		$('label[rel=_createlink_choose_file]').show();
		$('label[rel=_createlink_choose_page]').hide();
	}



	$("#" + type + "_form").show();
	//alert("#" + type + "_form");
	
	//alert($("#yuiTextEditor_text3114_createlink_url"));
}

function checkSelectedLinkType() {
	var linkType = $("input[@name=itmlinktype]:checked").val();
	return linkType;
}

getPath();
//Mode - webpage for link, structure for download
function openFileBrowser(editorID, mode, selectedFile) {
	if (mode == 'structure') {
		var currentFileID = $('input[name=chosenFileID]').val();
		var popupUrl = aPath + 'framework/chooseitem.html?browser=' + mode + '&formfield=YUIFile';
		var browserData = '$1$false$false';
		if (currentFileID > 0) browserData = browserData + '&file=' + currentFileID;
	} else {
		var currentPageID = $('input[name=chosenPageID]').val();
		var popupUrl = aPath + 'framework/chooseitem.html?browser=' + mode + '&formfield=YUIPage';
		var browserData = 'www$1';
		if (currentPageID > 0) browserData = browserData + '&file=' + currentPageID;
	}
		
		//var form = document.getElementById('appletlink');
		//var fileID = document.forms['appletlink'].itmcontentIDdownload.value;

	js_class_popupWindow(popupUrl + '&data=' + browserData, 700, 500, 'fileselect', 0, -1, -1, 1, 1);
}


function fileSelectCallbackYUIFile (selectedFile, path, fullPath) {
	$('#chosenFileName').text(fullPath);
	$('input[name=chosenFileID]').val(selectedFile);
}

function fileSelectCallbackYUIPage (selectedFile, path, fullPath) {
	$('#chosenPageName').text(fullPath);
	$('input[name=chosenPageID]').val(selectedFile);
}