﻿//  Ajax Form search function

ApplicationUtility.prototype.formSearch = function(searchObject) {
    if (searchObject !== undefined || searchObject != null) {
        this.last_search_object = searchObject;
        this.last_parcel_search = searchObject;
    }

    jQuery.ajax({
        cache: true,
        data: this.last_search_object,
        url: 'ajax/FormSearch.aspx',
        beforeSend: function() {
            jQuery('#search-results').html('').addClass('ajax-working');
            jQuery('.tabs li').eq(2).click();
        },
        success: function(data, textStatus, XMLHttpRequest) {
            jQuery('#search-results').html(data);
            jQuery('#search-results').focus();
            jQuery('#search-result-table').focus();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            jQuery('#search-results').html('<p>An error occurred while searching.</p>');
        },
        complete: function(XMLHttpRequest, textStatus) {
            jQuery('#search-results').removeClass('ajax-working');
            jQuery('#search-result-table tbody tr:odd').addClass('alt-row');
        }
    });
}


//  Single parcel(page) search
ApplicationUtility.prototype.parcelSearch = function(page) {
    this.last_parcel_search.page = page;

    jQuery.ajax({
        cache: true,
        data: this.last_parcel_search,
        url: 'ajax/ParcelSearch.aspx',
        beforeSend: function() {
            jQuery('#parcel-result').html('').addClass('ajax-working');
            jQuery('.tabs li').eq(3).click();
        },
        success: function(data, textStatus, XMLHttpRequest) {
            jQuery('#parcel-result').html(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            jQuery('#parcel-result').html('<p>An error occurred while searching.<br /><strong>Error: </strong>' + errorThrown + '</p>');
        },
        complete: function(XMLHttpRequest, textStatus) {
            jQuery('#parcel-result').removeClass('ajax-working');
            jQuery('table.parcel-result-table tbody').each(function() {
                jQuery(this).find('tr:odd').addClass('alt-row');
            });
            jQuery('#agsFeatureImage').lightBox();
            jQuery('.building-photos-table').lightBox({ fixedNavigation: true });
            jQuery('.building-photos').each(function() {
                jQuery(this).lightBox();
            });
            jQuery('.building-sketches').each(function() {
                jQuery(this).lightBox();
            });
        }
    });
}


//  Single parcel search
ApplicationUtility.prototype.singleParcelSearch = function(searchObject) {
    if (searchObject === undefined || searchObject == null) { return false; }

    jQuery.ajax({
        cache: true,
        data: searchObject,
        url: 'ajax/ParcelSearch.aspx',
        beforeSend: function() {
            jQuery('#parcel-result').html('').addClass('ajax-working');
            jQuery('.tabs li').eq(3).click();
        },
        success: function(data, textStatus, XMLHttpRequest) {
            jQuery('#parcel-result').html(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            jQuery('#parcel-result').html('ERROR');
        },
        complete: function(XMLHttpRequest, textStatus) {
            jQuery('#parcel-result').removeClass('ajax-working');
            jQuery('table.parcel-result-table tbody').each(function() {
                jQuery(this).find('tr:odd').addClass('alt-row');
            });
            jQuery('#agsFeatureImage').lightBox();
            jQuery('.building-photos-table').lightBox({ fixedNavigation: true });
            jQuery('.building-photos').each(function() {
                jQuery(this).lightBox();
            });
            jQuery('.building-sketches').each(function() {
                jQuery(this).lightBox();
            });
        }
    });
}


ApplicationUtility.prototype.identifyMapSearch = function(parcel){
    this.singleParcelSearch({ 'parcelid': parcel });
}


ApplicationUtility.prototype.selectionMapSearch = function(parcelList){
    this.formSearch({'parcelidlist': parcelList});
}
