//选择预约天数
function textNumToObj(obj)
{  
    var tex = parseInt($(obj).attr("oid"));
	var tex_tmp =tex;
	if(tex_tmp==1)
	{
		
		var tex_tmp='今天';
		$("#jsToNumber_tmp").text(tex_tmp);
		$("#oldhouse_days").val('1');
	}
	else
	{
		
		$("#oldhouse_days").val('5');
		var tex_tmp=tex_tmp+"天";
		
		$("#jsToNumber_tmp").text(tex_tmp);
	}
    $("#jsToNumber").text(tex);
}

$(function(){
	//显示隐藏房源
	var _len = $("#jsHouseBoxFunction").find(".houseItem_info").length;
	if(_len>5)
	{
		$("#jsPicDepotSZBtn").show();
	}
	else
	{
		$("#jsPicDepotSZBtn").hide();
	}
	$("#jsPicDepotSZBtn").find(".jsZK").bind("click",function(){
		$(this).hide().siblings(".jsSQ").show();
		$("#jsHouseBoxFunction").css("height","auto");
	});
	$("#jsPicDepotSZBtn").find(".jsSQ").bind("click",function(){
		$(this).hide().siblings(".jsZK").show();
		$("#jsHouseBoxFunction").css("height","80px");
	})

	//显示隐藏房源
	var _len2 = $("#jsHouseBoxFunction2").find(".houseItem_info").length;
	if(_len2>5)
	{
		$("#jsPicDepotSZBtn2").show();
	}
	else
	{
		$("#jsPicDepotSZBtn2").hide();
	}
	$("#jsPicDepotSZBtn2").find(".jsZK").bind("click",function(){
		$(this).hide().siblings(".jsSQ").show();
		$("#jsHouseBoxFunction2").css("height","auto");
	});
	$("#jsPicDepotSZBtn2").find(".jsSQ").bind("click",function(){
		$(this).hide().siblings(".jsZK").show();
		$("#jsHouseBoxFunction2").css("height","80px");
	})
	$(".jsCloseItem2").bind("click",function(){
		$("#delhfp2_tex").val($(this).attr("id"));
		openWin("jsDialogDel2");
		return false
	})
	$(".delhfp2").bind("click",function(){
		var delh_id = $("#delhfp2_tex").val();
		$("#"+delh_id).parents(".houseItem_info").remove();
	    _len = $("#jsHouseBoxFunction2").find(".houseItem_info").length;
	    if(_len>5)
		{
			$("#jsPicDepotSZBtn2").show();
		}
		else
		{
			$("#jsPicDepotSZBtn2").hide();
		}
		$("#jsHouseNumber2").text(_len);
	})
	//删除预约房源 
	 if (!window.XMLHttpRequest)
	 {
		$(".houseLink").hover(function(){
			$(this).find(".jsCloseItem").show()
		},function(){
			$(this).find(".jsCloseItem").hide()
		})
	 }
	$(".jsCloseItem").bind("click",function(){
		$("#delhfp_tex").val($(this).attr("id"));
		openWin("jsDialogDel");
		return false
	})
	$(".delhfp").bind("click",function(){
		var delh_id = $("#delhfp_tex").val();
		$("#"+delh_id).parents(".houseItem_info").remove();
	    _len = $("#jsHouseBoxFunction").find(".houseItem_info").length;
	    if(_len>5)
		{
			$("#jsPicDepotSZBtn").show();
		}
		else
		{
			$("#jsPicDepotSZBtn").hide();
		}
		$("#jsHouseNumber").text(_len);
	})
	//最多显示输入6个字
	countFunInput("jsRenovateInput",6);
	//增加分钟 并且 排序
	$("#jsRenovateHour dd a").click(function(){
		var _this=$(this);
		var i = 0;
		var oid = _this.attr("oid");
		
		var odl = $("#jsRenovateMinute").find("dl");
		var htmlInner ='<dl class="'+oid+'"><dt>'+oid+'</dt><dd><a href="#">00</a></dd><dd class="mr49"><a href="#">05</a></dd><dd><a href="#">10</a></dd><dd><a href="#">15</a></dd><dd><a href="#">20</a></dd><dd class="mr50"><a href="#">25</a></dd><dd><a href="#">30</a></dd><dd><a href="#">35</a></dd><dd><a href="#">40</a></dd><dd class="mr49"><a href="#">45</a></dd><dd><a href="#">50</a></dd><dd class="mr0"><a href="#">55</a></dd></dl>'
		
		var a = parseFloat(oid);
		var b = parseFloat(odl.eq(i).attr("class"));

		if($("#jsRenovateMinuteBox:visible"))
		{
			$("#jsRenovateMinuteBox").show()
		}

		if(!_this.hasClass("on"))
		{
			_this.addClass("on")
			if(odl.length == 0)
			{
				$("#jsRenovateMinute").append(htmlInner);
			}
			else
			{	
				for(i=0;i<odl.length;i++)
				{
					if(a - parseInt(odl.eq(i+1).attr("class")) < 0 && a - b > 0)
					{
						odl.eq(i).after(htmlInner)
						break	
					}
					else if(a - b > 0 && odl.eq(i+1).attr("class") == undefined)
					{
						odl.eq(i).after(htmlInner)
						break
					}
					else if(a - b < 0 && odl.eq(i+1).attr("class") == undefined)
					{
						odl.eq(i).before(htmlInner)
						break
					}
					else if(a - parseInt(odl.eq(i+1).attr("class") ) < 0 && a - b < 0)
					{
						odl.eq(i).before(htmlInner)
						break
					}
				}	
			}
		}
		else
		{
			_this.removeClass("on");
			var _input = $("#jaRenovateHourHide");
			$("#jsRenovateMinute").find("."+oid).find(".on").each(function(index, element) {
				var _str = oid+':'+$(this).text()
				_input.val(function(){
					return _input.val().replace(_str+',',"")
				}) 
			});
			$("#jsRenovateMinute").find("."+oid).remove();
			if( $("#jsRenovateMinute").find("dl").length <= 0)
			{
				$("#jsRenovateMinuteBox").hide()
			}
		}
		var _len = $("#jsRenovateMinute").find("a.on").length;
		$("#jsTimeNumber").text(_len);

		var _house = $("#jsHouseNumber").text();
		var _total = _len * _house;
		$("#jsTotalNumber").text(_total);

		return false;
	})
	//选择分钟
	$("#jsRenovateMinute").find("a").live("click",function(){
		var _hour = $(this).parents("dl").attr("class");
		var _str = _hour+":"+ $(this).text();
		var _input = $("#jaRenovateHourHide"); //传值 隐藏 input
		if($("#jsRenovateMinute").find("a.on").length >=10 && !$(this).hasClass("on"))
		{
			openWin("jsDialog");
				$("#jsDialog .text").empty().html("对不起!您设置的预约时间点已超限");
			return false
		}
		if($(this).hasClass("on"))
		{
			_input.val(function(){
				return _input.val().replace(_str+',',"")
			}) 		
		}
		if(!$(this).hasClass("on"))
		{
			_input.val(function(){
				return _input.val() + _str +','
			}) 	
		}	
		$(this).toggleClass("on");
		
		var _len = $("#jsRenovateMinute").find("a.on").length;
		$("#jsTimeNumber").text(_len);
		
		var _house = $("#jsHouseNumber").text();
		var _total = _len * _house;
		$("#jsTotalNumber").text(_total);

		return false
	})

	$(".savebutton").live("click",function(){
		if(checkPostProjectName())
		{
			$("#formaction").val("save_project");
			checkPostProjectTime();
			openWin("jsDialogSave");
		}
	})

	$(".resetbutton").live("click",function(){
		if(checkPostProjectName())
		{
			$("#formaction").val("reset_project");
			checkPostProjectTime();
			openWin("jsDialogSave");
		}
	})

	$(".setbutton").live("click",function(){
		if(checkPostProjectName())
		{
			checkPostProjectTime();
			openWin("jsDialogSave");
		}
	})

	$(".addbutton").live("click",function(){
		if(checkPostProjectName())
		{
			checkPostProjectTime();
			openWin("jsDialogSave");
		}
	})

	$(".savePrjoect").bind("click",function(){
		$("#project_form").submit();
	})
})

function checkPostProjectName()
{
	var project_name = $("#jsRenovateInput").val();
	if(project_name != "")
	{
		return true;
	}
	else
	{
		openWin("jsDialog");
		$("#jsDialog .text").empty().html("请填写方案名称！");
		
		return false;
	}
}

function checkPostProjectTime()
{
    var project_time = $("#jaRenovateHourHide").val();
    
    if(project_time == "")
    {
        $("#dialogSaveDiv").html("该方案没有设置刷新时间点，关联房源将不被刷新。<br />确定保存该预约刷新方案？");
    } else {
        $("#dialogSaveDiv").html("确定保存该预约刷新方案？");
    }
}

$(function(){
    if (!window.XMLHttpRequest)
    {
        $(".btnRelative").hover(function(){
            $(this).find(".closeItem").css("display","block");
        },function(){
            $(this).find(".closeItem").hide();
        })
    }
    $('.projectDel').bind("click",function(){
        openWin("jsProjectDel");
        $("#jsProjectDel_name").html($(this).attr("title"));
        $("#pid").val($(this).attr("id"));
        $("#secret_key").val($(this).attr("secret_key"));
        return false;
    })
    $(".delproject").bind("click",function(){
        var pid = $("#pid").val();
        var secret_key = $("#secret_key").val();
        $.ajax({
            type: "get",
            url: "http://nj.zsb.house365.com/reserve/delete_project/",
            data: {'secret_key':secret_key , 'project_id':pid },
            dataType: "json",
            success: function(data)
            {
                if( data.is_ok == '1' )
                {    
                    $("#dialogts #dialog_tip").html(data.msg);
                    openWin('dialogts');
                }
                else
                {    
                    $("#dialogts #dialog_tip").html(data.msg);
                    openWin('dialogts');
                }
               setTimeout(function(){window.location.href='http://nj.zsb.house365.com/reserve/project_manage/';}, 3000);
            }
        });
    })
})