function sendCodeBindClick(ajaxUrl, validate) {
    $('#send').bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
}

function clickSendCode(event) {
    var ajaxUrl = event.data.ajaxUrl;
    var validate = event.data.validate;
    var phone = $('#phone').val().trim();
    if (phone.length == 11) {
        var self = $(this);
        self.unbind('click');
        ajaxSendCode(ajaxUrl, self, phone, validate);
    } else {
        $('#notice').html('<span>请输入11位手机号</span>');
        $('#myModal').modal('show');
    }
}

function countDown(ajaxUrl, self, validate) {
    var second = 60;
    self.text(second + '秒后重发');
    var interval = setInterval(function () {
        second--;
        self.text(second + '秒后重发');
        if (second == 0) {
            window.clearInterval(interval);
            self.text('发送验证码');
            self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
        }
    }, 1000);
}

function ajaxSendCode(ajaxUrl, self, phone, validate) {
    var notice = $('#notice');
    var myModal = $('#myModal');
    $.ajax({
        type: 'POST',
        url: ajaxUrl + '/wp-admin/admin-ajax.php/',
        data: 'action=send_code&phone=' + phone + '&validate=' + validate,
        success: function (data) {
            switch (data.code) {
                case 2000:
                    countDown(ajaxUrl, self, validate);
                    break;
                case 2001:
                    self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
                    notice.html('<span>发送验证码失败</span>');
                    myModal.modal('show');
                    break;
                case 2002:
                    if(validate == 1){
                        notice.html('<span>此手机号码尚未注册</span>');
                    }else{
                        notice.html('<span>此手机号码已注册</span>');
                    }
                    myModal.modal('show');
                    self.bind('click', {ajaxUrl: ajaxUrl, validate: validate}, clickSendCode);
                    break;
                default:
                    break;
            }
        }
    });
}

function phoneAddValidate(){
    var phoneTest=/^(13[0-9]|15[0-35-9]|18[0-9]|14[57]|17[06-8])+\d{8}$/;
    $.validator.addMethod("format", function(value, element){
        return phoneTest.test(value);
    }, '手机号码格式有误');
}

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function scrollPullToRefresh(cb) {
    var pullUpFlag = false;
    var scroll = new IScroll('#wrapper', {
        click: true
    });
    scroll.on("scrollStart", function () {
        if (this.maxScrollY >= this.y && this.scrollerHeight > this.wrapperHeight && this.directionY == 1) {
            pullUpFlag = true;
        }
    });
    scroll.on("scrollEnd", function () {
        if (pullUpFlag) {
            pullUpFlag = false;
            cb();
        }
    });
    return scroll;
}

function ajax_get_house(url, loading, ajax_data, context) {
    loading.show();
    $.ajax({
        type: 'POST',
        url: url + '/wp-admin/admin-ajax.php/',
        data: ajax_data,
        success: function (data) {
            loading.hide();
            if (data.code == 2000) {
                $.each(data.result, function (index, value) {
                    var houseType = value.house_type;
                    var template = $('#template_' + houseType).html();
                    var div = $('<div class="row">');
                    div.append(template);
                    div.find('[data-attr=name]').text(value.name);
                    var tags = div.find('[data-attr=tags]');
                    if (houseType == 0) {
                        div.find('[data-attr=type]').text(value.literal);
                        div.find('[data-attr=address]').text(value.address);
                        div.find('[data-attr=latest_news]').text(value.latest_news);
                        var price = value.average_price;
                        if (price.length > 4) {
                            price = (price / 10000).toFixed(2) + '万';
                        }
                        div.find('[data-attr=price]').html('<em>' + price + '</em>/m<em>2</em>');
                    } else if (houseType == 1) {
                        div.find('[data-attr=community]').text(value.community_name);
                        div.find('[data-attr=apartment]').text(value.bp_literal);
                        div.find('[data-attr=covered_area]').text(value.covered_area + '平米');
                        div.find('[data-attr=price]').html('<em>' + (value.total_price / 10000) + '万</em>');
                    } else if (houseType == 2) {
                        div.find('[data-attr=community]').text(value.community_name);
                        div.find('[data-attr=apartment]').text(value.bp_literal);
                        div.find('[data-attr=covered_area]').text(value.covered_area + '平米');
                        div.find('[data-attr=decoration]').text(value.decoration);
                        var rent = value.rent;
                        if (rent.length > 4) {
                            rent = (rent / 10000).toFixed(2) + '万';
                        }
                        div.find('[data-attr=price]').html('<em>' + rent + '</em>/月');
                    }
                    var image = div.find('[data-attr=image]');
                    if (value.path){
                        image.attr('src', value.path);
                    }else{
                        image.attr('src', url + '/wp-content/themes/tospur/img/img.jpg');
                    }
                    if (value.tags) {
                        var tagsArray = value.tags.split(',');
                        console.log(tagsArray);
                        $.each(tagsArray, function (k, v) {
                            if (k < 3) {
                                tags.prepend('<span class="label">' + v + '</span>');
                            }
                        });
                    }
                    div.bind("tap", function () {
                        window.location.href = url + '/?page=detail&hid=' + value.id;
                    });
                    context.append(div);
                });
                ajax_data.index += 10;
                myScroll.refresh();
            }
        }
    });
}