$.fn.extend( {
    initnewrating: function ( value )
    {
        $( this ).attr( "rating", value );
        var value = $( this ).attr( "rating" );
        var rating = parseInt( value );
        var ishalfe = value.indexOf( ',' ) != -1;

        $( this ).find( 'a' ).attr( 'class', '' );
        $( this ).find( 'a' ).each( function ( index )
        {

            if ( rating == index && ishalfe )
            {
                $( this ).addClass( 'half' );
                return;
            }
            if ( rating > index )
            {
                $( this ).addClass( 'gold' );
                return;
            }
            $( this ).addClass( 'silver' );
        } );
    },
    stars: function ( starcallbackclick )
    {
        var countstars = 5;

        for ( var i = 0; i < countstars; i++ )
            $( this ).append( $( '<a href="javascript:void(0);"></a>' ) );

        $( this ).each( function () { Init( $( this ) ); } );
        $( this ).find( 'a' ).click( function ()
        {
            var currentindex = $( 'div.stars a' ).index( $( this ) );
            currentindex = currentindex % countstars;

            if ( $( "#" + $( this ).parent().attr( "id" ) ).attr( 'clicked' ) == 'true' )
            {
            }
            else
            {
                starcallbackclick( $( this ).parent().attr( "id" ), currentindex );
                $( "#" + $( this ).parent().attr( "id" ) ).attr( 'clicked', 'true' );
            }
        } );

        $( this ).find( 'a' ).hover( function ()
        {
            var currentindex = $( 'div.stars a' ).index( $( this ) );
            currentindex = currentindex % countstars;

            $( this ).parent().find( "a" ).each( function ( index )
            {
                if ( currentindex >= index )
                {
                    $( this ).attr( 'class', '' );
                    $( this ).addClass( 'gold' );
                }
            } );
        },
        function ()
        {
            var currentindex = $( 'div.stars a' ).index( $( this ) );
            currentindex = currentindex % countstars;

            $( this ).parent().find( "a" ).each( function ( index )
            {
                if ( currentindex <= index )
                {
                    $( this ).attr( 'class', '' );
                    $( this ).addClass( 'silver' );
                }
            } );
        } );

        $( this ).hover( function () { }, function () { Init( $( this ) ); } );

        function Init( parent )
        {
            var value = parent.attr( "rating" );
            var rating = parseInt( value );
            var ishalfe = value.indexOf( ',' ) != -1;

            parent.find( 'a' ).attr( 'class', '' );
            parent.find( 'a' ).each( function ( index )
            {

                if ( rating == index && ishalfe )
                {
                    $( this ).addClass( 'half' );
                    return;
                }
                if ( rating > index )
                {
                    $( this ).addClass( 'gold' );
                    return;
                }
                $( this ).addClass( 'silver' );
            } );
        }
    }
} );
