<?
//
//	funcs.php
//	ems sept2010
//

function generate_page_name($Id, $Name) {
//  return $Id . "/" . ereg_replace(' ','-',htmlspecialchars($Name));
//                  . $Id . "/" . ereg_replace("'","&quot;",ereg_replace(' ','-',ereg_replace('/','-',htmlspecialchars($Name))))

  $bad  = array(' ','/','"','\'');
  $good = array('-','-','','');
  return $Id . "/" . str_replace($bad,$good,htmlspecialchars($Name));
}

function add_small_preview_image($Id,$Name,$Description,$Source,$Year,$Width,$Height) {
  global $click_for_more_detail, $lang, $preview_types;

  if($Width>$Height) {
    $Small_Width = $preview_types['Small']['square'];
    $Small_Height = round(1.0 * $preview_types['Small']['square'] / ($Width/$Height));
  } else {
    $Small_Width = round(1.0 * $preview_types['Small']['square'] / ($Height/$Width));
    $Small_Height = $preview_types['Small']['square'];
  }
  
                // Articifial tooltips:
                echo "<div id=\"tooltip" . $Id . "\" class=\"tip\">\n"
                . "<b>" . htmlspecialchars($Name) . "</b>\n";
                if($Description != "") echo ": ";
                
                if(strlen($Description) < 350) echo htmlspecialchars($Description);
                else echo htmlspecialchars(substr($Description,0,344)) . "...";
                echo "    <p />"
                . "    <i>(" . $click_for_more_detail[$lang] . 
                ")</i>\n";
                echo "</div>\n";
                
                echo "<div class=\"image_boxes\" id=\"imagebox" . $Id . "\"\n"
//                 . "     onmouseover='document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"blue\"' "
//                 . "     onmouseout='document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"white\"' "
//                 . "     onclick='window.open(\"/detail.php?id=" . $Id . "\",\"Window1\",\"menubar=no,width=300,height=550,toolbar=no,status=no,scrollbars=yes,resizable=no\");'>\n"
                   . "     onmouseout ='popUp(event,\"tooltip" . $Id . "\"); document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"white\"' \n"
                   . "     onmouseover='popUp(event,\"tooltip" . $Id . "\"); document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"blue\"' \n"
                   . "     onclick='window.location=\"";

                echo "/"
                  . generate_page_name($Id,$Name)
                   . "\"'\n"
                   . ">\n"
                   . "    <div class=\"images\">\n";

                // Popups: http://blazonry.com/javascript/windows.php
// we don't have to encode this stuff, we're Unicode friendly, just -'s and &'s in names
//                . htmlentities(ereg_replace(" ","-",$Name),ENT_COMPAT,"UTF-8")
//                . ereg_replace(" ","-",ereg_replace("&","-",$Name))
                echo "      <a href=\"http://TotallyFreeImages.com/" . generate_page_name($Id,$Name) . "\">\n";

                $hash = sha1($Id);
                $first_dir_part = substr($hash,-2,1);
                $second_dir_part = substr($hash,-1,1);
                $smallthumbs_filename = 'http://tfi.s3.amazonaws.com/' . $preview_types["Small"]["subdir"]
                    . $first_dir_part . '/' . $second_dir_part . '/' . $hash . '.jpg';
  echo "<!-- Width: $Width, Height: $Height -->\n";                       
                echo "        <img class=\"imgs\" src=\"" . $smallthumbs_filename . "\" \n"
                   . "width=\"" . $Small_Width . "\" height=\"" . $Small_Height . "\" "
                   . "alt=\"" . str_replace('"','',htmlspecialchars($Name)) . "\" />\n";
                   
                echo
                     "      </a>\n"
                   . "    </div>\n"

                   . "    <div class=\"names\">" . htmlspecialchars($Name);
                   if($Year != 0) echo "<br /><span class=\"small_year\">(" . $Year . ")</span>";
                   echo "</div>\n"
                   . "</div>\n\n";
}


function add_tiny_preview_image($Id,$Name,$Description,$Source,$Year) {
  global $click_for_more_detail, $lang, $preview_types;
  
                // Articifial tooltips:
                echo "<div id=\"tooltip" . $Id . "\" class=\"tip\">\n"
                . "<b>" . htmlspecialchars($Name) . "</b>\n";
                if($Description != "") echo ": ";
                
                if(strlen($Description) < 350) echo htmlspecialchars($Description);
                else echo htmlspecialchars(substr($Description,0,344)) . "...";
                echo "    <p />"
                . "    <i>(" . $click_for_more_detail[$lang] . 
                ")</i>\n";
                echo "</div>\n";
                
                echo "<div class=\"small_image_boxes\" id=\"imagebox" . $Id . "\"\n"
//                 . "     onmouseover='document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"blue\"' "
//                 . "     onmouseout='document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"white\"' "
//                 . "     onclick='window.open(\"/detail.php?id=" . $Id . "\",\"Window1\",\"menubar=no,width=300,height=550,toolbar=no,status=no,scrollbars=yes,resizable=no\");'>\n"
                   . "     onmouseout ='popUp(event,\"tooltip" . $Id . "\"); document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"white\"' \n"
                   . "     onmouseover='popUp(event,\"tooltip" . $Id . "\"); document.getElementById(\"imagebox" . $Id . "\").style.borderColor = \"blue\"' \n"
                   . "     onclick='window.location=\"";

                echo "/"
                   . generate_page_name($Id, $Name)
                   . "\"'\n"
                   . ">\n"
                   . "    <div class=\"small_images\">\n";

                // Popups: http://blazonry.com/javascript/windows.php
// we don't have to encode this stuff, we're Unicode friendly, just -'s and &'s in names
//                . htmlentities(ereg_replace(" ","-",$Name),ENT_COMPAT,"UTF-8")
//                . ereg_replace(" ","-",ereg_replace("&","-",$Name))
                echo "      <a href=\"/"
                  . generate_page_name($Id,$Name)
                  . "\">\n";
                                

                $hash = sha1($Id);
                $first_dir_part = substr($hash,-2,1);
                $second_dir_part = substr($hash,-1,1);
                $tinythumbs_filename = 'http://tfi.s3.amazonaws.com/' . $preview_types["Tiny"]["subdir"]
                    . $first_dir_part . '/' . $second_dir_part . '/' . $hash . '.jpg';
                         
                echo "        <img class=\"imgs\" src=\"" . $tinythumbs_filename . "\" "
                   . "alt=\"" . str_replace('"','',htmlspecialchars($Name)) . "\" />\n";


                   echo "      </a>\n"
                   . "    </div>\n"

                   . "    <div class=\"small_names\">" . htmlspecialchars($Name);
                   if($Year != 0) echo "<br /><span class=\"small_small_year\">(" . $Year . ")</span>";
                   echo "</div>\n"
                   . "</div>\n\n";
}

function results_bar($total_results, $Page) {
  global $previous_page, $next_page, $NumPages, $lang, $_GET;

  echo "<div class=\"results\">\n";

  if($total_results) {
        // if($_GET['s'] != "")
        //echo ": \n";
        
        echo "<span class=\"buttons\">\n";
        if($Page > 0) {
          echo '<a href="/search?';
          if(isset($_GET['s'])) echo 's=' . $_GET['s'] . '&amp;';
          if(isset($_GET['type'])) echo 'type=' . $_GET['type'] . '&amp;';
          echo 'p=' . ($Page-1) 
//          . '&amp;'
//          . 'search_ref=' . $search_ref
          . '">'
          . "[" . $previous_page[$lang] . "]</a>\n";
        }
        if(($Page+1) < $NumPages) {
          echo '<a href="/search?';
          if(isset($_GET['s'])) echo 's=' . $_GET['s'] . '&amp;';
          if(isset($_GET['type'])) echo 'type=' . $_GET['type'] . '&amp;';
          echo 'p=' . ($Page+1)
//          . '&amp;'
//          . 'search_ref=' . $search_ref
          . '">'
          . "[" . $next_page[$lang] . "]</a>\n";
        }
        echo "</span>\n";
  }
  
  echo "  ";

  // if($_GET['s'] != "") 
  echo images_found($total_results);
  
  echo "\n</div>\n\n\n";
}


function images_found($total_results) {
  global $lang;
  
  $s = '';

  if($lang == 'de') {
    $s .= $total_results;
    $s .= ' Bild';
    if($total_results != 1) $s .= 'er';
    $s .= ' gefunden';
  }
  elseif($lang == 'en') {
    $s .= $total_results . ' image';
    if($total_results != 1) $s .= 's';
    $s .= ' found';
  }
  elseif($lang == 'es') {
    $s .= $total_results;
    $s .= ' imagen';
    if($total_results != 1) $s .= 'es';
    $s .= ' encontrado';
    if($total_results != 1) $s .= 's';
  }
  elseif($lang == 'fr') {
    $s .= $total_results;
    $s .= ' image';
    if($total_results != 1) $s .= 's';
    $s .= ' trouvée';
    if($total_results != 1) $s .= 's';
  }
  elseif($lang == 'ja') {
    $s .= chinese_number($total_results);
    $s .= '&#30011;&#20687;&#12364;&#35211;&#12388;&#12363;&#12426;&#12414;&#12375;&#12383;';
  }
  elseif($lang == 'pt') {
    $s .= $total_results;
    if($total_results != 1) $s .= ' imagens'; else $s .= ' imagem';
    $s .= ' encontrada';
    if($total_results != 1) $s .= 's';
  }
  elseif($lang == 'ro') {
    $s .= $total_results;
    $s .= ' imagin';
    if($total_results != 1) $s .= 'i au'; else $s .= 'e a';
    $s .= ' fost găsit';
    if($total_results != 1) $s .= 'e'; else $s .= 'a';
  }
  elseif($lang == 'ru') {
    $s .= $total_results;
    if($total_results != 1) $s .= ' &#1080;&#1079;&#1086;&#1073;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1103; &#1085;&#1072;&#1081;&#1076;&#1077;&#1085;&#1099;';
    else $s .= ' &#1080;&#1079;&#1086;&#1073;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1077; &#1085;&#1072;&#1081;&#1076;&#1077;&#1085;&#1086;';
  }
  elseif($lang == 'zh') {
    if($total_results != 1) {
      $s .= '&#30332;&#29694;';
      $s .= chinese_number($total_results);
      $s .= '&#31278;&#22294;&#20687;';
    } else $s .= '&#30332;&#29694;&#19968;&#20491;&#24418;&#35937;';
  }
  
  return $s;
}

function get_mysql_val($query, $column) {
      $result = mysql_query($query);
      $row = mysql_fetch_array($result, MYSQL_ASSOC);
      $value = $row[$column];
      mysql_free_result($result);
      
      return $value;
}

function show_table($table_id, $query) {
      $before = time();
      $result = mysql_query($query);
      $elapsed = time() - $before;
      $num_rows = mysql_numrows($result);
      $row_num = 0;
      echo "<!-- Retrieved in $elapsed seconds -->\n";
      echo "<table id=\"" . $table_id . "\">\n";
      while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        // Header
        if(!$row_num) {
          echo "  <tr>\n";
          foreach($row as $col => $val) echo "    <th>$col</th>\n";
          echo "  </tr>\n";
        }
        
        echo "  <tr>\n";
        foreach($row as $col => $val) echo "    <td class=\"" . $table_id."_".$col . "\">$val</td>\n";
        echo "  </tr>\n";
        ++$row_num;
      }
      echo "</table>\n";
      mysql_free_result($result);
}

function convert_to_latin($s) {
  
    return strtr($s,
    array(
    'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 
    'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 
    'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 
    'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 
    'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 
    'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 
    'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
)
);
}

?>