<?php
class Images
{

  var $aImages = null;
  var $aImagesPages = null;
  var $mData = null;

  function &getInstance( ){  
    static $oInstance = null;
    if( !isset( $oInstance ) ){  
      $oInstance = new Images( );  
    }  
    return $oInstance;  
  } // end function getInstance

  /**
  * Constructor
  * @return void
  */
  function Images( ){
    $this->generateCache( );
  } // end function Images

  /**
  * Return image data
  * @return array
  * @param int  $iImage
  */
  function throwImage( $iImage ){
    if( isset( $this->aImages[$iImage] ) ){
      return $this->aImages[$iImage];
    }
    else
      return null;
  } // end function throwPage

  /**
  * List images
  * @return array
  * @param int    $iPage
  * @param string $sFile
  */
  function listImages( $iPage, $sFile ){
    $content = null;
    if( isset( $this->aImagesPages[$iPage] ) ){
      $oTpl   =& TplParser::getInstance( );
      $iCount = count( $this->aImagesPages[$iPage] );
      $aComments = getCommetsQuantity( );
      $aVotes = getVotes( );

      for( $i = 0; $i < $iCount; $i++ ){
        $aData = $this->aImages[$this->aImagesPages[$iPage][$i]];
        $aData['iStyle']  = ( $i % 2 ) ? 0: 1;
        $aData['sStyle']  = ( $i == ( $iCount - 1 ) ) ? 'L': $i + 1;

        if( $aData['iComments'] == 1 ){
          $aData['iCommentsCount'] = isset( $aComments[$aData['iImage']] ) ? $aComments[$aData['iImage']] : 0;
          $oTpl->setVariables( 'aData', $aData );
          $aData['sComments'] = $oTpl->tbHtml( $sFile, 'IMAGES_COMMENTS' );
        }

        if( $aData['iVotes'] == 1 ){
          if( isset( $aVotes[$aData['iImage']] ) ){
            $aData['iVotesCount'] = $aVotes[$aData['iImage']]['iVotesCount'];
            $aData['fVotesAvarage'] = $aVotes[$aData['iImage']]['fVotesAvarage'];
            $oTpl->setVariables( 'aData', $aData );
            $aData['sVotes'] = $oTpl->tbHtml( $sFile, 'IMAGES_VOTES' );
          }
          else{
            $aData['sVotes'] = $oTpl->tbHtml( $sFile, 'IMAGES_NO_VOTES' );
          }
        }

        if( !empty( $aData['sName'] ) ){
          $oTpl->setVariables( 'aData', $aData );
          $aData['sNameContent'] = $oTpl->tbHtml( $sFile, 'IMAGES_NAME' );
        }

        $oTpl->setVariables( 'aData', $aData );
        $content .= $oTpl->tbHtml( $sFile, 'IMAGES_LIST' );
      } // end for

      if( isset( $content ) ){
        $oTpl->setVariables( 'aData', $aData );
        return $oTpl->tbHtml( $sFile, 'IMAGES_HEAD' ).$content.$oTpl->tbHtml( $sFile, 'IMAGES_FOOT' );
      }
    }
  } // end function listImages

  /**
  * Return array with prev and next images links
  * @return array
  * @param int  $iPage
  * @param int  $iImage
  */
  function getLinkPrevNext( $iPage, $iImage ){
    if( isset( $this->aImagesPages[$iPage] ) ){
      $iCount = count( $this->aImagesPages[$iPage] );
      for( $i = 0; $i < $iCount; $i++ ){
        if( $this->aImagesPages[$iPage][$i] == $iImage ){
          $iRemember = $i;
        }
      } // end for

      if( isset( $iRemember ) ){
        if( ($iRemember - 1) >= 0 && isset( $this->aImagesPages[$iPage][$iRemember - 1] ) ){
          $aReturn['iImagePrev'] = $this->aImagesPages[$iPage][$iRemember - 1];
        }
        if( ($iRemember - 1) < $iCount && isset( $this->aImagesPages[$iPage][$iRemember + 1] ) ){
          $aReturn['iImageNext'] = $this->aImagesPages[$iPage][$iRemember + 1];
        }
        if( isset( $aReturn ) )
          return $aReturn;
      }
    }
  } // end function getLinkPrevNext

  /**
  * Sort images
  * @return array
  * @param array  $aImages
  * @param string $sSort
  */
  function sortImages( $aImages, $sSort ){
    $iCount = count( $aImages );
    $sFunctionSort = 'sort';
    
    if( $_GET['sSort'] == 'id' ){
      $sFunctionSort = 'rsort';
      $sKey = 'iImage';
    }
    else{
      $sKey = 'sName';
    }

    for( $i = 0; $i < $iCount; $i++ ){
      $mValue = $this->aImages[$aImages[$i]][$sKey]; 
      $aSort[$i][0] = $mValue;
      $aSort[$i][1] = $aImages[$i];
    } // end for

    $sFunctionSort( $aSort );
    $aImages = null;

    for( $i = 0; $i < $iCount; $i++ ){
      $aImages[$i] = $aSort[$i][1];
    } // end for    
    return $aImages;
  } // end function sortImages 

  /**
  * Generate cache variables
  * @return void
  */
  function generateCache( ){
    global $config;

    if( !is_file( DB_IMAGES ) )
      return null;

    $aFile = file( DB_IMAGES );
    $iCount = count( $aFile );
    $sFunction = LANGUAGE.'_images';
    $iStatus = throwStatus( );
    
    $this->aImages = null;
    $this->aImagesPages = null;

    for( $i = 1; $i < $iCount; $i++ ){
      $aExp = explode( '$', $aFile[$i] );
      if( isset( $aExp[3] ) && $aExp[3] >= $iStatus ){
        if( !is_numeric( $aExp[1] ) )
          $aExp[1] = 0;
        $this->aImages[$aExp[0]] = $sFunction( $aExp );
        $this->aImages[$aExp[0]]['sLinkName'] = '?'.$aExp[0].','.change2Url( $this->aImages[$aExp[0]]['sName'] );
        $this->aImages[$aExp[0]]['iSizeValue1'] = $config['images_sizes'][$this->aImages[$aExp[0]]['iSize1']];
        $this->aImages[$aExp[0]]['iSizeValue2'] = $config['images_sizes'][$this->aImages[$aExp[0]]['iSize2']];
        $this->aImagesPages[$aExp[1]][] = $aExp[0];
      }
    } // end for
  } // end function generateCache
};

?>