function gallery_controller( p_arr_entiteti, p_size, p_url_template, p_url_target, p_galerija_suff )
{
    var THUMB_COUNT = 0;

    var SLIKA_ID = 1;
    var ENTITET_TIP = 0;
    var ENTITET_OPIS = 2;
    var VIDEO_TIP = 3;
    var VIDEO_CODE = 4;
    var KEYFIELD_VALUE = 5;
    var MD5 = 6;

    this._arr_entiteti = p_arr_entiteti;
    this._size = p_size;

    this._current_page = 1;
    this._current_index = 0;

    this._total_pages = 1;
    this._thumbs_per_page = 0;

    this._main_varijacija = null;

    this._galerija_suff = p_galerija_suff;

    // array sa brojem thumbova ovisno o velicini galerije
    this._arr_size_defs = [
    [1],
    ];

    this._arr_img_size = [
    ['670x465'],
    ];

    this._url_template = p_url_template;
    this._url_target = p_url_target;

    this._circular = false;

    this._init = function()
    {
        this._thumbs_per_page = this._arr_size_defs[ this._get_size_index() ][ THUMB_COUNT ];
        this._total_pages = Math.ceil( this._arr_entiteti.length / this._thumbs_per_page);
        this._main_varijacija = this._arr_img_size[ this._get_size_index() ][ 0 ];
        this.show_thumbs();

        this.show( 0 );
    }

    this.show_thumbs = function()
    {
        // popuni thumbs dio
        for ( index = 0; index < this._thumbs_per_page; index++ ){
            var _abs_index = this._get_absolute_index( index );

            var arr_entitet = this._arr_entiteti[ _abs_index ];

            if( _abs_index <= this._arr_entiteti.length -1 ){
                // prikazi thumb
                $( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( '<a href="javascript:obj_galerija_' + this._galerija_suff +'.show(' + index + ')"><img src="fw3k/util_scripts/get_slika_varijacija.php?slika_id=' + arr_entitet[ SLIKA_ID ] + '&var_suff=70x50" border="0"></a>' );
            } else {
                // prikazi prazan thumb
                $( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( '&nbsp;' );
            }
        }

    }

    this.show = function( p_index )
    {
        this._current_index = p_index;

        var arr_entitet = this._arr_entiteti[ this._get_absolute_index( p_index ) ];

        if( (arr_entitet != 'undefined') ){

            var _url_template = this._url_template;
            var _url_target = this._url_target;
            var _var_suff = this._main_varijacija;
            var _size = this._size;
            var _galerija_suff = this._galerija_suff;

            if( _url_target == null ){
                _url_target = '_self';
            }

            $( '#galerija_canvas_' + _galerija_suff ).fadeOut( 'normal', function()
            {

                $( '#galerija_spinner_' + _galerija_suff ).show();

                if( arr_entitet[ ENTITET_TIP ] == 1 ){
                    var _img_url = APP_REWRITE_BASE+'util_scripts/get_image.php?slika_id=' + arr_entitet[ SLIKA_ID ] + '&var_suff=' + _var_suff;
                    if( arr_entitet[ MD5 ] ){
                        _img_url = _img_url + '&_key=' + arr_entitet[ MD5 ];
                    }

                    var _html = '<img id="galerija_image" onload="javascript:obj_galerija_' + _galerija_suff + '.hide_spinner()" src="' + _img_url + '" border="0">';

                    if( _url_template != null ){
                        //alert( this._url_template );
                        _html = '<a href="' + _url_template.replace( '{id}', arr_entitet[ KEYFIELD_VALUE ] ) + '" target="' + _url_target + '">' + _html + '</a>';
                    }

                    // slika
                    $( '#galerija_canvas_' + _galerija_suff ).html( _html );
                } else {
                    var video_code = arr_entitet[ VIDEO_CODE ];
                    var video_tip = arr_entitet[ VIDEO_TIP ];
                    // video
                    $('#galerija_canvas_' + _galerija_suff ).load( 'index.php?cmd=ajax_show_video&video_code=' + video_code + '&video_tip=' + video_tip + '&size=' + _size, null, function()
                    {
                        // sakrij spinner
                        $( '#galerija_spinner_' + _galerija_suff ).hide();
                        $( '#galerija_canvas_' + _galerija_suff ).show();
                    }
                    );
                }
                $( '#galerija_canvas_caption_' + _galerija_suff ).text( arr_entitet[ ENTITET_OPIS ] );

            });


            // prikazi poziciju
            $( '#galerija_status_' + _galerija_suff ).text( (this._get_absolute_index( this._current_index ) + 1) + '/' + this._arr_entiteti.length );

        }
    }

    this.next = function()
    {
        if( this._get_absolute_index( this._current_index ) < (this._arr_entiteti.length - 1) ){
            this._current_index ++;

            // ako prelazimo na slijedecu stranicu
            if( this._current_index > this._thumbs_per_page - 1 ){
                this._current_index = 0;
                this._current_page++;

                this.show_thumbs();
            }

            this.show( this._current_index );
        } else {
            if( this._circular ){
                this._current_index = 0;
                this._current_page = 1;

                this.show( this._current_index );
            }
        }
    }

    this.prev = function()
    {
        if( this._get_absolute_index( this._current_index ) > 0 ){
            this._current_index --;

            // ako prelazimo na prethodnu stranicu
            if( this._current_index < 0){
                this._current_index = this._thumbs_per_page - 1;
                this._current_page--;

                this.show_thumbs();
            }

            this.show( this._current_index );
        }
    }

    this.next_page = function()
    {
        if( this._current_page < this._total_pages ){
            this._current_page ++;
            this._current_index = 0;

            this.show_thumbs();
            this.show( this._current_index );
        }
    }

    this.prev_page = function()
    {
        if( this._current_page > 1 ){
            this._current_page --;
            this._current_index = this._thumbs_per_page - 1;

            this.show_thumbs();
            this.show( this._current_index );
        }
    }


    this.hide_spinner = function()
    {
        $( '#galerija_spinner_' + this._galerija_suff ).hide();
        $( '#galerija_canvas_' + this._galerija_suff ).fadeIn( 'normal' );
    }

    this.show_spinner = function()
    {
        //$( '#galerija_canvas' ).html( '&nbsp;' );
        $( '#galerija_canvas_' + this._galerija_suff ).hide();
        $( '#galerija_spinner_' + this._galerija_suff ).show();
    }

    this._get_size_index = function()
    {

        if( this._size == 'home' ){
            return 0;
        }
        if( this._size == 'portfolio' ){
            return 0;
        }

        return null

    }

    // index je zero based
    this._get_absolute_index = function( p_index )
    {
        return (this._current_page - 1) * this._arr_size_defs[ this._get_size_index() ][ THUMB_COUNT ] + p_index;
    }

    this._init();
}
