function bp_checkins_script_css_loader()
{
    if (bp_checkins_is_user_area()) {
        wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false');
        wp_enqueue_script('gmap3', BP_CHECKINS_PLUGIN_URL_JS . '/gmap3.min.js', array('jquery'));
        wp_enqueue_style('bpcistyle', BP_CHECKINS_PLUGIN_URL_CSS . '/bpcinstyle.css');
        wp_enqueue_script('bp-ckeckins-dir', BP_CHECKINS_PLUGIN_URL_JS . '/bp-checkins-dir.js');
        bp_checkins_localize_script('dir');
        add_filter('bp_get_the_body_class', 'bp_checkins_body_class_is_user');
    }
}
function bp_checkins_filter_ajax_query($query_string, $object, $action)
{
    global $bp;
    if (empty($object)) {
        return false;
    }
    /* Set up the cookies passed on this AJAX request. Store a local var to avoid conflicts */
    if (!empty($_POST['cookie'])) {
        $_BP_CI_COOKIE = wp_parse_args(str_replace('; ', '&', urldecode($_POST['cookie'])));
    } else {
        $_BP_CI_COOKIE =& $_COOKIE;
    }
    $bp_ci_qs = false;
    /* we only want places */
    $bp_ci_qs[] = 'object=' . $object;
    //$fix_object_filter = str_replace(',groups', '', $object);
    /***
     * Check if any cookie values are set. If there are then override the default params passed to the
     * template loop
     */
    if (!empty($_BP_CI_COOKIE['bp-' . $object . '-filter']) && '-1' != $_BP_CI_COOKIE['bp-' . $object . '-filter']) {
        $bp_ci_qs[] = 'type=' . $_BP_CI_COOKIE['bp-' . $object . '-filter'];
        $bp_ci_qs[] = 'action=' . $_BP_CI_COOKIE['bp-' . $object . '-filter'];
        // bp-checkins filtering
    }
    if (bp_checkins_is_user_area() && $bp->displayed_user->id == $bp->loggedin_user->id) {
        $bp_ci_qs[] = 'show_hidden=1';
    }
    /* If page and search_terms have been passed via the AJAX post request, use those */
    if (!empty($_POST['page']) && '-1' != $_POST['page']) {
        $bp_ci_qs[] = 'page=' . $_POST['page'];
    }
    /* Now pass the querystring to override default values. */
    $query_string = empty($bp_ci_qs) ? '' : join('&', (array) $bp_ci_qs);
    $object_filter = '';
    if (isset($_BP_CI_COOKIE['bp-' . $object . '-filter']) && $_BP_CI_COOKIE['bp-' . $object . '-filter'] != 'friends_checkin') {
        $object_filter = $_BP_CI_COOKIE['bp-' . $object . '-filter'];
    }
    return apply_filters('bp_checkins_filter_ajax_query', $query_string, $object, $object_filter);
}
function bp_checkins_activity_querystring_filter($query_string, $object)
{
    // not on a checkin area, then return the query without changing it!
    if (!bp_is_current_component('checkins') && !bp_checkins_is_group_checkins_area()) {
        return $query_string;
    }
    /* Set up the cookies passed on this AJAX request. Store a local var to avoid conflicts */
    if (!empty($_POST['cookie'])) {
        $_BP_CI_COOKIE = wp_parse_args(str_replace('; ', '&', urldecode($_POST['cookie'])));
    } else {
        $_BP_CI_COOKIE =& $_COOKIE;
    }
    $defaults = array('page' => false);
    $r = wp_parse_args($query_string, $defaults);
    extract($r, EXTR_SKIP);
    //default values to filter on
    $object = 'checkins,groups';
    $action = 'activity_checkin,foursquare_checkin,place_checkin';
    $bp_ci_qs = false;
    $bp_ci_qs[] = 'object=' . $object;
    /***
     * Check if any cookie values are set. If there are then override the default params passed to the
     * template loop
     */
    if (!empty($_BP_CI_COOKIE['bp-checkins-filter']) && '-1' != $_BP_CI_COOKIE['bp-checkins-filter'] && $_BP_CI_COOKIE['bp-checkins-filter'] != 'friends_checkin') {
        $bp_ci_qs[] = 'type=' . $_BP_CI_COOKIE['bp-checkins-filter'];
        $bp_ci_qs[] = 'action=' . $_BP_CI_COOKIE['bp-checkins-filter'];
    } else {
        $bp_ci_qs[] = 'type=' . $object;
        $bp_ci_qs[] = 'action=' . $action;
    }
    if (!empty($_BP_CI_COOKIE['bp-checkins-filter']) && '-1' != $_BP_CI_COOKIE['bp-checkins-filter'] && $_BP_CI_COOKIE['bp-checkins-filter'] == 'friends_checkin') {
        // this is my trick to transsform a filter to a scope !
        $bp_ci_qs[] = 'scope=friends';
    }
    //includes the !public group checkins
    if (bp_checkins_is_user_area() && bp_is_my_profile()) {
        $bp_ci_qs[] = 'show_hidden=1';
    }
    if (!empty($page)) {
        $bp_ci_qs[] = 'page=' . $page;
    }
    //builds the bp_checkins query
    $query_string = empty($bp_ci_qs) ? '' : join('&', (array) $bp_ci_qs);
    return apply_filters('bp_checkins_activity_querystring_filter', $query_string, $object, $action);
}
    function widget($args, $instance)
    {
        global $bp;
        extract($args);
        if (bp_checkins_is_group_places_area() || bp_checkins_is_user_area() && bp_is_current_action('places-area') || bp_checkins_if_single_place()) {
            return false;
        }
        if (!$instance['max_places']) {
            $instance['max_places'] = 5;
        }
        if (!$instance['places_type']) {
            $instance['places_type'] = -1;
        }
        if ($instance['dynamic'] === false) {
            $instance['dynamic'] = 1;
        }
        echo $before_widget;
        echo $before_title . $instance['title'] . $after_title;
        ?>
		
		<?php 
        $widget_args = array('per_page' => $instance['max_places'], 'type' => $instance['places_type']);
        if ((int) $instance['dynamic'] == 1 && bp_displayed_user_id()) {
            $widget_args['user_id'] = bp_displayed_user_id();
        }
        if ((int) $instance['dynamic'] == 1 && bp_is_groups_component() && bp_is_single_item() && $bp->groups->current_group->status == 'public') {
            $widget_args['group_id'] = $bp->groups->current_group->id;
        }
        ?>
		
		<?php 
        if (!empty($widget_args['group_id']) || !empty($widget_args['user_id'])) {
            ?>
			
			<div class="item-options">
				
				<?php 
            if (bp_displayed_user_id()) {
                printf(__('%s&#039;s places', 'bp-checkins'), bp_core_fetch_avatar(array('item_id' => bp_displayed_user_id(), 'object' => 'user', 'type' => 'thumb', 'width' => 20, 'height' => 20)) . bp_core_get_userlink(bp_displayed_user_id()));
            }
            if (bp_is_groups_component() && bp_is_single_item()) {
                printf(__('%s&#039;s places', 'bp-checkins'), bp_core_fetch_avatar(array('item_id' => $bp->groups->current_group->id, 'object' => 'group', 'type' => 'thumb', 'width' => 20, 'height' => 20)) . '<a href="' . bp_get_group_permalink($bp->groups->current_group) . '">' . esc_attr($bp->groups->current_group->name) . '</a>');
            }
            ?>
	
				
			</div>
		
		<?php 
        }
        ?>

		<?php 
        if (bp_checkins_has_places($widget_args)) {
            ?>
			
			<ul id="widget-places-list" class="item-list">
				<?php 
            while (bp_checkins_has_places()) {
                bp_checkins_the_place();
                ?>

					<li>
						<div class="item-avatar">
							<?php 
                bp_checkins_places_avatar();
                ?>
						</div>
						<div class="item-title">
							<a href="<?php 
                bp_checkins_places_the_permalink();
                ?>
" title="<?php 
                bp_checkins_places_title();
                ?>
"><?php 
                bp_checkins_places_title();
                ?>
</a>
						</div>
						<?php 
                if (bp_checkins_places_is_live()) {
                    ?>
							<div class="item-meta">
								<span class="activity"><?php 
                    bp_checkins_places_live_status();
                    ?>
</span>
							</div>
						<?php 
                }
                ?>
						<div class="clear"></div>
					</li>

				<?php 
            }
            ?>

		<?php 
        } else {
            ?>

			<div class="widget-error">
				<p><?php 
            _e('Sorry, there was no places found.', 'bp-checkins');
            ?>
</p>
			</div>

		<?php 
        }
        ?>


		<?php 
        echo $after_widget;
        ?>
	<?php 
    }
function bp_checkins_has_places($args = '')
{
    global $bp, $places_template;
    $place_in_cache = false;
    // This keeps us from firing the query more than once
    if (empty($places_template)) {
        /***
         * Set the defaults for the parameters you are accepting via the "bp_checkins_has_places()"
         * function call
         */
        $defaults = array('id' => false, 'p' => false, 'group_id' => false, 'user_id' => false, 'per_page' => 10, 'paged' => 1, 'type' => false, 'places_category' => false, 'src' => false);
        $r = wp_parse_args($args, $defaults);
        extract($r, EXTR_SKIP);
        if (bp_checkins_is_group_places_area()) {
            $group_id = $bp->groups->current_group->id;
        } else {
            if (bp_action_variable(0) && bp_action_variable(0) != "category") {
                $p = bp_action_variable(0);
                $place = wp_cache_get('single_query', 'bp_checkins_single');
                if (false !== $place && $place->query->post->post_name == $p) {
                    $place_in_cache = $place;
                }
            } else {
                if (false !== wp_cache_get('single_query', 'bp_checkins_single')) {
                    wp_cache_delete('single_query', 'bp_checkins_single');
                }
            }
            if (bp_action_variable(0) && bp_action_variable(0) == "category" && bp_action_variable(1)) {
                $places_category = bp_action_variable(1);
            }
            if (bp_checkins_is_user_area() && bp_is_current_action('places-area')) {
                $user_id = $bp->displayed_user->id;
            }
        }
        if (empty($place_in_cache)) {
            $places_template = new BP_Checkins_Place();
            if (!empty($src)) {
                $places_template->get(array('per_page' => $per_page, 'search' => $src));
            } else {
                $places_template->get(array('id' => $id, 'p' => $p, 'group_id' => $group_id, 'user_id' => $user_id, 'per_page' => $per_page, 'paged' => $paged, 'type' => $type, 'places_category' => $places_category));
            }
        } else {
            $places_template = $place_in_cache;
        }
    }
    return $places_template->have_posts();
}