// JavaScript Document
if (!window.UI)
{
    var UI = function(){alert("192.168.0.1");};
    UI.toString = function(){return "192.168.0.1";};
}
UI.Ajax = {};
//LOADING的图标
LOADING = '<div class="loading"><img src="images/loading.gif" alt="loading...." width="32" height="32" /></div>';
//一些文本，多数用作MAP的控件
UI.spans = {};
//城市的label
UI.cities = [];

//将文本输入框的确定事件转交给相应的确定按钮来处理。
//这里有个命名规则：ButtunName = "btn_" + InputName.substring(InputName.indexOf('_')+1);
UI.returnDown = function(evt)
{
	var key = evt.which || evt.keyCode;
	if(key==Event.KEY_RETURN)
	{
		var name = Event.element(evt).id;
		var btn = 'btn_' + name.substring(name.indexOf('_')+1);
		$(btn).onclick();
	}
};

UI.search_check = function()
{
	if($F('keyword')=='' || $F('keyword')=='输入关键字')
	{
		return false;
	}
	else
	{
		return true;
	}
};
			
UI.showError = function ()
{
	UI.show("Error");
};

UI.overPage = function(){
	clientWidth = document.body.clientWidth;
	clientHeight = document.body.clientHeight;
	$('overopacity').style.top = '100px';
	$('overopacity').style.left = (clientWidth - 400)/2 + 'px';
	Element.show('opacity');
	Element.show('overopacity');
};

UI.unOverPage = function()
{
	Element.hide('opacity');
	Element.hide('overopacity');
};

UI.show = function(content, container, time)
{
	if(!container)
	{
		container = 'tip';
	}
	Element.show(container);
	$(container).innerHTML = content; 
	if(time != 0)
	{
		time *= 1000;
		setTimeout(function(){Element.hide(container);}, time);
	} 
};
//图像显示
UI.imgzoom = function(img,maxsize)
{
	var a=new Image();
	a.src=img.src;
	if(a.width > maxsize)
	{
		img.style.width = maxsize + "px";
	}else{
		img.style.width = a.width + "px";
	}
	return false;
};
//获取Cookie，UI.getCookie()函数只有一个表示cookie名称的参数。
//获得Cookie的原始值
UI.getCookie = function(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return UI.getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
		{
			break;
		}
	}
	return '';
};

//获得Cookie解码后的值
UI.getCookieVal = function (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	{
		endstr = document.cookie.length;
	}
	return decodeURI(document.cookie.substring(offset, endstr));
};

UI.showDiv = function(divName)
{
	var parent = $(divName).parentNode;
	var nodes = parent.childNodes;
	var len = nodes.length;
	for(var i=0; i < len; i++)
	{
		if(nodes[i].nodeType == 1)
		{
			if(nodes[i].getAttribute('id') == divName)
			{
				Element.show(nodes[i]);
			}
			else
			{
				Element.hide(nodes[i]);
			}
		}
	}
	Element.show(parent);
};

UI.getContent = function(container, url, pars, preTip)	{
	if(typeof preTip != "undefined")
	{
		$(container).innerHTML = preTip;
	}
//	if(this.Ajax[container]){
//		this.Ajax[container].abort();
//	}
	this.Ajax[container] = new Ajax.Updater(container, url, {method:'post', parameters:pars, onFailure: function(){ container.innerHTML = "网络错误";},evalScripts: true});
};

UI.domIsEmpty = function(container)
{
	content = $(container).innerHTML;
	if(content == "" || content.indexOf("images/loading.gif") >= 0)
	{
		return true;
	}
	return false;
};

UI.getNoRefreshContent = function(container, url, pars, preTip)	{
	if(UI.domIsEmpty(container))
	{
		UI.getContent(container, url, pars, preTip);
	}
	else
	{
		Element.show(container);
	}
};

UI.checkboxSerialize = function(name)
{
	cbs = document.getElementsByName(name);
	cbs = $A(cbs);
	var str = '';
	cbs.each(function(cb){
				if(cb.checked)
				{
					str += cb.value + ',';
				}
				//alert(node.nodeName + ': ' + node.innerHTML);
			});
	return str;
};

UI.postData = function(container, url, pars, preTip)	{
	if(typeof preTip != "undefined")
	{
		$(container).innerHTML = preTip;
	}
	if(this.Ajax[container]){
		this.Ajax[container].abort();
	}
	this.Ajax[container] = new Ajax.Updater(container, url, {method:'post', parameters:pars, onFailure: function(){ container.innerHTML = "网络错误";},evalScripts: true});
};

UI.encodeLat = function(lat,lng)
{
	return 2*parseFloat(lat) + parseFloat(lng);
};

UI.encodeLng = function(lat,lng)
{
	return lat + 2*lng;
};

UI.makeHistory = function(newHash)
{
	window.location.hash = newHash;
};

UI.switchTab = function(name)
{
	tabName = 'tab_' + name;
	var parent = $(tabName).parentNode;
	var nodes = parent.childNodes;
	var len = nodes.length;
	for(var i=0; i < len; i++)
	{
		if(nodes[i].nodeType == 1)
		{
			if(nodes[i].getAttribute('id') == tabName)
			{
				nodes[i].className = "tabSelected";
			}
			else
			{
				nodes[i].className = "tab";
			}
		}
	}
	contentName = 'content_' + name;
	UI.showDiv(contentName);
};

UI.addTag = function(tagspan, addto){
	what = addto ?  addto : "markerInfo_tags";
	var tag = tagspan.innerHTML;
	var str = $(what).value;
	//如果里面原来是空的，直接把tag放进去
	if( str == "" ){
		$(what).value = tag;
		tagspan.style.backgroundColor = "#669999";
		return false;
	}
	//对tag里面的非法字符进行替换处理
	str = str.replace(/\s+/g,",");
	str = str.replace(/，+/g,",");
	str = str.replace(/^,+/,"");
	str = str.replace(/,+$/,"");
	//对已有的tag进行是否重复的比对
	var arr = str.split(",");
	var num = arr.length;
	for( i = 0; i < num ; i++ )
	{
		if ( arr[i] == tag )
		{
			arr_copy = arr;
			arr_copy.slice( i + 1 );
			str = arr.slice( 0, i ).concat(arr_copy.slice( i + 1 ));
			$(what).value = str;
			tagspan.style.backgroundColor = "";
			return false;
		}
	}
	//最后处理并显示结果
	arr.push(tag)
	str = arr.join();
	$(what).value = str;
	tagspan.style.backgroundColor = "#669999";
};

UI.validPicUrl = function(img)
{
	return img.search(/(\.png|\.gif|\.jpg)$/i)
}

UI.domAppendPic = function (img, content, size, withRemove)
{
	if(UI.validPicUrl(img) <= 0)
	{
		UI.show('图片文件无效','imageTip', 10);
		return false;
	}
	if(UI.pics.search(img) >= 0)
	{
		UI.show('该图片已经存在，莫重复添加','imageTip', 10);
		return false;
	}
	UI.pics += '|'+img;
	var d = document.createElement('div');
	var i = new Image();
	if(size[0])
	{
		i.width = size[0];
	}
	if(size[1])
	{
		i.height = size[1];
	}
	i.src = img;
	i.border = 0;
	d.appendChild(i);
	if(withRemove && withRemove == true)
	{
		var r = document.createElement('a');
		var deltext = document.createTextNode('删除');
		r.appendChild(deltext);
		r.href = "javascript:void(0)";
		r.onclick = this.delPic.bindAsEventListener(this);
		$(d).appendChild(r);
	}
	$(content).appendChild(d);
	$("webImage").value="";
};

UI.domAppendTopicPic = function (img, content,size, withRemove)
{
	if(UI.validPicUrl(img) <= 0)
	{
		UI.show('图片文件无效','topic_imageTip', 10);
		return false;
	}
	if(UI.topicpics.search(img) >= 0)
	{
		UI.show('该图片已经存在，莫重复添加','topic_imageTip', 10);
		return false
	}
	UI.topicpics += '|'+img;
	var d = document.createElement('div');
	var i = new Image();
	if(size[0])
	{
		i.width = size[0];
	}
	if(size[1])
	{
		i.height = size[1];
	}
	i.src = img;
	d.appendChild(i);
	if(withRemove && withRemove == true)
	{
		var r = document.createElement('a');
		var deltext = document.createTextNode('删除');
		r.appendChild(deltext);
		r.href = "javascript:void(0)";
		r.onclick = this.delTopicPic.bindAsEventListener(this);
		$(d).appendChild(r);
	}
	$(content).appendChild(d);
	$('topic_webImage').value = '';
};


UI.delPic = function(evt)
{
	r = Event.element(evt);
	d = r.parentNode;
	i = d.getElementsByTagName('img')[0];
	UI.pics = UI.pics.replace('|'+i.src, '');
	Element.remove(d);
};

UI.delTopicPic = function(evt)
{
	r = Event.element(evt);
	d = r.parentNode;
	i = d.getElementsByTagName('img')[0];
	UI.topicpics = UI.topicpics.replace('|'+i.src, '');
	Element.remove(d);
};


UI.loadFlashPics = function(box){
	/*
	//use UI.pics
	UI.pics = UI.pics.replace(/\/images\/nopic.jpg\|/, '');
	var focus_width=250;
	var focus_height=200;
	var text_height=0;
	var swf_height = focus_height+text_height;
	var links='';
	var texts='|||';
	var out = '<object ID="focus_flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">';
	out += '<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="/images/flashbox.swf"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF">';
	out += '<param name="menu" value="false"><param name=wmode value="opaque">';
	out += '<param name="FlashVars" value="pics='+UI.pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">';
	out += '<embed ID="focus_flash" src="/images/flashbox.swf" wmode="opaque" FlashVars="pics='+UI.pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'" menu="false" bgcolor="#C5C5C5" quality="high" width="'+ focus_width +'" height="'+ swf_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	out += '</object>';
	$(box).innerHTML = out;
	*/
};

UI.DomAppend = function(box, content)
{
	
};

//pattern: {prop1:"str1",prop2:"str2"}
//replaces boundary+prop+boundary to str
String.prototype.multireplace = function(data, boundary)
{
	if(typeof boundary == "undefined")
	{
		boundary = "%";
	}
	str = this;
	for(prop in data)
	{
		if(typeof data[prop] != 'function')
		{
			str = str.replace(new RegExp(boundary + prop + boundary, "g"), data[prop]);
		}
	}
	return str;
};

Array.prototype.unique = function()
{
	var res = new Array();
	for (c in this)
	{
		if(typeof this[c] != 'function')
		{
  			res[this[c]] = c;
		}
	}
	return res;
};

Array.prototype.exists = function(v)  
{
	for(var i=0; i<this.length; i++)
	{
		if(this[i]==v)
		{
			return true;
		}
	}
	return false;
};

Array.prototype.del=function(n) 
{
	if(n<0)
		return this;
	else
		return this.slice(0,n).concat(this.slice(n+1,this.length));
};

if(typeof Array.prototype.splice==='undefined')
{
	Array.prototype.splice=function(a,c)
	{
		var i=0,e=arguments,d=this.copy(),f=a;
		if(!c){c=this.length-a;}
		for(i;i<e.length-2;i++)
		{this[a+i]=e[i+2];}
		for(a;a<this.length-c;a++)
		{this[a+e.length-2]=d[a-c];}
		this.length-=c-e.length+2;
		return d.slice(f,f+c);
	};
}

//UBB
//=================================BBCODE===============//
UI.ubb = {};
UI.ubb.textarea = "markerInfo_content";
UI.ubb.start=0;
UI.ubb.end=0;
UI.ubb.add = function(bbstart,bbend,txtBox){
	var pre = txtBox.value.substr(0, UI.ubb.start);
	var post = txtBox.value.substr(UI.ubb.start);
	txtBox.value = pre + bbstart + bbend + post;
	t_start = UI.ubb.start;
	if(typeof(txtBox.selectionStart) == "number")
	{
		txtBox.selectionStart = t_start + bbstart.length;
		txtBox.selectionEnd = t_start + bbstart.length;
	}
	else if(document.selection){
	}
};

UI.ubb.savePos = function(txtBox){
	//如果是Firefox(1.5)的话，方法很简单
	if(typeof(txtBox.selectionStart) == "number"){
		UI.ubb.start = txtBox.selectionStart;
		UI.ubb.start = txtBox.selectionEnd;
	}
	//下面是IE(6.0)的方法，麻烦得很，还要计算上'\n'
	else if(document.selection){
		var range = document.selection.createRange();
		if(range.parentElement().id == txtBox.id){
			// create a selection of the whole textarea
			var range_all = document.body.createTextRange();
			range_all.moveToElementText(txtBox);
			//两个range，一个是已经选择的text(range)，一个是整个textarea(range_all)
			//range_all.compareEndPoints()比较两个端点，如果range_all比range更往左(further to the left)，则                //返回小于0的值，则range_all往右移一点，直到两个range的UI.ubb.start相同。
			// calculate selection UI.ubb.start point by moving beginning of range_all to beginning of range
			for (UI.ubb.start=0; range_all.compareEndPoints("StartToStart", range) < 0; UI.ubb.start++)
				range_all.moveStart('character', 1);
			// get number of line breaks from textarea UI.ubb.start to selection UI.ubb.start and add them to UI.ubb.start
			// 计算一下\n
			for (var i = 0; i <= UI.ubb.start; i ++){
				if (txtBox.value.charAt(i) == '\n')
					UI.ubb.start++;
			}
			// create a selection of the whole textarea
			 var range_all = document.body.createTextRange();
			 range_all.moveToElementText(txtBox);
			 // calculate selection UI.ubb.start point by moving beginning of range_all to UI.ubb.start of range
			 for (UI.ubb.start = 0; range_all.compareEndPoints('StartToEnd', range) < 0; UI.ubb.start ++)
				 range_all.moveStart('character', 1);
				 // get number of line breaks from textarea UI.ubb.start to selection UI.ubb.start and add them to UI.ubb.start
				 for (var i = 0; i <= UI.ubb.start; i ++){
					 if (txtBox.value.charAt(i) == '\n')
						 UI.ubb.start ++;
				 }
			}
		}
	//document.getElementById("UI.ubb.start").value = UI.ubb.start;
	//document.getElementById("UI.ubb.start").value = UI.ubb.start;
};

UI.ubb.code = new Array();
UI.ubb.tags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[email]','[/email]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]','[url=]','[/url]','[flash]','[/flash]');
UI.ubb.bbstyle = function(bbnumber) {
	var txtarea = document.getElementById(UI.ubb.textarea);
	UI.ubb.add(UI.ubb.tags[bbnumber], UI.ubb.tags[bbnumber+1], txtarea);
	txtarea.focus();
};
//添加表情的js
UI.insertEmoticon = function (obj,num) {
	if (obj.parentNode.parentNode.getElementsByTagName("textarea")[0].value == '在这里写评论内容') {
		obj.parentNode.parentNode.getElementsByTagName("textarea")[0].value = "[EE" + num + "]" ;
	}else{
		obj.parentNode.parentNode.getElementsByTagName("textarea")[0].value += "[EE" + num + "]" ;
	}
};