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 FORMAT_SLIKA = 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 = [
    [4],
    [5],
    [3],
    [8]
    ];

    this._arr_img_size = [
    ['385x275', '196x275'],
    ['510x365', '260x365'],
    ['190x135', '96x135'],
    ['780x558', '397x558']
    ];

    this._url_template = p_url_template;
    this._url_target = p_url_target;

    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();
		
        if( this._arr_entiteti.length )
        {
        	this.show( 0 );
        }
    }

    this.show_thumbs = function()
    {
        var _html = '';
        // popuni thumbs dio
        for ( index = 0; index < this._thumbs_per_page + 1; 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  ){ // && _abs_index != this._current_index - ovo je ako se ne zele prikazivati thumbovi slika koje su prikazane na canvasu
                // prikazi thumb

                if( arr_entitet[ ENTITET_TIP ] != 1 ){
                    // video
                    _html = '<a href="javascript:obj_galerija_' + this._galerija_suff +'.show(' + index + ')"><img src="templates/assets/images/elements/video_icon_gallery.png" style="position: absolute; bottom: 3px; left: 0; width: 70px; height: 15px; z-index: 2" /><img src="fw3k/util_scripts/get_slika_varijacija.php?slika_id=' + arr_entitet[ SLIKA_ID ] + '&var_suff=70x50" border="0"></a>';
                } else {
                    // slika
                    _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>';
                }
                $( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( _html );
            } else {
                // prikazi prazan thumb
                $( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( '&nbsp;' );
            }
        }

    }

    this.show = function( p_index )
    {
        this._current_index = p_index;

        //this.show_thumbs(); // ovo treba ukoliko se zeli osvjezavanje thumbova pri svakom prikazu slike
        
        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';
            }
            
            var _current_varijacija = this._main_varijacija;
            
            if( arr_entitet[ FORMAT_SLIKA ] == 2 ){
                //alert( 'uspravni format!!' );
                // uspravni format
                _current_varijacija = this._arr_img_size[ this._get_size_index() ][ 1 ];
            }
            
            _var_suff = _current_varijacija;

            $( '#galerija_canvas_' + _galerija_suff ).fadeOut( 'normal', function()
            {

                $( '#galerija_spinner_' + _galerija_suff ).show();

                if( arr_entitet[ ENTITET_TIP ] == 1 ){

                    var _html = '<img id="galerija_image" onload="javascript:obj_galerija_' + _galerija_suff + '.hide_spinner()" src="fw3k/util_scripts/get_slika_varijacija.php?slika_id=' + arr_entitet[ SLIKA_ID ] + '&var_suff=' + _var_suff + '" 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();
                    }
                    );
                }

                if( _url_template != 'undefined' && _url_template != null ){
                    // postavi link na sve koji imaju klasu
                    // galerija_link_suff
                    $( '.galerija_link_' + _galerija_suff ).attr( 'href', _url_template.replace( '{id}', arr_entitet[ KEYFIELD_VALUE ] ) );
                    if( _url_target != 'undefined' && _url_target != null ){
                        $( '.galerija_link_' + _galerija_suff ).attr( 'target', _url_target );
                    }
                }
            });

            // prikazi caption
            $( '#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 );
        }
    }

    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 == 'normal' ){
            return 0;
        }
        if( this._size == 'large' ){
            return 1;
        }
        if( this._size == 'home' ){
            return 2;
        }
        if( this._size == 'fullsize' ){
            return 3;
        }

        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();
}
