// ieか？
var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);

var photolib_id;
var medialib_id;
var text_type;

//	フォトライブラリ起動(テキストエリア版)
function openPhotoLibForTextArea(url, id) {
	photolib_id = id;
	text_type = "textarea";
	openWinFixed(url, 900, 750, "phot_lib");
}

//	フォトライブラリ起動(テキスト)
function openPhotoLibForText(url, id) {
	photolib_id = id;
	text_type = "text";
	openWinFixed(url, 900, 750, "phot_lib");
}

//	フォトライブラリ起動(WYSIWYG)
function openPhotoLibForWysiwyg(url, id) {
	photolib_id = id;
	text_type = "wysiwyg";
	openWinFixed(url, 900, 750, "phot_lib");
}

//	メディアライブラリ起動(WYSIWYG)
function openMediaLibForWysiwyg(url, id) {
	medialib_id = id;
	text_type = "wysiwyg";
	openWinFixed(url, 900, 750, "phot_lib");
}


function getTextType() {
	return text_type;
}


//	範囲
function getAreaRange(obj) { 
	var pos = new Object(); 
	  
	if (isIE) { 
		obj.focus(); 
		var range = document.selection.createRange(); 
		var clone = range.duplicate(); 
		clone.moveToElementText(obj); 
		clone.setEndPoint( 'EndToEnd', range ); 
		  
		pos.start = clone.text.length - range.text.length; 
		pos.end = clone.text.length - range.text.length + range.text.length; 
	} 
	  
	else if(window.getSelection()) { 
		pos.start = obj.selectionStart; 
		pos.end = obj.selectionEnd; 
	} 
	  
	return pos; 
}

//	insefrt strong tag...
function insTagStrong(obj) {
	var target = document.getElementById(obj); 
	var pos = getAreaRange(target); 
	  
	var val = target.value; 
	var range = val.slice(pos.start, pos.end); 
	var beforeNode = val.slice(0, pos.start); 
	var afterNode = val.slice(pos.end); 
	var insertNode; 
	  
	if (range || pos.start != pos.end) { 
		insertNode = '[[bold]]' + range + '[[/bold]]'; 
		target.value = beforeNode + insertNode + afterNode; 
	} 
	  
	else if (pos.start == pos.end) { 
		insertNode = '[[bold]]' + '[[/bold]]';
		target.value = beforeNode + insertNode + afterNode; 
	}
}

//	insert image tag...
function insTagPhotoP(photolib, fileid, alignKey, hspace, vspace, photoSize, imgURL) {

	//	テキスト
	var target = document.getElementById(photolib_id); 

	var align = "";			//	指定なし
	var imgAlign = "";		//	指定なし
	if(alignKey == '2' ) {
		align = "left";
		imgAlign = "";
	} else if(alignKey == '3') {
		align = "center";
		imgAlign = "";
	} else if(alignKey == '4') {
		align = "right";
		imgAlign = "";
	} else if(alignKey == '5') {
		align = "textleft";
		imgAlign = "left";
	} else if(alignKey == '6') {
		align = "textright";
		imgAlign = "right";
	}
	
	var pOption = "";
	if (align!=""){
		pOption += (pOption!="") ? "," : "";
		pOption += "a="+align;
	}
	if (hspace!="" && hspace-0!=0){
		pOption += (pOption!="") ? "," : "";
		pOption += "hs="+(hspace-0);
	}
	if (vspace!="" && vspace-0!=0){
		pOption += (pOption!="") ? "," : "";
		pOption += "vs="+(vspace-0);
	}
	if (photoSize!="" && photoSize!="b"){
		pOption += (pOption!="") ? "," : "";
		pOption += "s="+photoSize;
	}
	if (pOption!=""){
		pOption = ":" + pOption;
	}
	
	var insertNode = '[[photo:' + photolib + ':' + fileid + pOption + ']]';
	
	if(text_type == 'wysiwyg') {
	    
	    insertImage(photolib_id, imgAlign, imgURL, insertNode, hspace, vspace);
	    return ;
	    
	}else if(text_type == 'textarea') {
	
		if (isIE){
			target.focus(); 
			var sel = document.selection.createRange(); 
	        var rang = target.createTextRange();
	        var cursorLocation = target.selectionStart;
	
			rang.moveToPoint(sel.offsetLeft, sel.offsetTop);
			rang.moveEnd("textedit");
			if(rang.text.replace(/\r/g,"").length != 0){
	            var las = (target.value.match(/(\r\n)*$/), RegExp.lastMatch.length);
	            cursorLocation = (target.value.length - (rang.text.length + las));
	        }else{
	            cursorLocation = (target.value.length - (rang.text.length));
	        }
		} else {	
			cursorLocation = target.selectionStart;
		}
		
		var val = target.value;  
		var beforeNode = val.slice(0, cursorLocation); 
		var afterNode = val.slice(cursorLocation);
		target.value = beforeNode + insertNode + afterNode; 
		
	} else {
	
		target.value = insertNode;
		
	}

}


//	insert file tag...
function insTagFile(url, linkTarget, name) {

	//	テキスト
	var target = document.getElementById(medialib_id); 

	var insertNode = url;
	
	if(text_type == 'wysiwyg') {
	    
	    insertHyperLink(medialib_id, url, linkTarget, name);
	    return ;
	    
	}else if(text_type == 'textarea') {
	
		if (isIE){
			target.focus(); 
			var sel = document.selection.createRange(); 
	        var rang = target.createTextRange();
	        var cursorLocation = target.selectionStart;
	
			rang.moveToPoint(sel.offsetLeft, sel.offsetTop);
			rang.moveEnd("textedit");
			if(rang.text.replace(/\r/g,"").length != 0){
	            var las = (target.value.match(/(\r\n)*$/), RegExp.lastMatch.length);
	            cursorLocation = (target.value.length - (rang.text.length + las));
	        }else{
	            cursorLocation = (target.value.length - (rang.text.length));
	        }
		} else {	
			cursorLocation = target.selectionStart;
		}
		
		var val = target.value;  
		var beforeNode = val.slice(0, cursorLocation); 
		var afterNode = val.slice(cursorLocation);
		target.value = beforeNode + insertNode + afterNode; 
		
	} else {
	
		target.value = insertNode;
		
	}

}



/* ----------------------------------------------------------------------  
  Function    : insertImage()
  Description : Inserts image into the WYSIWYG.
   ---------------------------------------------------------------------- */
function insertImage(n, pAlign, pImgURL, pAlt, pVSpace, pHSpace) {
	
	//var n = WYSIWYG_Popup.getParam('wysiwyg');
	
	// get values from form fields
	var src		= pImgURL;  	// document.getElementById('src').value;
	var alt		= pAlt;   		// document.getElementById('alt').value;
	var width	= "";  			// document.getElementById('width').value
	var height	= "";  			// document.getElementById('height').value
	var border	= "0";  		// document.getElementById('border').value
	var align	= pAlign; 		// document.getElementById('align').value
	var vspace	= pVSpace-0; 	// document.getElementById('vspace').value
	var hspace	= pHSpace-0;  	// document.getElementById('hspace').value
	
	// insert image
	WYSIWYG.insertImage(src, width, height, align, border, alt, hspace, vspace, n);
	
}

/* ---------------------------------------------------------------------- *\
  Function    : insertHyperLink() (changed)
  Description : Insert the link into the iframe html area
\* ---------------------------------------------------------------------- */
function insertHyperLink(n, pHref, pTarget, pName) {
	//var n = WYSIWYG_Popup.getParam('wysiwyg');
	
	// get values from form fields
	var href		= pHref;
	var target		= pTarget;
	var style		= "";
	var styleClass	= "";
	var name 		= pName;
	
	var html 		= "<a href=\""+href+"\" target=\""+target+"\">"+name+"</a>";
  
  	// insert link
	//WYSIWYG.insertLink(href, target, style, styleClass, name, n);
	WYSIWYG.insertHTML(html, n);
	
}


