function wpgmappity_update_meta_data($map, $map_id)
{
    global $wpdb;
    // define json_decode for PHP4 users
    if (!function_exists('json_decode')) {
        function json_decode($content, $assoc = false)
        {
            require_once wpgmappity_plugin_path() . 'classes/JSON.phps';
            if ($assoc) {
                $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
            } else {
                $json = new Services_JSON();
            }
            return $json->decode($content);
        }
    }
    wpgmappity_db_version();
    // JSON.stringify leaves \'s - remove them for json_decode
    $map = json_decode(stripslashes($map), true);
    $table = $wpdb->prefix . "wpgmappity_maps";
    $marker_table = $wpdb->prefix . "wpgmappity_markers";
    //die(var_dump($map));
    $wpdb->update($table, array('map_length' => $map['map_length'], 'map_height' => $map['map_height'], 'map_zoom' => $map['map_zoom'], 'center_lat' => $map['center_lat'], 'center_long' => $map['center_long'], 'map_type' => $map['map_type'], 'alignment' => $map['alignment'], 'map_address' => $map['map_address'], 'map_controls' => base64_encode(serialize($map['controls'])), 'route' => base64_encode(serialize($map['route'])), 'promote' => $map['promote'], 'scroll' => $map['scroll'], 'version' => $map['version']), array('id' => $map_id));
    // delete all old markers
    $query = $wpdb->prepare("\n    DELETE FROM {$marker_table}\n    WHERE map_id = {$map_id};");
    $wpdb->query($query);
    // re-add updated markers
    $i = 0;
    foreach ($map['markers'] as $marker) {
        $query = $wpdb->prepare("\n      INSERT INTO {$marker_table}\n      ( map_id, marker_lat, marker_long, marker_text, marker_url, marker_image )\n      VALUES ( %s, %s, %s, %s, %s, %s )", $map_id, $marker['lat'], $marker['long'], $marker['marker_text'], '', $marker['image']);
        $wpdb->query($query);
    }
    return $map_id;
}
function wpgmappity_init_table()
{
    global $wpdb;
    $wpgmappity_db_version = WPGMAPPITY_PLUGIN_CURRENT_DB;
    $map_table_name = $wpdb->prefix . "wpgmappity_maps";
    $marker_table_name = $wpdb->prefix . "wpgmappity_markers";
    wpgmappity_db_version();
    if ($wpdb->get_var("show tables like '{$map_table_name}'") != $map_table_name) {
        $map_sql = "CREATE TABLE " . $map_table_name . " (\r\n\t  id mediumint(9) NOT NULL AUTO_INCREMENT,\r\n          active int(1) DEFAULT '1' NOT NULL,\r\n          map_length VARCHAR(255) NOT NULL,\r\n          map_height VARCHAR(255) NOT NULL,\r\n          map_zoom VARCHAR(255) NOT NULL,\r\n          center_lat VARCHAR(255) NOT NULL,\r\n          center_long VARCHAR(255) NOT NULL,\r\n          map_type VARCHAR(255) NOT NULL,\r\n          alignment VARCHAR(255) NOT NULL,\r\n          scroll VARCHAR(255) NOT NULL,\r\n          map_address VARCHAR(1000) NOT NULL,\r\n          map_controls VARCHAR(1000) NOT NULL,\r\n          promote VARCHAR(255) NOT NULL,\r\n          version VARCHAR(255) NOT NULL,\r\n          route VARCHAR(2000) NOT NULL,\r\n\t  UNIQUE KEY id (id)\r\n\t);";
        $marker_sql = "CREATE TABLE " . $marker_table_name . " (\r\n\t  id mediumint(9) NOT NULL AUTO_INCREMENT,\r\n\t  map_id mediumint(9),\r\n          active int(1) DEFAULT '1' NOT NULL,\r\n          marker_lat VARCHAR(255) NOT NULL,\r\n          marker_long VARCHAR(255) NOT NULL,\r\n          marker_text VARCHAR(1000),\r\n          marker_url VARCHAR(1000),\r\n          marker_image VARCHAR(500),\r\n\t  UNIQUE KEY id (id)\r\n\t);";
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta($map_sql);
        dbDelta($marker_sql);
        add_option("wpgmappity_db_version", $wpgmappity_db_version);
    }
}