function Voter()
{   
    _this= this;
    
    this.addVotingOption= function( item )
    {
//         console.log( item );
//         console.log( item.rel );
        var key= 'vote_picture_'+parseInt( item.id );
//         console.log( this.voting_rights[ key ] );


        if( this.voting_rights[ key ] )
        {
            tmp= dojo.create( 'div', { 'class': 'voting_panel' }, item );

            tmp1= dojo.create( 'a', { 'class': 'vote_plus' }, tmp );
            tmp2= dojo.create( 'a', { 'class': 'vote_minus' }, tmp );

            dojo.connect( tmp1, 'click', this, this.votePlus );
            dojo.connect( tmp2, 'click', this, this.voteMinus );
        }
        else
        {
            tmp= dojo.create( 'div', { 'class': 'voting_panel' }, item );

            var toanim= dojo.create( 'span', { 'innerHTML': item.getAttribute( 'rel' ), 'class': 'vote_count' }, tmp );
        }
    }



    this.showVoteResults= function( data )
    {
        var panel= dojo.query( 'div.voting_panel', dojo.byId( data.id+'_vote' ) )[ 0 ];

        window.setTimeout( function()
            {
            panel.removeChild( panel.childNodes[ 1 ] );
            panel.removeChild( panel.childNodes[ 0 ] );
//             console.log( panel );
//             console.log( data );
            
            var toanim= dojo.create( 'span', { /*style: 'display: none', */ 'innerHTML': data.count, 'class': 'vote_count' }, panel );

/*            console.log( toanim );*/
            
            dojo.fadeIn( { 'node': toanim, 'duration': 700 } ).play();
            },
            350
            );
        
    }




    this.votePlus= function( event )
    {
        
//         console.log( 'Plus %o', event );

        var image= event.originalTarget.parentNode;
        
        var id= parseInt( image.parentNode.id );
        
        dojo.xhrGet(
            {
                'url': '/webservices/rightsWebservice.php?votePlus='+id,
                'preventCache': true,
                'handleAs': 'json',
                'timeout': 3000,
                'load': function( data )
                {
                    _this.showVoteResults( data );

                }

            }
            );
        dojo.fadeOut( { 'node': image.childNodes[ 0 ] } ).play();
        dojo.fadeOut( { 'node': image.childNodes[ 1 ] } ).play();
    }



    this.voteMinus= function( event )
    {
        
        var image= event.originalTarget.parentNode;

        var id= parseInt( image.parentNode.id );

        dojo.xhrGet(
            {
                'url': '/webservices/rightsWebservice.php?voteMinus='+id,
                'preventCache': true,
                'handleAs': 'json',
                'timeout': 3000,
                'load': function( data )
                {
                    _this.showVoteResults( data );

                }
            }
            );
            
        dojo.fadeOut( { 'node': image.childNodes[ 0 ] } ).play();
        dojo.fadeOut( { 'node': image.childNodes[ 1 ] } ).play();
        
    }





    this.addId= function( item )
    {
        this.imgids.push( parseInt( item.id ) );
    }



    this.addVotingOptions= function( rights )
    {
        this.voting_rights= rights;
//         console.log( rights );

        
        dojo.query( 'div.albums_list > div.poll_photo' ).forEach( this.addVotingOption, this );

        
    }
    


    this.init= function()
    {
        this.imgids= new Array();
        dojo.query( 'div.albums_list > div.poll_photo').forEach( this.addId, this );
        
        dojo.xhrGet(
            {
                'url': '/webservices/rightsWebservice.php?canvote_picture='+this.imgids.join( ',' ),
                'preventCache': true,
                'handleAs': 'json',
                'timeout': 3000,
                'load': function( data )
                {
                    _this.addVotingOptions( data );
                    
                }
                
            }
        );

//         dojo.query( 'div.albums_list > div.poll_photo').forEach( addVotingOptions, this );

    }

    this.init();

}










function initVotingScript()
{
 
    if( ( dojo.byId( 'contestWinRule' ) ) && ( dojo.byId( 'contestWinRule' ).className == 'auto' ) &&
        ( dojo.byId( 'isLoggedIn' ) ) && ( dojo.byId( 'isLoggedIn' ).className == '1' ) )
    {


        
        if( dojo.query( 'div.albums_list' ).length > 0 )
            var vt= new Voter();
    }
}





dojo.addOnLoad( initVotingScript );


