$(function() {
	
	//// photo and floorplan
	// init
	$mainurl =$(".property_photo_small_bg").eq(0).children("img").attr("src");
	$("#mainphoto img").attr('src',$mainurl);
	
	// if 0 property photo
	$has_property_photo =$(".property_photos_small_bg").children().is('.property_photo_small');
	if (!$has_property_photo)
		$("#mainphoto img").attr('src','image/noimage.gif');
	// property photo
	$(".property_photo_small_bg img").addClass('cursorhand').click(function(){
		$url =$(this).attr("src");
		$("#mainphoto img").attr('src',$url);
	});
	
	//// photo and floorplan 2
	// init 2
	/*
	*/
	$mainurl2 =$(".property_photo_small2_bg").eq(0).children("img").attr("src");
	$("#mainphoto img").attr('src',$mainurl2);
	
	$has_property_photo2 =$(".property_photos_small_bg").children().is('.property_photo_small2');
	if (!$has_property_photo2)
		$("#mainphoto img").attr('src','image/noimage.gif');
	$(".property_photo_small2_bg img").addClass('cursorhand').click(function(){
		$url2 =$(this).attr("src");
		$("#mainphoto img").attr('src',$url2);
	});
	
	
	
	// delete photo
	$(".delete_photo").addClass('cursorhand').click(function(){
		$id_photo =$(this).parent().attr("name");
		$url =$("#main").attr("name");
		
		$.get("delete_photo.php",{ id_photo: $id_photo },function(data){
			if (data){
				alert("图片已成功删除");
				window.location=$url;
			}else {
				alert("删除失败，请稍后再试");
			}
		});
		
	});
	
	// add fav
	$(".property_favor").addClass('cursorhand').click(function(){
		$id_account =$(this).attr("account");
		$id_property =$(this).attr("name");
		
		$.get("wish_add.php",{ id_account: $id_account, id_property: $id_property},function(data){
			if (data==100){
				alert("该房产已成功添加到你的收藏列表");
			}else if (data==10){
				alert("该房产已在你的收藏列表里");
			}else{
				alert("添加失败，请登陆后再试");
			}
		});
		
	});
	
	// add fav
	$(".property_favor2").addClass('cursorhand').click(function(){
		$id_account =$(this).attr("account");
		$id_property =$(this).attr("name");
		
		$.get("wish_add.php",{ id_account: $id_account, id_property: $id_property},function(data){
			if (data==100){
				alert("该房产已成功添加到你的收藏列表");
			}else if (data==10){
				alert("该房产已在你的收藏列表里");
			}else{
				alert("添加失败，请登陆后再试");
			}
		});
		
	});
	
	// remove fav
	$(".property_favor_remove").addClass('cursorhand').click(function(){
		$id_account =$(this).attr("account");
		$id_property =$(this).attr("name");
		//alert($id_account);
		//alert($id_property);
		$.get("wish_remove.php",{ id_account: $id_account, id_property: $id_property},function(data){
			if (data==100){
				alert("该房产已成功从你的收藏列表删除");
				window.location='my_wishlist.php';
			}else {
				alert("添加失败，请稍后再试");
			}
		});
		
	});
	
	// add contact sales property_developer_link
	
	// jdialog
	$(".property_contact").addClass('cursorhand').click(function(){
		$id_property =$(this).siblings(".property_favor").attr("name");
		//alert($id_property);
		window.location='contact_sales.php?id='+$id_property;
	});
	$(".property_contact2").addClass('cursorhand').click(function(){
		$id_property =$(this).siblings(".property_favor2").attr("name");
		//alert($id_property);
		window.location='contact_sales.php?id='+$id_property;
	});
	$(".property_contact3").addClass('cursorhand').click(function(){
		$id_property =$(this).siblings(".property_favor_remove").attr("name");
		//alert($id_property);
		window.location='contact_sales.php?id='+$id_property;
	});
	$(".property_developer_link").addClass('cursorhand').click(function(){
		//window.location='contact.php';
		window.location='contact_sales.php?from=developer';
	});
	
	// email sub_input
	$("#sub_input").click(function(){
		//$(this).attr("value","");
		//alert($id_property);
		
	});
	
	
	// delete enquiry
	$(".drop_enquiry").addClass('cursorhand').click(function(){
		$id_enquiry =$(this).attr("name");
		
		$.get("delete_enquiry.php",{ id_enquiry: $id_enquiry },function(data){
			if (data){
				alert("该咨询已删除");
				window.location="admin_enquiry.php";
			}else {
				alert("删除失败，请稍后再试");
			}
		});
		
	});
	// delete tracking
	$(".drop_tracking").addClass('cursorhand').click(function(){
		$id_tracking =$(this).attr("name");
		
		$.get("delete_tracking.php",{ id_tracking: $id_tracking },function(data){
			if (data){
				alert("该订单已成功删除");
				window.location="admin_tracking.php";
			}else {
				alert("删除失败，请稍后再试");
			}
		});
		
	});
	// delete contract
	$(".drop_contract").addClass('cursorhand').click(function(){
		$id_contract =$(this).attr("name");
		
		$.get("delete_contract.php",{ id_contract: $id_contract },function(data){
			if (data){
				alert("remove the contract successfully");
				window.location="admin_tracking.php";
			}else {
				alert("failed, try again.");
			}
		});
		
	});
	
	// subscribe
	$("#button_subscribe").click(function(){
		$email =$("#sub_input").attr("value");
		
		// 有效email
		$.get("is_valid_email.php",{ email: $email },function(data){
			if (data == true){
				// 添加到 maillist
				$.get("add_email.php",{ email: $email },function(data){
					if (data > 0){
						// 添加成功
						alert("您已成功注册到我们的铂地房地产俱乐部！我们热切期盼能够在未来帮助您！");
					}else {
						// 添加不成功
						alert("订阅失败，请稍后再试");
					}
				});
			}else {
				alert("对不起，用一个有效的电子邮件地址再试一次。");
			}
		});
		
		
	});
	
	
	// print
	$(".property_print").addClass('cursorhand').click(function(){
		$id_property =$(this).siblings(".property_favor2").attr("name");
		//window.location='print.php?id='+$id_property;
		window.open('print.php?id='+$id_property);
	});
	
	// userlist
	$("#userlist_guest").addClass('cursorhand').click(function(){
		$(this).attr('class','button_user_current');
		$(this).siblings("div").attr('class','button_user');
		$(this).addClass('cursorhand');
		$(this).siblings("div").addClass('cursorhand');
		
		$.get("get_userlist.php",{ type: 0, page: 1 },function(data){
			$("#userlist_content").html(data);
			
			$(".userlist_page").addClass('cursorhand').click(function(){
				$type =$(this).attr("type");
				$page_num =$(this).attr("name");
				
				$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
					$("#userlist_table").html(data);
				});
			});
			
		});
	});
	$("#userlist_sales").addClass('cursorhand').click(function(){
		$(this).attr('class','button_user_current');
		$(this).siblings("div").attr('class','button_user');
		$(this).addClass('cursorhand');
		$(this).siblings("div").addClass('cursorhand');
		
		$.get("get_userlist.php",{ type: 1, page: 1 },function(data){
			$("#userlist_content").html(data);
			
			$(".userlist_page").addClass('cursorhand').click(function(){
				$type =$(this).attr("type");
				$page_num =$(this).attr("name");
				
				$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
					$("#userlist_table").html(data);
				});
			});
			
		});
	});
	$("#userlist_admin").addClass('cursorhand').click(function(){
		$(this).attr('class','button_user_current');
		$(this).siblings("div").attr('class','button_user');
		$(this).addClass('cursorhand');
		$(this).siblings("div").addClass('cursorhand');
		
		$.get("get_userlist.php",{ type: 10, page: 1 },function(data){
			$("#userlist_content").html(data);
			
			$(".userlist_page").addClass('cursorhand').click(function(){
				$type =$(this).attr("type");
				$page_num =$(this).attr("name");
				
				$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
					$("#userlist_table").html(data);
				});
			});
			
		});
	});
	$("#userlist_manager").addClass('cursorhand').click(function(){
		$(this).attr('class','button_user_current');
		$(this).siblings("div").attr('class','button_user');
		$(this).addClass('cursorhand');
		$(this).siblings("div").addClass('cursorhand');
		
		$.get("get_userlist.php",{ type: 100, page: 1 },function(data){
			$("#userlist_content").html(data);
			
			$(".userlist_page").addClass('cursorhand').click(function(){
				$type =$(this).attr("type");
				$page_num =$(this).attr("name");
				
				$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
					$("#userlist_table").html(data);
				});
			});
			
		});
	});
	$("#userlist_developer").addClass('cursorhand').click(function(){
		$(this).attr('class','button_user_current');
		$(this).siblings("div").attr('class','button_user');
		$(this).addClass('cursorhand');
		$(this).siblings("div").addClass('cursorhand');
		
		$.get("get_userlist.php",{ type: 127, page: 1 },function(data){
			$("#userlist_content").html(data);
			
			$(".userlist_page").addClass('cursorhand').click(function(){
				$type =$(this).attr("type");
				$page_num =$(this).attr("name");
				
				$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
					$("#userlist_table").html(data);
				});
			});
			
		});
	});
	
	$(".userlist_page").addClass('cursorhand').click(function(){
		$type =$(this).attr("type");
		$page_num =$(this).attr("name");
		
		$.get("get_userlist_table.php",{ type: $type, page: $page_num },function(data){
			$("#userlist_table").html(data);
		});
	});
	
});
