<?php


function get_project_gallery_image_link($id, $pic_height = 400)
{
    $stock_img_link = "";
    
    $row = get_project_gallery_image($id);
    $image_file_name = $row['unique_name'];
    $image_file_path_name = company_path().'/project_images/'.item_img_name($image_file_name);
    
//    if (file_exists(company_path().'/project_images/'.item_img_name($image_file_name).".jpg")) 
    if (file_exists(company_path().'/project_images/'.item_img_name($image_file_name))) 
    {
     // 31/08/08 - rand() call is necessary here to avoid caching problems. Thanks to Peter D.
//            $stock_img_link .= "<img id='item_img' alt = '[".$image_file_name.".jpg".
//                    "]' src='".$image_file_path_name.
//                    ".jpg?nocache=".rand()."'"." height='$pic_height' border='0'>";
            $stock_img_link .= "<img id='item_img' alt = '[".$image_file_name.
                    "]' src='".$image_file_path_name.
                    "?nocache=".rand()."'"." height='$pic_height' border='0'>";
    } 
    
    return $stock_img_link;
}

function display_project_gallery_images($project_id, $section_id, $from_date_, $to_date_)
{
    $from_date = date2sql($from_date_); 
    $to_date = date2sql($to_date_);
    
    $sec_result = get_project_gallery_image_sections($project_id,$section_id, $from_date, $to_date);
    
    if(!(db_num_rows($sec_result) > 0)){
        display_note("No images found for the entered filtering criteria.");
        return;
    }
    
    start_table(TABLESTYLE);

    $th = array(_(""), "" );

//    table_header($th);
    
    while ($myrow = db_fetch($sec_result))
    {
        start_row();
        $s = text_bold("Section: ".$myrow['section_name']);
        labelheader_cell($s, "colspan=2");
        end_row();
        display_project_gallery_section_images($project_id, $myrow['section_id'], $from_date, $to_date);
    }
    
    end_table();
}


function display_project_gallery_section_images($project_id, $section_id, $from_date, $to_date)
{

    $site_list = get_project_gallery_section_images($project_id, $section_id, $from_date, $to_date);
    
    $col_no = 1;
    while ($myrow = db_fetch($site_list))
    {
        if($col_no == 1){
            start_row();
        }
        
        display_project_gallery_image($myrow);
        
        
        if($col_no == 2){
            end_row();
            $col_no = 1;
        }
        else{
            $col_no++;
        }

    }

    
}


function display_project_gallery_image($myrow)
{

        echo "<td>";
            start_table(TABLESTYLE_NOBORDER, "width=90%");
                start_row();
                $stock_img_link = get_project_gallery_image_link($myrow['id']);
                label_cell($stock_img_link, "align=center");
                end_row();
                
                start_row();
                $date=date_create($myrow['image_date']);
                $s = (date_format($date,"F d, Y"))."\r\n";
//                $s .= text_bold("Section: ").$myrow['section_name']."\r\n";
                $s .= ($myrow['description']);
                label_cell(nl2br($s));
                end_row();
            end_table();
        
        echo "</td>";

}