function gallery2_controller( p_arr_entiteti, p_size, p_url_template, p_url_target, p_galerija_suff )
{
	this._app_rewrite_base = APP_REWRITE_BASE;
	if(p_arr_entiteti.length == 0) return false;
	
	
    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 SET_URL = 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 = [
    [5],
    [6],
    [3],
    [1],
    [1],
    [1],
    [1],
    [12],
    [12],
    [6],
	[6],
	[4]
    ];

    this._arr_img_size = [
    ['480x360'],
    ['607x405'],
    ['190x135'],
    ['90x82'],
    ['262x320'],
    ['262x196'],
    ['150x100'],
    ['110x80'],
    ['110x80'],
    ['110x80'],
	['135x90'],
	['110x80']
    ];

    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();

        this.show( 0 );
    }

    this.show_thumbs = function()
    {
        var _html = '';
        // 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 ];
            
            var _url_template = this._url_template;

            if( _abs_index <= this._arr_entiteti.length -1 ){
                // 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="' + slika_get_variation_url(arr_entitet[SLIKA_ID], '70x50') + '" border="0"></a>';
                } else 
                {
                    var item_href = '';
                    if( typeof( arr_entitet[ SET_URL ] ) == 'undefined' || arr_entitet[ SET_URL ] === null )
                    {
                    	item_href = _url_template.replace( '{id}', arr_entitet[ KEYFIELD_VALUE ] );
                    }
                    else
                    {
                    	item_href = arr_entitet[ SET_URL ];
                    }
                	// slika
                    _html = '<a href="' + item_href + '"><img src="' + slika_get_variation_url(arr_entitet[ SLIKA_ID ], '105x70') + '" border="0"></a>';
                    _naslov = '<h2 class="naslov"><a href="' + item_href + '">' + arr_entitet[ ENTITET_OPIS ] + '</a></h2>';
                    _lead = '<span class="tekst_lead">' + arr_entitet[ 3 ] + '</span>';
                }
                //$( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( _html );
                
                if (arr_entitet[ 3 ] != null)
                	$( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( _html + _naslov + _lead );
                else
                	$( '#galerija_thumb_' + (index + 1) + '_' + this._galerija_suff ).html( _html + _naslov );	
                
            } 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 _html = '<img id="galerija_image" onload="javascript:obj_galerija_' + _galerija_suff + '.hide_spinner()" src="' + slika_get_variation_url(arr_entitet[SLIKA_ID], _var_suff) + '" border="0">';
                    var _link = '<img src="templates/assets/images/button_opsirnije_voditelj.gif" alt="Opširnije" />';
                    
                    if( _url_template != null ){
                        //alert( this._url_template );
                        _html = '<a href="' + _url_template.replace( '{id}', arr_entitet[ KEYFIELD_VALUE ] ) + '" target="' + _url_target + '">' + _html + '</a>';
                        
                        _link = '<a href="' + _url_template.replace( '{id}', arr_entitet[ KEYFIELD_VALUE ] ) + '" target="' + _url_target + '">' + _link + '</a>';
                    }

                    // slika
                    $( '#galerija_canvas_' + _galerija_suff ).html( _html );
                    // link
                    $( '#galerija_link_' + _galerija_suff ).html( _link );
                    
                } else {
                    var video_code = arr_entitet[ VIDEO_CODE ];
                    var video_tip = arr_entitet[ VIDEO_TIP ];
                    // video
                    $('#galerija_canvas_' + _galerija_suff ).load( this._app_rewrite_base + '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();
                    }
                    );
                }


            });

            // prikazi caption
            $( '#galerija_canvas_caption_' + _galerija_suff ).text( arr_entitet[ ENTITET_OPIS ] );
            
            // prikazi lead
            $( '#galerija_canvas_lead_' + _galerija_suff ).text( arr_entitet[ 3 ] );

            // prikazi poziciju
            $( '#galerija_status_' + _galerija_suff ).text( (this._get_absolute_index( this._current_index ) + 1) + '/' + this._arr_entiteti.length );
            
            $( '#galerija_pages_' + _galerija_suff ).text( this._current_page + '/' + this._total_pages );

        }
    }

    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 == 'v_thumb' ){
            return 3;
        }
        if( this._size == 'n_thumb' ){
            return 4;
        }
        if( this._size == 'vp_thumb' ){
            return 5;
        }
        if( this._size == 'vv_thumb' ){
            return 6;
        }
        if( this._size == 'vv_thumb_small' ){
            return 7;
        }
        if( this._size == 'vv_thumb_small2' ){
            return 8;
        }
        if( this._size == 'vv_thumb_small3' ){
            return 9;
        }
		if( this._size == 'vv_thumb_premier' ){
            return 10;
        }
        if( this._size == 'vv_thumb_televizija' ){
            return 11;
        }

        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();
}
