﻿jQuery(function() {

    //  prevent form submit
    jQuery('form').bind("submit", function() { return false; });

    //  IE is goofy...
    jQuery('#parcelSearch input[type=text], input.txt').keypress(function(e) {
        if (e.keyCode == 13) {
            jQuery(this).blur();
            jQuery('#btnSearch').focus().click();
            return false;
        }
    });

    //  Handle form search result paging
    jQuery('.paging li span.page-button').live('click', function() {
        if (undefined === jQuery(this).data('page')) { return; }
        var _page = jQuery(this).data('page');
        ApplicationUtility.last_search_object.page = _page;
        ApplicationUtility.formSearch();
        return false;
    });

    //  Handle column sorting form search result
    jQuery('#search-result-table thead th,#search-result-table tfoot th').live('click', function() {
        if (undefined === jQuery(this).data('orderBy')) { return false; }
        ApplicationUtility.last_search_object.page = (undefined === jQuery('#paging-current-page').data('page')) ? 1 : jQuery('#paging-current-page').data('page');
        ApplicationUtility.last_search_object.orderByColumnIndex = jQuery(this).data('orderBy');
        //  if already asc, sort desc
        ApplicationUtility.last_search_object.orderByDesc = (jQuery(this).hasClass('asc')) ? 'true' : '';
        ApplicationUtility.formSearch();
        return false;
    });

    //  Handle click of a row from a form search
    jQuery('#search-result-table tbody tr').live('click', function() {
        if (undefined === jQuery(this).data('page')) { return false; }
        var _page = jQuery(this).data('page');
        ApplicationUtility.parcelSearch(_page);
        return false;
    });

    //  Single parcel result Paging
    jQuery('div.parcel-paging span').live('click', function() {
        if (undefined === jQuery(this).data('page')) { return false; }
        var _page = jQuery(this).data('page');
        ApplicationUtility.parcelSearch(_page);
        return false;
    });

    //  Open and close functionality of parcel result tables
    jQuery('#content .parcel-item h4 img').live('click', function() {
        var _table = jQuery(this).parents('div.parcel-item').children('div.parcel-item-table-wrapper');
        var _this = jQuery(this);
        switch (_table.css('display')) {
            case 'none':
                _table.slideDown(200, function() {
                    _this.attr('src', 'images/icons/close.png');
                });
                break;
            default:
                _table.slideUp(200, function() {
                    _this.attr('src', 'images/icons/open.png');
                });
        }
        return false;
    });

    //  Save search click
    jQuery('#bookmarkSearch, #bookmarkParcel').live('click', function() {
        var _this = jQuery(this);
        if (undefined === _this.data('bookmark')) { return false; }
        var _bookmarkUrl = _this.data('bookmark');
        var _parcel = (undefined === _this.data('parcel')) ? false : _this.data('parcel');
        var _title = 'Osceola County Parcel Search';
        _title += (_parcel == false) ? '' : ' - ' + _parcel;
        ApplicationUtility.addBookmark(_title, _bookmarkUrl);
    });

    jQuery('.view_search').live('click', function(e) {
        jQuery('.tabs li').eq(0).click();
    });

});
