require_once __DIR__ . '/inc/php/functions/collab.functions.php';
}
// CMS Feed
if ($_REQUEST['idx'] == 'cms') {
    Settings::getInstance()->IDX_FEED = 'cms';
}
// Default IDX Page
if (isset($_GET['load_page'])) {
    if ($_GET['load_page'] == 'search_form') {
        if (!in_array(Settings::getInstance()->SKIN, array('lec-2014'))) {
            $_GET['load_page'] = 'search';
        }
    }
}
// Validate URI feed
$uri = Http_Uri::getFullUri();
if (preg_match('/^\\/idx\\/(map\\/)?([a-zA-Z0-9_-]+)\\/?$/', $uri, $matches)) {
    $uri_feed = $matches[2];
    $default_feed = !empty(Settings::getInstance()->IDX_FEED_DEFAULT) ? Settings::getInstance()->IDX_FEED_DEFAULT : Settings::getInstance()->IDX_FEED;
    // Invalid feed
    if (!in_array($uri_feed, array('map')) && !Util_IDX::getFeed($uri_feed) || $uri_feed === $default_feed) {
        $url_redirect = $matches[1] === 'map/' ? Settings::getInstance()->URLS['URL_IDX_MAP'] : Settings::getInstance()->SETTINGS['URL_IDX'];
        header('Location: ' . $url_redirect);
        exit;
    }
}
// Switch feed
if (!empty($_REQUEST['feed'])) {
    Util_IDX::switchFeed($_REQUEST['feed']);
}
// Global Variables
$search_where = (!empty($search_where) ? implode(' AND ', $search_where) : '') . (!empty($search_having) ? " HAVING " . implode(' OR ', $search_having) : '');
// Count Search Query
if (empty(Settings::getInstance()->MODULES['REW_IDX_MAPPING']) || empty($polygons)) {
    $search_count_query = "SELECT SQL_CACHE COUNT(`t1`.`" . $idx->field('ListingMLS') . "`) AS `total` FROM `" . $idx->getTable() . "` `t1`" . (!empty($search_where) ? ' WHERE ' . $search_where : '');
} else {
    if ($idx->getLink() == 'cms') {
        $search_count_query = "SELECT SQL_CACHE COUNT(`t1`.`" . $idx->field('ListingMLS') . "`) AS `total` FROM `" . $idx->getTable() . "` `t1`" . (!empty($search_where) ? ' WHERE ' . $search_where : '');
    } else {
        $search_count_query = "SELECT SQL_CACHE COUNT(`total`) AS total" . " FROM (SELECT `t1`.`" . $idx->field('ListingMLS') . "` AS `total`, `t2`.`Point`" . " FROM `" . $idx->getTable() . "` `t1`" . " JOIN `" . REWIDX_TABLE_GEOPOINTS . "` `t2`" . " ON `t1`.`" . $idx->field('ListingMLS') . "` = `t2`.`ListingMLS`" . " AND `t1`.`" . $idx->field('ListingType') . "` = `t2`.`ListingType`" . (!empty($search_where) ? ' WHERE ' . $search_where : '') . ") as listings";
    }
}
global $search_results_count;
// Execute Count Query
$search_results_count = $db_idx->fetchQuery($search_count_query);
// REWMOD: Andrew S - March 19/15 - Implemented Eric B's Two Buttons in IDX Snippets
if (!empty($_REQUEST['snippet']) && strpos(Http_Uri::getUri(), '/blog/') !== 0 && (!empty($_REQUEST['button_save_search']) || !empty($_REQUEST['button_refine_search']))) {
    // REWMOD: Andrew S - March 30/15 - Bug fix: Remove false entries from criteria -- stops searches without any specified entry from breaking.
    $criteria = $_REQUEST;
    foreach ($criteria as $key => $val) {
        if ($val == false) {
            unset($criteria[$key]);
        }
    }
    $page->container('idx_buttons')->addModule('idx-buttons', array('save_search' => $_REQUEST['button_save_search'], 'refine_search' => $_REQUEST['button_refine_search'], 'criteria' => $criteria, 'total_listings' => $search_results_count['total']))->display();
}
// END REWMOD
// MLS Compliance, Search Result Limit
if (isset($_COMPLIANCE['limit']) && $search_results_count['total'] >= $_COMPLIANCE['limit']) {
    $results_limit = $_COMPLIANCE['limit'];
} else {
    $results_limit = $search_results_count['total'];
 /**
  * Load Extra Sources
  * @return void
  */
 protected function extraSources()
 {
     // Include Scheme's Styles
     $this->addStylesheet('schemes/' . $this->getScheme() . '/scheme.less');
     // Exclude Popup
     if (!isset($_GET['popup'])) {
         // Add Mobile bookmark
         $this->page->container('ajax')->module('mobile-bookmark');
     }
     if (Http_Uri::getUri() === '/' && Settings::getInstance()->SETTINGS['agent'] === 1) {
         $this->addJavascript('js/jquery.nouislider.min.js');
     }
     // Run parent
     parent::extraSources();
 }