var selected_menu_item_id = null;
var subitem_holder_suffix = '_sub';
function check_showmenu(event)
{
	var target = event.srcElement || event.target;
	try {
		if (!target) {
			return;
		}
		
		var targetid = target.getAttribute('id');
		if (selected_menu_item_id != null) {
			var subitem_holder = document.getElementById(selected_menu_item_id + subitem_holder_suffix);
			
			var ishide = false;
			if (target.tagName != 'A' && target.tagName != 'IMG' && target.tagName != 'DIV') {
				ishide = true;
			} else if (target.tagName == 'IMG' && targetid && targetid != selected_menu_item_id) {
				ishide = true;
			}
			
			if (ishide) {
				subitem_holder.style.display = 'none';
				selected_menu_item_id = null;
			}
		}
		
		if ((target.tagName != 'A' && target.tagName != "IMG") || !targetid) {
			return;
		}
		
		var subitem_holder = document.getElementById(targetid + subitem_holder_suffix);
		if (!subitem_holder) {
			return;
		}
		
		selected_menu_item_id = targetid;
		subitem_holder.style.display = 'block';
		
	} catch (e){
		alert(e);
	}
}

function hidemenu(event)
{
	if (!selected_menu_item_id) {
		return;
	}
	
	var target = event.srcElement || event.target;
	var targetid = target.getAttribute('id');
	if (selected_menu_item_id == targetid) {
		return;
	}
	
	var subitem_holder = document.getElementById(selected_menu_item_id + subitem_holder_suffix);
	if (!subitem_holder) {
		return;
	}
	
	subitem_holder.style.display = 'none';
	selected_menu_item_id = null;
}

function bound_showmenu_event()
{
	boundEvent(document, 'mouseover', check_showmenu);
}

function boundEvent(obj, event, func)
{
	if (window.attachEvent) {
		obj.attachEvent('on'+event, func);
	}
	else if (window.addEventListener) {
		obj.addEventListener(event, func, true);
	}
}

boundEvent(window, 'load', bound_showmenu_event);
boundEvent(document, 'click', hidemenu);


function setFirstFocus()
{
	if (typeof(first_focus_target) == 'undefined')
		return;
	if (document.getElementById(first_focus_target))
		document.getElementById(first_focus_target).focus();
}
boundEvent(window, 'load', setFirstFocus);

function setCurrentUrlStyle()
{
	var currentLocation = document.location.href;
	var links = document.getElementsByTagName('a');
	for (var i=0; i<links.length; i++) {
		var a = links[i];
		var href = a.getAttribute('href');
		if (href && currentLocation.indexOf(href) != -1) {
			var existedClass = a.getAttribute('class') ?  a.getAttribute('class') : '';
			a.setAttribute('className', existedClass + ' selected');
			a.setAttribute('class', existedClass + ' selected');
		}
	}
}
boundEvent(window, 'load', setCurrentUrlStyle);

function trim(str) 
{ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

function refreshImage(id, baseUrl)
{
	var d = new Date();
	document.getElementById(id).src = baseUrl + '?t=' + d.getTime();
}

function gosearch(pto)
{
	if (typeof(page_location) == 'undefined')
		return;

	var page = document.getElementById('PageIndex');
	if (page) {
		page = page.value;
		if (pto) {
			page = parseInt(page) + pto;
		}
		var psize = document.getElementById('PageSize').value;
		page_location += '?page='+page+'&psize='+psize;
	}
	
	var lookup = document.getElementById('FilterWay');
	var keywords = document.getElementById('FilterKeywords');
	
	if (lookup && keywords) {
		lookup = lookup.value;
		keywords = keywords.value;
		if (trim(keywords)) {
			if (page_location.indexOf('?') == -1) {
				page_location += '?';
			} else {
				page_location += '&';
			}
			page_location += 'way='+lookup+"&words="+keywords;
		}
	}
	window.location.href = page_location;
}

function gosize(s)
{
	if (typeof(text_holder_id) == 'undefined')
		return;
	
	document.getElementById(text_holder_id).style.fontSize = s;
}

function doAddFavorite(sTitle, sURL)
{
	if(document.all) {
		window.external.AddFavorite(sURL, sTitle);
	} else {
		window.sidebar.addPanel(sTitle, sURL, "");
	}
}

function init_editor(textarea_trigger, content_css)
{
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "insertdatetime,inlinepopups,advimage",
		language : "zh",
		textarea_trigger : textarea_trigger,
		width : "550",
		height: "200",
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontsizeselect,|,forecolor,backcolor",
		theme_advanced_buttons2 : "link,unlink,|,bullist,numlist,|,sub,sup,charmap,|,outdent,indent,blockquote,|,insertdate,inserttime,|,removeformat,|,image",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_path : false,
		theme_advanced_resizing : true,
		valid_elements : "a[id|style|rel|rev|charset|hreflang|dir|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],-strong/-b[class|style],-em/-i[class|style],-strike[class|style],-u[class|style],+p[style|dir|class|align],-ol[class|style],-ul[class|style],-li[class|style],br,img[id|dir|lang|longdesc|usemap|style|class|src|onmouseover|onmouseout|border=0|alt|title|hspace|vspace|width|height|align],-sub[style|class],-sup[style|class],-blockquote[dir|style],-table[border=0|cellspacing|cellpadding|width|height|class|align|summary|style|dir|id|lang|bgcolor|background|bordercolor],-tr[id|lang|dir|class|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor],tbody[id|class],thead[id|class],tfoot[id|class],-td[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor|scope],-th[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|scope],caption[id|lang|dir|class|style],-div[id|dir|class|align|style],-span[style|class|align],-pre[class|align|style],address[class|align|style],-h1[style|dir|class|align],-h2[style|dir|class|align],-h3[style|dir|class|align],-h4[style|dir|class|align],-h5[style|dir|class|align],-h6[style|dir|class|align],hr[class|style],-font[face|size|style|id|class|dir|color],dd[id|class|title|style|dir|lang],dl[id|class|title|style|dir|lang],dt[id|class|title|style|dir|lang]",
		cleanup : true,
		verify_html : true,
		preformatted : true,
		// Example content CSS (should be your site CSS)
		content_css : content_css
	});
}

function printIt()
{
	document.getElementById('bar').style.display = 'none';
	window.print();
}
