// JS LinkImageTags version 0.01
// Author:yueyanzhao
// QQ:85659925
// MSN:yyz.crossn@hotmail.com
// gmail:yueyanzhao@gmail.com

function showDiv(which,button){
	newaddress(which,button);//调整位置

	var mytextid=button+'_text';
			
	hideDiv();

	switch (button)
	{
		case 'linkdiv':
			document.getElementById('linkdiv').style.display='block';
			var  mytitleid=button+'_title';
			break;
		case 'imgdiv':
			document.getElementById('imgdiv').style.display='block';
			var mytitleid=button+'_alt';
			break;

		case 'smilediv':
			document.getElementById('smilediv').style.display='block';
			break;

	}
	
	if( (button == 'linkdiv') || (button == 'imgdiv') )
    {
        document.getElementById(mytextid).value='http://';
        document.getElementById(mytitleid).value='';
        document.getElementById(mytextid).select();//输入网址框选中状态
    };
}



function newaddress(which,whichdiv){
	mydiv=document.getElementById(which);
	myscrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	myscrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	document.getElementById(whichdiv).style.top=myscrollTop+mydiv.getBoundingClientRect().top+25+'px';
	document.getElementById(whichdiv).style.left=myscrollLeft+mydiv.getBoundingClientRect().left+1+'px';
}

function hideDiv(){//隐藏插入图片的div 注意：这个参数的which跟其他的不是一样的
	
	document.getElementById('imgdiv').style.display='none';
	document.getElementById('linkdiv').style.display='none';
	document.getElementById('smilediv').style.display='none';
}

function imgOk(imagetextid,imagealtid,which){
	var theImageLink=document.getElementById(imagetextid).value;
	var theImageAlt=document.getElementById(imagealtid).value;
	var theImage;
	if (theImageLink!=''&&theImageLink!='http://')
		{
			theImage='[img='+theImageLink+']'+theImageAlt +'[/img]';
		}
	else
		{	
			theImage='';
		}
	edInsertContent(which, theImage);
}

function linkOk(linktextid,linktitleid,which){
	var theLinkText=document.getElementById(linktextid).value;
	var theLinkTitle=document.getElementById(linktitleid).value;
	var theLink;

	if(theLinkText!=''&&theLinkText!='http://'){
		if (theLinkTitle!='')
		{
			theLink='<a href="' 
				+ theLinkText+'" >' +theLinkTitle+ '</a>';
		}
		else 
		{
			theLink='<a href="' 
				+ theLinkText+'" >' +theLinkText+ '</a>';
		}
	}
	else
		{	
			theLink='';
		}
	edInsertContent(which, theLink);

	
}

function insertsmile(smile,which){
	
	edInsertContent(which,smile);

	
}

function edInsertContent(which, myValue) {
    myField = document.getElementById(which);
		
		var startPos = myField.selectionStart;
		
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		
		if (startPos)
		{
			myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		}
		else{
			
			myField.value +=myValue;
			
		}
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
		
	
}


function postnlou(authorlink,author,whichlou){
	whichlou= document.getElementById(whichlou).innerHTML;
	if (authorlink!='')
	{
		myfield='<a href="'+authorlink+'">'+author+'</a> ['+whichlou+'楼]  。。。';
	}
	else
	{
		myfield=author+' ['+whichlou+'楼]  。。。';
	}
	//alert(myfield);

	edInsertContent('comment', myfield);


}




function quotepost(authorlink,author,whichlou,content){
	whichlou= document.getElementById(whichlou).innerHTML;
	content= document.getElementById(content).innerHTML;
	
	//content=content.toLowerCase();//转换成小写

	var reg3=new RegExp("<p>((.|\n)*?)<\/p>","ig");
	content=content.replace(reg3,'$1');//去除p段落标记

	var reg=new RegExp("(<div class=(quote|\"quote\")>)((.|\n)*?)(<\/div>((\n)?)<\/div>)","ig");

	content=content.replace(reg,'');

	var reg2= new RegExp("<img ((.|\n)*?)((\/)?)>","ig");
	content=content.replace(reg2,'');//去除img标记
	

	if (authorlink!='')
	{
		myfield='[quote]<a href="'+authorlink+'">'+author+'</a> ['+whichlou+'楼] 说道：'+content+'[/quote]';
	}
	else
	{
		myfield='[quote]'+author+' ['+whichlou+'楼]  说道：'+content+'[/quote]';
	}
	//alert(myfield);

	edInsertContent('comment', myfield);


}




