	$(document).ready(function(){
		var email = $("#email"),
			password = $("#password"),
			to_logout = $('#to_logout'),
			allFields = $([]).add(email).add(password).add(to_logout),
			tips = $("#validateTips");
		
		//导航提示
		$('#next').qtip({
			content: {text:'试试这里，你会有新发现!'},
			style: { 
			  width: 150,
			  padding: 5,
			  background: '#A2D959',
			  color: 'black',
			  border: {
				 width: 7,
				 radius: 5,
				 color: '#A2D959'
			  },
			  tip: 'topLeft'
			}
		});
		
		//导航提示
		$('#addnew').qtip({
			content: {text:'我也来标注一个地图'},
			style: { 
			  width: 150,
			  padding: 5,
			  background: '#A2D959',
			  color: 'black',
			  border: {
				 width: 7,
				 radius: 5,
				 color: '#A2D959'
			  },
			  tip: 'topLeft'
			}
		});
		
		//提示窗口
		$('#dialog').dialog({
			autoOpen: false,
			modal:true,
			buttons:{
				'确定':function(){
					$(this).dialog('close');
				}
			}
		});

		//标注列表窗口
		$('#v-list').dialog({
			autoOpen: false,
			bgiframe: true,
			width:250,
			height:480,
			position:['right',60],
			title:'我的标注列表'
		});

		//账号内容
		$('#account-dialog').dialog({
			autoOpen: false,
			bgiframe: true,
			modal:true,
			width:300,
			height:400,
			position:['center',60],
			title:'账号',
			buttons:{
				'确定':function(){
					$('#u-msg').html('<img src="images/loading.gif" />数据传递中,请稍候......');
					$.ajax({
						url:"account.php",
						type:'POST',
						dataType:'html',
						timeout:20000,
						data:{v:'updateAccount',oldpassword:$('#oldpassword').attr('value'),newpassword:$('#newpassword').attr('value'),renewpassword:$('#renewpassword').attr('value'),email:$('#email').attr('value')},
						error:function(){alert('somethings error,#8')},
						success:function(data){
							switch (data)
							{
								case '0':
									var msg = '更新成功';
									break;
								case '1':
									var msg = '更新成功';
									break;
								case '-1':
									var msg = '旧密码不正确';
									break;
								case '-2':
									var msg = '两次新密码输入不一致';
									break;
								case '-4':
									var msg = 'Email 格式有误';
									break;
								case '-5':
									var msg = 'Email 被禁止';
									break;
								case '-6':
									var msg = '该 Email 已经被注册';
									break;
								case '-7':
									var msg = '没有做任何修改';
									break;
								default:
									var msg = '未知错误　#9'.$data;
									break;
							}
							$('#u-msg').html(msg);
						}
					});
				}
			}
		});


		
		

		//账号标注按钮操作
		$('#myAccount').click(function(){
			changeAccount();
		});
		


		
		//标注列表按钮操作
		$('#myList').click(function(){
			getList();
		});
		
		//新标注按钮操作
		$('#newpoint,#addnew').click(function(){
			newPoint();		
		});


		//注册
		$('#reg_dialog').dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			position:['center',60],
			buttons: {
				'提交':function(){
					$('#msg').html('<img src="images/loading.gif" />数据传递中,请稍候......');
					//提交的ajax内容
					$.ajax({
						url:'account.php',
						type:'POST',
						dataType:'html',
						timeout:20000,
						data:{email:$('#email').attr('value'),username:$('#username').attr('value'),password:$('#password').attr('value'),v:'reg'},
						error:function(){alert('somethins error,#5')},
						success:function(data){
							if (data > 0)
							{
								//处理数据
								loginedMenu();
								$('#reg_dialog').dialog('close');
								$('#dialog').dialog('open');
								$('#dialog').html('注册成功,数据载入中......');
								setTimeout(function(){$('#dialog').dialog('close')}, 2000);
							}
							else
							{
								if(data == -1) {
									$('#msg').html('用户名不合法');
								} else if(data == -2) {
									$('#msg').html('包含要允许注册的词语');
								} else if(data == -3) {
									$('#msg').html('用户名已经存在');
								} else if(data == -4) {
									$('#msg').html('Email格式有误');
								} else if(data == -5) {
									$('#msg').html('Email不允许注册');
								} else if(data == -6) {
									$('#msg').html('该Email已经被注册');
								} else if(data == -7) {
									$('#msg').html('所有项均为必填');
								} else {
									$('#msg').html('发生错误，错误代码1'+data+',如有必要，请<a href="mailto:service@zainar.net">联系管理员</a>');
								}
							};
						}
					});
				}
			}
		});
		
		//登录
		$("#login_dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			width: 340,
			height: 290,
			position:['center',60],
			buttons: {
				'提交':function(){
					$.ajax({
							url:'account.php',
							type:'POST',
							dataType:'html',
							timeout:20000,
							data:{email:$('#email').attr('value'),pass:$('#password').attr('value'),v:'login'},
							error:function(){alert('somethins error')},
							success:function(data){
								if (data > 0)
								{
									loginedMenu();
									$('#login_dialog').dialog('close');

								}else if(data == -1){
									$('#msg').html('没有这个用户，请检查Email输入是否正确');
								}else if (data == -2){
									$('#msg').html('密码不正确,请重新填写');
								}else{
									$('#msg').html('发生错误，错误代码'+data+',如有必要，请<a href="mailto:service@zainar.net">联系管理员</a>');
								}
							}
						});
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});



		

		//注册按钮事件
		$('#to_reg,#to_reg2,#to_reg3').click(function(){
			$('#reg_dialog').dialog('open');
			$('#reg_dialog').html('<img src="images/loading.gif" />');
			$.ajax({
					url:'dialog.php',
					type:'GET',
					dataType:'html',
					timeout:20000,
					data:{v:'reg'},
					error:function(){alert('error,no return ajax value #1')},
					success:function(data){
						$('#reg_dialog').html(data);
					}
			});
		});
		
		
		
		//登录按钮事件
		$('#to_login').click(function(){
			btn_login();
		});

		$('#toLogout').click(function(){
			btn_logout();
		});
		
		//主动跳出登录
		var getaction = getParameter("a");
		if (getaction == 'login')
		{
			btn_login();
		}
		
	});
	
	
//实现GET取值
function getParameter(sProp) {
         var re = new RegExp(sProp + "=([^\&]*)", "i");
         var a = re.exec(document.location.search);
         if (a == null)
		{
			return null;
		}
         return a[1];
};

function newPoint(){
	//判断登录
	if ($.cookie('uid'))
	{
		window.location.href = 'add.php';	
	}
	else
	{
		btn_login();
	}
}

function btn_logout(){
	$.ajax({
			url:'account.php',
			type:'POST',
			dataType:'html',
			timeout:20000,
			data:{v:'logout'},
			error:function(){alert('error, no return ajax value #3')},
			success:function(data){
				$('#dialog').dialog('open');
				$('#dialog').html('您已经成功退出.数据载入中......')
				setTimeout(function(){window.location.reload()},1000);
		}
	});
}

function btn_login(){
	$('#login_dialog').dialog('open');
		$('#login_dialog').html('<img src="images/loading.gif" />');
		$.ajax({
				url:'dialog.php',
				type:'GET',
				dataType:'html',
				timeout:20000,
				data:{v:'login'},
				error:function(){alert('error,no return ajax value #2')},
				success:function(data){
					$('#login_dialog').html(data);
				}
		});
}
		
//新标注资料
function openContentDialog(){
	$('#newpoint-dialog').dialog('open');
}

function getList(){
	$('#v-list').dialog('open');
	$('#v-list').html('<img src="images/loading.gif" />');
	$.ajax({
		url:"account.php",
		type:'POST',
		dataType:'html',
		timeout:20000,
		data:{v:'getlist'},
		error:function(){alert('somethings error,#6')},
		success:function(data){
			$('#v-list').html(data);
		}
	});
}

//账号窗口操作
function changeAccount(){
	$('#account-dialog').dialog('open');
	$('#account-dialog').html('<img src="images/loading.gif" />');
	$.ajax({
		url:"dialog.php",
		type:"POST",
		dataType:"html",
		timeout:20000,
		data:{v:'myaccount',uid:$.cookie('uid')},
		error:function(){alert('somethings error,#7')},
		success:function(data){
			$('#account-dialog').html(data);
	}
	});
}
		
function loginedMenu(){
	$('#account_menu').html('');
	$('#account_menu').append('<li><a href="#" id="toLogout" title="退出">退出</a></li>').find('#toLogout').click(function(){
			btn_loutou();
		});
	$('#account_menu').append('<li><a href="#" id="myAccount" title="账号">账号</a></li>').find('#myAccount').click(function(){
			changeAccount();
		});
	$('#account_menu').append('<li><a href="#" id="myList" title="标注列表">列表</a></li>').find('#myList').click(function(){
			getList();
		});
	$('#account_menu').append('<li><a href="#" id="newPoint" title="新的标注">新标注</a></li>').find('#newPoint').click(function(){
			newPoint();
		});
}

function edit_save() {
	
	var p = window.frames['ifrHtmlEditor'];
	var obj = p.window.frames['HtmlEditor'];
	var status = p.document.getElementById('uchome-editstatus').value;
	if(status == 'code') {
		document.getElementById('data-content').value = p.document.getElementById('sourceEditor').value;
	} else if(status == 'text') {
		if(is_ie) {
			obj.document.body.innerText = p.document.getElementById('dvtext').value;
			document.getElementById('data-content').value = obj.document.body.innerHTML;
		} else {
			obj.document.body.textContent = p.document.getElementById('dvtext').value;
			var sOutText = obj.document.body.innerHTML;
			document.getElementById('data-content').value = sOutText.replace(/\r\n|\n/g,"<br>");
		}
	} else {
		document.getElementById('data-content').value = obj.document.body.innerHTML;
	}
	
		
}

