$(document).ready(function()
{
    var ja = window.location.pathname;
    var o = ja.split('/');
    var js = o[2];
    var c = o[3];
    function create_desc(ss)
    {
        var jj = ss.split('_');
        var s = '';
        s += '<ul>';
        for (i = 0; i < jj.length; i++) 
        {
            s += '<li align="left">' + jj[i] + '</li>';
        }
        s += '</ul>';
        return s;
    }
    function load_listings(r, page, closeBtn)
    {
        GUnload();
        var si = (page !== null) ? 'search/' + page : 'search/';
        var sc = ($('#limit > :selected').exists()) ? $('#limit > :selected').val() : '10';
        var sl = $('input#price').val().replace(/[^0-9]/g, '');
        var sn = $('input#rooms').val();
        var sa = $('input#size').val().replace(/[^0-9]/g, '');
        var sr = $('input#keyword').val();
        var so = set_districts();
        $.post(si, 
        {
            limit: sc,
            sort: 'date',
            order: 'desc',
            price: sl,
            rooms: sn,
            size: sa,
            keyword: sr,
            district: so,
            type: c
        }, function(n)
        {
            var s = '\n' + '<table id="count_r" border="0">\n' + '   <tr>\n' + '       <td align="center" width="40%">\n' + '           Found <span class="numerals">' + n.pager.listings + '</span> Listings\n' + '       </td>\n' + '       <td align="center" width="40%">' + '           Page <span class="numerals">' + n.pager.current + '</span> of Total <span class="numerals">' + n.pager.total + '</span> pages ' + '       </td>\n' + '       <td align="left">\n' + '           <select name="limit" id="limit">\n' + '               <option value="10">10</option>\n' + '               <option value="25">25</option>\n' + '               <option value="50">50</option>\n' + '           </select>\n' + '       </td>\n' + '   </tr>\n' + '</table>\n' + '<table id="report" cellspacing=0 cellpadding="10" width="100%" border="0">\n' + '   <tr>\n' + '       <th align="left" colspan="2">Date</th>\n' + '       <th align="left">Property</th>\n' + '       <th align="left">Location</th>\n' + '       <th>Rooms</th>\n' + '       <th>Price</th>\n' + '   </tr>';
            $(n.listings).each(function()
            {
                s += '     <tr class="odd"><td onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');"><div class="ui-icon ui-icon-plus"></div></td>\n' + '         <td onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');">' + this.date + '</td>\n' + '         <td align="left"  onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');">\n' + '             <a href="' + window.location + '/' + this.id + '" onclick="return false;">' + this.property + '</a>\n' + '         </td>\n' + '         <td align="left" onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');">' + this.location + '</td>\n' + '         <td align="center" onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');">' + this.rooms + '</td>\n' + '         <td align="center" onclick="get_map(\'' + this.street + '\', \'' + this.property + '\', \'' + this.id + '\', \'../coord\',\'' + c + '\');">$ ' + format_price(this.price) + '</td>\n' + '     </tr>\n' + '     <tr class="even" style="display:none;">\n' + '         <td colspan="6">\n';
                if (this.loggedin == true) 
                {
                    s += '<div class="description">' + this.details + '<br/></div>\n';
                }
                else 
                {
                    s += '<div class="description">' + create_desc(this.memo) + '</div>\n';
                }
                s += '             <div class="details">\n' + '                 <div id="m' + this.id + '" class="mbox" style="display:block;width:49%;height:300px;float:left;">\n' + '                     We couldn\'t find a Map for this property\n' + '                 </div>\n' + '                 <div id="p' + this.id + '" class="mbox" style="display:block;width:49%;height:300px;float:right;">\n' + '                 </div>\n' + '              </div>\n' + '         </td>\n' + '     </tr>';
            });
            s += '</table>';
            $(r).empty();
            $(r).html(s);
            if (n.pager.previous) 
            {
                var sj = '<span class="fg-button ui-state-default ui-corner-all overlay-prev">Page ' + ((page != '') ? page - 1 : '1') + '</span>';
                $(sj).insertBefore(closeBtn).click(function()
                {
                    $(r).empty();
                    $(closeBtn).prevAll().remove();
                    load_listings(r, n.pager.previous, closeBtn);
                });
            }
            if (n.pager.next) 
            {
                var je = '<span class="fg-button ui-state-default ui-corner-all overlay-next">Page ' + ((page != '') ? page + 1 : '2') + '</span>';
                $(je).insertBefore(closeBtn).click(function()
                {
                    $(r).empty();
                    $(closeBtn).prevAll().remove();
                    load_listings(r, n.pager.next, closeBtn);
                });
            }
        }, "json");
    }
    $('button#search_button').click(function()
    {
        $('.pager').prevAll().remove();
        load_listings('#results', '', '.pager');
    });
    $('#count_r').livequery(function()
    {
        $("#limit").jqSelecta(
        {
            closeButton: true,
            autoScrollOverflowX: false,
            autoScrollOverflowY: false,
            autoSelected: true,
            titleSetFrom: 'text',
            width: '100px',
            columns: 1,
            title: 'Results Per page'
        });
    });
    load_listings('#results', '', '.pager');
    $('#keyword').val("Enter the name of the property or a keyword to search e.g. 'Marina' or 'MRT'").css(
    {
        'color': 'grey'
    });
});
