<?
/**
* @version $Id: ponygallery.php,v 1.5 2005/10/09 05:31:26 gizmola Exp $
* @package Ponygallery
* @copyright (C) 2004 - 2005 Danial Taherzadeh
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Ponygallery is Free Software
*/

/* Based on
	AkoGallery - A Mambo Gallery Component
	 Copyright (C) 2003  by  Arthur Konze
	 Homepage   : www.mamboportal.com
	 Version    : 2.1 beta 1
	 License    : GNU GPL Public License
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

 
    /**************************************************\
    Start
    Functions
    \**************************************************/

    # Don't allow direct linking

    defined ('_VALID_MOS') or die( 'Direct Access to this location is not allowed.' );
	
    # Variables - Don't change anything here!!!
   
    require( $mosConfig_absolute_path . "/administrator/components/com_ponygallery/config.ponygallery.php" );
    require_once( $mosConfig_absolute_path . "/administrator/components/com_ponygallery/class.ponygallery.php" );

    $picturepath=$mosConfig_live_site . $ag_pathimages . "/";

    $thumbnailpath=$mosConfig_live_site . $ag_paththumbs . "/";

    $uid=intval( mosGetParam( $_REQUEST, "uid", 0 ) );
    // list of common inclusions:
    if (file_exists($mosConfig_absolute_path."/components/com_ponygallery/language/".$mosConfig_lang.".php")){ 
	    include($mosConfig_absolute_path."/components/com_ponygallery/language/".$mosConfig_lang.".php");
    } else { 
		include($mosConfig_absolute_path."/components/com_ponygallery/language/english.php");
    }
    # Check for Editor rights

    $is_editor = (strtolower( $my->usertype ) == 'editor' || strtolower( $my->usertype ) == 'administrator' || strtolower( $my->usertype ) == 'super administrator');

    $is_user=(strtolower( $my->usertype ) <> '');
?>

<?php
    function GalleryHeader ( )
        {
            global $database, $catid, $Itemid, $id, $is_editor, $ag_showrating, $ag_showcomment;

            /**************************************************\
            Start
            Styles for output
            \**************************************************/
?>

			<style>
			table.directory { 
			    color: #000033; 
			    background-color: #e7edd8; 
			} 
			
			.grayb { 
			    background-color: #f8f8f8; 
			} 
			  
			table.directory td { 
			    font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
			    font-size: 8pt; 
			    font-weight: normal; 
			    text-align:left; 
			} 
			
			table.directory th { 
			    font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
			    font-size: 8pt; 
			    text-align:left;
			    background-color: #e9dc00;
				color: #ffffff;
			    font-weight:bold; 	 
			} 
			.directorypath {
			    font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
			    font-size: 8pt; 
			    font-weight: bold; 
			    text-align:left; 
				color: gray;
			    background-color:#f2f2f2 ;
			
			} 
			 .smallgm {
			    font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
			    font-size: 7pt; 
				color: red;
			}  
			   .smallgsm {
			    font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; 
			    font-size: 7pt; 
				color: green;
			}  
			  
			</style>
            <form name='searchgalform' action='index.php' target=_top method='POST'>

			<table width='98%' cellpadding='3' cellspacing='4' border='0' align='center' class='newspane'>

            <tr><td class='componentheading'><?php echo _PONYGALLERY_GALLERY;?></td></tr>

            <tr><td>

            <table width='100%' border='0' cellspacing='0' cellpadding='3'><tr valign=left>

            <td class='sectiontableheader' width='100%'>
			<a href='index.php?option=com_ponygallery'><img src='components/com_ponygallery/images/home.gif' hspace='6' border='0' align='absmiddle'></a> 

	<?php
			if ( $catid <> '' ) {
				//echo " :: " . ShowCategoryPathLink( $catid );
                echo ShowCategoryPathLink( $catid );
			} else if ( $id ) {
            	$database->setQuery(
                        "SELECT a.*, cc.name AS category" . "\nFROM #__ponygallery AS a, #__ponygallery_catg AS cc"
                            . "\nWHERE a.catid=cc.cid AND a.id=$id" . "\n AND cc.access<='$gid'" );
				$rows=$database->loadObjectList();
				$row =& $rows[0];
				echo ShowCategoryPathLink( $row->catid );

			}
            echo "</td>";
            echo "<td align='right' class='directorypath' valign='middle'>";
            echo "<INPUT TYPE='hidden' NAME='option' value='com_ponygallery'><INPUT TYPE='hidden' NAME='Itemid' value='$Itemid'>";
            echo "<INPUT TYPE='hidden' NAME='func' value='special'><INPUT TYPE='hidden' NAME='sorting' value='find'>";
            echo "<INPUT TYPE='text' NAME='sstring' style='border: 1px solid; font: 10px Arial' onBlur=\"if(this.value=='') this.value='Search';\" onFocus=\"if(this.value=='Search') this.value='';\" VALUE='Search'>";
            echo "</td>";
            echo "</tr></table>
            </form>
            </td></tr><tr><td align='right'>";

            echo "<a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=userpannel'><b>"._PONYGALLERY_USER_PANEL."</b></a> - ";
            echo "<a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=special'>"._PONYGALLERY_MOST_VIEWED."</a>";

            if ( $ag_showrating )
                echo " - <a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=special&sorting=rating'>"._PONYGALLERY_TOP_RATED."</a>";

            echo " - <a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=special&sorting=lastadd'>"._PONYGALLERY_LAST_ADDED."</a>";

            if ( $ag_showcomment )
                echo " - <a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=special&sorting=lastcomment'>"._PONYGALLERY_LAST_COMMENTED."</a>";

            echo "</td></tr><tr><td>";

            return;
        }

    function GalleryFooter() {
    	global $ag_suppresscredits;
    	
	    echo "</td></tr>";
	
	    echo "<tr><td align='left'>
		<font size='1' face='arial' color='gray'>[RM]: "._PONYGALLERY_REG_MEMBERS." Directory</font>
		<br>
		<font  size='1' face='arial' color='gray'>[SM]: "._PONYGALLERY_SPECIAL_MEMBERS." Directory</font>
				</td></tr>";
		?>

            <tr>
                <td align = 'right'>
    	<?php 
    	if (!$ag_suppresscredits) {
        	echo "<font class = 'small'>";
        	echo 'Powered by: <a href = "http://www.mamboy.com/">PonyGallery</a><br>';
        	echo '</font>';
    	}	
        ?>
                </td></tr>
        <?php
        
         //- /Credits			
         echo "</table>";
         return;
	}

    function bb_decode ( $text )
        {
            $text=nl2br( $text );

            static $bbcode_tpl = array();

            static $patterns = array();

            static $replacements = array();

            // First: If there isn't a "[" and a "]" in the message, don't bother.

            if ( (strpos( $text, "[" ) === false || strpos( $text, "]" ) === false) ) {
                return $text;
                }

            // [b] and [/b] for bolding text.

            $text=str_replace( "[b]", '<b>', $text );

            $text=str_replace( "[/b]", '</b>', $text );

            // [u] and [/u] for underlining text.

            $text=str_replace( "[u]", '<u>', $text );

            $text=str_replace( "[/u]", '</u>', $text );

            // [i] and [/i] for italicizing text.

            $text=str_replace( "[i]", '<i>', $text );

            $text=str_replace( "[/i]", '</i>', $text );

            if ( !count( $bbcode_tpl ) )
                {

                    // We do URLs in several different ways..

                    $bbcode_tpl['url']='<span class="bblink"><a href="{URL}" target="_blank">{DESCRIPTION}</a></span>';

                    $bbcode_tpl['email']='<span class="bblink"><a href="mailto:{EMAIL}">{EMAIL}</a></span>';

                    $bbcode_tpl['url1']=str_replace( '{URL}', '\\1\\2', $bbcode_tpl['url'] );

                    $bbcode_tpl['url1']=str_replace( '{DESCRIPTION}', '\\1\\2', $bbcode_tpl['url1'] );

                    $bbcode_tpl['url2']=str_replace( '{URL}', 'http://\\1', $bbcode_tpl['url'] );

                    $bbcode_tpl['url2']=str_replace( '{DESCRIPTION}', '\\1', $bbcode_tpl['url2'] );

                    $bbcode_tpl['url3']=str_replace( '{URL}', '\\1\\2', $bbcode_tpl['url'] );

                    $bbcode_tpl['url3']=str_replace( '{DESCRIPTION}', '\\3', $bbcode_tpl['url3'] );

                    $bbcode_tpl['url4']=str_replace( '{URL}', 'http://\\1', $bbcode_tpl['url'] );

                    $bbcode_tpl['url4']=str_replace( '{DESCRIPTION}', '\\2', $bbcode_tpl['url4'] );

                    $bbcode_tpl['email']=str_replace( '{EMAIL}', '\\1', $bbcode_tpl['email'] );

                    // [url]xxxx://www.phpbb.com[/url] code..

                    $patterns[1]="#\[url\]([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\[/url\]#si";

                    $replacements[1]=$bbcode_tpl['url1'];

                    // [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).

                    $patterns[2]="#\[url\]([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\[/url\]#si";

                    $replacements[2]=$bbcode_tpl['url2'];

                    // [url=xxxx://www.phpbb.com]phpBB[/url] code..

                    $patterns[3]="#\[url=([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/url\]#si";

                    $replacements[3]=$bbcode_tpl['url3'];

                    // [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).

                    $patterns[4]="#\[url=([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/url\]#si";

                    $replacements[4]=$bbcode_tpl['url4'];

                    // [email]user@domain.tld[/email] code..

                    $patterns[5]="#\[email\]([a-z0-9\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";

                    $replacements[5]=$bbcode_tpl['email'];
                }

            $text=preg_replace( $patterns, $replacements, $text );

            return $text;
        }

    ##############################################################

    switch ( $func )
        {
        case 'special':
            include( 'components/com_ponygallery/sub_viewspecial.php' );

            break;

        case 'detail':
            include( 'components/com_ponygallery/sub_viewdetails.php' );

            break;

        case 'votepic':
            include( 'components/com_ponygallery/sub_votepic.php' );

            break;

        case 'editpic':
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery", _PONYGALLERY_YOU_NOT_LOGED);
                    die();
                }

            GalleryHeader();
            editPic( $uid, $option, $thumbnailpath );

            break;

        case 'savepic':
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery", _PONYGALLERY_YOU_NOT_LOGED );
                    die();
                }

            savePic( $option );

            break;

        case 'deletepic':
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery", _PONYGALLERY_YOU_NOT_LOGED );
                    die();
                }

            deletePic( $uid, $option );
            break;

        case 'commentpic':
            include( 'components/com_ponygallery/sub_commentpic.php' );

            break;

        case 'deletecomment':
            GalleryHeader();

            include( 'components/com_ponygallery/sub_deletecomment.php' );

            break;

        case 'viewcategory':
            GalleryHeader();

            $database->setQuery( "SELECT count(cid) FROM #__ponygallery_catg" . "\n  WHERE cid = '" . $catid . "'"
                                     . "\n AND access<='$gid'" );

            $is_allowed=$database->loadResult();

            if ( $is_allowed < 1 )
                {
                    mosRedirect( sefRelToAbs( "index.php?option=com_ponygallery" ),
                                 _PONYGALLERY_NOT_ACCESS_THIS_DIRECTORY );
                    die();
                }

            echo dmoz( $catid, $Itemid );

            # Feststellen der Anzahl der verfügbaren Datensätze

            $database->setQuery(
                "SELECT count(id) FROM #__ponygallery as a " . "\n left join #__ponygallery_catg as c on c.cid=a.catid"
                    . "\n  WHERE a.published = '1' AND a.catid = '" . $catid . "'" . " AND a.approved = 1"
                    . "\n AND c.access<='$gid'" );

            $count=$database->loadResult();
            # Berechnen der Gesamtseiten

            $gesamtseiten=floor( $count / $ag_perpage );

            $seitenrest=$count % $ag_perpage;

            if ( $seitenrest > 0 ) {
                $gesamtseiten++;
                }

            # Feststellen der aktuellen Seite

            if ( isset( $startpage ) )
                {
                    if ( $startpage > $gesamtseiten ) {
                        $startpage=$gesamtseiten;
                        }
                    else if ( $startpage < 1 ) {
                        $startpage=1;
                        }
                }
            else {
                $startpage=1;
                }

            echo "<p>"._PONYGALLERY_THERE_ARE." $count "._PONYGALLERY_PICTURES_IN_CATEGORY."<br>";

            echo "Pages: ";

            # Ausgeben der Seite zurueck Funktion

            $seiterueck=$startpage - 1;

            if ( $seiterueck > 0 ) {
                echo "<a href=index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$catid&startpage=$seiterueck><b>«</b></a> ";
                }

            #Ausgeben der einzelnen Seiten

            for ( $i=1; $i <= $gesamtseiten; $i++ )
                {
                    if ( $i == $startpage ) {
                        echo "$i ";
                        }
                    else {
                        echo "<a href=index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$catid&startpage=$i>$i</a> ";
                        }
                }

            # Ausgeben der Seite vorwärts Funktion

            $seitevor=$startpage + 1;

            if ( $seitevor <= $gesamtseiten ) {
                echo "<a href=index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$catid&startpage=$seitevor><b>»</b></a> ";
                }

            # Limit und Seite Vor- & Rueckfunktionen

            $start=($startpage - 1) * $ag_perpage;

            echo "</p>";

            $database->setQuery(
                "SELECT name FROM #__ponygallery_catg WHERE cid = '" . $catid . "' " . "\n ORDER BY name" );

            $catname=$database->loadResult();

            echo "<table  class='sectiontableentry2' width='100%' border='0' cellspacing='1' cellpadding='4'>";

            echo "<tr><td colspan='3' class='sectiontableheader' width='100%'>$catname</td></tr>";

            $database->setQuery(
                "SELECT * FROM #__ponygallery as a " . "\n left join #__ponygallery_catg as c on c.cid=a.catid"
                    . "\n  WHERE a.published = '1' AND a.catid = '" . $catid . "'" . " AND a.approved = 1"
                    . "\n AND c.access<='$gid'" . "\n ORDER BY a.id DESC LIMIT " . $start . "," . $ag_perpage );

            //$database->setQuery($query1);

            $rows=$database->loadObjectList();

            echo "<tr>";

            $rowcounter=0;

            foreach ( $rows as $row1 )
                {
                    if ( ($rowcounter % 3 == 0) AND ($rowcounter <> 0) )
                        echo "</tr><tr>";

                    echo "<td class='sectiontableentry1' width='34%' align='center' valign='bottom'>";

                    echo "<a href='index.php?option=com_ponygallery&Itemid=$Itemid&func=detail&id=$row1->id'><img src='$thumbnailpath$row1->imgthumbname' border='0'></a><br />";

                    echo "<b>$row1->imgtitle</b>";

                    if ( $ag_showdetail )
                        echo "<br />"._PONYGALLERY_HITS.": $row1->imgcounter";

                    if ( $ag_showrating )
                        {
                            if ( $row1->imgvotes > 0 )
                                {
                                    $fimgvotesum=number_format( $row1->imgvotesum / $row1->imgvotes, 2, ",", "." );

                                    $frating="$fimgvotesum ($row1->imgvotes Votes)";
                                }
                            else {
                                $frating=_PONYGALLERY_NO_VOTES;
                                }

                            echo "<br />"._PONYGALLERY_RATING.": $frating";
                        }

                    if ( $ag_showcomment )
                        {

                            # Check how many comments exist

                            $database->setQuery( "SELECT cmtid FROM #__ponygallery_comments WHERE cmtpic='$row1->id'" );

                            $comments_result=$database->query();

                            $comments=mysql_num_rows( $comments_result );

                            echo "<br />"._PONYGALLERY_COMMENT.": $comments";
                        }

                    echo "</td>";

                    $rowcounter++;
                }

            if ( $rowcounter % 3 <> 0 )
                {
                    for ( $i=1; $i <= (3 - ($rowcounter % 3)); $i++ ) {
                        echo "<td class='sectiontableentry1' width='34%' valign='top'> </td>";
                        }
                }

            echo "</tr></table>";

            break;

        #########################################################################################

        case "showupload":
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery", _PONYGALLERY_YOU_NOT_LOGED );
                    die();
                }

            GalleryHeader();
            showUpload( $option, $batchul );
            break;

        case "uploadhandler":
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery", _PONYGALLERY_YOU_NOT_LOGED );
                    die();
                }

            require_once( $mosConfig_absolute_path . "/administrator/components/com_ponygallery/config.ponygallery.php" );
            echo "<p />";

            $tag=ereg_replace( ".*\.([^\.]*)$", "\\1", $screenshot_name );
            $tag=strtolower( $tag );

            if ( $_FILES["screenshot"]["size"] > $ag_maxfilesize )
                {
                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel",
                                 "Error : Max allowed file size is " . $ag_maxfilesize . " bytes" );
                    die();
                }

            //Check for right format
            if ( ($tag == "jpeg") || ($tag == "jpg") || ($tag == "gif") || ($tag == "png") )
                {
                    if ( file_exists( $mosConfig_absolute_path . $ag_pathimages . "/$screenshot_name" ) )
                        {
                            mosRedirect( "index.php?option=com_ponygallery&func=showupload",
                                         _PONYGALLERY_SITVUD );
                            die();
                        }

                    if ( strlen( $screenshot ) > 0 and $screenshot != "none" )
                        move_uploaded_file( $screenshot, $mosConfig_absolute_path . $ag_pathimages . "/$screenshot_name" );

                    echo "<?php echo _PONYGALLERY_UPLOAD_COMP;?><br />";

                    $img_info=getimagesize( $mosConfig_absolute_path . $ag_pathimages . "/$screenshot_name" );

                    if ( $img_info[0] > $ag_maxwidth or $img_info[1] > $ag_maxheight )
                        {
                            unlink( $mosConfig_absolute_path . $ag_pathimages . "/$screenshot_name" );
                            mosRedirect( "index.php?option=com_ponygallery&func=showupload",
                                         _PONYGALLERY_MAX_WIGHT_HIGHT . ':' . $ag_maxwidth . 'x ' . $ag_maxheight );
                        }

                    if ( $thumbcreation )
                        resize_image( $mosConfig_absolute_path . $ag_pathimages . "/$screenshot_name",
                                      $mosConfig_absolute_path . $ag_paththumbs . "/tn_$screenshot_name",
                                      "$ag_thumbwidth",
                                      "$ag_thumbcreation",
                                      "$ag_thumbquality" );

                    echo _PONYGALLERY_THUMBNAIL_COMP . '<br />';

                    $row = new mosponygallery( $database );

                    if ( !$row->bind( $_POST, _PONYGALLERY_APPROVED_OWNER_PUBLISH ) )
                        {
                            echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
                            exit();
                        }

                    $row->imgdate = mktime();
                    $row->owner = $my->username;
                    $row->published = 1;

                    if ( $ag_approve ) {
                        $row->approved = 0;
                    }
                    else {
                        $row->approved = 1;
                    }

                    $row->imgfilename = $screenshot_name;
                    $row->imgthumbname = "tn_" . $screenshot_name;
                    $row->useruploaded = 1;

                    if ( !$row->store() )
                        {
                            echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
                            exit();
                        }
                    else
                        {
                            // messaging for new items
                            require_once( "$mosConfig_absolute_path/components/com_messages/messages.class.php" );

                            $database->setQuery( "SELECT id FROM #__users WHERE sendEmail='1'" );
                            $users = $database->loadResultArray();

                            foreach ( $users as $user_id )
                                {
                                    $msg=new mosMessage( $database );
                                    $msg->send( $my->id,
                                                $user_id,
                                                "New Picture Uploaded",
                                                sprintf( _PONYGALLERY_NEW_CONTENT_SUBMITED . " %s " . _PONYGALLERY_TITLED ." %s.",
                                                         $my->username,
                                                         $row->imgtitle ) );
                                }
                        }

                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel", _PONYGALLERY_PIC_SUCCESSFULLY_ADD );
                }
            else {
                mosRedirect( "index.php?option=com_ponygallery&func=showupload", _PONYGALLERY_INVALID_IMG_TYPE );
                }

            break;

        case "userpannel":
            if ( !$my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery&func=showupload", _PONYGALLERY_YOU_NOT_LOGED );
                    die();
                }

            GalleryHeader();
            ;
            userPannel();
            break;

        #########################################################################################

        case "send2friend":
            $send2friendname=mosGetParam( $_POST, "send2friendname", '' );

            $send2friendemail=mosGetParam( $_POST, "send2friendemail", '' );
            $from2friendname=mosGetParam( $_POST, "from2friendname", '' );
            $from2friendemail=mosGetParam( $_POST, "from2friendemail", '' );
            $id=intval( mosGetParam( $_POST, "id", '' ) );

            $text=$from2friendname . " (" . $from2friendemail . ")"
                      . " "._PONYGALLERY_INVITE_VIEW_PIC."\r \n";
            $text.=$mosConfig_live_site . "/index.php?option=com_ponygallery&func=detail&id=$id \r \n";

            $subject=$mosConfig_sitename . ' - ' . _PONYGALLERY_RECCOMEND_PIC_FROM_FREND;
            $headers.=_PONYGALLERY_ODDDOD.": " . $from2friendname . " <" . $from2friendemail . ">\r\n";
            $headers.=_PONYGALLERY_ODGOVV.": <" . $from2friendemail . ">\r\n";
            $headers.="X-Priority: 3\r\n";
            $headers.="X-MSMail-Priority: Low\r\n";
            $headers.="X-Mailer: Mambo Open Source 4.5\r\n";

            @mail( $send2friendemail, $subject, $text, $headers );
            ?>

            <script>
                alert("<?php echo _PONYGALLERY_MAIL_SENT;?>");

                document.location.href
                    = '<?php echo sefRelToAbs("index.php?option=com_ponygallery&func=detail&id=$id"); ?>';</script>

            <?php
                break;

        default:
            GalleryHeader();

            echo "<table width='100%' border='0' cellspacing='1' cellpadding='4'>";

            echo "<tr><td class='sectiontableheader' width='100%'>"._PONYGALLERY_CATEGORIES."</td></tr>";

            $query1
                ="SELECT * FROM #__ponygallery_catg WHERE published = '1' and parent = 0 \n  ORDER BY ordering"; // ORDER BY ordering ASC";

            $database->setQuery( $query1 );

            $rows=$database->loadObjectList();

            $line=1;

            foreach ( $rows as $row1 )
                {
                    $linecolor=($line % 2) + 1;

                    echo "<tr class='sectiontableentry" . $linecolor . "'><td width='100%' valign='top'>";

                    if ( $row1->image <> "" )
                        {
                            echo
                                "<a href=index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$row1->cid>";

                            echo
                                "<img src='images/stories/$row1->image' align='$row1->image_position' hspace='4' border='0'/></a>("
                                    . GetNumberOfLinks( $row1->cid ) . " "._PONYGALLERY_PICTURES.")";
                        }

                    echo "<a href=index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$row1->cid><b>$row1->name</b></a> <font size='-7'>("
                             . GetNumberOfLinks( $row1->cid ) . " "._PONYGALLERY_PICTURES.")</font>";

                    if ( $row1->access > 1 ) {
                        echo ' <font class="smallgsm">'._PONYGALLERY_SM.'</font>';
                        }
                    else if ( $row1->access > 0 ) {
                        echo ' <font class="smallgm">'._PONYGALLERY_RM.'</font>';
                        }

                    echo "<br />";

                    echo "$row1->description<p />";

                    $database->setQuery(
                        "SELECT count(id) FROM #__ponygallery WHERE published = '1' AND catid = '" . $row1->cid . "'"
                            . " AND approved = 1" );

                    $count=$database->loadResult();



                    echo "</tr>";

                    $line++;
                }

            echo "</tr></table>";

            break;
        }

    GalleryFooter();

    function showUpload ( $option, $batchul )
        {
            global $my, $database, $ag_maxuserimage, $ag_category;

            $database->setQuery( "select count(id) from #__ponygallery where owner = '$my->username'" );
            $count_pic=$database->loadResult();

            if ( $count_pic >= $ag_maxuserimage )
                {
                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel",
                                 ""._PONYGALLERY_MAY_ADD_MAX_OFF." " . $ag_maxuserimage . " "._PONYGALLERY_PICTURES."" );
                    die();
                }

            $clist=ShowDropDownCategoryList( $ag_category, "catid", ' size="1"' );
            ?>

            <script language = "javascript" type = "text/javascript">
                function checkMe()
                    {
                    var form = document.adminForm;

                    // do field validation

                    if (form.imgtitle.value == "")
                        {
                        alert("<?php echo _PONYGALLERY_PIC_MUST_HAVE_TITLE;?>");
                        return false;
                        }

                    else if (form.catid.value == "0")
                        {
                        alert("<?php echo _PONYGALLERY_MUST_SELECT_CATEGORY;?>");
                        return false;
                        }

                    else if (form.screenshot.value == "")
                        {
                        alert("<?php echo _PONYGALLERY_MUST_HAVE_FNAME;?>");
                        return false;
                        }

                    else
                        {
                        form.submit();
                        }
                    }
            </script>

            <table cellpadding = "4" cellspacing = "0" border = "0" width = "100%">
                <tr class = "sectiontableheader">
                    <td bgcolor = "White">
                       <br>

                        <?php echo _PONYGALLERY_NEW_PICTURE;?>
                    </td>
                </tr>
            </table>

            <table cellpadding = "4" cellspacing = "1" border = "0" width = "100%" class = "adminform">
                <form action = 'index.php?func=uploadhandler' method = 'post' name = 'adminForm'
                    enctype = 'multipart/form-data' onsubmit = "checkform();">
                    <input type = 'hidden' name = 'option' value = '<?php echo $option;?>'/>

                    <tr>
                        <td width = "20%" align = "right">
                            <?php echo _PONYGALLERY_TITLE;?>:</td>

                        <td width = "80%">
                            <input class = "inputbox" type = "text" name = "imgtitle" size = "50" maxlength = "100"
                                value = "<?php echo htmlspecialchars( $row->imgtitle, ENT_QUOTES );?>"/>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_CATEGORY;?>:</td>

                        <td>


                            <?php
                                echo $clist;
                            ?>

                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_DESCRIPTION;?>:</td>

                        <td>
                            <textarea class = "inputbox" cols = "40" rows = "5" name = "imgtext" width = "500"><?php
                                    echo htmlspecialchars( $row->imgtext, ENT_QUOTES );
                                ?></textarea>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_AUTHOR_OWNER;?>:</td>

                        <td>
                            <b><?php
                                echo $my->username;
                            ?></b>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_PICTURE;?>:</td>

                        <td>
                            <input type = 'file' name = 'screenshot'/>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_CR_THUMBNAIL;?></td>

                        <td>
                            <input type = 'checkbox' name = 'thumbcreation' value = 'ON' checked/>
                        </td>
                    </tr>

                    <tr>
                        <td colspan = "2" align = "center">
                            <input type = 'button' value = 'Upload' class = button onClick = "checkMe();"/>
                        </td>
                    </tr>
            </table>

            <input type = "hidden" name = "id" value = "<?php echo $row->id; ?>"/>

            <input type = "hidden" name = "option" value = "<?php echo $option;?>"/>

            <input type = "hidden" name = "task" value = ""/>

            </form>

<?php
        }

    ////************** edit picture

    function editPic ( $uid, $option, $thumbnailpath )
        {
            global $my, $database, $mosConfig_absolute_path, $ag_category;

            # oop database connector
            $row=new mosponygallery( $database );

            # load the row from the db table
            $row->load( $uid );

            if ( $row->owner !== $my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel",
                                 _PONYGALLERY_NOT_ALOWED_EDIT_PIC);
                    die();
                }

            $clist=ShowDropDownCategoryList( $row->catid, "catid", ' size="1"' );
?>

            <script language = "javascript" type = "text/javascript">
                function checkMe()
                    {
                    var form = document.adminForm;

                    // do field validation

                    if (form.imgtitle.value == "")
                        {
                        alert("<?php echo _PONYGALLERY_PIC_MUST_HAVE_TITLE;?>");
                        return false;
                        }

                    else if (form.catid.value == "0")
                        {
                        alert("<?php echo _PONYGALLERY_MUST_SELECT_CATEGORY;?>");
                        return false;
                        }

                    else
                        {
                        form.submit();
                        }
                    }
            </script>

            <table cellpadding = "4" cellspacing = "0" border = "0" width = "100%">
                <tr class = "sectiontableheader">
                    <td bgcolor = "White">
                      <br>

                        <?php echo _PONYGALLERY_NSDES;?>
                    </td>
                </tr>
            </table>

            <table cellpadding = "4" cellspacing = "1" border = "0" width = "100%" class = "adminform">
                <form action = 'index.php?func=savepic' method = 'post' name = 'adminForm'
                    enctype = 'multipart/form-data' onsubmit = "checkform();">
                    <input type = 'hidden' name = 'option' value = '<?php echo $option;?>'/>

                    <tr>
                        <td width = "20%" align = "right">
                            <?php echo _PONYGALLERY_TITLE ;?>:</td>

                        <td width = "80%">
                            <input class = "inputbox" type = "text" name = "imgtitle" size = "50" maxlength = "100"
                                value = "<?php echo htmlspecialchars( $row->imgtitle, ENT_QUOTES );?>"/>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_CATEGORY ;?>:</td>

                        <td>
                            <?php
                                echo $clist;
                                /*         
                                         ShowCategoryPath($ag_category); 
                                         <input type="hidden" name="catid"  value="<?php echo $ag_category;" />
                                */
                            ?>

                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_DESCRIPTION ;?>:</td>

                        <td>
                            <textarea class = "inputbox" cols = "40" rows = "5" name = "imgtext" width = "500"><?php
                                    echo htmlspecialchars( $row->imgtext, ENT_QUOTES );
                                ?></textarea>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_AUTHOR_OWNER ;?>:</td>

                        <td>
                            <b><?php
                                echo $my->username;
                            ?></b>
                        </td>
                    </tr>

                    <tr>
                        <td valign = "top" align = "right">
                            <?php echo _PONYGALLERY_PICTURE ;?>:</td>

                        <td>
                            <img src = '<?php echo "$thumbnailpath/".$row->imgthumbname; ?>' name = "imagelib"
                                width = "80" height = "80" border = "2" alt = "<?php echo _PONYGALLERY_THUMB_PREVIEW;?>"/>
                        </td>
                    </tr>

                    <tr>
                        <td colspan = "2" align = "center">
                            <input type = 'button' value = '<?php echo _PONYGALLERY_SAVE;?>' class ="button" onClick = "checkMe();"/>
                        </td>
                    </tr>
            </table>

            <input type = "hidden" name = "id" value = "<?php echo $row->id; ?>"/>

            <input type = "hidden" name = "option" value = "<?php echo $option;?>"/>

            <input type = "hidden" name = "task" value = "savepic"/>

            </form>

<?php
        }

    function resize_image ( $src_file, $dest_file, $new_size, $method, $dest_qual )
        {
            $imagetype=array
                (
                1 => 'GIF',
                2 => 'JPG',
                3 => 'PNG',
                4 => 'SWF',
                5 => 'PSD',
                6 => 'BMP',
                7 => 'TIFF',
                8 => 'TIFF',
                9 => 'JPC',
                10 => 'JP2',
                11 => 'JPX',
                12 => 'JB2',
                13 => 'SWC',
                14 => 'IFF'
                );

            $imginfo=getimagesize( $src_file );

            if ( $imginfo == null )
                die( "ERROR: Source file not found!" );

            $imginfo[2]=$imagetype[$imginfo[2]];

            // GD can only handle JPG & PNG images
            if ( $imginfo[2] != 'JPG' && $imginfo[2] != 'PNG' && $imginfo[2] != 'GIF'
                && ($method == 'gd1' || $method == 'gd2') )
                die( "ERROR: GD can only handle JPG and PNG files!" );

            // height/width
            $srcWidth=$imginfo[0];
            $srcHeight=$imginfo[1];

            echo "Creating thumbnail from $imginfo[2], $imginfo[0] x $imginfo[1]...<br>";

            $ratio=max( $srcWidth, $srcHeight ) / $new_size;
            $ratio=max( $ratio, 1.0 );
            $destWidth=(int)($srcWidth / $ratio);
            $destHeight=(int)($srcHeight / $ratio);

            // Method for thumbnails creation
            switch ( $method )
                {
                case "gd1":
                    if ( !function_exists( 'imagecreatefromjpeg' ) ) {
                        die( 'GD image library not installed!' );
                        }

                    if ( $imginfo[2] == 'JPG' )
                        $src_img=imagecreatefromjpeg( $src_file );

                    else if ( $imginfo[2] == 'JPG' )
                        $src_img=imagecreatefromjpeg( $src_file );

                    else
                        $src_img=imagecreatefromgif( $src_file );

                    if ( !$src_img )
                        {
                            $ERROR=$lang_errors['invalid_image'];
                            return false;
                        }

                    $dst_img=imagecreate( $destWidth, $destHeight );
                    imagecopyresized( $dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth,
                                      $srcHeight );
                    imagejpeg( $dst_img, $dest_file, $dest_qual );
                    imagedestroy( $src_img );
                    imagedestroy( $dst_img );
                    break;

                case "gd2":
                    if ( !function_exists( 'imagecreatefromjpeg' ) ) {
                        die( 'GD image library not installed!' );
                        }

                    if ( !function_exists( 'imagecreatetruecolor' ) ) {
                        die( 'GD2 image library does not support truecolor thumbnailing!' );
                        }

                    if ( $imginfo[2] == 'JPG' ) {
                        $src_img=imagecreatefromjpeg( $src_file );
                        }
                    else if ( $imginfo[2] == 'PNG' ) {
                        $src_img=imagecreatefrompng( $src_file );
                        }
                    else {
                        $src_img=imagecreatefromgif( $src_file );
                        }

                    if ( !$src_img )
                        {
                            $ERROR=$lang_errors['invalid_image'];
                            return false;
                        }

                    $dst_img=imagecreatetruecolor( $destWidth, $destHeight );
                    imagecopyresampled( $dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth,
                                        $srcHeight );
                    imagejpeg( $dst_img, $dest_file, $dest_qual );
                    imagedestroy( $src_img );
                    imagedestroy( $dst_img );
                    break;
                }

            // Set mode of uploaded picture
            chmod( $dest_file, octdec( '755' ) );

            // We check that the image is valid
            $imginfo=getimagesize( $dest_file );

            if ( $imginfo == null ) {
                return false;
                }
            else {
                return true;
                }
        }

        
    /**************************************************\
                         Start
                       Functions
    \**************************************************/
    function userPannel ( )
        {
            global $database, $my, $ag_approve;
?>

            <table width = "100%" border = "0" cellspacing = "0" cellpadding = "4">
                <tr>
                    <td align = "center" valign = "middle">
                        &nbsp;</td>

                    <td>
                        &nbsp;</td>

                    <td colspan = "5" align = "right" valign = "middle">
                        <input type = "button" name = "Button" value = "<?php echo _PONYGALLERY_NEW_PICTURE;?>"
                            onClick = "javascript:location.href='index.php?option=com_ponygallery&func=showupload';"
                            class = "button">
                    </td>
                </tr>

                <tr class = "sectiontableheader">
                    <td width = "15" align = "center" valign = "middle"></td>

                    <td width = "60%">
                        <?php echo _PONYGALLERY_PIC_NAME;?>
                    </td>

                    <td width = "15">
                        <?php echo _PONYGALLERY_HITS;?></td>

                    <td width = "35%">
                        <?php echo _PONYGALLERY_CATEGORY;?></td>

                    <td colspan = "2">
                        <?php echo _PONYGALLERY_ACTION;?></td>

            <?php
                if ( $ag_approve )
                    {
            ?>

                        <td>
                            <?php echo _PONYGALLERY_APPROWED;?></td>

            <?php
                    }
            ?>

                </tr>

                <?php
                    $database->setQuery( "select * from #__ponygallery where owner='$my->username'" );
                    $rows=$database->loadObjectList();
                    $k=0;

                    if ( count( $rows ) )
                        {
                            foreach ( $rows as $row )
                                {
                                    $k=1 - $k;
                ?>

                                        <tr class = "sectiontableentry<?php echo $k+1?>">
                                            <td width = "15" align = "center" valign = "middle">
                                                <div align = "center">
                                                    <img src = "images/M_images/arrow.png" width = "9" height = "9">
                                                </div></td>

                                            <td>
                                                <a href = "index.php?option=com_ponygallery&func=detail&id=<?php echo $row->id?>"><?php
                                                    echo $row->imgtitle
                                                ?></a></td>

                                            <td><?php
                                                    echo $row->imgcounter;
                                                ?></td>

                                            <td><?php
                                                    echo ShowCategoryPath( $row->catid );
                                                ?></td>

                                            <td align = "center" width = "20">
                                                <a href = "index.php?option=com_ponygallery&func=editpic&uid=<?php echo $row->id?>"
                                                    title = "<?php echo _PONYGALLERY_EDIT;?>">

                                                <img src = "components/com_ponygallery/images/edit.png" width = "15" height = "15"
                                                    border = "0"></a></td>

                                            <td align = "center" width = "20">
                                            <a href="javascript:if (confirm('<? echo _PONYGALLERY_SURE_DELETE_SELECT_ITEM; ?>')){ location.href='index.php?option=com_ponygallery&func=deletepic&uid=<?php echo $row->id?>';}" title="<?php echo _PONYGALLERY_DELETE; ?>">
                                            <img src = "components/com_ponygallery/images/edit_trash.png" width = "15" height = "15"
                                                    border = "0"></a></td>

                                            <?php
                                                if ( $ag_approve )
                                                    {
                                                        if ( $row->approved ) {
                                                            $a_pic="tick.png";
                                                            }
                                                        else {
                                                            $a_pic="publish_x.png";
                                                            }
                                            ?>

                                                            <td align = "center" width = "20">
                                                                <img src = "components/com_ponygallery/images/<?php echo $a_pic?>"
                                                                    border = "0"></a></td>

                                            <?php
                                                    }
                                            ?>

                                        </tr>

                <?php
                                }
                        }
                    else
                        {
                ?>

                                <tr class = "sectiontableentry<?php echo $k+1?>">
                                    <td width = "15" align = "center" valign = "middle">
                                        <div align = "center">
                                            <img src = "images/M_images/arrow.png" width = "9" height = "9">
                                        </div></td>

                                    <td colspan = "5">
                                        <?php echo _PONYGALLERY_NOT_HAVE_PIC;?></td>
                                </tr>

                <?php
                        }
                ?>

            </table>

            <br>

<?php
        }

    function savePic ( $option )
        {
            global $database, $ag_approve;

            $row=new mosponygallery( $database );

            if ( !$row->bind( $_POST, "approved owner published imgfilename imgthumbname" ) )
                {
                    echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
                    exit();
                }

            $row->imgdate=mktime();
            $row->owner=$my->username;
            $row->published=1;

            if ( $ag_approve ) {
                $row->approved=0;
                }
            else {
                $row->approved=1;
                }

            //$row->imgfilename = $screenshot_name;
            //$row->imgthumbname = "tn_".$screenshot_name;
            $row->useruploaded=1;

            if ( !$row->store() )
                {
                    echo "<script> alert('" . $row->getError() . "'); window.history.go(-1); </script>\n";
                    exit();
                }

            mosRedirect( "index.php?option=com_ponygallery&func=userpannel", _PONYGALLERY_UIC_SUCCESS_IPDATED);
        }

    function deletePic ( $uid, $option )
        {
            global $database, $my, $mosConfig_absolute_path, $ag_pathimages, $ag_paththumbs;

            $database->setQuery( "select owner from #__ponygallery where id=" . intval( $uid ) );
            $own=$database->loadResult();

            if ( $own !== $my->username )
                {
                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel",
                                 _PONYGALLERY_NOT_ALOWED_DELETE_PIC);
                    die();
                }

            if ( $uid )
                {
                    $row=new mosponygallery( $database );
                    $row->load( $uid );

                    if ( unlink( $mosConfig_absolute_path . $ag_pathimages . "/$row->imgfilename" ) )
                        {
                            //If image is deleted, delete thumb
                            if ( unlink( $mosConfig_absolute_path . $ag_paththumbs . "/$row->imgthumbname" ) )
                                {
                                    $database->setQuery( "DELETE FROM #__ponygallery_comments WHERE cmtpic=$uid" );

                                    if ( !$database->query() ) {
                                        echo "<script> alert('" . $database->getErrorMsg()
                                                 . "'); window.history.go(-1); </script>\n";
                                        }

                                    $database->setQuery( "DELETE FROM #__ponygallery WHERE id=$uid" );

                                    if ( !$database->query() ) {
                                        echo "<script> alert('" . $database->getErrorMsg()
                                                 . "'); window.history.go(-1); </script>\n";
                                        }
                                }
                            else {
                                die(_PONYGALLERY_NOT_DELETE_THMB_IMAGE_FILE );
                                }
                        }
                    else {
                        die( _PONYGALLERY_FFFFFFF );
                        }

                    mosRedirect( "index.php?option=com_ponygallery&func=userpannel",
                                 _PONYGALLERY_PIC_COMMENT_DELETED );
                }
        }

    function ShowDropDownCategoryList ( $cat, $cname = "cat", $extra = null, $flag = 0 )
        {
            global $database, $ag_category, $gid;

            $arr_cat=explode( ",", $ag_category );

            $category="<select name=\"$cname\" class=\"inputbox\" $extra>";

            if ( $flag == 1 ) {
                $add_category=true;
                }

            if ( $add_category ) {
                $category.="<OPTION VALUE=0></OPTION>";
                }
            else {
                $category.="<OPTION VALUE='0'></OPTION>";
                }

            $query="select * from #__ponygallery_catg \n where access<='" . $gid . "'" . "\nORDER BY name asc";

            $database->setQuery( $query );
            $result=$database->query();

            $num_rows=mysql_num_rows( $result );

            $i=0;

            while ( $i < $num_rows )
                {
                    $category_id=mysql_result( $result, $i, "cid" );
                    $category_name=mysql_result( $result, $i, 'name' . $mosConfig_lang );

                    if ( in_array( $category_id, $arr_cat ) )
                        {
                            $category_list.="<OPTION  VALUE='$category_id' ";

                            if ( $category_id == $cat ) {
                                $category_list.=" SELECTED";
                                }

                            $category_list.=">" . ShowCategoryPath( $category_id ) . "</OPTION>\n";
                        }

                    $i++;
                }

            $categories=explode( "\n", $category_list );
            asort( $categories );
            $category.=implode( "\n", $categories );

            $category.="</select>";

            return $category;
        }

    function dmoz ( $catid, $Itemid )
        {
            global $database, $travelConfig, $task, $gid;

            $database->setQuery(
                "SELECT d.*" . "\n FROM #__ponygallery_catg AS d" . "\n WHERE d.parent=$catid and d.published=1 \n"
                    . "\n ORDER BY d.ordering" );
            $rows = $database->loadObjectList();
            $num_rows=ceil( count( $rows ) / 4 );
            $index=0;

            $output='<table cellspacing="0" cellpadding="4" border="0" width="100%" class="directory">';
            $output.='<tr >';
            $output.='	<th colspan="4" >';
            $output
                .="<img src='components/com_ponygallery/images/home.gif' hspace='6' border='0' align='absmiddle'>"._PONYGALLERY_POMKAT;
            $output.='	</th>';
            $output.='	</tr>';
            $output.='';

            for ( $row_count=0; $row_count < $num_rows; $row_count++ )
                {
                    $output.='<tr >';

                    for ( $col_count=0; $col_count < 4; $col_count++ )
                        {
                            $output.='<td width="' . floor( 100 / 4 ) . '%" valign="top">';
                            $cur_name=$rows[$index];

                            if ( trim( $cur_name ) == "" ) {
                                $output.='&nbsp';
                                }
                            else
                                {
                                    $output.='<img src="images/M_images/arrow.png"><a href="' . sefRelToAbs(
                                                                                                    "index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$cur_name->cid" ) . '">' . $cur_name->name
                                                 . ' (' . GetNumberOfLinks( $cur_name->cid ) . ')</a>';

                                    if ( $cur_name->access > 1 ) {
                                        $output.=' <font class="smallgm">'._PONYGALLERY_SM.'</font>';
                                        }
                                    else if ( $cur_name->access > 0 ) {
                                        $output.=' <font class="smallgsm">'._PONYGALLERY_RM.'</font>';
                                        }
                                }

                            $index++;

                            $output.='<br>' . $cur_name->description . '</td>';
                        }

                    $output.='</tr>';
                }

            $output.='	</table>';
            return $output;
        }

    function ShowCategoryPathLink ( $cat )
        {
            global $database, $mosConfig_lang, $gid, $Itemid;

            $cat=intval( $cat );

            $parent_id=1000;

            while ( $parent_id )
                {
                    $query="select * from #__ponygallery_catg where cid=$cat and published=1 \n AND access<='" . $gid
                               . "'";

                    $database->setQuery( $query );
                    $result=$database->query();

                    $parent_id=@mysql_result( $result, 0, 'parent' );
                    $cid=@mysql_result( $result, 0, 'cid' );
	
                    $name="<a href='" . sefRelToAbs("index.php?option=com_ponygallery&Itemid=$Itemid&func=viewcategory&catid=$cat" )."'>" . @mysql_result( $result,0,'name' ) . "</a>";

                    // write path
                    if ( empty( $path ) ) {
                        $path=$name;
                        }
                    else {
                        $path=$name . ' » ' . $path;
                        }

                    // next looping
                    $cat=$parent_id;
                }

            $home="<a href='" . sefRelToAbs( "index.php?option=com_ponygallery&Itemid=$Itemid" ) . "'>". _PONYGALLERY_HOME."</a>";
            $pathName=$home . ' » ' . $path . " ";
            return $pathName;
        }

    function GetNumberOfLinks ( $cat )
        {
            global $database, $gid;

            $queue[]=intval($cat);

            while ( list( $key, $cat )=each( $queue ) )
                {
                    // get children
                    $query="select cid from #__ponygallery_catg where parent=$cat and published=1";

                    $database->setQuery( $query );
                    $result=$database->query();
                    $total=mysql_num_rows( $result );
                    // put them in queue
                    $j=0;

                    while ( $j < $total )
                        {
                            $val=mysql_fetch_row( $result );
                            $queue[]=$val[0];
                            $j++;
                        }
                }

            reset( $queue );
            $query="SELECT count(id) as count FROM #__ponygallery  where ( 0!=0";

            while ( list( $key, $cat )=each( $queue ) ) {
                $query.=" or catid = $cat";
                }

            $query=$query . " ) and published=1 AND approved = 1";

            $database->setQuery( $query );
            $result=$database->query();

            $val=mysql_fetch_row( $result );
            return $val[0];
        }

    function ShowCategoryPath ( $cat )
        {
            global $database, $mosConfig_lang, $gid;

            $cat=intval( $cat );

            $parent_id=1000;

            while ( $parent_id )
                {
                    // read name and parent_id
                    $query="select * from #__ponygallery_catg where cid=$cat \n AND access<='" . $gid . "'";

                    $database->setQuery( $query );
                    $result=$database->query();

                    $parent_id=@mysql_result( $result, 0, 'parent' );
                    $name=@mysql_result( $result, 0, 'name' );

                    // write path
                    if ( empty( $path ) ) {
                        $path=$name;
                        }
                    else {
                        $path=$name . ' » ' . $path;
                        }

                    // next looping
                    $cat=$parent_id;
                }

            return $path . " ";
        }

    /**************************************************\
                         End
    \**************************************************/
?>