var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_safari = (userAgent.indexOf('webkit') != -1 || userAgent.indexOf('safari') != -1);
function $(id) {
    return document.getElementById(id);
}
function strlen(str) {
    return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
function isUndefined(variable) {
    return typeof variable == 'undefined' ? true : false;
}
function setMenuPosition(showid, offset) {
    var showobj = $(showid);
    var menuobj = $(showid + '_menu');
    if (isUndefined(offset)) offset = 0;
    if (showobj) {
        showobj.pos = fetchOffset(showobj);
        showobj.X = showobj.pos['left'];
        showobj.Y = showobj.pos['top'];
        showobj.w = showobj.offsetWidth;
        showobj.h = showobj.offsetHeight;
        menuobj.w = menuobj.offsetWidth;
        menuobj.h = menuobj.offsetHeight;
        if (offset != -1) {
            menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
            menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
        } else if (offset == -1) {
            menuobj.style.left = (document.body.clientWidth - menuobj.w) / 2 + 'px';
            var divtop = document.documentElement.scrollTop + (document.documentElement.clientHeight - menuobj.h) / 2;
            if (divtop > 100) divtop = divtop - 100;
            menuobj.style.top = divtop + 'px';
        }
        if (menuobj.style.clip && !is_opera) {
            menuobj.style.clip = 'rect(auto, auto, auto, auto)';
        }
    }
}
function fetchOffset(obj) {
    var left_offset = obj.offsetLeft;
    var top_offset = obj.offsetTop;
    while ((obj = obj.offsetParent) != null) {
        left_offset += obj.offsetLeft;
        top_offset += obj.offsetTop;
    }
    return { 'left': left_offset, 'top': top_offset };
}
function showFace(showid, target) {
	var div = $('face_bg');
	if(div) {
		div.parentNode.removeChild(div);
	}
	div = document.createElement('div');
	div.id = 'face_bg';
	div.style.position = 'absolute';
	div.style.left = div.style.top = '0px';
	div.style.width = '100%';
	div.style.height = document.body.scrollHeight + 'px';
	div.style.backgroundColor = '#000';
	div.style.zIndex = 10000;
	div.style.display = 'none';
	div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0,finishOpacity=100,style=0)';
	div.style.opacity = 0;
	div.onclick = function() {
		$(showid+'_menu').style.display = 'none';
		$('face_bg').style.display = 'none';
	}
	$('appenddiv').appendChild(div);	
	if($(showid + '_menu') != null) {
		$(showid+'_menu').style.display = '';
	} else {
		var faceDiv = document.createElement("div");
		faceDiv.id = showid+'_menu';
		faceDiv.className = 'facebox';
		faceDiv.style.position = 'absolute';
		var faceul = document.createElement("ul");
		for(i=1; i<11; i++) {
			var faceli = document.createElement("li");
			faceli.innerHTML = '<img src="/imges/cartoon/' + i + '.gif" onclick="insertFace(\'' + showid + '\',' + i + ', \'' + target + '\')" style="cursor:pointer;position:relative;" />';
			faceul.appendChild(faceli);
		}
		faceDiv.appendChild(faceul);
		$('appenddiv').appendChild(faceDiv)
	}
	//定位菜单
	setMenuPosition(showid, 0);
	div.style.display = '';
}
//插入表情
function insertFace(showid, id, target) {
	var faceText = '[6621B'+id+'E]';
	if($(target) != null) {
		insertContent(target, faceText);
	}
	$(showid+'_menu').style.display = 'none';
	$('face_bg').style.display = 'none';
}
function insertContent(target, text) {
    var obj = $(target);
    selection = document.selection;
    checkFocus(target);
    if (!isUndefined(obj.selectionStart)) {
        var opn = obj.selectionStart + 0;
        obj.value = obj.value.substr(0, obj.selectionStart) + text + obj.value.substr(obj.selectionEnd);
    } else if (selection && selection.createRange) {
        var sel = selection.createRange();
        sel.text = text;
        sel.moveStart('character', -strlen(text));
    } else {
        obj.value += text;
    }
}
function checkFocus(target) {
    var obj = $(target);
    if (!obj.hasfocus) {
        obj.focus();
    }
}