<?php
if( isset( $aActions['a'] ) && is_numeric( $aActions['a'] ) ){
  $iImage = $aActions['a'];
  $aData = $oImage->throwImage( $iImage );
  if( isset( $aData ) ){
    if( isset( $_POST['iVote'] ) && is_numeric( $_POST['iVote'] ) && !isset( $_COOKIE['SimVotes'][$iImage] ) ){
      setCookie( 'SimVotes['.$iImage.']', '1', time( ) + 86400 );
      saveVote( $iImage, $_POST['iVote'] );
      header( 'Location: '.$_SERVER['PHP_SELF'].$aData['sLinkName'] );
      exit;
    }
    
    unset( $sPrevLink, $sExtLink, $sVotes, $sVotesForm, $sComments, $sCommentsForm, $sCommentsList, $sTitle );

    $aNextPrev = $oImage->getLinkPrevNext( $aData['iPage'], $iImage );
    $aData['sPagesTree'] = $oPage->throwImagePagesTree( $aData['iPage'] );

    if( !empty( $aData['sDescription'] ) )
      $aData['sDescription'] = changeTxt( $aData['sDescription'], 'nlNds' ); 
    
    if( isset( $aNextPrev['iImagePrev'] ) ){
      $aDataPrev = $oImage->throwImage( $aNextPrev['iImagePrev'] );
      $sPrevLink = $oTpl->tbHtml( $config['default_images_template'], 'PREV_LINK' );
    }

    if( isset( $aNextPrev['iImageNext'] ) ){
      $aDataNext = $oImage->throwImage( $aNextPrev['iImageNext'] );
      $sNextLink = $oTpl->tbHtml( $config['default_images_template'], 'NEXT_LINK' );
    }

    if( $aData['iComments'] == 1 ){
      if( isset( $_POST['sOption'] ) && $_POST['sOption'] == 'saveComment' && !empty( $_POST['sContent'] ) && !eregi( "\[url|<a href=|<a href=\"h", $_POST['sContent'] ) && !empty( $_POST['sName'] ) ){
        addComment( $_POST, $iImage );
        header( 'Location: '.$_SERVER['PHP_SELF'].$aData['sLinkName'].'&iCommentAdded=1' );
        exit;
      }

      $sCommentsList = $oTpl->tbHtml( $config['default_images_template'], 'COMMENTS_TITLE' );
      $sCommentsList .= listComments( $config['default_images_template'], $iImage );
      $oTpl->unsetVariables( );
      $sCommentsForm = isset( $iCommentAdded ) ? $oTpl->tbHtml( 'messages.tpl', 'COMMENT_ADDED' ) : $oTpl->tbHtml( $config['default_images_template'], 'COMMENTS_FORM' );
    }

    if( $aData['iVotes'] == 1 ){
      $aVotes = getVotes( $aData['iImage'] );
      if( isset( $aVotes ) ){
        $sVotes = $oTpl->tbHtml( $config['default_images_template'], 'VOTES' );
      }
      else{
        $sVotes = $oTpl->tbHtml( $config['default_images_template'], 'NO_VOTES' );
      }
      $sVotesForm = $oTpl->tbHtml( $config['default_images_template'], 'VOTE_FORM' );
    }

    $sImageDescription = !empty( $aData['sDescription'] ) ? $oTpl->tbHtml( $config['default_images_template'], 'DESCRIPTION' ) : null;
    $sImageName = !empty( $aData['sName'] ) ? $oTpl->tbHtml( $config['default_images_template'], 'NAME' ) : null;
    $sPagesTree = !empty( $aData['sPagesTree'] ) ? $oTpl->tbHtml( $config['default_images_template'], 'PAGES_TREE' ) : null;
    if( !empty( $aData['sNameTitle'] ) || !empty( $aData['sName'] ) ){
      $sTitle = strip_tags( ( !empty( $aData['sNameTitle'] ) ? $aData['sNameTitle'] : $aData['sName'] ).' - ' );        
    }

    $oTpl->unsetVariables( );
    $content .= $oTpl->tbHtml( $config['default_images_template'], 'CONTAINER' );
  }
  else{
    $content .= $oTpl->tbHtml( 'messages.tpl', 'ERROR' );
  }
}
?>