﻿$(document).ready(function () {
    bindEditPage();
    bindListPage();
    //initTooltip();
});

function initTooltip() {

    $.tools.tooltip.addEffect("myEffect",

    // show function
	function (done) {

	    // 'this' variable is a reference to the tooltip API
	    var conf = this.getConf(), tip = this.getTip();

	    // peform your effect. for example:
	    tip.css({ opacity: 1, width: '+=50' });

	    // after you have finished you must do
	    done.call();
	},

    // hide function
	function (done) {

	    // peform your effect. for example:
	    this.getTip().animate({ opacity: 0, width: '-=50' }, done);
	}
);
    $("[title]").tooltip();
}

function bindListPage() {
    $("#select_all").click(function () {
        $("input[name=ids]'").attr("checked", $("#select_all").attr("checked"));
    });
    $("a.delete").click(function () {
        var ids = "";
        $("input[name=ids]'").each(function () {
            if ($(this).attr("checked")) {
                ids += $(this).val() + ",";
            }
        });
        if (ids == "") {
            alert("请选择要删除的记录");
        }
        else {
            var url = $(this).attr("rel");
            if (url.indexOf("?") >=0 )
            {
                DoPost(url, "&ids=" + ids);
            }
            else 
            {
                DoPost(url, "?ids=" + ids);    
            }
        }
    });

    $("select.numPerPage").change(function () {
        document.cookie = "numPerPage=" + $(this).val();
        window.location.reload();
    });
}

function bindEditPage() {
    $("a.back").click(function () {
        history.back();
    });
}

function DoPost(url, param) {
    $.ajax({
        type: 'POST',
        url: url + param,
        dataType: "json",
        cache: false,
        success: ajaxSuccess,
        error: ajaxError
    });
}

function ajaxSuccess(data) {
    if (data.message) {
        alert(data.message);
    }
    if (data.action == 'reload') {
        // window.location.replace(window.location);
        window.location.reload();
    }
    else {
        window.alert(data.message);
    }
}

function ajaxError(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest.responseText);
    //alert("Failed due to unknown reason, please try later.");
}
