$(function () {

var newcp_tpl = '<table width="630" border="0" cellpadding="0" cellspacing="0" class="line">'
              + '<tr class="line">'
              + '<td width="70" align="left">({icon})</td>'
              + '<td width="70" align="left">({address})</td>'
              + '<td width="160" align="left"><a href="./details/index.php?sid=({space_id})">({name})</a></td>'
              + '<td width="330" align="left">({campaign})</td>'
              + '</tr>'
              + '</table>';

$("#pid_sp").change(function () {
    var pid = $(this).val();
    if (pid == '') { return false; }
    location.href = './list/index.php?pid='+pid+'&sp=1';
});
$("#pid_tr").change(function () {
    var pid = $(this).val();
    if (pid == '') { return false; }
    location.href = './list/index.php?pid='+pid+'&tr=1';
});
$("#pid_bk").change(function () {
    var pid = $(this).val();
    if (pid == '') { return false; }
    location.href = './list/index.php?pid='+pid+'&bk=1';
});

var crt_area = 3;

$("#new > ul > li > a").each(function () {
    $(this).click(function() {
        var target_area = $(this).parent("li").attr('id').substr(4);
        if (target_area == crt_area+'b') {
            return false;
        }
        crt_area = $(this).parent("li").attr('id').substr(4);
        $("#new > ul > li").each(function() {
            var area_id = $(this).attr('id');
            if (area_id == 'new0'+crt_area) {
                $(this).attr('id', 'new0'+crt_area+'b');
            } else {
                $(this).attr('id', area_id.substr(0, 5));
            }
        });
        $.post('./xml/rtn_newcp_space_list.php', {aid:crt_area}, function (xml) {
            $("#new_tb").html('');
            if (xml) {
                $("space", xml).each(function () {
                    var line = newcp_tpl;
                    line = line.replace(/\(\{space_id\}\)/g, $(this).attr("id"));
                    line = line.replace(/\(\{icon\}\)/g, $(this).children("icon").text());
                    line = line.replace(/\(\{address\}\)/g, $(this).children("address").text());
                    line = line.replace(/\(\{name\}\)/g, $(this).children("name").text());
                    var campaign = '';
                    if ($(this).children("campaign")) {
                        campaign = $(this).children("campaign").text();
                    }
                    line = line.replace(/\(\{campaign\}\)/g, campaign);
                    $("#new_tb").append(line);
                });
            }
        });
    });
});

});
