﻿//图片窗口
var g_sysPictureWin = null;
var g_textAreaID = null;

//获得系统表情的html表
function pic_get_system_pictuer_window(cid,txtareaid)
{
	if(txtareaid)
	{
		g_textAreaID = txtareaid;
	}
	if(g_sysPictureWin)
	{
		if(cid)
		{
			g_sysPictureWin.curcid = cid;
		}
		else
		{
			g_sysPictureWin.curcid = 0;
		}
	}
	else
	{
		g_sysPictureWin = g_vs.createWindow("pictureWin", null, null, -1, 0, 0, "350px" , "85px", false, "", null, false, true);
		if(g_sysPictureWin)
		{
			g_sysPictureWin.client.style.padding = "1 1 1 1";
			g_sysPictureWin.client.style.overflow = "hidden";
			//g_vs.imgFactory.changeImg(g_sysFaceWin.client, "img/system-face/all.gif", true);
			g_sysPictureWin.client.style.backgroundColor = "#F3FAFF";
			//g_sysPictureWin.client.onselectstart = fa_onfacecellselectstart;
			var table=document.createElement('<table width=330 border=0 align="left" cellSpacing=0 cellPadding=0></table>');
			//alert(table);
			if(table)
			{		
				row = table.insertRow();
				if(row)
				{
					row.style.padding = "6 0 12 0";
					row.align = "center";
					cell = row.insertCell();
					if(cell)
					{
						cell.width="75px";
						cell.align = "right";
						cell.innerHTML='<span class="STYLE11">'+"图片链接:"+'</span>';
						cell.style.paddingRight =4;
					}					
					cell = row.insertCell();
					if(cell)
					{
						cell.colSpan="2";
						cell.width="252px";
						cell.align="left";
						pic_dir = document.createElement('<input style="width:252px" type="text" name="pI" id="picInput" onkeydown="return checkPicInput();" tabindex="1" value="http://" >');
						//pic_dir.onclick=picture_clearInput;
						
						pic_dir.style.imeMode = 'disabled';
						//pic_dir.maxLength=50;
						cell.appendChild(pic_dir);
					}
					
				}
				
				//bottom
				row = table.insertRow();
				if(row)
				{
					cell = row.insertCell();				
					if(cell)
					{
						cell.innerHTML = "&nbsp;";
					}				
					cell = row.insertCell();				
					if(cell)
					{
						cell.align="right";
						cell.vAlign="middle";
						cell.width="170px";
						cell.height = "20px";
						lo_btnLogin = g_vs.createNewBtn("确 定", null, pic_onBtnOK);
						cell.appendChild(lo_btnLogin);

					}
					cell = row.insertCell();
					if(cell)
					{
						cell.align="right";
						cell.vAlign="middle";
						cell.width="54px";
						cell.height = "20px";
						lo_btnLogin = g_vs.createNewBtn("取 消", null, pic_onBtnCancel);
						cell.appendChild(lo_btnLogin);
					}					
				}	
			}														
			if(cid)
			{
				g_sysPictureWin.curcid = cid;
			}
			else
			{
				g_sysPictureWin.curcid = 0;
			}
			//g_sysFaceWin.div.style.overflow='hidden';
			//g_sysFaceWin.div.style.padding='0 0 0 0';
			g_sysPictureWin.client.appendChild(table);
			g_sysPictureWin.client.insertAdjacentHTML("beforeEnd", "<span style='display:none;'>.</span>");
		}
	}	
	return g_sysPictureWin;
}

function checkPicInput()
{	
	var key = event.keyCode;
	// press enter
	if(key == 13)
	{
		return pic_onBtnOK();
	}	
	return true;
}

function picture_clearInput()
{
	var el = event.srcElement;
	if(el)
	{
		el.value = "http://";		
	}
}

function pic_hide_system_pictuer_window()
{
	if(g_sysPictureWin)
	{
		g_sysPictureWin.hide();
	}
	return;
}

function pic_onBtnCancel()
{
	var el = document.getElementById("picInput");
	if(el)
	{
		el.value = "http://";		
	}
	g_sysPictureWin.hide();
	return false;
}

function pic_onBtnOK()
{
	//var txtarea = document.getElementById("bc_rep_area");
	
	var curr = 0;
	var txtarea = document.getElementById(g_textAreaID);
	var el = document.getElementById("picInput");	
	
	if(el && txtarea)
	{
		if( (el.value != "")&&(el.value.length > 7))
		{	
			curr = el.value.indexOf("http://");
			if(curr != 0) 
			{
				// no start with "http://",
				curr = -1;
			}
			else
			{
				txtarea.value += '[img]'+ el.value + '[/img]';
				curr = 0;
			}			
		}
		else
		{
			curr = -1;
		}
	}	
	if(curr == -1)
	{
		//bad input, try agin
		alert("请输入正确的图片地址");
		el.value = "http://";
		return false;
	}
	else
	{
		// 正确的输入了图片地址或者元素不存在, 退出				
		el.value = "http://";
		g_sysPictureWin.hide();
		return false;
	}
}

function pic_URL2Messgae(msg)
{
	return msg;
}

function pic_cleanObj(obj)
{
	g_vs.clearObject(obj);
	obj.pic_num =0;
	obj.pic_id = null;
	obj.pic_url = null;
	
	return;
}
function pic_Array2icon(obj)
{
	return pic_Array2URL(obj,"/img/bbspic.gif");
}

function pic_Array2URL(obj,src_path)
{
	var i;
	var pic_id;
	var pic_url;
	var img;
	var span;
//alert("Begin pic_Array2URL() obj.pic_num="+ obj.pic_num);
	if(!obj)
	{
		return false;
	}
	for(i=0; i< obj.pic_num; i++)
	{
		pic_id = obj.pic_id[i];
		if(src_path == null)
		{
			pic_url = obj.pic_url[i];
		}
		else
		{
			pic_url = src_path;
		}
//alert("obj.pic_id="+pic_id+"  obj.pci_url="+ obj.pic_url);
		img = g_vs.imgFactory.newImage(pic_url);
		img.align="absbottom" ;
		span = document.getElementById(pic_id);
		if(span && img)
		{
			span.appendChild(img);
		}
	}
	obj.pic_num =0;
	obj.pic_id = null;
	obj.pic_url = null;
	return true;
}

function pic_Messgae2Array(msg)
{
	var Obj = new Object();
	Obj.pic_id = new Array();
	Obj.pic_url = new Array();
	Obj.pic_num = 0;
	Obj.msg = "";
	var pic_num = 0;
		
	var pidID;
	var img_start_pos =0, img_end_pos=0, next_pos=0;
	var tmp_msg = "";
//alert("in msg= " + msg);
	img_start_pos = msg.indexOf('[img]http://',next_pos);
	while(img_start_pos != -1)
	{
		if(img_start_pos > next_pos)
		{
			tmp_msg += msg.substring(next_pos,img_start_pos);
		}
		img_end_pos = msg.indexOf('[/img]',next_pos+12);
		if(img_end_pos != -1)
		{
			//tmp_msg += '<img src="' + msg.substring(img_start_pos+5,img_end_pos) + '">';
			pidID = g_vs.genUID();
			tmp_msg += '<span id=' + pidID + '></span>';
			Obj.pic_id[pic_num] = pidID;
			Obj.pic_url[pic_num] = msg.substring(img_start_pos+5,img_end_pos);
			pic_num ++;			
			
			next_pos = img_end_pos+6;
			img_start_pos = msg.indexOf('[img]http://',next_pos);
		}
		else
		{
			img_start_pos = -1;
		}		
	}	
	if(img_start_pos == -1)
	{
		tmp_msg += msg.substring(next_pos);
	}
//alert('tmp_msg=' + tmp_msg);		
	//return tmp_msg;
	
	Obj.msg = tmp_msg;
	Obj.pic_num = pic_num;
//alert("out msg="+Obj.msg+" pic_num=" + Obj.pic_num);
	return Obj;
}
