/** @file bilbordy.js
 *  @brief plik zawiera definicje klasy bilbord odpowiedzialnej za działanie
 *  bilbordów w systemie empCms
 *  @author Kamil Filipczyk
 *  @version 1.0
 *  @date 03-07-2010
 */

function bilbord( obiekt, baner_opoznienie,baner_efekt, baner_kolor, bilbord_typ, bilbord_wysokosc, banery )
{
    this.obiekt = obiekt ;
    this.baner_opoznienie = baner_opoznienie ;
    this.baner_kolor = baner_kolor ;
    this.baner_efekt = baner_efekt ;
    this.bilbord_typ = bilbord_typ ;
    this.bilbord_wysokosc = bilbord_wysokosc ;
    this.banery = banery ;

    this.baner_bilbord_timer = null ;
    this.baner_preload = false ;
    this.baner_index_url = 0 ;
    this.baner_index = 0 ;

    function uruchom_rotacje_banerowDef()
    {
        if( this.baner_bilbord_timer == null ) this.baner_bilbord_timer = setInterval( obiekt+".rotuj_baner()" , this.baner_opoznienie ) ;
    }
    this.uruchom_rotacje_banerow = uruchom_rotacje_banerowDef

    function ponow_rotacje_banerowDef()
    {
        if( this.baner_bilbord_timer == null ) this.baner_bilbord_timer = setInterval( obiekt+".rotuj_baner()" , baner_opoznienie ) ;
        this.baner_index++ ;
        if( this.baner_index == this.banery.length ) this.baner_index = 0 ;
    }
    this.ponow_rotacje_banerow = ponow_rotacje_banerowDef

    function rotuj_banerDef()
    {
        this.baner_index_url = this.baner_index ;
        // chowam poprzedni baner
        if( ( this.baner_index - 1)<0 )
        {
            if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' ) $( this.obiekt+'_baner_tresc_'+(this.banery.length-1) ).hide() ;
            if( this.bilbord_typ == 'kwadraty' ) $( this.obiekt+'_baner_opis_'+(this.banery.length-1) ).hide() ; 
            
            $( this.obiekt+'_baner_obraz_'+(this.banery.length-1) ).hide() ;
        }
        else
        {
           
            if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' ) $( this.obiekt+'_baner_tresc_'+(this.baner_index-1) ).hide() ;
             if( this.bilbord_typ == 'kwadraty' ) $( this.obiekt+'_baner_opis_'+(this.baner_index-1) ).hide() ;
            $( this.obiekt+'_baner_obraz_'+(this.baner_index-1) ).hide() ;
        }

        if( this.banery[ this.baner_index ].obraz != null ) // jest obraz do zaladowania
        {
            if( this.banery[ this.baner_index ].obraz.complete == true ) // obraz zaladowany do cachu
            {
                if( this.baner_preload == true )
                {
                    clearInterval( this.baner_bilbord_timer ) ;
                    this.baner_bilbord_timer = setInterval( obiekt+".rotuj_baner()" , this.baner_opoznienie ) ;
                    $( this.obiekt+'_baner_loading' ).hide() ;
                    if( $( this.obiekt+'_banery_menu' ) != null ) $( this.obiekt+'_banery_menu' ).show() ;
                    this.baner_preload = false ;
                }

                if( this.baner_bilbord_timer != null && this.baner_efekt == 'przenikanie' ) {
                    $( this.obiekt+'_baner_obraz_'+this.baner_index ).setOpacity(0) ;
                    $( this.obiekt+'_baner_obraz_'+this.baner_index ).show() ;
                    if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' )
                    {
                        if( this.bilbord_typ == 'kwadraty' )
                        {
                            $( this.obiekt+'_baner_opis_'+this.baner_index ).setOpacity(0) ;
                            $( this.obiekt+'_baner_opis_'+this.baner_index ).show() ;
                        }
                        $( this.obiekt+'_baner_tresc_'+this.baner_index ).setOpacity(0) ;
                        $( this.obiekt+'_baner_tresc_'+this.baner_index ).show() ;
                    }
                }
                if( this.baner_bilbord_timer != null && this.baner_efekt == 'rozwijanie' )
                {
                    $( this.obiekt+'_baner_obraz_'+this.baner_index ).hide();
                    if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' ) $( this.obiekt+'_baner_tresc_'+this.baner_index ).hide();
                    if( this.bilbord_typ == 'kwadraty'  ) $( this.obiekt+'_baner_opis_'+this.baner_index ).hide();
                }
            }
            else // obraz nie zaladowany
            {
                if( this.baner_preload == false )
                {
                    // wylaczam rotacje jak wlaczona i zalaczam sprawdzanie zaladowania obrazku
                    clearInterval( this.baner_bilbord_timer ) ;
                    this.baner_bilbord_timer = setInterval( obiekt+".rotuj_baner()" , 500 ) ;
                    $( this.obiekt+'_baner_loading' ).show() ;
                    if( $( this.obiekt+'_banery_menu' ) != null ) $( this.obiekt+'_banery_menu' ).hide() ;
                    this.baner_preload = true ;
                }

                return ;
            }
        }
        else //obrazek załadowany
        {
            if( this.baner_bilbord_timer != null && this.baner_efekt == 'przenikanie' ) {
                $(this.obiekt+'_baner_obraz_'+this.baner_index ).setOpacity(0) ;
                $(this.obiekt+'_baner_obraz_'+this.baner_index ).show() ;
                if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' )
                {
                    $(this.obiekt+'_baner_tresc_'+this.baner_index ).setOpacity(0) ;
                    $(this.obiekt+'_baner_tresc_'+this.baner_index ).show() ;
                    if( this.bilbord_typ == 'kwadraty' )
                    {
                        $(this.obiekt+'_baner_opis_'+this.baner_index ).setOpacity(0) ;
                        $(this.obiekt+'_baner_opis_'+this.baner_index ).show() ;
                    }
                }
            }
            if( this.baner_bilbord_timer != null && this.baner_efekt == 'rozwijanie' )
            {
                $( this.obiekt+'_baner_obraz_'+this.baner_index ).hide();
                if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' ) $( this.obiekt+'_baner_tresc_'+this.baner_index ).hide();
                if( this.bilbord_typ == 'kwadraty'  ) $( this.obiekt+'_baner_opis_'+this.baner_index ).hide();
                
            }
        }

        // zaznaczam linki
        if( this.bilbord_typ == 'zakładki' || this.bilbord_typ == 'kwadraty' )
        {
            for( var i = 0 ; i < this.banery.length ; i++ )
            {
                if( i == this.baner_index ) $( this.obiekt+'_baner_menu_'+this.banery[ i ].id ).className = 'baner_menu_'+this.bilbord_typ+'_zaznaczony' ;
                else $( this.obiekt+'_baner_menu_'+this.banery[ i ].id ).className = "baner_menu_"+this.bilbord_typ ;
            }
        }

        if( this.baner_bilbord_timer != null && this.baner_efekt == 'przenikanie' )
        {
            if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy'  ) new Effect.Opacity( this.obiekt+'_baner_tresc_'+this.baner_index, {
                from: 0,
                to: 1,
                duration: 1
            });
             if( this.bilbord_typ == 'kwadraty' ) new Effect.Opacity( this.obiekt+'_baner_opis_'+this.baner_index, {
                from: 0,
                to: 1,
                duration: 1
            });
            new Effect.Opacity( this.obiekt+'_baner_obraz_'+this.baner_index, {
                from: 0,
                to: 1,
                duration: 1
            });
        }
        else if( this.baner_bilbord_timer != null && this.baner_efekt == 'rozwijanie' )
        {
            if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' ) new Effect.SlideDown( this.obiekt+'_baner_tresc_'+this.baner_index );
            if( this.bilbord_typ == 'kwadraty' ) new Effect.SlideDown( this.obiekt+'_baner_opis_'+this.baner_index );
            
            new Effect.SlideDown( this.obiekt+'_baner_obraz_'+this.baner_index );
        }
        else
        {
            $( this.obiekt+'_baner_obraz_'+this.baner_index ).show() ;

            if( this.bilbord_typ == 'kwadraty' || this.bilbord_typ == 'slajdy' )
            {
                $( this.obiekt+'_baner_tresc_'+this.baner_index ).show() ;
                 if( this.bilbord_typ == 'kwadraty' )
            {
                $( this.obiekt+'_baner_opis_'+this.baner_index ).show() ;
            }
            }
        }

        if( this.baner_bilbord_timer != null )
        {
            this.baner_index++ ;
            if( this.baner_index == banery.length ) this.baner_index = 0 ;
        }
    }
    this.rotuj_baner = rotuj_banerDef

    function bilbord_zatrzymajDef( index )
    {
        if( this.baner_bilbord_timer != null || this.baner_index != index )
        {
            // chowam wszystkie banery
            for( var i = 0 ; i < this.banery.length ; i++ )
            {
                $( this.obiekt+'_baner_obraz_'+i ).hide();
                if( this.bilbord_typ == 'kwadraty' ) $( this.obiekt+'_baner_tresc_'+i ).hide() ;
                 if( this.bilbord_typ == 'kwadraty' ) $( this.obiekt+'_baner_opis_'+i ).hide() ;
            }

            if( this.baner_bilbord_timer != null ) clearInterval( this.baner_bilbord_timer ) ;
            this.baner_bilbord_timer = null ;
            this.baner_index = index ;

            this.rotuj_baner() ;
        }
    }
    this.bilbord_zatrzymaj = bilbord_zatrzymajDef

    function baner_przeniesDef()
    {
        this.baner_dodaj_klikniecie( this.banery[ this.baner_index_url ].id )

        if( this.banery[ this.baner_index_url ].url != "" )
        {
            window.location = this.banery[ this.baner_index_url ].url ;
        }
    }
    this.baner_przenies = baner_przeniesDef

    function baner_dodaj_klikniecieDef( id )
    {
        var ajax = new Ajax.Request( 'index.php' ,
        {
            method: 'post',
            parameters: {
                zapytanie: 'tak',
                klasa : 'bilbordy',
                zapytanie_jakie: 'dodaj_klikniecie',
                id : id
            },
            onComplete: function( originalRequest )
            {
            },
            encoding : 'UTF-8'
        });
    }
    this.baner_dodaj_klikniecie = baner_dodaj_klikniecieDef

}




