// JavaScript Document
U = Class.create();

U.prototype = {
	initialize: function () {
		this.name = UI.getCookie("eemap_autologin_name");
		this.refreshBar();
		//this.refreshBar();
	}
};

//登陆，注册，邀请模块
Object.extend(U.prototype, {	
	logout: function ()
	{
		this.id = 0;
		this.name = '';
		//UI.show("正在登出");
		myAjax = UI.getContent('tip', '/ajax/comm?do=logout', '', LOADING);
		//var myAjax = new Ajax.Request(, {method: 'post', parameters: '', onComplete: function(){window.location.reload();}});
	},
	logined: function ()
	{
		return UI.getCookie("eemap_autologin_name");
	},
	forgot_pwd : function (email)
	{
		myAjax = UI.getContent('forgetTip',"/ajax/comm?do=forgot_pwd", "email=" + email, LOADING);
	},
	refreshBar: function()
	{
		if(this.name == '')
		{
			//'overopacity', url, {method: 'get', parameters: pars, onFailure: UI.showError, evalScripts:true}
			$('userBar').innerHTML = '欢迎你，请' + '<a href="javascript: void(0)" onclick=\'UI.overPage();UI.getContent("overopacity", "/ajax/login?do=login", "",LOADING);\'>' + '登陆' + '</a> <a href="javascript: void(0)" onclick=\'UI.overPage();UI.getContent("overopacity", "/ajax/login?do=register", "",LOADING);\'>' + '注册' + '</a>';
			if(window.location.href.search(/(user\/$|mailbox|account|edit|add)/) > 0)
			{
				window.location = "http://" + window.location.hostname;
			}
			Element.hide('tag_mymap');
		}
		else
		{
			$('userBar').innerHTML = '欢迎你，' + this.name + ' '+'&nbsp;<a href="/account/">设置</a>&nbsp;<a href="/mailbox/inbox/">邮箱<span id=\'info_msg\'></span></a>&nbsp;<a href="javascript: void(0)" onclick="user.logout();">' + '退出' + '</a>';
			Element.show('tag_mymap');
			UI.getContent('info_msg','/ajax/comm?do=getunreadmsg');	
		}
	}
});

//点模块
Object.extend(U.prototype, {
	removeGetPListener: function()
	{
		if(UI.opaqueListener)
		{
			GEvent.removeListener(UI.opaqueListener);
		}
	},
	addGetPListener : function()
	{
		if(UI.opaqueListener)
		{
			GEvent.removeListener(UI.opaqueListener);
			UI.opaqueListener = GEvent.addListener(UI.map, 'click', UI.getMPos);
		}
		else
		{
			UI.opaqueListener = GEvent.addListener(UI.map, 'click', UI.getMPos);
		}
	},
	addMarker_start: function()
	{
		UI.disableDetailClick();
		this.addGetPListener();
	},
	editMarker: function(id)
	{
		Element.show("info_box_b");
		UI.getContent('info_box_b','/ajax/editpoint?id='+id,'backUrl='+escape(window.location),LOADING);
	},
	editmarker_cancel: function()
	{
		UI.enableDetaiClick();
		if(UI.editMarkerPosition_lastestMarker)
		{
			UI.map.removeOverlay(UI.editMarkerPosition_lastestMarker);
		}
		this.removeGetPListener();
		if(window.location.href.search(/(addpoint|editpoint)/) > 0)
		{
			window.history.back();
		}
		else
		{
			$("info_box_b").innerHTML = '';
			Element.hide("info_box");
			ml.reshow();
		}
	},
	saveMarker : function(opt)
	{
		var zoom = parseInt(UI.map.getZoom());
		var id = parseInt($("markerInfo_id").innerHTML);
		var title = $("add_markerInfo_title").value;
		
		var content = $("markerInfo_content").value;
		var tags = $("markerInfo_tags").value;
		var lat = parseFloat($("markerInfo_lat").innerHTML);
		var lng = parseFloat($("markerInfo_lng").innerHTML);
		var pics = UI.pics;
		var warning_title = "";
		if(title=="")
		{
			warning_title += "标题必须填写！\n";
		}
		if(!title.match(/^[\[\]\-\w\u4e00-\u9fa5]*$/g))
		{
			warning_title += "标题中含有非法字符！\n";
		}
		if(!tags.match(/^[ ,，\w\u4e00-\u9fa5]*$/g))
		{
			warning_title += "标签中含有非法字符！\n";
		}
		var warning_lat = isNaN(lat) ? "请在地图上标记点！" : '';
		var warning = warning_title+warning_lat;
		if(warning!='')
		{
			alert(warning);
			return false;
		}
		Element.hide('info_box');
		$("add_markerInfo_title").value = '';
		$("markerInfo_tags").value = '';
		$("markerInfo_content").value = '';
		$("markerInfo_lat").innerHTML = 'lat';
		$("markerInfo_lng").innerHTML = 'lng';
		$("imgbox").innerHTML = '';
		UI.pics = '';
		
		var marker = new M(id, lat, lng,  zoom, '', title, content, '','', '', tags);
		//marker.topicId = topicId;
		UI.show(LOADING, "mid_tip", 0);
		marker.pics = pics;
		marker.save(opt);
		return true;
	},
	editMarkerPosition: function(lat, lng) {
		//清除原来的标记。
		UI.map.clearOverlays();
		//var oldMarker = new GMarker(new GPoint(lat,lng));
		var oldIcon = new GIcon(DEFAULT_ICON);
		oldIcon.image = "http://labs.google.com/ridefinder/images/mm_20_gray.png";
		var oldMarker = new M('',lat, lng, '',oldIcon,'');
		//oldMarker.enableDragging();
		UI.map.addOverlay(oldMarker);
		UI.map.panTo(new GLatLng(lat,lng));
		if(UI.editStat == true)
		{
			GEvent.removeListener(UI.opaqueListener);
			UI.editStat = false;
		}
		UI.opaqueListener = GEvent.addListener(UI.map, "click", UI.getMPos);
		UI.editStat = true;
	},
	delMarker: function(id)
	{
		if(!this.logined())
		{
			alert("删除前请先登陆！");
			return false;
		}
		if(!confirm("你确定要删除这个点？"))
		{
			return false;
		}
		var pars = "id=" + id;
		UI.getContent('tip','/ajax/comm?do=delpoint',pars,LOADING);
	}
});

//加专题模块
Object.extend(U.prototype, {
	saveTopic: function(){
		//var zoom = UI.map.getZoom();
		//newTopic.m_id = $('addtopic_id').innerHTML;
		ml.title = $F('addtopic_title');
		ml.content = $F('addtopic_content');
		ml.tags = $F('addtopic_tags');
		ml.pics = UI.topicpics;
		ml.logo = $('topiclogo').value;
		var warning = "";
		if(ml.title=="")
		{
			warning += "标题必须填写！\n";
			$('addtopic_title').focus();
		}
		if(!ml.title.match(/^[\[\]\-\w\u4e00-\u9fa5]*$/g))
		{
			warning += "标题中含有非法字符！\n";
			$('addtopic_title').focus();
		}
		if(!ml.tags.match(/^[ ,，\w\u4e00-\u9fa5]*$/g))
		{
			warning += "标签中含有非法字符！\n";
			$('addtopic_tags').focus();
		}
		if(warning!='')
		{
			alert(warning);
			UI.showDiv('step1');
			//$('addtopic_title').focus();
			return false;
		}
		$('addTopic_save').disabled = true;

		ml.save();
	}
});

//Tag模块
Object.extend(U.prototype, {
	appendTag: function (tags, id, type)
	{
		if(!this.logined())
		{
			alert("添加前请先登陆！");
			return false;
		}
		type = type ? type : 'point';
		var pars = "tags="+ tags.replace(/&/g,"%26") + "&id=" + id + "&type=" + type;
		UI.getContent('addTagRst','/ajax/comm?do=appendtag',pars,LOADING);
	}
});

//好友模块
Object.extend(U.prototype, {
	addFriend: function (name)
	{
		if(!this.logined())
		{
			alert("添加前请先登陆！");
			return false;
		}
		UI.getContent('tip','/ajax/comm?do=addfriend',"name=" + name,LOADING);
	},
	delFriend: function (name)
	{
		if(!this.logined())
		{
			alert("删除前请先登陆！");
			return false;
		}
		if(!confirm("你确定要删除这个好友？"))
		{
			return false;
		}
		UI.getContent('tip','/ajax/comm?do=delfriend',"name=" + name,LOADING);
		//Element.hide('myfriend_colum_'+id);
	},
	invite: function(email)
	{
		var myAjax = UI.getContent('invite_tip','/ajax/comm?do=invite', "email="+email, "正在发送...");
		Element.show('invite_tip');
	}
});

//收藏模块
Object.extend(U.prototype, {
	addFav: function (type,id)
	{
		if(!this.logined())
		{
			alert("收藏前请先登陆！");
			return false;
		}
		var myAjax = UI.getContent('tip','/ajax/comm?do=addFav', "type=" + type + "&id="+id, LOADING);
	},
	delFav:function (type, id)
	{
		if(!this.logined())
		{
			alert("收藏前请先登陆！");
			return false;
		}
		var myAjax = UI.getContent('tip','/ajax/comm?do=delFav', "type=" + type + "&id="+id, LOADING);
	}
});
