$(function () {

var result_tpl = '<table width="350" border="0" class="list_tbl" id="space({space_id})">'
               + '<tr>'
               + '<td width="50" rowspan="2">({marker})</td>'
               + '<td align="left"><a href="../details/index.php?sid=({space_id})"><strong>({name})</strong></a></td>'
               + '<td width="100">&nbsp;</td>'
               + '</tr>'
               + '<tr>'
               + '<td align="left">({address})</td>'
               + '<td width="100" align="right">({contact})</td>'
               + '</tr>'
               + '</table>';

var newcp_tpl = '<table width="350" border="0" class="cam_tbl">'
              + '<tr>'
              + '<td width="65" rowspan="2" align="left" valign="top">({icon})</td>'
              + '<td>({address})　<a href="../details/index.php?sid=({space_id})">({name})</a></td>'
              + '</tr>'
              + '<!--tr>'
              + '<td>({campaign})</td>'
              + '</tr-->'
              + '</table>';

if ($("#googlemap").size() > 0) {
    var map = new Map();
    var pref_id = parseInt($("#pid").val());
    var zoom = 11;
    if ($("#list_result > table").size() <= 10) {
        switch (pref_id) {
        case 1:
        case 47:
            zoom = 7;
            break;
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 16:
        case 17:
        case 19:
        case 21:
        case 23:
        case 24:
        case 30:
        case 32:
        case 38:
        case 39:
        case 44:
        case 45:
        case 46:
            zoom = 8;
            break;
        default:
            zoom = 9;
        }
    }
    map._initialize(zoom);
    map._setCustomInfoWindow();
    $("#list_result > table").each(function () {
        var space_id = $(this).attr('id').substr(5);
        if (space_id != '') {
            var kind = $("#kind"+space_id).val();
            var status = $("#status"+space_id).val();
            if (kind != '' && status != '') {
                var mlatlng = new GLatLng(parseFloat($("#lat"+space_id).val()),parseFloat($("#lng"+space_id).val()));
                var iw_val = {
                    'space_id' : space_id,
                    'name' : $("#name"+space_id).html(),
                    'address' : $("#address"+space_id).html(),
                    'contact' : $("#contact"+space_id).html()
                };
                map._setMarkerWithCustomIW(mlatlng, space_id, kind, status, iw_val);
            }
        }
    });
    map._addMarkers(6);

    if ($("#map_exp").size() > 0) {
        $("#map_exp").fadeIn('slow');
        $("#btn_meclose").click(function () {
            $("#map_exp").hide();
        });
    }
}

var search_func = function () {
    map._clearMarkers();
    $("#list_result").html('');
    $("#list_newcp").html('');
    $("#err_result").hide();
    $("#err_newcp").hide();
    $.post('./xml/rtn_space_list.php', $("#search_form").serialize(), function (xml) {
        var s_cnt = 0;
        var scp_cnt = 0;
        if (xml) {
            $("space", xml).each(function () {
                var space_id = parseInt($(this).attr('id'));
                var lat = parseFloat($(this).children("latitude").text());
                var lng = parseFloat($(this).children("longitude").text());

                if ($(this).children('kind').text() != '' && $(this).children('status').text() != '' && lat != 0 && lng != 0) {
                    var iw_val = {
                        'space_id' : space_id,
                        'name' : $(this).children("name").text(),
                        'address' : $(this).children("fulladdress").text(),
                        'contact' : $(this).children("contact").text()
                    };
                    map._setMarkerWithCustomIW(
                        new GLatLng(lat,lng),
                        space_id,
                        $(this).children('kind').text(),
                        $(this).children('status').text(),
                        iw_val
                    );
                }

                var line = result_tpl;
                line = line.replace(/\(\{space_id\}\)/g, space_id);
                line = line.replace(/\(\{marker\}\)/g, $(this).children("marker").text());
                line = line.replace(/\(\{address\}\)/g, $(this).children("fulladdress").text());
                line = line.replace(/\(\{name\}\)/g, $(this).children("name").text());
                line = line.replace(/\(\{contact\}\)/g, $(this).children("contact").text());
                $("#list_result").append(line);
                s_cnt++;

                if ($(this).attr('new') == 1 || $(this).attr('cp') == 1) {
                    var line = newcp_tpl;
                    line = line.replace(/\(\{space_id\}\)/g, space_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).attr('cp') == 1) {
                        var campaign = $(this).children("campaign").text();
                        line = line.replace(/\(\{campaign\}\)/g, campaign);
                        line = line.replace(/<\!--tr>/g, '<tr>');
                        line = line.replace(/<\/tr-->/g, '</tr>');
                    } else {
                        line = line.replace(/\(\{campaign\}\)/g, '');
                    }
                    $("#list_newcp").append(line);
                    scp_cnt++;
                }
            });
        }
        map._addMarkers(6);
        if (s_cnt == 0) {
            $("#err_result").show();
        }
        if (scp_cnt == 0) {
            $("#err_newcp").show();
        }
    });
};

$("#search_form").submit(function () {
    return false;
});
$("#btn_add_search").click(search_func);
$("#sp").click(search_func);
$("#tr").click(search_func);
$("#bk").click(search_func);
$("#cp").click(search_func);
$("#new").click(search_func);
$("#cmn").change(search_func);
$("#cmx").change(search_func);
});

