function cw_in_images_get_image_info($image_id, $title)
{
    global $tables;
    $tmp = cw_query_first("select * from {$tables['webmaster_images']} where id='{$image_id}'");
    $tmp = cw_image_info('webmaster_images', $tmp);
    $tmp['id'] = $image_id;
    $tmp['title'] = cw_get_langvar_by_name('image_' . $title);
    return $tmp;
}
function cw_ps_get_featured_offer()
{
    global $tables, $domain_attributes, $addons, $smarty;
    $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
    $fields = array("{$tables['ps_offers']}.offer_id", "{$tables['ps_offers']}.offer_id as id", 'title', 'description', 'enddate', 'priority', 'position');
    $offer = array();
    $from_tbls[] = 'ps_offers';
    if (isset($addons['multi_domains'])) {
        $conditions = cw_md_get_available_domains();
        if ($conditions !== false) {
            $query_joins['attributes_values'] = array('on' => "{$tables['ps_offers']}.offer_id = {$tables['attributes_values']}.item_id and {$tables['attributes_values']}.item_type = '" . PS_ATTR_ITEM_TYPE . "' and {$tables['attributes_values']}.attribute_id='" . $domain_attributes[PS_ATTR_ITEM_TYPE] . "' and {$tables['attributes_values']}.value in " . $conditions, 'is_inner' => 1);
        }
    }
    $query_joins['ps_bonuses'] = array('on' => "{$tables['ps_offers']}.offer_id = {$tables['ps_bonuses']}.offer_id", 'is_inner' => 1);
    $query_joins['ps_conditions'] = array('on' => "{$tables['ps_offers']}.offer_id = {$tables['ps_conditions']}.offer_id", 'is_inner' => 1);
    $where[] = "{$tables['ps_offers']}.enddate >= '" . cw_core_get_time() . "'";
    $having[] = "COUNT({$tables['ps_bonuses']}.bonus_id) > 0";
    $having[] = "COUNT({$tables['ps_conditions']}.cond_id) > 0";
    $groupbys[] = "{$tables['ps_offers']}.offer_id";
    $orderbys[] = 'priority DESC';
    $orderbys[] = 'position';
    $_query_joins = $query_joins;
    $_where = $where;
    $_having = $having;
    $_groupbys = $groupbys;
    $_fields = $fields;
    global $customer_id, $user_info;
    $customer_zone = null;
    if (isset($customer_id) && !empty($customer_id)) {
        if (!isset($user_info) || empty($user_info)) {
            $user_info = cw_user_get_info($customer_id, 1);
        }
        //cw_user_get_info($customer_id, 256)
        //cw_user_get_addresses
        //cw_user_get_addresses_smarty
        //cw_user_get_default_address
        //if ($info_type & 256)
        //$zones = cw_call('cw_cart_get_zones', array('address' => $address, 'is_shipping' => 1));
        $customer_zone = cw_func_call('cw_cart_get_zone_ship', array('address' => $user_info['current_address'], 'type' => 'D'));
    }
    //let's try to find an offer with the required category defined in the offer's condition
    global $cat;
    if (isset($cat) && !empty($cat)) {
        $query_joins['ps_cond_details'] = array('on' => "{$tables['ps_offers']}.offer_id = {$tables['ps_cond_details']}.offer_id", 'is_inner' => 1);
        $where[] = "{$tables['ps_cond_details']}.object_type = '" . PS_OBJ_TYPE_CATS . "'";
        $where[] = "{$tables['ps_cond_details']}.object_id = '" . $cat . "'";
        $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
        if (empty($customer_zone)) {
            $offer = cw_stripslashes(cw_query_first($search_query . ' LIMIT 1'));
        } else {
            $cat_offers = cw_stripslashes(cw_query_hash($search_query, 'offer_id', false));
            if (!empty($cat_offers)) {
                $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
                $from_tbls[] = 'ps_cond_details';
                $fields[] = 'offer_id';
                $where[] = "({$tables['ps_cond_details']}.object_type = '" . PS_OBJ_TYPE_ZONES . "' AND {$tables['ps_cond_details']}.object_id != '{$customer_zone}')";
                $where[] = "{$tables['ps_cond_details']}.offer_id IN ('" . implode("', '", array_keys($cat_offers)) . "')";
                $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
                $zone_offers = cw_stripslashes(cw_query_hash($search_query, 'offer_id', false));
                $suitable_offers = array_diff_key($cat_offers, $zone_offers);
                if (!empty($suitable_offers)) {
                    $offer = array_shift($suitable_offers);
                    unset($suitable_offers);
                }
            }
        }
    }
    //if there is no suitable offer in the store, let's take the first one
    if (empty($offer)) {
        $query_joins = $_query_joins;
        $where = $_where;
        $having = $_having;
        $groupbys = $_groupbys;
        $fields = $_fields;
        if (!empty($customer_zone)) {
            $query_joins['ps_cond_details'] = array('on' => "{$tables['ps_offers']}.offer_id = {$tables['ps_cond_details']}.offer_id");
            $where[] = "({$tables['ps_cond_details']}.object_type = '" . PS_OBJ_TYPE_ZONES . "' AND {$tables['ps_cond_details']}.object_id = '{$customer_zone}')";
            $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
            $offer = cw_stripslashes(cw_query_first($search_query . ' LIMIT 1'));
            if (empty($offer)) {
                $where = $_where;
                $where[] = "({$tables['ps_cond_details']}.object_type != '" . PS_OBJ_TYPE_ZONES . "' OR {$tables['ps_cond_details']}.object_type IS NULL)";
                $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
                $offer = cw_stripslashes(cw_query_first($search_query . ' LIMIT 1'));
            }
        } else {
            $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
            $offer = cw_stripslashes(cw_query_first($search_query . ' LIMIT 1'));
        }
        unset($_query_joins, $_where, $_having, $_groupbys);
    }
    if (!empty($offer)) {
        $fields = $from_tbls = $query_joins = $where = $groupbys = $having = $orderbys = array();
        $fields[] = '*';
        $from_tbls[] = PS_IMG_TYPE;
        $where[] = "id = '{$offer['id']}'";
        $where[] = "avail = 1";
        $search_query = cw_db_generate_query($fields, $from_tbls, $query_joins, $where, $groupbys, $having, $orderbys);
        $offer_image_info = cw_query_first($search_query);
        if (!empty($offer_image_info) && is_array($offer_image_info)) {
            $offer['img'] = cw_image_info(PS_IMG_TYPE, $offer_image_info);
            $smarty->assign('ps_image_width', $offer['img']['image_x']);
        }
    }
    return $offer;
}
cw_load('files', 'image');
if (!isset($available_images[$type])) {
    cw_close_window();
}
$images = cw_query($sql = "SELECT " . ($available_images[$type] == "U" ? "id" : "image_id") . " as id, image_path, image_x, image_y, image_size, alt FROM " . $tables[$type] . " WHERE id = '{$id}' AND avail = 1 ORDER BY orderby");
if (empty($images)) {
    cw_close_window();
}
$navigation = cw_core_get_navigation($target, count($total_items), $page);
$navigation['script'] = "index.php?target=popup_image&type={$type}&id={$id}&title=" . urlencode($title);
$smarty->assign('navigation', $navigation);
$max_x = 0;
// get max height
foreach ($images as $k => $v) {
    $images[$k] = cw_image_info($type, $v);
    if ($images[$k]['image_x'] > $max_x) {
        $max_x = $images[$k]['image_x'];
    }
}
if (!empty($title)) {
    $smarty->assign('title', $title);
}
$smarty->assign('max_x', $max_x);
$smarty->assign('images_count', count($images));
$smarty->assign('images', $images);
$smarty->assign('id', $id);
$smarty->assign('type', $type);
$smarty->assign('area', $area);
$smarty->assign('body_onload', 'changeImg(0);');
$location = array();
function cw_image_get($type, $image_id)
{
    global $tables, $available_images;
    $where = $available_images[$type]['type'] != 'U' ? 'image_id' : 'id';
    $image = cw_query_first($sql = "select * from " . $tables[$type] . " where {$where}='{$image_id}'");
    $image[$where] = $image_id;
    return cw_image_info($type, $image);
}